Given an undirected graph, we want to orient the edges so that they become strongly connected components.
1 is a vertex depth-first search, which means there are edges that cannot be passed.
3 is the correct edge depth-first search
2 is an inadvertent mistake.
3 python
for v1, v2 in edgelist:
if (v1, v2) not in answer:
answer[(v1, v2)] = "->"
answer[(v2, v1)] = "<-"
def visit(e):
(v1, v2) = e
for v3 in edges[v2]:
if v3 == v1:
continue
if (v2, v3) in answer:
continue
answer[(v2, v3)] = "->"
answer[(v3, v2)] = "<-"
visit((v2, v3))
visit((v1, v2))
for v1, v2 in edgelist:
if (v1, v2) not in answer:
answer[(v1, v2)] = "->"
answer[(v2, v1)] = "<-"
stack = [(v1, v2)]
while stack:
v1, v2 = stack.pop()
for v3 in edges[v2]:
if v3 == v1:
continue
if (v2, v3) in answer:
continue
answer[(v2, v3)] = "->"
answer[(v3, v2)] = "<-"
stack.append((v2, v3))
for v1, v2 in edgelist:
if (v1, v2) not in answer:
stack = [(v1, v2)]
while stack:
v1, v2 = stack.pop()
if (v1, v2) in answer:
continue
answer[(v1, v2)] = "->"
answer[(v2, v1)] = "<-"
for v3 in edges[v2]:
if v3 == v1:
continue
stack.append((v2, v3))
This page is auto-translated from /nishio/辺の深さ優先探索 using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.