-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle-detection.py
More file actions
57 lines (48 loc) · 1.3 KB
/
cycle-detection.py
File metadata and controls
57 lines (48 loc) · 1.3 KB
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
class Graph:
def __init__(self):
self.edges={}
self.vertices=[]
def addNode(self,i,j):
if i not in self.vertices:
self.vertices.append(i)
#self.edges.append(i)
if i not in self.edges:
self.edges[i] = []
if j not in self.vertices:
self.vertices.append(j)
# if j not in self.edges:
# self.edges[j]=[]
self.edges[i].append(j)
#self.edges[j].append(i)
def cycleCheck(g):
visited=[False]*(len(g.vertices)+1)
for root in g.vertices:
if visited[root]==False:
return helper(g,visited,root)
return True
def helper(g,visited,root):
return dfs(g,root,visited)
def dfs(g,root,visited):
visited[root]=True
if root not in g.edges or len(g.edges[root])<=0:
return True
for v in g.edges[root]:
if visited[v]==True:
return False
if dfs(g,v,visited) ==False:
return False
visited[root]=False
return True
if __name__=="__main__":
g=Graph()
g.addNode(1,2)
g.addNode(3,4)
g.addNode(3, 5)
g.addNode(1,3)
#g.addNode(5,2)
g.addNode(2,6)
g.addNode(7,8)
g.addNode(8,6)
g.addNode(5,8)
#g.addNode(4,1)
print("Cycle Detected" if cycleCheck(g)==False else "Cyle Not Present")