Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
3 | * of California | |
4 | * All rights reserved. | |
5 | * | |
6 | * This software is open-source under the BSD license; see either | |
7 | * "license.txt" or | |
8 | * http://jung.sourceforge.net/license.txt for a description. | |
9 | */ | |
10 | /* | |
11 | * Created on Feb 3, 2004 | |
12 | */ | |
13 | package edu.uci.ics.jung.algorithms.blockmodel; | |
14 | ||
15 | import java.util.*; | |
16 | import java.util.HashSet; | |
17 | import java.util.Iterator; | |
18 | import java.util.Set; | |
19 | ||
20 | import edu.uci.ics.jung.graph.Graph; | |
21 | import edu.uci.ics.jung.graph.Vertex; | |
22 | ||
23 | /** | |
24 | * An EquivalenceRelation holds a number of Equivalent vertices from the | |
25 | * same graph. | |
26 | * | |
27 | * created Feb 3, 2004 | |
28 | * @author danyelf | |
29 | */ | |
30 | public class EquivalenceRelation { | |
31 | ||
32 | private Set equivalenceSets; | |
33 | private Graph graph; | |
34 | ||
35 | /** | |
36 | * Input is the basic data structure underneath: a Set of Sets. | |
37 | * The sets must be mutually exclusive, non-empty, | |
38 | * and contain only vertices from the graph. A reference to the | |
39 | * underlying sets is maintained; be careful not to accidently | |
40 | * modify them after the ER is created. | |
41 | */ | |
42 | 9 | public EquivalenceRelation(Set rv, Graph g) { |
43 | 9 | this.equivalenceSets = Collections.unmodifiableSet( rv ); |
44 | 9 | this.graph = g; |
45 | 9 | } |
46 | ||
47 | /** | |
48 | * Returns the common graph to which all the vertices belong | |
49 | */ | |
50 | public Graph getGraph() { | |
51 | 2 | return graph; |
52 | } | |
53 | ||
54 | /** | |
55 | * Returns the set of vertices that do not belong to an particular equivalence class. | |
56 | * Takes O(n) time by walking through the whole graph and checking all vertices that | |
57 | * are not in any equivalence relation. | |
58 | */ | |
59 | public Set getSingletonVertices() { | |
60 | 5 | Set allVerticesInEquivalence = new HashSet(); |
61 | 5 | for (Iterator iter = equivalenceSets.iterator(); iter.hasNext();) { |
62 | 6 | Set s = (Set) iter.next(); |
63 | 6 | allVerticesInEquivalence.addAll(s); |
64 | } | |
65 | 5 | Set allVertices = new HashSet(graph.getVertices()); |
66 | 5 | allVertices.removeAll(allVerticesInEquivalence); |
67 | 5 | return allVertices; |
68 | } | |
69 | ||
70 | /** | |
71 | * Iterates through all the equivalence sets. Does not return any singletons. | |
72 | * @return an Iterator of Sets of vertices. | |
73 | */ | |
74 | public Iterator getAllEquivalences() { | |
75 | 6 | return equivalenceSets.iterator(); |
76 | } | |
77 | ||
78 | /** | |
79 | * Returns the part of the relation that contains this vertex: it is, of course, a Set | |
80 | * If the vertex does not belong to any relation, null is returned. | |
81 | */ | |
82 | public Set getEquivalenceRelationContaining(Vertex v) { | |
83 | 16 | for (Iterator iter = equivalenceSets.iterator(); iter.hasNext();) { |
84 | 22 | Set s = (Set) iter.next(); |
85 | 22 | if (s.contains(v)) |
86 | 13 | return s; |
87 | } | |
88 | 3 | return null; |
89 | } | |
90 | ||
91 | /** | |
92 | * Returns the number of relations defined. | |
93 | */ | |
94 | public int numRelations() { | |
95 | 5 | return equivalenceSets.size(); |
96 | } | |
97 | ||
98 | public String toString() { | |
99 | 0 | return "Equivalence: " + equivalenceSets; |
100 | } | |
101 | ||
102 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |