Skip to content
Snippets Groups Projects
Commit df1ade8c authored by kraleva's avatar kraleva
Browse files

fix fillorder function

parent 5958db90
Branches
No related tags found
1 merge request!29IterativeSCC
...@@ -135,6 +135,36 @@ public class ConnectedComponentGraph { ...@@ -135,6 +135,36 @@ public class ConnectedComponentGraph {
stack.push(v); 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 // The main function that finds and prints all strongly
// connected components // connected components
public ArrayList<Long> getSCCs() { public ArrayList<Long> getSCCs() {
...@@ -148,9 +178,10 @@ public class ConnectedComponentGraph { ...@@ -148,9 +178,10 @@ public class ConnectedComponentGraph {
// Fill vertices in stack according to their finishing // Fill vertices in stack according to their finishing
// times // times
for (Long i : this.adj.keySet()) for (Long i : this.adj.keySet())
if (visited.get(i) == false) if (visited.get(i) == false)
fillOrder(i, visited, stack); fillOrderIterative(i, visited, stack);
// Create a reversed ConnectedComponentGraph // Create a reversed ConnectedComponentGraph
ConnectedComponentGraph gr = getTranspose(); ConnectedComponentGraph gr = getTranspose();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment