Betweenness(中介中心度)這個度量很有意思。類似於我們身邊那種社交達人,我們認識的不少朋友可能都是通過他/她認識的,這個人起到了中介的作用。
中介中心性指的是一個結點擔任其它兩個結點之間最短路的橋樑的次數。一個結點充當“中介”的次數越高,它的中介中心度就越大。如果要考慮標準化的問題,可以用一個結點承擔最短路橋樑的次數除以所有的路徑數量。
其公式可以表述爲:
其中:是結點
代碼實現如下:
def getNeighbor(nodes,edges):
#此函數是爲了創建圖的數據結構,結構爲
#{"1":[2,3,4]
#"2":[4]
#"3":[1,2]
#"4":[3]
#}
#key爲結點,value爲其指向的結點的列表
nodeDict={}
for node in nodes:
neighbor=[]
for edge in edges:
if edge[0]==node:
neighbor.append(edge[1])
nodeDict[node]=neighbor
return nodeDict
def findAllPath(graph,start,end,path=[]):
#獲取start到end的所有路徑,返回列表,[start,node1,node2,...,end]
path = path +[start]
if start == end:
return [path]
paths = [] #存儲所有路徑
for node in graph[start]:
if node not in path:
newpaths = findAllPath(graph,node,end,path)
for newpath in newpaths:
paths.append(newpath)
return paths
def getAllShortestPath(nodes,edges):
#獲取start到end的所有最短路徑
shortestDict={}
graph=getNeighbor(nodes,edges)
for start in nodes:
for end in nodes:
paths=findAllPath(graph,start,end)
if len(paths)>=1:
shortestPath=getAllShortestRow(paths) #找到路徑最短的序列對應的行號
shortestDict[str(start)+","+str(end)]=shortestPath #key值爲start與end的拼接字符串
return shortestDict
def getAllShortestRow(paths):
#找到start到end的所有路徑中,最短的路徑
lengthDict={}
count=0
for path in paths:
lengthDict[len(path)]=[]
for path in paths:
lengthDict[len(path)].append(count)
count+=1
lens=list(sorted(lengthDict.keys()))
shortest=[]
for row in lengthDict[lens[0]]:
shortest.append(paths[row])
return shortest
def getBetweenness(node,shortestDict):
#計算Betweenness
sum=0
for k,vs in shortestDict.items():
count=0
for v in vs:
if node in v:
count+=1
sum+=count/len(v)
return sum
def getResult(nodes,edges):
shortestDict=getAllShortestPath(nodes,edges)
resultDict={}
for node in nodes:
resultDict[node]=getBetweenness(node,shortestDict)
return resultDict
if __name__ == "__main__":
vertices = []
edges = []
f=open("BetweennessrResult.txt","w+")
nodeStr=open("./data/nodes.txt").read()
for l in nodeStr.splitlines():
vertices.append(int(l))
edgeStr=open("./data/edges.txt").read()
for l in edgeStr.splitlines():
edges.append((int(l.split(",")[0]),int(l.split(",")[1])))
result=getResult(vertices,edges)
for k,v in result.items():
f.write(str(k)+"\t"+str(v)+"\n")
f.close()
計算結果爲:
node數據形式爲:
edges數據形式爲: