1 | package weka.clusterers.forMetisMQI.graph; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.HashSet; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Set; |
---|
9 | import java.util.SortedSet; |
---|
10 | import java.util.TreeSet; |
---|
11 | import java.util.Map.Entry; |
---|
12 | |
---|
13 | import edu.uci.ics.jung.algorithms.filters.FilterUtils; |
---|
14 | |
---|
15 | |
---|
16 | public class Subgraph { |
---|
17 | |
---|
18 | private UndirectedGraph g = null; |
---|
19 | |
---|
20 | private Set<Node> nodes = null; |
---|
21 | |
---|
22 | private HashMap<Node,Integer> ID = null; |
---|
23 | private HashMap<Node,Integer> ED = null; |
---|
24 | private HashMap<Integer,List<Node>> bucketGain = null; |
---|
25 | private SortedSet<Integer> gainSet = null; |
---|
26 | private boolean recomputeGain = true; |
---|
27 | |
---|
28 | public Subgraph(UndirectedGraph g){ |
---|
29 | this.g = g; |
---|
30 | nodes = new HashSet<Node>(); |
---|
31 | ID = new HashMap<Node,Integer>(); |
---|
32 | ED = new HashMap<Node,Integer>(); |
---|
33 | bucketGain = new HashMap<Integer, List<Node>>(); |
---|
34 | gainSet = new TreeSet<Integer>(); |
---|
35 | } |
---|
36 | |
---|
37 | public Subgraph(UndirectedGraph g, Set<Node> nodes) { |
---|
38 | this.g = g; |
---|
39 | this.nodes = new HashSet<Node>(); |
---|
40 | this.ID = new HashMap<Node,Integer>(); |
---|
41 | this.ED = new HashMap<Node,Integer>(); |
---|
42 | this.bucketGain = new HashMap<Integer, List<Node>>(); |
---|
43 | this.gainSet = new TreeSet<Integer>(); |
---|
44 | Iterator<Node> nodesIterator = nodes.iterator(); |
---|
45 | while(nodesIterator.hasNext()) { |
---|
46 | addVertex(nodesIterator.next()); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public UndirectedGraph getGraph() { |
---|
51 | return g; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Adds to the subgraph the node u iff u belongs to the graph. |
---|
56 | * @param u |
---|
57 | */ |
---|
58 | public void addVertex(Node u) { |
---|
59 | if(g.containsVertex(u)) { |
---|
60 | nodes.add(u); |
---|
61 | ID.put(u, 0); |
---|
62 | ED.put(u, 0); |
---|
63 | recomputeGain = true; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | private void computeDegree() { |
---|
68 | Iterator<Node> subgraphIterator = nodes.iterator(); |
---|
69 | while(subgraphIterator.hasNext()) { |
---|
70 | Node u = subgraphIterator.next(); |
---|
71 | Iterator<Node> nborsIterator = g.getNeighbors(u).iterator(); |
---|
72 | int newID = 0; |
---|
73 | int newED = 0; |
---|
74 | while(nborsIterator.hasNext()) { |
---|
75 | Node v = nborsIterator.next(); |
---|
76 | if(contains(v)) |
---|
77 | newID = newID + g.findEdge(u, v).getWeight(); |
---|
78 | else |
---|
79 | newED = newED + g.findEdge(u, v).getWeight(); |
---|
80 | } |
---|
81 | ID.put(u, newID); |
---|
82 | ED.put(u, newED); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | private void computeGain() { |
---|
87 | bucketGain.clear(); |
---|
88 | gainSet.clear(); |
---|
89 | ID.clear(); |
---|
90 | ED.clear(); |
---|
91 | computeDegree(); |
---|
92 | Iterator<Node> subgraphIterator = nodes.iterator(); |
---|
93 | while(subgraphIterator.hasNext()) { |
---|
94 | Node u = subgraphIterator.next(); |
---|
95 | int gainU = ED.get(u) - ID.get(u); |
---|
96 | if(!bucketGain.containsKey(gainU)) { |
---|
97 | bucketGain.put(gainU, new ArrayList<Node>()); |
---|
98 | } |
---|
99 | bucketGain.get(gainU).add(u); |
---|
100 | gainSet.add(gainU); |
---|
101 | } |
---|
102 | recomputeGain = false; |
---|
103 | } |
---|
104 | |
---|
105 | public Node getCandidate() { |
---|
106 | return getCandidate(new HashSet<Node>()); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * |
---|
111 | * @param marked |
---|
112 | * @return the candidate node for swap or <code>null</code> if there aren't available nodes. |
---|
113 | */ |
---|
114 | public Node getCandidate(Set<Node> marked) { |
---|
115 | if(recomputeGain) |
---|
116 | computeGain(); |
---|
117 | Node candidate = null; |
---|
118 | Iterator<Integer> iterator = ((TreeSet<Integer>)gainSet).descendingIterator(); |
---|
119 | while(iterator.hasNext() && (candidate == null)) { |
---|
120 | int gain = iterator.next(); |
---|
121 | Iterator<Node> nodes = bucketGain.get(gain).iterator(); |
---|
122 | while(nodes.hasNext() && (candidate == null)) { |
---|
123 | Node u = nodes.next(); |
---|
124 | if(!marked.contains(u)) |
---|
125 | candidate = u; |
---|
126 | } |
---|
127 | } |
---|
128 | return candidate; |
---|
129 | } |
---|
130 | |
---|
131 | public int gain(Node u){ |
---|
132 | if(recomputeGain) |
---|
133 | computeGain(); |
---|
134 | return ED.get(u) - ID.get(u); |
---|
135 | } |
---|
136 | |
---|
137 | public void removeVertex(Node u) { |
---|
138 | if(recomputeGain) |
---|
139 | computeGain(); |
---|
140 | int gainU = gain(u); |
---|
141 | nodes.remove(u); |
---|
142 | ID.remove(u); |
---|
143 | ED.remove(u); |
---|
144 | List<Node> l = bucketGain.get(gainU); |
---|
145 | l.remove(l.indexOf(u)); |
---|
146 | if(l.size() == 0) { |
---|
147 | bucketGain.remove(gainU); |
---|
148 | gainSet.remove(gainU); |
---|
149 | } |
---|
150 | recomputeGain = true; |
---|
151 | } |
---|
152 | |
---|
153 | public int getVertexCount() { |
---|
154 | return nodes.size(); |
---|
155 | } |
---|
156 | |
---|
157 | public Edge findEdge(Node v1, Node v2) { |
---|
158 | Edge e = null; |
---|
159 | if(contains(v1) && contains(v2)) |
---|
160 | e = g.findEdge(v1, v2); |
---|
161 | return e; |
---|
162 | } |
---|
163 | |
---|
164 | public int getWeight(Node u, Node v) { |
---|
165 | int ret = -1; |
---|
166 | if(containsEdge(u,v)) |
---|
167 | ret = g.findEdge(u, v).getWeight(); |
---|
168 | return ret; |
---|
169 | } |
---|
170 | |
---|
171 | public Iterator<Node> iterator() { |
---|
172 | return nodes.iterator(); |
---|
173 | } |
---|
174 | |
---|
175 | public boolean containsEdge(Node u, Node v) { |
---|
176 | return contains(u) && contains(v) && g.containsEdge(u, v); |
---|
177 | } |
---|
178 | |
---|
179 | public boolean contains(Node u) { |
---|
180 | return nodes.contains(u); |
---|
181 | } |
---|
182 | |
---|
183 | public List<Node> getNeighbors(Node u) { |
---|
184 | List<Node> neighbors = new ArrayList<Node>(); |
---|
185 | Iterator<Node> iterator = g.getNeighbors(u).iterator(); |
---|
186 | while(iterator.hasNext()) { |
---|
187 | Node v = iterator.next(); |
---|
188 | if(contains(v) && containsEdge(u, v)) |
---|
189 | neighbors.add(v); |
---|
190 | } |
---|
191 | return neighbors; |
---|
192 | } |
---|
193 | |
---|
194 | public int getExternalDegree() { |
---|
195 | if(recomputeGain) |
---|
196 | computeGain(); |
---|
197 | int acc = 0; |
---|
198 | Iterator<Integer> it = ED.values().iterator(); |
---|
199 | while(it.hasNext()) |
---|
200 | acc = acc + it.next(); |
---|
201 | return acc; |
---|
202 | } |
---|
203 | |
---|
204 | @Override |
---|
205 | public String toString() { |
---|
206 | if(getVertexCount() == 0) |
---|
207 | return "[]"; |
---|
208 | StringBuffer out =new StringBuffer("["); |
---|
209 | Iterator<Node> it = nodes.iterator(); |
---|
210 | while(it.hasNext()) { |
---|
211 | Node u = it.next(); |
---|
212 | out.append(u.toString() + ","); |
---|
213 | } |
---|
214 | out.setLength(out.length() - 1); |
---|
215 | out.append("]"); |
---|
216 | return out.toString(); |
---|
217 | } |
---|
218 | |
---|
219 | public List<Node> getBoundary() { |
---|
220 | if(recomputeGain) |
---|
221 | computeGain(); |
---|
222 | Iterator<Entry<Node,Integer>> EDIterator = ED.entrySet().iterator(); |
---|
223 | List<Node> boundary = new ArrayList<Node>(); |
---|
224 | while(EDIterator.hasNext()) { |
---|
225 | Entry<Node,Integer> entry = EDIterator.next(); |
---|
226 | if(entry.getValue() > 0) |
---|
227 | boundary.add(entry.getKey()); |
---|
228 | } |
---|
229 | return boundary; |
---|
230 | } |
---|
231 | |
---|
232 | private Subgraph() { |
---|
233 | |
---|
234 | } |
---|
235 | |
---|
236 | @Override |
---|
237 | public Subgraph clone() { |
---|
238 | Subgraph clone = new Subgraph(); |
---|
239 | clone.g = g.clone(); |
---|
240 | |
---|
241 | clone.nodes = new HashSet<Node>(); |
---|
242 | Iterator<Node> graphIterator =clone.g.getVertices().iterator(); |
---|
243 | while(graphIterator.hasNext()) { |
---|
244 | Node u = graphIterator.next(); |
---|
245 | if(contains(u)) |
---|
246 | clone.nodes.add(u); |
---|
247 | } |
---|
248 | |
---|
249 | clone.bucketGain = new HashMap<Integer, List<Node>>(); |
---|
250 | clone.gainSet = new TreeSet<Integer>(); |
---|
251 | clone.ID = new HashMap<Node, Integer>(); |
---|
252 | clone.ED = new HashMap<Node, Integer>(); |
---|
253 | |
---|
254 | clone.computeGain(); |
---|
255 | |
---|
256 | return clone; |
---|
257 | } |
---|
258 | |
---|
259 | public UndirectedGraph createInducedSubgraph() { |
---|
260 | return FilterUtils.createInducedSubgraph(nodes,g); |
---|
261 | } |
---|
262 | |
---|
263 | public int totalDegree() { |
---|
264 | int result = 0; |
---|
265 | Iterator<Node> nodeIterator = iterator(); |
---|
266 | while(nodeIterator.hasNext()) { |
---|
267 | int degree = g.degree(nodeIterator.next()); |
---|
268 | result += degree; |
---|
269 | } |
---|
270 | return result; |
---|
271 | } |
---|
272 | } |
---|