diff --git a/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java b/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java index 7476a1893dc5dd60b22fc52950cf568692114452..fce80a39b83fc3cd19f02d93611102b0274c0351 100644 --- a/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java +++ b/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java @@ -135,6 +135,36 @@ public class ConnectedComponentGraph { stack.push(v); } + public void fillOrderIterative(Long v, HashMap<Long, Boolean> visited, Stack<Long> stack) { + // Create a stack for filling + Stack<Long> helperStack = new Stack<Long>(); + + // Push the current source node + helperStack.push(v); + while (helperStack.empty() == false) { + // Pop a vertex from stack and print it + v = helperStack.peek(); + helperStack.pop(); + // Stack may contain same vertex twice. So + // we need to print the popped item only + // if it is not visited. + if (visited.get(v) == false) { + System.out.print(v + " "); + visited.put(v, true); + } + + Iterator<Long> itr = adj.get(v).iterator(); + + while (itr.hasNext()) { + Long s = itr.next(); + if (!visited.get(s)) + helperStack.push(s); + } + + stack.push(v); + } + } + // The main function that finds and prints all strongly // connected components public ArrayList<Long> getSCCs() { @@ -148,9 +178,10 @@ public class ConnectedComponentGraph { // Fill vertices in stack according to their finishing // times + for (Long i : this.adj.keySet()) if (visited.get(i) == false) - fillOrder(i, visited, stack); + fillOrderIterative(i, visited, stack); // Create a reversed ConnectedComponentGraph ConnectedComponentGraph gr = getTranspose();