简体   繁体   中英

Networkx Minimum Spanning Tree - precision issues?

I am creating a graph from a weighted adjacency matrix the size of 222 x 222 nodes. All of the weights given in the matrix are a floating point numbers between 0.42757498546089029 and 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. This doesn't occur while plotting the same with igraph . Documentation of Networkx is silent about precision issues. Do you know why it might occur? 具有潜在精度问题的图的最小生成树 图的最小生成树

networkx code:

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). 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. I suspect that you might have plotted the graph without specifying the "weights" parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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