forked from abcsds/Prim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleL.py
More file actions
41 lines (31 loc) · 869 Bytes
/
Copy pathexampleL.py
File metadata and controls
41 lines (31 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
"""
An example for Prim's Algorithm
"""
__author__ = """Alberto Barradas (http://github.com/abcsds/)"""
try:
import matplotlib.pyplot as plt
except:
raise
try:
import networkx as nx
except:
raise
from prim import *
# G=nx.read_edgelist("test.edgelist")
G=nx.read_edgelist("flights.edgelist")
# mst = prim(G,'A')
mst = prim(G,'Madrid')
othr = [n for n in G.edges() if n not in mst]
print "Minimal Spanning Tree: ", mst
pos=nx.spring_layout(G)
# nodes
nx.draw_networkx_nodes(G, pos, node_size=500)
# edges
nx.draw_networkx_edges(G, pos, edgelist=mst, width=6)
nx.draw_networkx_edges(G, pos, edgelist=othr, width=6, alpha=0.5, edge_color='b', style='dashed')
# labels
nx.draw_networkx_labels(G,pos,font_size=20,font_family='sans-serif')
plt.axis('off')
# plt.savefig("weighted_graph.png") # save as png
plt.show() # display