2
2
# Created On: 12th August 2017
3
3
from collections import defaultdict
4
4
5
-
6
5
class Graph (object ):
6
+ ''' class for creating a graph '''
7
7
def __init__ (self ):
8
8
self .graph = defaultdict (list )
9
9
self .count = 0
@@ -25,7 +25,7 @@ def get_code(self):
25
25
return inspect .getsource (Graph )
26
26
27
27
28
- class WeightedGraph :
28
+ class WeightedGraph ( object ) :
29
29
"""
30
30
A graph with a numerical value (weight) on edges
31
31
"""
@@ -77,11 +77,12 @@ def kruskal_mst(self):
77
77
return edges_explored
78
78
79
79
@staticmethod
80
- def kruskal_complexity ():
80
+ def kruskal_time_complexity ():
81
81
return '''Worst case: O(E log(V)) where E in the number of edges and V the number of vertexes'''
82
82
83
83
@classmethod
84
84
def kruskal_code (cls ):
85
+ ''' Returns the code for current class '''
85
86
import inspect
86
87
return inspect .getsource (cls .kruskal_mst )
87
88
@@ -94,11 +95,11 @@ def topological_sort(self):
94
95
for vertex in range (self .count ):
95
96
# Call the recursive function only if not visited
96
97
if not visited [vertex ]:
97
- self .topological_sort_rec (vertex , visited , stack )
98
+ self ._topological_sort_rec (vertex , visited , stack )
98
99
99
100
return stack
100
101
101
- def topological_sort_rec (self , vertex , visited , stack ):
102
+ def _topological_sort_rec (self , vertex , visited , stack ):
102
103
''' Recursive function for topological Sort '''
103
104
# Mark the current node in visited
104
105
visited [vertex ] = True
@@ -107,7 +108,7 @@ def topological_sort_rec(self, vertex, visited, stack):
107
108
try :
108
109
for adjacent_node in self .graph [vertex ]:
109
110
if visited [adjacent_node ] == False :
110
- self .topological_sort_rec (adjacent_node , visited , stack )
111
+ self ._topological_sort_rec (adjacent_node , visited , stack )
111
112
except KeyError :
112
113
return
113
114
@@ -121,6 +122,7 @@ def get_code(self):
121
122
122
123
123
124
class CheckCycleDirectedGraph (object ):
125
+ ''' Class to check cycle in directed graph '''
124
126
def __init__ (self ):
125
127
self .graph = {}
126
128
self .count = 0
@@ -146,11 +148,11 @@ def check_cycle(self):
146
148
stack = [False ] * len (self .graph )
147
149
for vertex in range (len (self .graph )):
148
150
if visited [vertex ] == False :
149
- if self .check_cycle_rec (visited , stack , vertex ) == True :
151
+ if self ._check_cycle_rec (visited , stack , vertex ) == True :
150
152
return True
151
153
return False
152
154
153
- def check_cycle_rec (self , visited , stack , vertex ):
155
+ def _check_cycle_rec (self , visited , stack , vertex ):
154
156
''' Recursive function for finding the cycle '''
155
157
# Mark the current node in visited and also add it to the stack
156
158
visited [vertex ] = True
@@ -159,7 +161,7 @@ def check_cycle_rec(self, visited, stack, vertex):
159
161
# mark all adjacent nodes of the current node
160
162
for adjacentNode in self .graph [vertex ]:
161
163
if visited [adjacentNode ] == False :
162
- if self .check_cycle_rec (visited , stack , adjacentNode ) == True :
164
+ if self ._check_cycle_rec (visited , stack , adjacentNode ) == True :
163
165
return True
164
166
elif stack [adjacentNode ] == True :
165
167
return True
@@ -176,6 +178,7 @@ def get_code(self):
176
178
177
179
178
180
class CheckCycleUndirectedGraph (object ):
181
+ ''' Class to check cycle in undirected graph '''
179
182
def __init__ (self ):
180
183
self .graph = {}
181
184
self .count = 0
@@ -202,19 +205,19 @@ def check_cycle(self):
202
205
for vertex in range (len (self .graph )):
203
206
# Call the recursive function only if not visited
204
207
if visited [vertex ] == False :
205
- if self .check_cycle_rec (visited , - 1 , vertex ) == True :
208
+ if self ._check_cycle_rec (visited , - 1 , vertex ) == True :
206
209
return True
207
210
return False
208
211
209
- def check_cycle_rec (self , visited , parent , vertex ):
212
+ def _check_cycle_rec (self , visited , parent , vertex ):
210
213
''' Recursive function for finding the cycle '''
211
214
# Mark the current node in visited
212
215
visited [vertex ] = True
213
216
214
217
# mark all adjacent nodes of the current node
215
218
for adjacentNode in self .graph [vertex ]:
216
219
if visited [adjacentNode ] == False :
217
- if self .check_cycle_rec (visited , vertex , adjacentNode ) == True :
220
+ if self ._check_cycle_rec (visited , vertex , adjacentNode ) == True :
218
221
return True
219
222
elif parent != adjacentNode :
220
223
return True
0 commit comments