-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.py
52 lines (40 loc) · 1.4 KB
/
DFS.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''
we will create a set for storing the value of the visited nodes to keep track of the visited nodes of the graph.
After the above process, we will declare a function with the parameters as visited nodes, the graph itself and the node respectively. And inside the function, we will check whether any node of the graph is visited or not using the “if” condition. If not, then we will print the node and add it to the visited set of nodes.
Then we will go to the neighboring node of the graph and again call the DFS function to use the neighbor parameter.
NOTE: for this implementation to work,
you have to mark leaf node in a self loop while defining the graph
else it will give KeyError
'''
visited = set() # Set to keep track of visited nodes of graph.
def dfs(visited, graph, node): # function for dfs
if node not in visited:
print(node, end = " ")
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
# eg 1
# graph = {
# '5': ['3', '7'],
# '3': ['2', '4'],
# '7': ['8'],
# '2': [],
# '4': ['8'],
# '8': []
# }
# print("Following is the Depth-First Search")
# dfs(visited, graph, '5')
# op 1
# 5 3 2 4 8 7
# eg 2
graph2 = {
'0': ['1', '9'],
'1': ['2'],
'2': ['0', '3'],
'9': ['3'],
'3': ['3']
}
print("\nFollowing is the Depth-First Search for graph 2")
dfs(visited, graph2, '0')
# op2
# 0 1 2 3 9