graphit/graph_algorithms.py
def brandes_betweenness_centrality(graph, nodes=None, normalized=True, weight=None, endpoints=False):
    """
    Brandes algorithm for betweenness centrality.
    
    Betweenness centrality is an indicator of a node's centrality in a network.
    It is equal to the number of shortest paths from all vertices to all others
    that pass through that node. A node with high betweenness centrality has a
    large influence on the transfer of items through the network, under the
    assumption that item transfer follows the shortest paths.
    
    Original publication:
    Brandes, Ulrik. "A faster algorithm for betweenness centrality*."
    Journal of mathematical sociology 25.2 (2001): 163-177.
    
    :param graph:      Graph to calculate Brandes betweenness centrality
    :type graph:       Graph class instance
    :param normalized: normalize betweenness centrality measure between 0 and 1
    :type normalized:  bool
    :param weight:     edge attribute to use as edge weight
    :type weight:      string
    
    :rtype:            :py:dict
    """

    betweenness = dict.fromkeys(graph.nodes, 0.0)
    nodes = nodes or graph.nodes

    for node in nodes:
        # single source shortest paths
        S = []
        P = {}
        for v in graph.nodes:
            P[v] = []
        D = {}
        sigma = dict.fromkeys(graph.nodes, 0.0)
        sigma[node] = 1.0
        if weight is None:  # use BFS
            D[node] = 0
            Q = [node]
            while Q:  # use BFS to find shortest paths
                v = Q.pop(0)
                S.append(v)
                Dv = D[v]
                sigmav = sigma[v]
                for w in graph.adjacency[v]:
                    if w not in D:
                        Q.append(w)
                        D[w] = Dv + 1
                    if D[w] == Dv + 1:  # this is a shortest path, count paths
                        sigma[w] += sigmav
                        P[w].append(v)  # predecessors

        else:
            # use Dijkstra's algorithm
            seen = {node: 0}
            c = count()
            Q = []  # use Q as heap with (distance,node id) tuples
            heapq.heappush(Q, (0, next(c), node, node))
            while Q:
                (dist, _, pred, v) = heapq.heappop(Q)
                if v in D:
                    continue  # already searched this node.
                sigma[v] += sigma[pred]  # count paths
                S.append(v)
                D[v] = dist
                for w in graph.adjacency[v]:
                    vw_dist = dist + graph.edges[(v, w)].get(weight, 1)
                    if w not in D and (w not in seen or vw_dist < seen[w]):
                        seen[w] = vw_dist
                        heapq.heappush(Q, (vw_dist, next(c), v, w))
                        sigma[w] = 0.0
                        P[w] = [v]
                    elif vw_dist == seen[w]:  # handle equal paths
                        sigma[w] += sigma[v]
                        P[w].append(v)

        # accumulation
        delta = dict.fromkeys(S, 0)
        correction = 1 if endpoints else 0
        if endpoints:
            betweenness[node] += len(S) - correction
        while S:
            w = S.pop()
            coeff = (1 + delta[w]) / sigma[w]
            for v in P[w]:
                delta[v] += sigma[v] * coeff
            if w != node:
                betweenness[w] += delta[w] + correction

    # Rescale betweenness values
    scale = 0.5 if graph.directed else None
    nr_nodes = float(len(graph))
    if normalized:
        scale = None
        if endpoints and nr_nodes >= 2:
            scale = 1 / (nr_nodes * (nr_nodes - 1))
        elif nr_nodes > 2:
            scale = 1 / ((nr_nodes - 1) * (nr_nodes - 2))

    if scale is not None:
        scale = scale * nr_nodes / len(nodes)
        for v in betweenness:
            betweenness[v] *= scale

    return betweenness

    Brandes algorithm for betweenness centrality.
    
    Betweenness centrality is an indicator of a node's centrality in a network.
    It is equal to the number of shortest paths from all vertices to all others
    that pass through that node. A node with high betweenness centrality has a
    large influence on the transfer of items through the network, under the
    assumption that item transfer follows the shortest paths.
    
    Original publication:
    Brandes, Ulrik. "A faster algorithm for betweenness centrality*."
    Journal of mathematical sociology 25.2 (2001): 163-177.
    
    :param graph:      Graph to calculate Brandes betweenness centrality
    :type graph:       Graph class instance
    :param normalized: normalize betweenness centrality measure between 0 and 1
    :type normalized:  bool
    :param weight:     edge attribute to use as edge weight
    :type weight:      string
    
    :rtype:            :py:dict
    
