Skip to content

Commit 098c360

Browse files
committed
Added functions to check cycle in directed and undirected graph
1 parent 09af5d4 commit 098c360

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

pygorithm/data_structures/graph.py

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Author: OMKAR PATHAK
22
# Created On: 12th August 2017
33

4-
class AdjacencyList(object):
4+
class Graph(object):
55
def __init__(self):
66
self.graph = {}
77
self.count = 0
@@ -24,9 +24,9 @@ def add_edge(self, from_vertex, to_vertex):
2424
def get_code(self):
2525
''' returns the code for the current class '''
2626
import inspect
27-
return inspect.getsource(AdjacencyList)
27+
return inspect.getsource(Graph)
2828

29-
class TopologicalSort(AdjacencyList):
29+
class TopologicalSort(Graph):
3030
def topological_sort(self):
3131
visited = [False] * self.count # Marking all vertices as not visited
3232
stack = [] # Stack for storing the vertex
@@ -58,3 +58,103 @@ def get_code(self):
5858
''' returns the code for the current class '''
5959
import inspect
6060
return inspect.getsource(TopologicalSort)
61+
62+
class CheckCycleDirected(object):
63+
def __init__(self):
64+
self.graph = {}
65+
self.count = 0
66+
67+
def print_graph(self):
68+
''' for printing the contents of the graph '''
69+
for i in self.graph:
70+
print(i,'->',' -> '.join([str(j) for j in self.graph[i]]))
71+
72+
def add_edge(self, from_vertex, to_vertex):
73+
''' function to add an edge in the graph '''
74+
# check if vertex is already present
75+
if from_vertex in self.graph.keys():
76+
self.graph[from_vertex].append(to_vertex)
77+
self.count += 1
78+
else:
79+
self.graph[from_vertex] = [to_vertex]
80+
self.count += 1
81+
82+
def check_cycle(self):
83+
''' This function will return True if graph is cyclic else return False '''
84+
visited = [False] * len(self.graph)
85+
stack = [False] * len(self.graph)
86+
for vertex in range(len(self.graph)):
87+
if visited[vertex] == False:
88+
if self.check_cycle_rec(visited, stack, vertex) == True:
89+
return True
90+
return False
91+
92+
def check_cycle_rec(self, visited, stack, vertex):
93+
''' Recursive function for finding the cycle '''
94+
# Mark the current node in visited and also add it to the stack
95+
visited[vertex] = True
96+
stack[vertex] = True
97+
98+
# mark all adjacent nodes of the current node
99+
for adjacentNode in self.graph[vertex]:
100+
if visited[adjacentNode] == False:
101+
if self.check_cycle_rec(visited, stack, adjacentNode) == True:
102+
return True
103+
elif stack[adjacentNode] == True:
104+
return True
105+
106+
# The node needs to be poped from
107+
# recursion stack before function ends
108+
stack[vertex] = False
109+
return False
110+
111+
def get_code(self):
112+
''' returns the code for the current class '''
113+
import inspect
114+
return inspect.getsource(CheckCycleDirected)
115+
116+
class CheckCycleUndirected(object):
117+
def __init__(self):
118+
self.graph = {}
119+
self.count = 0
120+
121+
def print_graph(self):
122+
''' for printing the contents of the graph '''
123+
for i in self.graph:
124+
print(i,'->',' -> '.join([str(j) for j in self.graph[i]]))
125+
126+
def add_edge(self, fromVertex, toVertex):
127+
''' for adding the edge beween two vertices '''
128+
# check if vertex is already present,
129+
if fromVertex in self.graph.keys() and toVertex in self.graph.keys():
130+
self.graph[fromVertex].append(toVertex)
131+
self.graph[toVertex].append(fromVertex)
132+
else:
133+
# else make a new vertex
134+
self.graph[fromVertex] = [toVertex]
135+
self.graph[toVertex] = [fromVertex]
136+
137+
def check_cycle(self):
138+
''' This function will return True if graph is cyclic else return False '''
139+
visited = [False] * len(self.graph) # Marking all vertices as not visited
140+
for vertex in range(len(self.graph)):
141+
# Call the recursive function only if not visited
142+
if visited[vertex] == False:
143+
if self.check_cycle_rec(visited, -1, vertex) == True:
144+
return True
145+
return False
146+
147+
def check_cycle_rec(self, visited, parent, vertex):
148+
''' Recursive function for finding the cycle '''
149+
# Mark the current node in visited
150+
visited[vertex] = True
151+
152+
# mark all adjacent nodes of the current node
153+
for adjacentNode in self.graph[vertex]:
154+
if visited[adjacentNode] == False:
155+
if self.check_cycle_rec(visited, vertex, adjacentNode) == True:
156+
return True
157+
elif parent != adjacentNode:
158+
return True
159+
160+
return False

0 commit comments

Comments
 (0)