-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle_detection.py
More file actions
33 lines (25 loc) · 1.01 KB
/
Copy pathcycle_detection.py
File metadata and controls
33 lines (25 loc) · 1.01 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
# ノードは、未探索0・探索中1・探索済み2 の3状態を持つ
# 探索中の経路で、「探索中」の状態のノードに行き着いたら、それはサイクルになってる
# graphは隣接ノード、Nはノード数
# O(N + E)
def is_acyclic(graph, N):
state = [0] * N
for start in range(N):
if state[start] != 0:
continue
searching = [(start, False)]
while searching:
cur_node, path_finished = searching.pop()
if path_finished:
state[cur_node] = 2
continue
state[cur_node] = 1
searching.append((cur_node, True))
for nxt_node in graph[cur_node]:
if state[nxt_node] == 2:
continue
elif state[nxt_node] == 1:
return False
elif state[nxt_node] == 0:
searching.append((nxt_node, False))
return True