How to compute a special form of Betweenness Centrality in python

江枫思渺然 提交于 2020-06-18 14:42:59

问题


First, I use NetworkX to represent graphs in python. Second, this is the predefined Betweenness centrality in NetworkX. The problem is, we defined the Betweenness Centrality without the denominator. So, c_B(v) is just the sum over all shortes paths they go through the vertex v. Is there a way to modify the predefined Betweenness centrality, such that I get "our" definition of Betweenness centrality? I tried already the load_centrality, but this does not fit too.

Best regards, Matthias


回答1:


I don't think there is a straightforward way to obtain your betweenness centrality from the original function (at least not to me). One easy way to obtain it is by implementing a function yourself. Look below for an example. The example would only be efficient if your graph is not too big (the calculation takes ~ 9 seconds for a graph with 120 vertices).

import networkx as nx

def bc(G):
    vertices = G.nodes()
    new_bc = {}
    paths = defaultdict(dict)

    # Get shortest paths between all pairs of vertices
    for i, vertex in enumerate(vertices[:-1]):
        for o_vertex in vertices[i+1:]:
            paths[vertex][o_vertex] = [path for path in
                                       nx.all_shortest_paths(G, vertex, o_vertex)]

    for vertex in vertices:
        counter = 0
        for i, vertex1 in enumerate(vertices[:-1]):
            for vertex2 in vertices[i+1:]:
                for path in paths[vertex1][vertex2]:
                    if vertex in path[1:-1]:
                        counter += 1
        new_bc[vertex] = counter

    return new_bc

Where bc[node] if the number of shortest paths in G passing through node.



来源:https://stackoverflow.com/questions/46516544/how-to-compute-a-special-form-of-betweenness-centrality-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!