MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / dfs

Function dfs

graphs/basic_graphs.py:79–100  ·  view source on GitHub ↗

>>> dfs({1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}, 1) 1 2 4 5 3

(g, s)

Source from the content-addressed store, hash-verified

77
78
79def dfs(g, s):
80 """
81 >>> dfs({1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}, 1)
82 1
83 2
84 4
85 5
86 3
87 """
88 vis, _s = {s}, [s]
89 print(s)
90 while _s:
91 flag = 0
92 for i in g[_s[-1]]:
93 if i not in vis:
94 _s.append(i)
95 vis.add(i)
96 flag = 1
97 print(i)
98 break
99 if not flag:
100 _s.pop()
101
102
103"""

Callers

nothing calls this directly

Calls 3

appendMethod · 0.45
addMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected