【问题标题】:Prim's algorithm on existing graphPrim 对现有图的算法
【发布时间】: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


    【解决方案1】:

    利用节点u=y[i]、v=y[i+1]和权重=y[i+2],创建图的邻接矩阵或邻接表,然后应用prim算法,可以发现这里有一个很好的简单教程:Prim’s Minimum Spanning Tree

    【讨论】:

    • 感谢您的支持。我设法通过使用 A = nx.to_numpy_matrix(G,G.node()) 将图转换为邻接矩阵来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2010-10-20
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多