习题6.1(3)
1.代码实现
点击查看代码
import networkx as nx
import matplotlib.pyplot as plt
G=nx.DiGraph()
G.add_nodes_from([1,2,3,4,5,6])
edges_with_weights=[(1,3,3),(2,1,7),(2,3,1),(3,4,8),(4,1,12),(5,4,9),(5,6,3),(6,2,1)]
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.运行结果