-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_loop.py
27 lines (26 loc) · 1.13 KB
/
find_loop.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
def findloop_dfs(al, v, vis, instack, stack): # if vertex v can reach some vertex that is on a loop, the loop can be found. In other words, if some vertex was reached from v without finding a loop, it is not on a loop.
vis[v] = 1
instack[v] = 1
stack.append(v)
for i in al[v]:
if vis[i] == 0:
res = findloop_dfs(al, i, vis, instack, stack)
if res != None:
return res
elif instack[i] == 1: # found a loop, if the graph is undirectional, use elif instack[i] == 1 and i != stack[-2]
for j in range(len(stack)): # find which vertices are on the loop
if stack[j] == i:
res = stack[j:]
stack.pop(-1)
instack[v] = 0
return res
stack.pop(-1)
instack[v] = 0
def findloop(al: 'adjacent list'): # suppose each loop is separate. In other words, one vertex can only belong to one loop.
n = len(al)
vis = [0] * n
for i in range(n):
if vis[i] == 0:
res = findloop_dfs(al, i, vis, [0]*n, [])
if res != None:
return res