2 * Copyright (c) 2004, the JUNG Project and the Regents of the University
5 * Created on Jan 28, 2004
7 * This software is open-source under the BSD license; see either
9 * http://jung.sourceforge.net/license.txt for a description.
11 package edu.uci.ics.jung.algorithms.blockmodel;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.List;
23 import org.apache.commons.collections15.CollectionUtils;
24 import org.apache.commons.collections15.Transformer;
26 import edu.uci.ics.jung.graph.Graph;
27 import edu.uci.ics.jung.graph.util.Pair;
30 * Identifies sets of structurally equivalent vertices in a graph. Vertices <i>
31 * i</i> and <i>j</i> are structurally equivalent iff the set of <i>i</i>'s
32 * neighbors is identical to the set of <i>j</i>'s neighbors, with the
33 * exception of <i>i</i> and <i>j</i> themselves. This algorithm finds all
34 * sets of equivalent vertices in O(V^2) time.
36 * <p>You can extend this class to have a different definition of equivalence (by
37 * overriding <code>isStructurallyEquivalent</code>), and may give it hints for
38 * accelerating the process by overriding <code>canPossiblyCompare</code>.
39 * (For example, in a bipartite graph, <code>canPossiblyCompare</code> may
40 * return <code>false</code> for vertices in
41 * different partitions. This function should be fast.)
43 * @author Danyel Fisher
45 public class StructurallyEquivalent<V,E> implements Transformer<Graph<V,E>, VertexPartition<V,E>>
47 public VertexPartition<V,E> transform(Graph<V,E> g)
49 Set<Pair<V>> vertex_pairs = getEquivalentPairs(g);
51 Set<Set<V>> rv = new HashSet<Set<V>>();
52 Map<V, Set<V>> intermediate = new HashMap<V, Set<V>>();
53 for (Pair<V> p : vertex_pairs)
55 Set<V> res = intermediate.get(p.getFirst());
57 res = intermediate.get(p.getSecond());
58 if (res == null) // we haven't seen this one before
59 res = new HashSet<V>();
60 res.add(p.getFirst());
61 res.add(p.getSecond());
62 intermediate.put(p.getFirst(), res);
63 intermediate.put(p.getSecond(), res);
65 rv.addAll(intermediate.values());
67 // pick up the vertices which don't appear in intermediate; they are
68 // singletons (equivalence classes of size 1)
69 Collection<V> singletons = CollectionUtils.subtract(g.getVertices(),
70 intermediate.keySet());
71 for (V v : singletons)
73 Set<V> v_set = Collections.singleton(v);
74 intermediate.put(v, v_set);
78 return new VertexPartition<V, E>(g, intermediate, rv);
82 * For each vertex pair v, v1 in G, checks whether v and v1 are fully
83 * equivalent: meaning that they connect to the exact same vertices. (Is
84 * this regular equivalence, or whathaveyou?)
86 * Returns a Set of Pairs of vertices, where all the vertices in the inner
87 * Pairs are equivalent.
91 protected Set<Pair<V>> getEquivalentPairs(Graph<V,?> g) {
93 Set<Pair<V>> rv = new HashSet<Pair<V>>();
94 Set<V> alreadyEquivalent = new HashSet<V>();
96 List<V> l = new ArrayList<V>(g.getVertices());
100 if (alreadyEquivalent.contains(v1))
103 for (Iterator<V> iterator = l.listIterator(l.indexOf(v1) + 1); iterator.hasNext();) {
104 V v2 = iterator.next();
106 if (alreadyEquivalent.contains(v2))
109 if (!canPossiblyCompare(v1, v2))
112 if (isStructurallyEquivalent(g, v1, v2)) {
113 Pair<V> p = new Pair<V>(v1, v2);
114 alreadyEquivalent.add(v2);
124 * Checks whether a pair of vertices are structurally equivalent.
125 * Specifically, whether v1's predecessors are equal to v2's predecessors,
126 * and same for successors.
128 * @param g the graph in which the structural equivalence comparison is to take place
129 * @param v1 the vertex to check for structural equivalence to v2
130 * @param v2 the vertex to check for structural equivalence to v1
132 protected boolean isStructurallyEquivalent(Graph<V,?> g, V v1, V v2) {
134 if( g.degree(v1) != g.degree(v2)) {
138 Set<V> n1 = new HashSet<V>(g.getPredecessors(v1));
141 Set<V> n2 = new HashSet<V>(g.getPredecessors(v2));
145 Set<V> o1 = new HashSet<V>(g.getSuccessors(v1));
146 Set<V> o2 = new HashSet<V>(g.getSuccessors(v2));
152 // this neglects self-loops and directed edges from 1 to other
153 boolean b = (n1.equals(n2) && o1.equals(o2));
157 // if there's a directed edge v1->v2 then there's a directed edge v2->v1
158 b &= ( g.isSuccessor(v1, v2) == g.isSuccessor(v2, v1));
161 b &= ( g.isSuccessor(v1, v1) == g.isSuccessor(v2, v2));
168 * This is a space for optimizations. For example, for a bipartite graph,
169 * vertices from different partitions cannot possibly be compared.
174 protected boolean canPossiblyCompare(V v1, V v2) {