-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycleug.py
57 lines (43 loc) · 1013 Bytes
/
cycleug.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
from collections import defaultdict
class Graph:
"""docstring for gra"""
def __init__(self, ver):
# super(gra, self).__init__()
self.ver = ver
self.graph=defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
self.graph[v].append(u)
def cycleutil(self,v,visited,parent):
visited[v]=True
for i in self.graph[v]:
if visited[i]==False:
if self.cycleutil(i,visited,v):
return True
elif parent!=i:
return True
return False
def cycle(self):
visited=[False]*self.ver
for i in range(self.ver):
if visited[i]==False:
if self.cycleutil(i,visited,-1)==True:
return True
return False
g = Graph(5)
g.addEdge(1, 0)
g.addEdge(0, 2)
g.addEdge(2, 0)
g.addEdge(0, 3)
g.addEdge(3, 4)
if g.cycle():
print ("Graph contains cycle")
else :
print ("Graph does not contain cycle ")
g1 = Graph(3)
g1.addEdge(0,1)
g1.addEdge(1,2)
if g1.cycle():
print ("Graph contains cycle")
else :
print ("Graph does not contain cycle ")