首页 > 其他 > 详细

15.六度分隔理论验证

时间:2020-11-09 18:10:00      阅读:30      评论:0      收藏:0      [点我收藏+]
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)

 

15.六度分隔理论验证

原文:https://www.cnblogs.com/waterr/p/13948029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!