习题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.运行结果