22# Created On: 12th August 2017
33from collections import defaultdict
44
5-
65class Graph (object ):
6+ ''' class for creating a graph '''
77 def __init__ (self ):
88 self .graph = defaultdict (list )
99 self .count = 0
@@ -25,7 +25,7 @@ def get_code(self):
2525 return inspect .getsource (Graph )
2626
2727
28- class WeightedGraph :
28+ class WeightedGraph ( object ) :
2929 """
3030 A graph with a numerical value (weight) on edges
3131 """
@@ -77,11 +77,12 @@ def kruskal_mst(self):
7777 return edges_explored
7878
7979 @staticmethod
80- def kruskal_complexity ():
80+ def kruskal_time_complexity ():
8181 return '''Worst case: O(E log(V)) where E in the number of edges and V the number of vertexes'''
8282
8383 @classmethod
8484 def kruskal_code (cls ):
85+ ''' Returns the code for current class '''
8586 import inspect
8687 return inspect .getsource (cls .kruskal_mst )
8788
@@ -94,11 +95,11 @@ def topological_sort(self):
9495 for vertex in range (self .count ):
9596 # Call the recursive function only if not visited
9697 if not visited [vertex ]:
97- self .topological_sort_rec (vertex , visited , stack )
98+ self ._topological_sort_rec (vertex , visited , stack )
9899
99100 return stack
100101
101- def topological_sort_rec (self , vertex , visited , stack ):
102+ def _topological_sort_rec (self , vertex , visited , stack ):
102103 ''' Recursive function for topological Sort '''
103104 # Mark the current node in visited
104105 visited [vertex ] = True
@@ -107,7 +108,7 @@ def topological_sort_rec(self, vertex, visited, stack):
107108 try :
108109 for adjacent_node in self .graph [vertex ]:
109110 if visited [adjacent_node ] == False :
110- self .topological_sort_rec (adjacent_node , visited , stack )
111+ self ._topological_sort_rec (adjacent_node , visited , stack )
111112 except KeyError :
112113 return
113114
@@ -121,6 +122,7 @@ def get_code(self):
121122
122123
123124class CheckCycleDirectedGraph (object ):
125+ ''' Class to check cycle in directed graph '''
124126 def __init__ (self ):
125127 self .graph = {}
126128 self .count = 0
@@ -146,11 +148,11 @@ def check_cycle(self):
146148 stack = [False ] * len (self .graph )
147149 for vertex in range (len (self .graph )):
148150 if visited [vertex ] == False :
149- if self .check_cycle_rec (visited , stack , vertex ) == True :
151+ if self ._check_cycle_rec (visited , stack , vertex ) == True :
150152 return True
151153 return False
152154
153- def check_cycle_rec (self , visited , stack , vertex ):
155+ def _check_cycle_rec (self , visited , stack , vertex ):
154156 ''' Recursive function for finding the cycle '''
155157 # Mark the current node in visited and also add it to the stack
156158 visited [vertex ] = True
@@ -159,7 +161,7 @@ def check_cycle_rec(self, visited, stack, vertex):
159161 # mark all adjacent nodes of the current node
160162 for adjacentNode in self .graph [vertex ]:
161163 if visited [adjacentNode ] == False :
162- if self .check_cycle_rec (visited , stack , adjacentNode ) == True :
164+ if self ._check_cycle_rec (visited , stack , adjacentNode ) == True :
163165 return True
164166 elif stack [adjacentNode ] == True :
165167 return True
@@ -176,6 +178,7 @@ def get_code(self):
176178
177179
178180class CheckCycleUndirectedGraph (object ):
181+ ''' Class to check cycle in undirected graph '''
179182 def __init__ (self ):
180183 self .graph = {}
181184 self .count = 0
@@ -202,19 +205,19 @@ def check_cycle(self):
202205 for vertex in range (len (self .graph )):
203206 # Call the recursive function only if not visited
204207 if visited [vertex ] == False :
205- if self .check_cycle_rec (visited , - 1 , vertex ) == True :
208+ if self ._check_cycle_rec (visited , - 1 , vertex ) == True :
206209 return True
207210 return False
208211
209- def check_cycle_rec (self , visited , parent , vertex ):
212+ def _check_cycle_rec (self , visited , parent , vertex ):
210213 ''' Recursive function for finding the cycle '''
211214 # Mark the current node in visited
212215 visited [vertex ] = True
213216
214217 # mark all adjacent nodes of the current node
215218 for adjacentNode in self .graph [vertex ]:
216219 if visited [adjacentNode ] == False :
217- if self .check_cycle_rec (visited , vertex , adjacentNode ) == True :
220+ if self ._check_cycle_rec (visited , vertex , adjacentNode ) == True :
218221 return True
219222 elif parent != adjacentNode :
220223 return True
0 commit comments