# coding: utf-8 #1.算法 import csv from igraph import Graph as IGraph path="net.data" edges=[]#边长集合 with open(path,"r") as file: for row in csv.reader(file.read().splitlines()): u,v=[i for i in row] edges.append((u,v)) g=IGraph.TupleList(edges, directed=False,#是否有向 vertex_name_attr="name",#点的名字 edge_attrs=None,#边长属性 weights=False)#权重-路径 plist=[] target=7 for v in g.vs:#v代表对象;v["name"]代表节点 paths=g.get_all_shortest_paths(v["name"])#v["name"]代表所有节点名称 for path in paths:#筛选包含7的最短路径 if target in path and target!=path[0] and target!=path[-1]: plist.append(path)#7不是第一个也不是最后一个才行 last=[] times=0 for path in plist: if set((path[0],path[-1])) not in last:#去重 last.append(set((path[0],path[-1])))#用set集合去重 times+=1 for i in last: print(i) print(times) #2.封装调用 btvs=[] for p in zip(g.vs,g.betweenness()): btvs.append({"name":p[0]["name"],"betweenness":p[1]}) print(btvs) #3.排序 btvs=sorted(btvs,key=lambda x:x["betweenness"],reverse=True) print(btvs)