15.六度分隔理论验证
import networkx as nx import csv import numpy as np with open("quakers_nodelist.csv") as nodecsv: nodereader = csv.reader(nodecsv) nodes = [i for i in nodereader][1:] node_names = [n[0] for n in nodes] with open("quakers_edgelist.csv") as edgecsv: edgereader = csv.reader(edgecsv) edges = [tuple(e) for e in edgereader][1:] G = nx.Graph() G.add_nodes_from(node_names) G.add_edges_from(edges) components = nx.connected_components(G) largest_component = max(components, key=len) subgraph = G.subgraph(largest_component) shortest_path_list = [] for i in subgraph.nodes(): for j in subgraph.nodes(): if i != j: i_j_path = len(nx.shortest_path(subgraph, source=i, target=j)) - 1 shortest_path_list.append(i_j_path) average_path_length = np.mean(shortest_path_list) print(average_path_length)