Skip to content

Commit

Permalink
Correct the wrong iterative DFS implementation (TheAlgorithms#867)
Browse files Browse the repository at this point in the history
* Update DFS.py

* Update DFS.py
  • Loading branch information
AtomicVar authored and poyea committed Jun 4, 2019
1 parent c2552cd commit f9b8dbf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions graphs/DFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ 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 in explored:
continue

explored.add(v)

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

Expand Down

0 comments on commit f9b8dbf

Please sign in to comment.