nx.degree(G)
DVweight = G.degree()
degree_sum =sum(span for n, span in DVweight)#各节点度数之和
degree_max =max(span for n, span in DVweight)#节点最大度数
代码
import networkx as nx #导入networkx包import random #导入random包import matplotlib.pyplot as plt
G = nx.Graph()
H = nx.path_graph(100)
G.add_nodes_from(H)defrand_edge(vi,vj,p=0.2):
probability =random.random()if(probability<p):
G.add_edge(vi,vj)
i=0while(i<100):
j=0while(j<i):
rand_edge(i,j)
j +=1
i +=1
number_components = nx.number_connected_components(G)
largest_components =max(nx.connected_components(G), key=len)
nx.degree(G)
DVweight = G.degree()
degree_sum =sum(span for n, span in DVweight)#各节点度数之和
degree_max =max(span for n, span in DVweight)#节点最大度数print("度数之和: "+str(degree_sum))print("节点最大度数:"+str(degree_max))print("最大连通子图:"+str(largest_components))print("最大连通子图长度:"+str(len(largest_components)))print("连通子图个数: "+str(nx.number_connected_components(G)))
nx.draw_networkx(G, with_labels=True)
plt.show()