-
Notifications
You must be signed in to change notification settings - Fork 73
/
using-graph-dfs.py
47 lines (37 loc) · 1.05 KB
/
using-graph-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
# A program to print 'Hello, World!' by Depth-first search in graph
class Graph:
def __init__(self):
self.graph = {}
def addEdge(self, uid, value, connections):
vertex = {
"uid": uid,
"value": value,
"connections": connections
}
self.graph[uid] = vertex
def DFS(self, visited, start, goal):
visited.append(self.graph[start])
print(self.graph[start]["value"], end='')
for neighbour in self.graph[start]["connections"]:
if neighbour not in visited:
self.DFS(visited, neighbour, goal)
def start_DFS(self, start, goal):
visited = []
self.DFS(visited, start, goal)
print()
#
graph = Graph()
graph.addEdge(0, 'h', [1, 7])
graph.addEdge(1, 'e', [2, 5])
graph.addEdge(2, 'l', [3, 4])
graph.addEdge(3, 'l', [])
graph.addEdge(4, 'o', [])
graph.addEdge(5, ',', [6])
graph.addEdge(6, ' ', [])
graph.addEdge(7, 'w', [8])
graph.addEdge(8, 'o', [9, 12])
graph.addEdge(9, 'r', [10, 11])
graph.addEdge(10, 'l', [])
graph.addEdge(11, 'd', [])
graph.addEdge(12, '!', [])
graph.start_DFS(0, 12)