import networkx as netx
import networkx.algorithms as al
# build graph
g = netx.DiGraph()
edges = [(1,2),(2,3),(3,4),(4,1),(1,2),(2,5),(5,3),(3,4),(4,1)]
g.add_edges_from(edges)
# find cycles
cycles = al.simple_cycles(g)
# assuming that the exterior cycle will contain the most nodes
for cycle in cycles:
print len(cycle)
我找到了解决这个问题的方法networkx.算法图书馆:
结果应为:
>>5
>>6
相关问题 更多 >
编程相关推荐