习题6.1(2)

1.代码实现

点击查看代码
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_nodes_from([1,2,3,4,5,6])
edges_with_weights=[(1,2,7),(1,3,3),(1,4,12),(2,6,1),(3,4,8),(4,5,9),(5,6,3)]
G.add_weighted_edges_from(edges_with_weights)
pos=nx.spring_layout(G)
edge_labels = nx.get_edge_attributes(G,'weight')

nx.draw(G,pos,with_labels=True,font_weight='bold')
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
plt.show()

2.运行结果

posted on 2024-10-27 16:25  克卜勒星球  阅读(5)  评论(0编辑  收藏  举报

导航