문제

Consider I have the following graph:

A -> B
B -> C
C -> D
C -> A

What is the easiest way to find that A -> B -> C -> A is a circular relation? Is there such a function already built into NetworkX or another easy to use Python library?

도움이 되었습니까?

해결책

networkx.simple_cycles does this for you.

>>> import networkx as nx
>>> G = nx.DiGraph()
>>> G.add_edge('A', 'B')
>>> G.add_edge('B', 'C')
>>> G.add_edge('C', 'D')
>>> G.add_edge('C', 'A')
>>> nx.simple_cycles(G)
[['A', 'B', 'C', 'A']]

다른 팁

Use Depth-First Search to detect cycles in a graph.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top