-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathgraph_depth_first_search.py
103 lines (85 loc) · 2.45 KB
/
graph_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# Date: 2017-12-28
#
# Description:
# Depth first search of a directed graph.
#
# Approach:
# Uses dictionary to store adjacency list of graph having list of adjacent
# nodes corresponding to each node.
#
# Recursion is used to visit all reachable nodes with respect to start node.
# Use a visited dictionary to keep track all nodes that are already visited.
#
# Reference:
# https://www.geeksforgeeks.org/depth-first-traversal-for-a-graph/
#
# Complexity:
# O(V + E)
# V = number of vertexes/nodes
# E = number of edges
import collections
class Graph(object):
"""Implements graph and performs DFS."""
def __init__(self):
"""Initializes empty graph having no vertexes/edges."""
self.graph = collections.defaultdict(list)
def add_edge(self, start, end):
"""Adds an edge to graph"""
self.graph[start].append(end)
def dfs_util(self, current_vertex, visited):
"""Utility function for DFS from some current vertex"""
print(current_vertex)
visited[current_vertex] = True
for x in self.graph[current_vertex]:
if visited.has_key(x):
if not visited[x]:
self.dfs_util(x, visited)
else:
pass
else:
print('{0} - dont have any adjacent vertex'.format(x))
def dfs_specific(self, start):
"""
Performs DFS traversal from a specific vertex - start .i.e will traverse
all vertexes reachable from start
"""
print('\nTraversing vertexes reachable from {0}'.format(start))
visited = {v : False for v in self.graph}
self.dfs_util(start, visited)
def dfs_all(self):
"""Performs DFS traversal such that all vertexes in a graph gets covered."""
print('\nTraversing all vertexes')
visited = {k: False for k in self.graph}
for k in self.graph:
if not visited[k]:
self.dfs_util(k, visited)
g = Graph()
g.add_edge(10, 11)
g.add_edge(10, 12)
g.add_edge(11, 13)
g.add_edge(13, 14)
g.add_edge(15, 16)
# This gives 10, 11, 13, 14, 12.
# Note that 15, 16 is not reachable from 10 so these 2 vertexes will not be
# printed.
g.dfs_specific(10)
# To traverse all vertexes of a graph we will use modified dfs.
g.dfs_all()
# Output:
# -----------------------
# Traversing vertexes reachable from 10
# 10
# 11
# 13
# 14 - dont have any adjacent vertex
# 12 - dont have any adjacent vertex
#
# Traversing all vertexes
# 10
# 11
# 13
# 14 - dont have any adjacent vertex
# 12 - dont have any adjacent vertex
# 15
# 16 - dont have any adjacent vertex