圖中結點的betweenness計算

Betweenness(中介中心度)這個度量很有意思。類似於我們身邊那種社交達人,我們認識的不少朋友可能都是通過他/她認識的,這個人起到了中介的作用。
中介中心性指的是一個結點擔任其它兩個結點之間最短路的橋樑的次數。一個結點充當“中介”的次數越高,它的中介中心度就越大。如果要考慮標準化的問題,可以用一個結點承擔最短路橋樑的次數除以所有的路徑數量。
其公式可以表述爲:
Cb(vi)=svitVσst(vi)σstC_b(v_i)=\sum_{s\not=v_i\not=t\in V} \frac{\sigma_{st}(v_i)}{\sigma_{st}}
其中:σst\sigma_{st}是結點vivjv_i到v_j的最短路徑的個數
σst(vi)vivjvi\sigma_{st}(v_i)代表v_i到v_j的最短路徑中經過v_i的路徑個數

代碼實現如下:

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數據形式爲:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章