Brandes algorithm for betweenness centrality.
Betweenness centrality is an indicator of a node's centrality in a network.
It is equal to the number of shortest paths from all vertices to all others
that pass through that node. A node with high betweenness centrality has a
large influence on the transfer of items through the network, under the
assumption that item transfer follows the shortest paths.

Original publication:
Brandes, Ulrik. "A faster algorithm for betweenness centrality*."
Journal of mathematical sociology 25.2 (2001): 163-177.
Brandes algorithm for betweenness centrality . Betweenness centrality is an indicator of a node ' s centrality in a network . It is equal to the number of shortest paths from all vertices to all others that pass through that node . A node with high betweenness centrality has a large influence on the transfer of items through the network under the assumption that item transfer follows the shortest paths . Original publication : Brandes Ulrik . " A faster algorithm for betweenness centrality * . " Journal of mathematical sociology 25 . 2 ( 2001 ) : 163 - 177 .
def brandes_betweenness_centrality(graph, nodes=None, normalized=True, weight=None, endpoints=False):
    betweenness = dict.fromkeys(graph.nodes, 0.0)
    nodes = nodes or graph.nodes
    for node in nodes:
        S = []
        P = {}
        for v in graph.nodes:
            P[v] = []
        D = {}
        sigma = dict.fromkeys(graph.nodes, 0.0)
        sigma[node] = 1.0
        if weight is None:  
            D[node] = 0
            Q = [node]
            while Q:   to find shortest paths
                v = Q.pop(0)
                S.append(v)
                Dv = D[v]
                sigmav = sigma[v]
                for w in graph.adjacency[v]:
                    if w not in D:
                        Q.append(w)
                        D[w] = Dv + 1
                    if D[w] == Dv + 1:  
                        sigma[w] += sigmav
                        P[w].append(v)  
        else:
            seen = {node: 0}
            c = count()
            Q = []  
            heapq.heappush(Q, (0, next(c), node, node))
            while Q:
                (dist, _, pred, v) = heapq.heappop(Q)
                if v in D:
                    continue  
                sigma[v] += sigma[pred]  
                S.append(v)
                D[v] = dist
                for w in graph.adjacency[v]:
                    vw_dist = dist + graph.edges[(v, w)].get(weight, 1)
                    if w not in D and (w not in seen or vw_dist < seen[w]):
                        seen[w] = vw_dist
                        heapq.heappush(Q, (vw_dist, next(c), v, w))
                        sigma[w] = 0.0
                        P[w] = [v]
                    elif vw_dist == seen[w]:  
                        sigma[w] += sigma[v]
                        P[w].append(v)
        delta = dict.fromkeys(S, 0)
        correction = 1 if endpoints else 0
        if endpoints:
            betweenness[node] += len(S) - correction
        while S:
            w = S.pop()
            coeff = (1 + delta[w]) / sigma[w]
            for v in P[w]:
                delta[v] += sigma[v] * coeff
            if w != node:
                betweenness[w] += delta[w] + correction
    scale = 0.5 if graph.directed else None
    nr_nodes = float(len(graph))
    if normalized:
        scale = None
        if endpoints and nr_nodes >= 2:
            scale = 1 / (nr_nodes * (nr_nodes - 1))
        elif nr_nodes > 2:
            scale = 1 / ((nr_nodes - 1) * (nr_nodes - 2))
    if scale is not None:
        scale = scale * nr_nodes / len(nodes)
        for v in betweenness:
            betweenness[v] *= scale
    return betweenness
