简体   繁体   English

熊猫蟒蛇的欧几里得最小生成树

[英]Euclidean Minimum Spanning Tree in pandas python

I have a data frame which consists: 我有一个数据框,其中包括:

source  dest  euclidean
 A       B       0.5
 A       C       1.5
 A       D       0.5
 A       E       0.8
 B       C       0.5
 B       D       6.5
 B       E       5.4
 B       A       4.8
 C       B       4.3
 C       D       3.6
 C       E       2.6
 C       A       3.5
 D       B       8.0
 D       C       2.7
 D       E       7.7
 D       A       7.3

I want to find Minimum Spanning Tree Which connects these points, where the weights edges are euclidean distance. 我想找到连接这些点的最小生成树,权重边是欧几里德距离。

I tried using a method shown in Geeks for geeks EMST : 我尝试使用极客EMST极客所示的方法:

g = Graph(4)
for index,row in df.iterrows():
  g.addEdge(row['source'],row['dest'],row['euclidean'])

g.KruskalMST()

But it gave an error. 但是它给出了一个错误。

Is there any other way I Can find this? 我还能找到其他方式吗? Any leads will be helpful 任何线索都将有所帮助

Using networkx , you can use 使用networkx ,您可以使用

from networkx import *

g = Graph()
for index,row in df.iterrows():
  g.add_edge(row['source'],row['dest'],weight=row['euclidean'])

>>> list(minimum_spanning_edges(g))
[('A', 'E', {'weight': 0.8}),
 ('C', 'E', {'weight': 2.6}),
 ('C', 'D', {'weight': 2.7}),
 ('B', 'C', {'weight': 4.3})]

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

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