edu.uci.ics.jung.algorithms.blockmodel.StructurallyEquivalent Maven / Gradle / Ivy
/*
* Copyright (c) 2004, the JUNG Project and the Regents of the University
* of California
* All rights reserved.
* Created on Jan 28, 2004
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* http://jung.sourceforge.net/license.txt for a description.
*/
package edu.uci.ics.jung.algorithms.blockmodel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Pair;
/**
* Identifies sets of structurally equivalent vertices in a graph. Vertices
* i and j are structurally equivalent iff the set of i's
* neighbors is identical to the set of j's neighbors, with the
* exception of i and j themselves. This algorithm finds all
* sets of equivalent vertices in O(V^2) time.
*
* You can extend this class to have a different definition of equivalence (by
* overriding isStructurallyEquivalent
), and may give it hints for
* accelerating the process by overriding canPossiblyCompare
.
* (For example, in a bipartite graph, canPossiblyCompare
may
* return false
for vertices in
* different partitions. This function should be fast.)
*
* @author Danyel Fisher
*/
public class StructurallyEquivalent implements Transformer, VertexPartition>
{
public VertexPartition transform(Graph g)
{
Set> vertex_pairs = getEquivalentPairs(g);
Set> rv = new HashSet>();
Map> intermediate = new HashMap>();
for (Pair p : vertex_pairs)
{
Set res = intermediate.get(p.getFirst());
if (res == null)
res = intermediate.get(p.getSecond());
if (res == null) // we haven't seen this one before
res = new HashSet();
res.add(p.getFirst());
res.add(p.getSecond());
intermediate.put(p.getFirst(), res);
intermediate.put(p.getSecond(), res);
}
rv.addAll(intermediate.values());
// pick up the vertices which don't appear in intermediate; they are
// singletons (equivalence classes of size 1)
Collection singletons = CollectionUtils.subtract(g.getVertices(),
intermediate.keySet());
for (V v : singletons)
{
Set v_set = Collections.singleton(v);
intermediate.put(v, v_set);
rv.add(v_set);
}
return new VertexPartition(g, intermediate, rv);
}
/**
* For each vertex pair v, v1 in G, checks whether v and v1 are fully
* equivalent: meaning that they connect to the exact same vertices. (Is
* this regular equivalence, or whathaveyou?)
*
* Returns a Set of Pairs of vertices, where all the vertices in the inner
* Pairs are equivalent.
*
* @param g
*/
protected Set> getEquivalentPairs(Graph g) {
Set> rv = new HashSet>();
Set alreadyEquivalent = new HashSet();
List l = new ArrayList(g.getVertices());
for (V v1 : l)
{
if (alreadyEquivalent.contains(v1))
continue;
for (Iterator iterator = l.listIterator(l.indexOf(v1) + 1); iterator.hasNext();) {
V v2 = iterator.next();
if (alreadyEquivalent.contains(v2))
continue;
if (!canPossiblyCompare(v1, v2))
continue;
if (isStructurallyEquivalent(g, v1, v2)) {
Pair p = new Pair(v1, v2);
alreadyEquivalent.add(v2);
rv.add(p);
}
}
}
return rv;
}
/**
* Checks whether a pair of vertices are structurally equivalent.
* Specifically, whether v1's predecessors are equal to v2's predecessors,
* and same for successors.
*
* @param g the graph in which the structural equivalence comparison is to take place
* @param v1 the vertex to check for structural equivalence to v2
* @param v2 the vertex to check for structural equivalence to v1
*/
protected boolean isStructurallyEquivalent(Graph g, V v1, V v2) {
if( g.degree(v1) != g.degree(v2)) {
return false;
}
Set n1 = new HashSet(g.getPredecessors(v1));
n1.remove(v2);
n1.remove(v1);
Set n2 = new HashSet(g.getPredecessors(v2));
n2.remove(v1);
n2.remove(v2);
Set o1 = new HashSet(g.getSuccessors(v1));
Set o2 = new HashSet(g.getSuccessors(v2));
o1.remove(v1);
o1.remove(v2);
o2.remove(v1);
o2.remove(v2);
// this neglects self-loops and directed edges from 1 to other
boolean b = (n1.equals(n2) && o1.equals(o2));
if (!b)
return b;
// if there's a directed edge v1->v2 then there's a directed edge v2->v1
b &= ( g.isSuccessor(v1, v2) == g.isSuccessor(v2, v1));
// self-loop check
b &= ( g.isSuccessor(v1, v1) == g.isSuccessor(v2, v2));
return b;
}
/**
* This is a space for optimizations. For example, for a bipartite graph,
* vertices from different partitions cannot possibly be compared.
*
* @param v1
* @param v2
*/
protected boolean canPossiblyCompare(V v1, V v2) {
return true;
}
}