简体   繁体   English

Networkx最小生成树-精度问题?

[英]Networkx Minimum Spanning Tree - precision issues?

I am creating a graph from a weighted adjacency matrix the size of 222 x 222 nodes. 我正在从一个222 x 222节点大小的加权邻接矩阵创建图。 All of the weights given in the matrix are a floating point numbers between 0.42757498546089029 and 1.6671726002927263 . 矩阵中给出的所有权重都是介于0.427574985460890291.6671726002927263之间的1.6671726002927263 nx.minimum_spanning_tree(G, weight = "weight") method gives me the first picture below, meanwhile if I multiply all matrix values by 100.0 the same method gives me the second picture. nx.minimum_spanning_tree(G, weight = "weight")方法给我下面的第一张图片,同时,如果我将所有矩阵值乘以100.0则相同的方法给我第二张图片。 This doesn't occur while plotting the same with igraph . 使用igraph绘制相同内容时不会发生这种情况。 Documentation of Networkx is silent about precision issues. Networkx文档Networkx提及精度问题。 Do you know why it might occur? 你知道为什么会发生吗? 具有潜在精度问题的图的最小生成树 图的最小生成树

networkx code: networkx代码:

G=nx.from_numpy_matrix(M)
G1=nx.minimum_spanning_tree(G, weight = "weight")

labels = {i : node_names[i][1] for i in G1.nodes()}
colors = {i : node_attributes[labels[i]] for i in G1.nodes()}
for i in G1.nodes():
    G1.node[i]["color"] = 'white'
    G1.node[i]["style"] = "filled"    
    G1.node[i]["fillcolor"] = colors[i]
color=nx.get_node_attributes(G1,'color')
fillcolor=nx.get_node_attributes(G1,'fillcolor')
H=nx.relabel_nodes(G1,labels)
nx.draw(H, scale=30, nodelist=H.nodes(), linewidths=0, with_labels = True, node_size=500,font_size=8)

igraph code: 文字代码:

    g = igraph.Graph.Weighted_Adjacency(M.tolist())
    for i, v in enumerate(g.vs):
        v["color"] = colors[i]
        v["label"] = labels[i]
        v["frame_color"] = colors[i]
        v["label_size"] = 10
        v["size"] = 26
    G = g.spanning_tree(weights='weight', return_tree=True)
    G.to_undirected()
    igraph.plot(G, labels=False, bbox = (900, 900), margin=40, loops=False

)

What you see is expected behaviour and not a precision issue at all. 您所看到的是预期的行为,而不是完全的精度问题。 As the name suggests, the spring layout "simulates" the action of springs between your nodes onto their positions. 顾名思义,弹簧布局将节点之间的弹簧动作“模拟”到其位置上。 The node positions are initialised on a circle, and then the force of the springs is applied to your nodes for a certain number of iterations (50 by default). 节点位置在一个圆上初始化,然后将弹簧力施加到您的节点一定的迭代次数(默认为50)。 With weak connection weights, your nodes will more or less remain on the circle (first case), with strong weights your nodes will gravitate towards the centre (second case). 如果连接权重较弱,则节点将或多或少保留在圆上(第一种情况),如果权重较大,则节点将偏向中心(第二种情况)。

In igraph, by default, the unweighted graph is used to compute the layout and you need to give the plotting routine the weights explicitly. 在igraph中,默认情况下,未加权图用于计算布局,您需要显式赋予绘图例程权重。 I suspect that you might have plotted the graph without specifying the "weights" parameter. 我怀疑您可能未指定“ weights”参数就绘制了图形。

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

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