简体   繁体   English

Python Networkx 检测循环/圆圈

[英]Python Networkx detecting loops/circles

Given the following example:给出以下示例:

Is there a possibilty to detect a loop in the network (I1, I2,I3, C6, C7, I5) ?是否有可能检测网络中的环路(I1, I2,I3, C6, C7, I5)

I tried: simple_cycles → it works fine with 3 nodes, but not with more than 3.我试过: simple_cycles → 它适用于 3 个节点,但不能超过 3 个。

I would need to detect the circle with all nodes and the "input" node ("I1") and the "output" ("I3") .我需要用所有节点和“输入”节点("I1")和“输出” ("I3")检测圆。

I recreated your graph as such:我重新创建了你的图表:

import networkx as nx

g = nx.DiGraph([('P', 'I0'), ('I0', 'I1'), ('I1', 'I2'),
                ('I2', 'I3'), ('I1', 'I5'), ('I5', 'C7'),
                ('C7', 'C6'), ('C6', 'I3'), ('I3', 'C9')])

You were searching for simple cycles but there is none in the above graph:您正在寻找简单的循环,但上图中没有:

>>> list(nx.simple_cycles(g))
[]

so you have to search for cycles in the undirected graph.所以你必须在无向图中搜索循环。 You have to cast your graph to an undirected graph.您必须将图形转换为无向图。 For undirected graphs, the cycle_basis function is what you seem to need:对于无向图, cycle_basis函数似乎是您需要的:

>>> nx.cycle_basis(g.to_undirected())
[['I5', 'C7', 'C6', 'I3', 'I2', 'I1']]

Yes, if you use the method nx.simple_cycles(G) , you will get the cycle in the graph as a set of nodes that are in a cycle (as I've understudd it).是的,如果您使用方法nx.simple_cycles(G) ,您将在图中获得循环作为循环中的一组节点(因为我已经理解了它)。 For more info check this out.有关更多信息,请查看此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM