diff --git a/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java b/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java index 9504289a703259db68c28ad8783f63c2ca125eac..7476a1893dc5dd60b22fc52950cf568692114452 100644 --- a/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java +++ b/mapbuilder/src/main/java/map/builder/utilities/ConnectedComponentGraph.java @@ -63,6 +63,44 @@ public class ConnectedComponentGraph { return list; } + public ArrayList<Long> DFS(Long s, HashMap<Long, Boolean> visited) { + // Create a stack for DFS + Stack<Long> stack = new Stack<Long>(); + ArrayList<Long> vertices = new ArrayList<Long>(); + + // Push the current source node + stack.push(s); + + while (stack.empty() == false) { + // Pop a vertex from stack and print it + s = stack.peek(); + stack.pop(); + + // Stack may contain same vertex twice. So + // we need to print the popped item only + // if it is not visited. + if (visited.get(s) == false) { + System.out.print(s + " "); + visited.put(s, true); + vertices.add(s); + } + + // Get all adjacent vertices of the popped vertex s + // If a adjacent has not been visited, then push it + // to the stack. + Iterator<Long> itr = adj.get(s).iterator(); + + while (itr.hasNext()) { + Long v = itr.next(); + if (!visited.get(v)) + stack.push(v); + } + + } + + return vertices; + } + // Function that returns reverse (or transpose) of this ConnectedComponentGraph public ConnectedComponentGraph getTranspose() { ConnectedComponentGraph g = new ConnectedComponentGraph(); @@ -130,7 +168,7 @@ public class ConnectedComponentGraph { // Print Strongly connected component of the popped vertex if (visited.get(v) == false) { - ArrayList<Long> component = gr.DFSUtil(v, visited); + ArrayList<Long> component = gr.DFS(v, visited); if (maxLength < component.size()) { maxLength = component.size(); largestComponent = component;