mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Correct the wrong iterative DFS implementation (#867)
* Update DFS.py * Update DFS.py
This commit is contained in:
parent
c2552cdfcd
commit
f9b8dbf9db
@ -18,10 +18,15 @@ def dfs(graph, start):
|
|||||||
explored, stack = set(), [start]
|
explored, stack = set(), [start]
|
||||||
explored.add(start)
|
explored.add(start)
|
||||||
while stack:
|
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]:
|
for w in graph[v]:
|
||||||
if w not in explored:
|
if w not in explored:
|
||||||
explored.add(w)
|
|
||||||
stack.append(w)
|
stack.append(w)
|
||||||
return explored
|
return explored
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user