问题描述
我有一个列表作为c4_leaves = [56,78,90,112]
.我正在尝试使用c4_leaves
中的这些元素作为节点来创建完整的图形.这是我尝试过的;
I have a list as c4_leaves = [56,78,90,112]
. I'm trying to create a complete graph using these elements in c4_leaves
as nodes. Here's what I've tried;
V_ex = c4_leaves
G_ex = nx.Graph()
G_ex.add_nodes_from(V_ex)
G_ex = nx.complete_graph(4)
for u,v in G_ex.edges():
G_ex[u][v]['distance'] = distance(points33, u, v)
然后上图的最小生成树为:
And then the minimum spanning tree of the above graph as :
T_ex= nx.minimum_spanning_tree(G_ex, weight='distance')
F_ex = list(T_ex.edges())
当我绘制G_ex
时,它给了我正确的图形,但是当我打印最小生成树的细节时,它显示了T_ex.nodes() = [0,1,2,3,56,78,90,112]
.
When I draw G_ex
, it gives me the correct graph, but when I print details of the minimum spanning tree, it shows that T_ex.nodes() = [0,1,2,3,56,78,90,112]
.
有人可以告诉我我正在做的错误吗?
Can someone show me the mistake I'm doing?
推荐答案
而不是使用complete_graph
来与其他节点生成新的完整图形,而是按如下所示创建所需图形:
Instead of using complete_graph
, which generates a new complete graph with other nodes, create the desired graph as follows:
import itertools
import networkx as nx
c4_leaves = [56,78,90,112]
G_ex = nx.Graph()
G_ex.add_nodes_from(c4_leaves)
G_ex.add_edges_from(itertools.combinations(c4_leaves, 2))
对于有向图,请使用:
G_ex.add_edges_from(itertools.permutations(c4_leaves, 2))
这篇关于Networkx:为给定的节点集创建完整的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!