【发布时间】:2019-11-29 23:31:49
【问题描述】:
我使用文本输入文件绘制了一个图形,现在我必须对其应用 prim 算法。我该怎么做 ?下面是我使用文本文件生成图表的代码
import matplotlib.pyplot as plt
import networkx as nx
f= open('input10.txt')
G=nx.Graph()
x=f.read()
x=x.split()
y=[float(i) for i in x]
for i in range(1,30,3):
G.add_node(y[i],pos=(y[i+1],y[i+2]))
def last_index(y):
return len(y)-1
z=last_index(y)
for i in range(31,z-3,5):
G.add_edge(y[i],y[i+1],weight=(y[i+2]))
pos=nx.get_node_attributes(G,'pos')
weight=nx.get_edge_attributes(G,'weight')
plt.figure()
nx.draw(G,pos)
【问题讨论】:
标签: matplotlib graph networkx prims-algorithm