-
Notifications
You must be signed in to change notification settings - Fork 134
/
DetectCycleDirected.py
59 lines (45 loc) · 1.5 KB
/
DetectCycleDirected.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
# Program to detect cycle or loop in a directed graph
from collections import defaultdict
class Graph:
def __init__(self, directed=False):
self.graph = defaultdict(list)
self.directed = directed
def addEdge(self, frm, to):
self.graph[frm].append(to)
if self.directed is False:
self.graph[to].append(frm)
else:
self.graph[to] = self.graph[to]
def isCyclicUtil(self, s, visited, recurStack):
if visited[s] is False:
recurStack[s] = True
visited[s] = True
# traverse vertices adjacent to vertex
for i in self.graph[s]:
if (not visited[i]) and self.isCyclicUtil(i, visited, recurStack):
return True
elif recurStack[i]:
return True
recurStack[s] = False
return False
def isCyclic(self):
visited = {i: False for i in self.graph}
recurStack = {i: False for i in self.graph}
# traverse for all the vertices of graph
for v in self.graph:
if self.isCyclicUtil(v, visited, recurStack):
return True
return False
if __name__ == '__main__':
# make a directed graph
graph = Graph(True)
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(1, 2)
graph.addEdge(2, 0)
graph.addEdge(2, 3)
graph.addEdge(3, 3)
if graph.isCyclic():
print("Cycle exists")
else:
print("No cycle in the graph")