-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycledetectionINgraphs.py
125 lines (113 loc) · 2.74 KB
/
cycledetectionINgraphs.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from collections import deque
def create_dir_adj(edges):
numOfNodes=1+max([e[1]for e in edges]+[e[0]for e in edges])
adj=[[0]*numOfNodes for _ in range(numOfNodes)]
for e in edges:
adj[e[0]][e[1]]=1
return adj
def create_undir_adj(edges):
numOfNodes=1+max([e[1]for e in edges]+[e[0]for e in edges])
adj=[[0]*numOfNodes for _ in range(numOfNodes)]
for e in edges:
adj[e[0]][e[1]]=adj[e[1]][e[0]]=1
return adj
def bfs(adj):
vis=set()
n=len(adj)
for i in range(n):
if i in vis:
continue
vis.add(i)
q=deque([i])
while q:
ele=q.pop()
print(ele,end=' ')
for adjele in range(n):
if adj[ele][adjele] and adjele not in vis:
vis.add(adjele)
q.appendleft(adjele)
print()
def dfs(adj):
vis=set()
n=len(adj)
for i in range(n):
if i in vis:
continue
vis.add(i)
st=deque([i])
while st :
ele=st.pop()
print(ele,end='')
for adjele in range(n):
if adj[ele][adjele] and adjele not in vis:
vis.add(adjele)
st.append(adjele)
print()
def detect_cycle_bfs(adj):
vis=set()
n=len(adj)
for i in range(n):
if i in vis:
continue
vis.add(i)
q=deque([[i,-1]])
while q :
ele,par=q.pop()
for adjele in range(n):
if adj[ele][adjele]:
if adjele not in vis:
vis.add(adjele)
q.appendleft([adjele,ele])
elif adjele!=par:
print(f"cycle found at {ele}-{adjele}")
return
print("no cycle found")
def detect_cycle_dfs(adj):
vis=set()
n=len(adj)
for i in range(n):
if i in vis:
continue
vis.add(i)
st=deque([[i,-1]])
while st:
ele,par=st.pop()
for adjele in range(n):
if adj[ele][adjele]:
if adjele not in vis:
vis.add(adjele)
st.append([adjele,ele])
elif adjele!=par:
print(f"cycle detected at {ele}- {adjele}")
return
print("no cycle found")
edges=[[0,1],[1,2],[1,3],[2,4],[3,4],[5,7],[7,6]]
adj_dir=create_dir_adj(edges)
adj_undir=create_undir_adj(edges)
for x in adj_dir:
print(x)
print()
for x in adj_undir:
print(x)
print()
print("bfs for dir")
bfs(adj_dir)
print()
print("bfs for undir")
bfs(adj_undir)
print()
print("dfs for dir")
dfs(adj_dir)
print()
print("dfs for undir")
dfs(adj_undir)
print()
print("cycle detection usin bfs")
detect_cycle_bfs(adj_undir)
print()
print("cycle detection using dfs")
detect_cycle_dfs(adj_undir)
# for dir graphs there shoud not be two incoming edeges for a node A-->B
-->
# for undir graphs ther should not be to edges like A--B
--