sjh*_*e14 7 python graph-theory networkx python-3.x jupyter
在 NetworkX 中采用以下无向图对象。
G_3 = nx.Graph()
G_3.add_nodes_from([1,2,3,4,5])
G_3.add_weighted_edges_from([(1,2,1), (2,1,1), (2,4,1), (4,2,1), (2,3,1), (3,2,1), (3,4,1), (4,3,1), (1,4,1), (4,1,1),(5,2,1),(5,1,1)])
Run Code Online (Sandbox Code Playgroud)
该图看起来像这样。有三个基本循环,我想使用这些函数nx.cycle_basis并nx.find_cycle获取组成每个循环的节点和边的列表。
当我使用 时nx.cycle_basis,我得到以下结果:
cycles_3 = [c for c in nx.cycle_basis(G_3)]
print(cycles_3)
>>> [[2, 5, 1], [2, 4, 1], [2, 3, 4]]
Run Code Online (Sandbox Code Playgroud)
这是正确的输出。然而,当我使用该nx.find_cycle函数时,我最终得到以下输出:
num_nodes = len(G_3.nodes())
edges_in_cycle = {}
for i in range(1,num_nodes+1):
edges_in_cycle[i] = nx.find_cycle(G_3,i)`
edges_in_cycle
>>>
{1: [(2, 4), (4, 3), (3, 2)],
2: [(2, 1), (1, 4), (4, 2)],
3: [(2, 1), (1, 4), (4, 2)],
4: [(4, 2), (2, 1), (1, 4)],
5: [(2, 1), (1, 4), (4, 2)]}
Run Code Online (Sandbox Code Playgroud)
如何nx.find_cycle(G_3,source=5)产生不涉及顶点 5 的输出?请让我知道如何使用此函数提取特定周期的边缘信息。