(G, weight, normalized=True)
return straightness_centrality
Calculates straightness centrality.
Calculates straightness centrality.
Calculates straightness centrality .
def _straightness_centrality(G, weight, normalized=True):
    """
    Calculates straightness centrality.
    """
    straightness_centrality = {}

    for n in G.nodes():
        straightness = 0
        sp = nx.single_source_dijkstra_path_length(G, n, weight=weight)

        if len(sp) > 0 and len(G) > 1:
            for target in sp:
                if n != target:
                    network_dist = sp[target]
                    euclidean_dist = _euclidean(n, target)
                    straightness = straightness + (euclidean_dist / network_dist)
            straightness_centrality[n] = straightness * (1.0 / (len(G) - 1.0))
            # normalize to number of nodes-1 in connected part
            if normalized:
                if len(sp) > 1:
                    s = (len(G) - 1.0) / (len(sp) - 1.0)
                    straightness_centrality[n] *= s
                else:
                    straightness_centrality[n] = 0
        else:
            straightness_centrality[n] = 0.0
    return straightness_centrality
def _straightness_centrality ( G , weight , normalized = True ) : straightness_centrality = { } for n in G . nodes ( ) : straightness = 0 sp = nx . single_source_dijkstra_path_length ( G , n , weight = weight ) if len ( sp ) > 0 and len ( G ) > 1 : for target in sp : if n != target : network_dist = sp [ target ] euclidean_dist = _euclidean ( n , target ) straightness = straightness + ( euclidean_dist / network_dist ) straightness_centrality [ n ] = straightness * ( 1.0 / ( len ( G ) - 1.0 ) ) # normalize to number of nodes-1 in connected part if normalized : if len ( sp ) > 1 : s = ( len ( G ) - 1.0 ) / ( len ( sp ) - 1.0 ) straightness_centrality [ n ] *= s else : straightness_centrality [ n ] = 0 else : straightness_centrality [ n ] = 0.0 return straightness_centrality