なもり分解(有向グラフ)
code: Namori_decomposition_directed
P = 0 * N # iから初めてループに入る頂点 for i in range(N):
now = i
while True:
S.append(now)
loop_len = len(S) - S.index(now)
while S:
j = S.pop()
if j == now: break
for d, j in enumerate(S):
code: Namori_decomposition_directed2.py
def Namori_decomposition_directed(A):
"""
N頂点N辺の有向グラフ(単純連結でなくてもよい)から閉路を取り出す
Aは Ai := 頂点iから出ている辺の行先
返り値Cyclesは各閉路を要素とする二次元配列
"""
N = len(A)
Cycles = []
for i in range(N):
S = []
now = i
S.append(now)
if is_cycle:
Cycles.append([])
while S:
s = S.pop()
if is_cycle:
if s == now:
is_cycle = 0
else:
return Cycles