Skip to content

Instantly share code, notes, and snippets.

@phc15
Last active October 1, 2021 06:21
Show Gist options
  • Save phc15/b52a4e761f98e328a6eb9bd85753cb95 to your computer and use it in GitHub Desktop.
Save phc15/b52a4e761f98e328a6eb9bd85753cb95 to your computer and use it in GitHub Desktop.
void DFS(Graph graph, int s, boolean[] visited) {
Stack<Integer> stack = new Stack<Integer>();
visited[s] = true;
stack.push(s);
while (!stack.isEmpty()) {
int S = stack.pop();
System.out.println("Visited vertex: " + S);
for (int n : graph.adj.get(S)) {
if (!visited[n]) {
visited[n] = true;
stack.push(n);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment