211 |
*/ |
*/ |
212 |
public static Object[] findComponents(Iterator nodes, Object property, |
public static Object[] findComponents(Iterator nodes, Object property, |
213 |
ConstGraph g) { |
ConstGraph g) { |
214 |
|
return findComponents(nodes, property, g, null); |
215 |
|
} |
216 |
|
|
217 |
|
/** From given nodes, finds components disconnected along a given |
218 |
|
* non-directed property, |
219 |
|
* and for each component returns the node of highest degree as a |
220 |
|
* representative. Additionally, gives the representative of the |
221 |
|
* largest component. The representatives are tried to be found |
222 |
|
* in the given candidate node set. |
223 |
|
* @return an array: |
224 |
|
* the first element is a <code>Set</code> of representatives, |
225 |
|
* the other element is the representative of largest component |
226 |
|
*/ |
227 |
|
public static Object[] findComponents(Iterator nodes, Object property, |
228 |
|
ConstGraph g, Set candidates) { |
229 |
Set visited = new HashSet(); |
Set visited = new HashSet(); |
230 |
Set components = new HashSet(); |
Set components = new HashSet(); |
231 |
Object largest = null; |
Object largest = null; |
238 |
int visitedsize = visited.size(); |
int visitedsize = visited.size(); |
239 |
visited.add(node); |
visited.add(node); |
240 |
representative = recurseDFS(node, property, g, visited, |
representative = recurseDFS(node, property, g, visited, |
241 |
|
candidates, |
242 |
new DFSRet()).representative; |
new DFSRet()).representative; |
243 |
int growth = visited.size() - visitedsize; |
int growth = visited.size() - visitedsize; |
244 |
if(growth > largestsize) { |
if(growth > largestsize) { |
245 |
largestsize = growth; |
largestsize = growth; |
246 |
largest = representative; |
largest = representative; |
247 |
} |
} |
248 |
components.add(representative); |
if(candidates == null || representative != null) |
249 |
|
components.add(representative); |
250 |
|
else |
251 |
|
components.add(node); |
252 |
} |
} |
253 |
} |
} |
254 |
return new Object[] {components, largest}; |
return new Object[] {components, largest}; |
261 |
* <code>degree</code> is the highest degree |
* <code>degree</code> is the highest degree |
262 |
*/ |
*/ |
263 |
static DFSRet recurseDFS(Object start, Object property, ConstGraph g, |
static DFSRet recurseDFS(Object start, Object property, ConstGraph g, |
264 |
Set visited, DFSRet ret) { |
Set visited, Set candidates, DFSRet ret) { |
265 |
Object representative = null; |
Object representative = null; |
266 |
int representativeDegree = -1; |
int representativeDegree = -1; |
267 |
Iterator conns = findConnected_Iter(g, start, property); |
Iterator conns = findConnected_Iter(g, start, property); |
271 |
degree++; |
degree++; |
272 |
if(!visited.contains(found)) { |
if(!visited.contains(found)) { |
273 |
visited.add(found); |
visited.add(found); |
274 |
ret = recurseDFS(found, property, g, visited, ret); |
ret = recurseDFS(found, property, g, visited, candidates, ret); |
275 |
if(ret.degree > representativeDegree) { |
if((candidates == null || candidates.contains(found)) |
276 |
|
&& ret.degree > representativeDegree) { |
277 |
representativeDegree = ret.degree; |
representativeDegree = ret.degree; |
278 |
representative = ret.representative; |
representative = ret.representative; |
279 |
} |
} |
282 |
if(representativeDegree > degree) { |
if(representativeDegree > degree) { |
283 |
ret.representative = representative; |
ret.representative = representative; |
284 |
ret.degree = representativeDegree; |
ret.degree = representativeDegree; |
285 |
} else { |
} else if (candidates == null || candidates.contains(start)) { |
286 |
ret.representative = start; |
ret.representative = start; |
287 |
ret.degree = degree; |
ret.degree = degree; |
288 |
|
} else { |
289 |
|
// Not a single node was a candidate, so we have to return null |
290 |
|
ret.representative = null; |
291 |
|
ret.degree = -1; |
292 |
} |
} |
293 |
return ret; |
return ret; |
294 |
} |
} |