ó žÃÒYc@sädZddlmZmZddlmZddlZddlZdddgZ de de dd„Z de ddd „Zde ddd „Zd „Zd „Zd „Zd„Zd„Ze dd„Ze dd„ZdS(s Betweenness centrality measures.iÿÿÿÿ(theappushtheappop(tcountNtbetweenness_centralitytedge_betweenness_centralitytedge_betweennessc Cs tj|dƒ}|dkr'|}n%tj|ƒtj|jƒ|ƒ}x‰|D]}|dkr€t||ƒ\} } } nt|||ƒ\} } } |r¼t || | | |ƒ}qSt || | | |ƒ}qSWt |t |ƒd|d|j ƒd|ƒ}|S(s£ Compute the shortest-path betweenness centrality for nodes. Betweenness centrality of a node $v$ is the sum of the fraction of all-pairs shortest paths that pass through $v$ .. math:: c_B(v) =\sum_{s,t \in V} \frac{\sigma(s, t|v)}{\sigma(s, t)} where $V$ is the set of nodes, $\sigma(s, t)$ is the number of shortest $(s, t)$-paths, and $\sigma(s, t|v)$ is the number of those paths passing through some node $v$ other than $s, t$. If $s = t$, $\sigma(s, t) = 1$, and if $v \in {s, t}$, $\sigma(s, t|v) = 0$ [2]_. Parameters ---------- G : graph A NetworkX graph. k : int, optional (default=None) If k is not None use k node samples to estimate betweenness. The value of k <= n where n is the number of nodes in the graph. Higher values give better approximation. normalized : bool, optional If True the betweenness values are normalized by `2/((n-1)(n-2))` for graphs, and `1/((n-1)(n-2))` for directed graphs where `n` is the number of nodes in G. weight : None or string, optional (default=None) If None, all edge weights are considered equal. Otherwise holds the name of the edge attribute used as weight. endpoints : bool, optional If True include the endpoints in the shortest path counts. Returns ------- nodes : dictionary Dictionary of nodes with betweenness centrality as the value. See Also -------- edge_betweenness_centrality load_centrality Notes ----- The algorithm is from Ulrik Brandes [1]_. See [4]_ for the original first published version and [2]_ for details on algorithms for variations and related metrics. For approximate betweenness calculations set k=#samples to use k nodes ("pivots") to estimate the betweenness values. For an estimate of the number of pivots needed see [3]_. For weighted graphs the edge weights must be greater than zero. Zero edge weights can produce an infinite number of equal length paths between pairs of nodes. References ---------- .. [1] Ulrik Brandes: A Faster Algorithm for Betweenness Centrality. Journal of Mathematical Sociology 25(2):163-177, 2001. http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf .. [2] Ulrik Brandes: On Variants of Shortest-Path Betweenness Centrality and their Generic Computation. Social Networks 30(2):136-145, 2008. http://www.inf.uni-konstanz.de/algo/publications/b-vspbc-08.pdf .. [3] Ulrik Brandes and Christian Pich: Centrality Estimation in Large Networks. International Journal of Bifurcation and Chaos 17(7):2303-2318, 2007. http://www.inf.uni-konstanz.de/algo/publications/bp-celn-06.pdf .. [4] Linton C. Freeman: A set of measures of centrality based on betweenness. Sociometry 40: 35–41, 1977 http://moreno.ss.uci.edu/23.pdf gt normalizedtdirectedtkN(tdicttfromkeystNonetrandomtseedtsampletnodest"_single_source_shortest_path_basict"_single_source_dijkstra_path_basict_accumulate_endpointst_accumulate_basict_rescaletlent is_directed( tGRRtweightt endpointsR t betweennessRtstStPtsigma((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/betweenness.pyRsS     c Cstj|dƒ}|jtj|jƒdƒƒ|dkrF|}n%tj|ƒtj|jƒ|ƒ}xh|D]`}|dkrŸt ||ƒ\}} } nt |||ƒ\}} } t ||| | |ƒ}qrWx|D] } || =qÝWt |t |ƒd|d|jƒƒ}|S(sÎCompute betweenness centrality for edges. Betweenness centrality of an edge $e$ is the sum of the fraction of all-pairs shortest paths that pass through $e$ .. math:: c_B(e) =\sum_{s,t \in V} \frac{\sigma(s, t|e)}{\sigma(s, t)} where $V$ is the set of nodes, $\sigma(s, t)$ is the number of shortest $(s, t)$-paths, and $\sigma(s, t|e)$ is the number of those paths passing through edge $e$ [2]_. Parameters ---------- G : graph A NetworkX graph. k : int, optional (default=None) If k is not None use k node samples to estimate betweenness. The value of k <= n where n is the number of nodes in the graph. Higher values give better approximation. normalized : bool, optional If True the betweenness values are normalized by $2/(n(n-1))$ for graphs, and $1/(n(n-1))$ for directed graphs where $n$ is the number of nodes in G. weight : None or string, optional (default=None) If None, all edge weights are considered equal. Otherwise holds the name of the edge attribute used as weight. Returns ------- edges : dictionary Dictionary of edges with betweenness centrality as the value. See Also -------- betweenness_centrality edge_load Notes ----- The algorithm is from Ulrik Brandes [1]_. For weighted graphs the edge weights must be greater than zero. Zero edge weights can produce an infinite number of equal length paths between pairs of nodes. References ---------- .. [1] A Faster Algorithm for Betweenness Centrality. Ulrik Brandes, Journal of Mathematical Sociology 25(2):163-177, 2001. http://www.inf.uni-konstanz.de/algo/publications/b-fabc-01.pdf .. [2] Ulrik Brandes: On Variants of Shortest-Path Betweenness Centrality and their Generic Computation. Social Networks 30(2):136-145, 2008. http://www.inf.uni-konstanz.de/algo/publications/b-vspbc-08.pdf gRRN(R R tupdatetedgesR R R RRRRt_accumulate_edgest _rescale_eRR( RRRRR RRRRRRtn((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/betweenness.pyRs >       cCst|||||ƒS(N(R(RRRRR ((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/betweenness.pyR×sc Csg}i}x|D]}g||        (  c Cs•tj|dƒ}x||r|jƒ}d||||}x*||D]}||c|||7 s$    i  W  #