-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_cycle_using_recursive_dfs.py
49 lines (42 loc) · 1.3 KB
/
detect_cycle_using_recursive_dfs.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
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 dfs_recursive(adj,vis,ele,par):
vis.add(ele)
for adjele in range(len(adj)):
if adj[ele][adjele]:
if adjele not in vis:
if dfs_recursive(adj,vis,adjele,ele):
return True
elif adjele!=par:
print(f"cycle found at {ele}-{adjele}")
return True
return False
def detect_cycle_undirected_dfs_recursive(adj):
vis=set()
n=len(adj)
for i in range(n):
if i in vis:
continue
if dfs_recursive(adj,vis,i,-1):
return
print("no cycle found")
edges = [[0, 1], [1, 2], [1, 3], [2, 4], [3, 4], [5, 7], [7, 6]]
adj_undir = create_undir_adj(edges)
print()
print("cycle detection using recursive dfs")
detect_cycle_undirected_dfs_recursive(adj_undir)
#output
# [0, 1, 0, 0, 0, 0, 0, 0]
# [1, 0, 1, 1, 0, 0, 0, 0]
# [0, 1, 0, 0, 1, 0, 0, 0]
# [0, 1, 0, 0, 1, 0, 0, 0]
# [0, 0, 1, 1, 0, 0, 0, 0]
# [0, 0, 0, 0, 0, 0, 0, 1]
# [0, 0, 0, 0, 0, 0, 0, 1]
# [0, 0, 0, 0, 0, 1, 1, 0]
# cycle detection using recursive dfs
# cycle found at 3-1