-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepthFirstSearch.py
57 lines (50 loc) · 1.49 KB
/
depthFirstSearch.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
53
54
55
56
57
from pprint import pprint
def DFS(graph, node, discovered):
process_node(node)
for adjacent in graph[node]:
if adjacent not in discovered:
discovered[adjacent] = node
DFS(graph, adjacent, discovered)
def process_node(node):
print(f"Visiting node {node}")
def construct_path(u, v, discovered):
path = []
if v in discovered:
path.append(v)
walk = v
while walk is not u:
parent = discovered[walk]
path.append(parent)
walk = parent
path.reverse()
return path
def DFS_complete(graph):
"""Perform DFS for entire graph and return forest as a dictionary"""
forest = {}
for vertex in graph.keys():
print(f"creating tree for node: {vertex}")
if vertex not in forest:
forest[vertex] = None
DFS(graph, vertex, forest)
return forest
if __name__ == '__main__':
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': [],
'G': ['H'],
'H': ['G']
}
# # DFS
# discovered = {'A': None}
# DFS(graph, 'A', discovered)
# print(f"discovered: {discovered}")
# # Constructing path between nodes using DFS output
# print("Path from A to F is: ", end="")
# print(construct_path('A', 'F', discovered))
# Creating forest of discovery trees for entire graph
print("Constructing forest for entire graph...")
print(DFS_complete(graph))