简体   繁体   中英

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 :

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

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})]

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