Update Graph.java

for dfsWithoutRecursion, when a node popped from the stack it should be checked that the current node is visited or not. Since stack has duplicated nodes, some nodes is visired doubly.
This commit is contained in:
Ömer 2021-06-05 23:04:19 +03:00 committed by GitHub
parent ab93764e9c
commit 812057b43c
1 changed files with 7 additions and 5 deletions

View File

@ -29,6 +29,7 @@ public class Graph {
stack.push(start);
while (!stack.isEmpty()) {
int current = stack.pop();
if(!isVisited[current]){
isVisited[current] = true;
visit(current);
for (int dest : adjVertices.get(current)) {
@ -37,6 +38,7 @@ public class Graph {
}
}
}
}
public void dfs(int start) {
boolean[] isVisited = new boolean[adjVertices.size()];