forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Depth_First_Search.py
51 lines (40 loc) · 1.08 KB
/
Depth_First_Search.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
class graph:
def __init__(self, num_vertices):
self.num_vertices = num_vertices
self.adj_list = [[] for _ in xrange(num_vertices)]
def add_edge(self, source, destination):
self.adj_list[source].append(destination)
def dfs(self, start):
visited = [False] * self.num_vertices
stack_vertex = []
visited[start] = True
stack_vertex.append(start)
while len(stack_vertex) != 0:
current = stack_vertex.pop()
print current,
for vertex in self.adj_list[current]:
if (not visited[vertex]):
visited[vertex] = True
stack_vertex.append(vertex)
# Create a graph with 8 vertices
graph = graph(8)
# Create arcs
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(1, 4)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)
graph.add_edge(3, 6)
graph.add_edge(4, 0)
graph.add_edge(4, 5)
graph.add_edge(5, 6)
graph.add_edge(5, 7)
graph.add_edge(6, 2)
graph.add_edge(7, 3)
print "Depth First Traversal is:",
graph.dfs(0)
print ""
# Expected Output
# Depth First Traversal is: 0 2 3 6 1 4 5 7