def brandes_betweenness_centrality ( graph , nodes = None , normalized = True , weight = None , endpoints = False ) : betweenness = dict . fromkeys ( graph . nodes , 0.0 ) nodes = nodes or graph . nodes for node in nodes : S = [ ] P = { } for v in graph . nodes : P [ v ] = [ ] D = { } sigma = dict . fromkeys ( graph . nodes , 0.0 ) sigma [ node ] = 1.0 if weight is None : D [ node ] = 0 Q = [ node ] while Q : v = Q . pop ( 0 ) S . append ( v ) Dv = D [ v ] sigmav = sigma [ v ] for w in graph . adjacency [ v ] : if w not in D : Q . append ( w ) D [ w ] = Dv + 1 if D [ w ] == Dv + 1 : sigma [ w ] += sigmav P [ w ] . append ( v ) else : seen = { node : 0 } c = count ( ) Q = [ ] heapq . heappush ( Q , ( 0 , next ( c ) , node , node ) ) while Q : ( dist , _ , pred , v ) = heapq . heappop ( Q ) if v in D : continue sigma [ v ] += sigma [ pred ] S . append ( v ) D [ v ] = dist for w in graph . adjacency [ v ] : vw_dist = dist + graph . edges [ ( v , w ) ] . get ( weight , 1 ) if w not in D and ( w not in seen or vw_dist < seen [ w ] ) : seen [ w ] = vw_dist heapq . heappush ( Q , ( vw_dist , next ( c ) , v , w ) ) sigma [ w ] = 0.0 P [ w ] = [ v ] elif vw_dist == seen [ w ] : sigma [ w ] += sigma [ v ] P [ w ] . append ( v ) delta = dict . fromkeys ( S , 0 ) correction = 1 if endpoints else 0 if endpoints : betweenness [ node ] += len ( S ) - correction while S : w = S . pop ( ) coeff = ( 1 + delta [ w ] ) / sigma [ w ] for v in P [ w ] : delta [ v ] += sigma [ v ] * coeff if w != node : betweenness [ w ] += delta [ w ] + correction scale = 0.5 if graph . directed else None nr_nodes = float ( len ( graph ) ) if normalized : scale = None if endpoints and nr_nodes >= 2 : scale = 1 / ( nr_nodes * ( nr_nodes - 1 ) ) elif nr_nodes > 2 : scale = 1 / ( ( nr_nodes - 1 ) * ( nr_nodes - 2 ) ) if scale is not None : scale = scale * nr_nodes / len ( nodes ) for v in betweenness : betweenness [ v ] *= scale return betweenness
Brandes algorithm for betweenness centrality.
Brandes algorithm for betweenness centrality .
"""
    Brandes algorithm for betweenness centrality.
    
    Betweenness centrality is an indicator of a node's centrality in a network.
    It is equal to the number of shortest paths from all vertices to all others
    that pass through that node. A node with high betweenness centrality has a
    large influence on the transfer of items through the network, under the
    assumption that item transfer follows the shortest paths.
    
    Original publication:
    Brandes, Ulrik. "A faster algorithm for betweenness centrality*."
    Journal of mathematical sociology 25.2 (2001): 163-177.
    
    :param graph:      Graph to calculate Brandes betweenness centrality
    :type graph:       Graph class instance
    :param normalized: normalize betweenness centrality measure between 0 and 1
    :type normalized:  bool
    :param weight:     edge attribute to use as edge weight
    :type weight:      string
    
    :rtype:            :py:dict
    """ # single source shortest paths # use BFS # use BFS to find shortest paths # this is a shortest path, count paths # predecessors # use Dijkstra's algorithm # use Q as heap with (distance,node id) tuples # already searched this node. # count paths # handle equal paths # accumulation # Rescale betweenness values
None
:py:dict
graph
Graph to calculate Brandes betweenness centrality
Graph to calculate Brandes betweenness centrality
nodes
normalized
normalize betweenness centrality measure between 0 and 1
normalize betweenness centrality measure between 0 and 1
weight
edge attribute to use as edge weight
edge attribute to use as edge weight
endpoints