习题6.1代码
import numpy as np
import pandas as pd
import cvxpy as cp
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
Times New Roman + SimSun + WFM Sans SC
simsum宋体, times new roman -*, simhei黑体, kaiti楷体,
dengxian等线, fangsong仿宋, Microsoft Yahei微软雅黑
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['figure.figsize'] = [4, 3]
plt.rcParams['font.size'] = 12
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
nodes_labels = ['v' + str(i) for i in range(1, 7)]
nodes_labels = dict(zip(range(1,7), nodes_labels))
L1 = [(1,2), (1,3), (1,4), (2,3), (2,6), (3,4), (4,5), (5,6)]
G1 = nx.Graph()
G1.add_edges_from(L1)
L2 = [(1,2,7), (1,3,3), (1,4,12), (2,3,1), (2,6,1), (3,4,8), (4,5,9), (5,6,3)]
G2 = nx.Graph()
G2.add_weighted_edges_from(L2)
L3 = [(2,1,7), (1,3,3), (4,1,12), (2,3,1), (6,2,1), (3,4,8), (5,4,9), (5,6,3)]
G3 = nx.DiGraph()
G3.add_weighted_edges_from(L3)
fig = plt.figure(dpi=400, figsize=(9,3))
ax = fig.add_subplot(131)
pos1 = nx.planar_layout(G1)
nx.draw(G1, pos1, labels=nodes_labels, with_labels='True', font_color='w', node_size=100, font_size=8)
ax1 = fig.add_subplot(132)
pos2 = nx.planar_layout(G2)
nx.draw(G2, pos2, labels=nodes_labels, with_labels='True', font_color='w', node_size=100, font_size=8)
edgelabels = nx.get_edge_attributes(G2, 'weight')
nx.draw_networkx_edge_labels(G2, pos2, edge_labels=edgelabels, font_size=6)
ax2 = fig.add_subplot(133)
pos3 = nx.planar_layout(G3)
nx.draw(G3, pos3, labels=nodes_labels, with_labels='True', font_color='w', node_size=100, font_size=8)
edgelabels = nx.get_edge_attributes(G3, 'weight')
nx.draw_networkx_edge_labels(G3, pos3, edge_labels=edgelabels, font_size=6)
fig.show()