-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.py
44 lines (32 loc) · 896 Bytes
/
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
from collections import defaultdict
class graph:
def __init__(self):
self.graph = defaultdict(list)
def AddEdge(self,src,dest):
self.graph[src].append(dest)
def DFS(self,index,visited):
#create a queue and visted array
visited[index] = True
print(index,end='')
for i in self.graph[index]:
if visited[i] == False:
self.DFS(i,visited)
def DFStravesal(self,index):
visited = [False]*(len(self.graph))
self.DFS(index,visited)
Graph = graph()
Graph.AddEdge(0,1)
Graph.AddEdge(1,0)
Graph.AddEdge(0,2)
Graph.AddEdge(2,0)
Graph.AddEdge(1,3)
Graph.AddEdge(3,1)
Graph.AddEdge(1,4)
Graph.AddEdge(4,1)
Graph.AddEdge(3,4)
Graph.AddEdge(4,3)
Graph.AddEdge(3,5)
Graph.AddEdge(5,3)
Graph.AddEdge(4,5)
Graph.AddEdge(4,5)
Graph.DFStravesal(0)