-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycledg.py
60 lines (39 loc) · 904 Bytes
/
cycledg.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
from collections import defaultdict
class Graph:
"""docstring for Graph"""
def __init__(self, ver):
# super(Graph, self).__init__()
self.ver = ver
self.graph=defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def cycleutil(self,v,visited,rec):
visited[v]=True
rec[v]=True
for i in self.graph[v]:
if visited[i]==False:
if self.cycleutil(i,visited,rec):
return True
elif rec[i]==True:
return True
rec[v]=False
return False
def cycle(self):
visited=[False]*self.ver
rec=[False]*self.ver
for i in range(self.ver):
if visited[i]==False:
if self.cycleutil(i,visited,rec)==True:
return True
return False
g = Graph(4)
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
if g.cycle():
print ("Graph has a cycle")
else:
print( "Graph has no cycle")