Skip to content
Snippets Groups Projects
Commit 9be6f5d6 authored by gsavin's avatar gsavin
Browse files

Add feature to handle an non-connected graphs. In such case, result will be a...

Add feature to handle an non-connected graphs. In such case, result will be a spanning trees forest.
parent 4485ac87
Branches
No related merge requests found
......@@ -175,8 +175,43 @@ public class Prim extends AbstractSpanningTree {
Collections.sort(epool, cmp);
while (pool.size() < graph.getNodeCount()) {
if (epool.size() == 0) {
//
// This case is triggered is there are several connected
// components in the graph. A node which has not been used
// is selected to continue the process.
//
Iterator<? extends Node> nodes = this.graph.getNodeIterator();
Node toAdd = null;
while (toAdd == null && nodes.hasNext()) {
toAdd = nodes.next();
if (pool.contains(toAdd.getId()))
toAdd = null;
if (toAdd != null && toAdd.getDegree() == 0) {
pool.add(toAdd);
toAdd = null;
}
}
if (toAdd != null) {
pool.add(toAdd);
iteE = toAdd.getLeavingEdgeIterator();
while (iteE.hasNext()) {
epool.add(iteE.next());
}
}
}
e = epool.poll();
if (e == null)
throw new NullPointerException();
current = null;
if (!pool.contains(e.getNode0()))
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment