We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c2552cd commit f9b8dbfCopy full SHA for f9b8dbf
graphs/DFS.py
@@ -18,10 +18,15 @@ def dfs(graph, start):
18
explored, stack = set(), [start]
19
explored.add(start)
20
while stack:
21
- 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
22
+
23
+ if v in explored:
24
+ continue
25
26
+ explored.add(v)
27
28
for w in graph[v]:
29
if w not in explored:
- explored.add(w)
30
stack.append(w)
31
return explored
32
0 commit comments