Skip to content

Correct the wrong iterative DFS implementation #867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update DFS.py
  • Loading branch information
ZJUGuoShuai authored Jun 2, 2019
commit e5552e5af9667a246459c2b5e174660b1a5981cc
7 changes: 5 additions & 2 deletions graphs/DFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ def dfs(graph, start):
explored, stack = set(), [start]
explored.add(start)
while stack:
v = stack.pop() # the only difference from BFS is to pop last element here instead of first one
v = stack.pop() # one difference from BFS is to pop last element here instead of first one

if v not in explored:
explored.add(v)

for w in graph[v]:
if w not in explored:
explored.add(w)
stack.append(w)
return explored

Expand Down