forked from K1ng04/DepthFirstSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.py
More file actions
33 lines (29 loc) · 695 Bytes
/
dfs.py
File metadata and controls
33 lines (29 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
graph = {1: [2,3],
2: [4, 5],
3: [6],
4: [],
5: [6],
6: [7]
}
visited = []
queue = []
def dfs(visited, graph, val):
"""
Function used to illustrate the Depth First Search Algorithm Using Python
1
/ \
2 3
/ \ \
4 5-> 6
\
7
"""
if val not in visited:
print(val)
visited.append(val)
try:
for neighbour in graph[val]:
dfs(visited, graph, neighbour)
except:
pass
dfs(visited, graph, 1)