ó žÃÒYc@ sodZddlmZddlZddlmZddgZdd„Z de d„Z dde d „Z dS( sAFunctions for computing reaching centrality of a node or a graph.iÿÿÿÿ(tdivisionN(tpairwisetglobal_reaching_centralitytlocal_reaching_centralityc sat|ƒd}|dkr dSˆdkr4d|St‡‡fd†t|ƒDƒƒ}||S(s×Returns the average weight of an edge in a weighted path. Parameters ---------- G : graph A networkx graph. path: list A list of vertices that define the path. weight : None or string, optional (default=None) If None, edge weights are ignored. Then the average weight of an edge is assumed to be the multiplicative inverse of the length of the path. Otherwise holds the name of the edge attribute used as weight. iic3 s,|]"\}}ˆj||fˆVqdS(N(tedges(t.0titj(tGtweight(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pys 'sN(tlentNonetsumR(RtpathR t path_lengtht total_weight((RR s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pyt_average_weights  %c  stj|dˆƒr'tjdƒ‚n|jdˆƒ‰ˆdkrWtjdƒ‚nˆd k r‡‡fd†}tj|d|ƒ}ntj|ƒ}t}g|jƒD]-\}}|||d|dˆd|ƒ^q¯}t|ƒ‰t ‡fd†|Dƒƒt |ƒd S( sxReturns the global reaching centrality of a directed graph. The *global reaching centrality* of a weighted directed graph is the average over all nodes of the difference between the local reaching centrality of the node and the greatest local reaching centrality of any node in the graph [1]_. For more information on the local reaching centrality, see :func:`local_reaching_centrality`. Informally, the local reaching centrality is the proportion of the graph that is reachable from the neighbors of the node. Parameters ---------- G : DiGraph A networkx DiGraph. weight : None or string, optional (default=None) Attribute to use for edge weights. If ``None``, each edge weight is assumed to be one. A higher weight implies a stronger connection between nodes and a *shorter* path length. normalized : bool, optional (default=True) Whether to normalize the edge weights by the total sum of edge weights. Returns ------- h : float The global reaching centrality of the graph. Examples -------- >>> import networkx as nx >>> G = nx.DiGraph() >>> G.add_edge(1, 2) >>> G.add_edge(1, 3) >>> nx.global_reaching_centrality(G) 1.0 >>> G.add_edge(3, 2) >>> nx.global_reaching_centrality(G) 0.75 See also -------- local_reaching_centrality References ---------- .. [1] Mones, Enys, Lilla Vicsek, and Tamás Vicsek. "Hierarchy Measure for Complex Networks." *PLoS ONE* 7.3 (2012): e33799. https://dx.doi.org/10.1371/journal.pone.0033799 R sedge weights must be positiveisSize of G must be positivec sˆ|jˆdƒS(Ni(tget(tutvtd(RR (s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pytpstpathst normalizedc3 s|]}ˆ|VqdS(N((Rtc(tmax_lrc(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pys |siN( tnxtis_negatively_weightedt NetworkXErrortsizeR t shortest_pathRtitemstmaxR R ( RR Rt as_distancetshortest_pathst centralitytnodeRtlrc((RRR s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pyR+s5  = c  sc|d kr·tjˆdˆƒr3tjdƒ‚nˆjdˆƒ‰ˆdkrctjdƒ‚nˆd k rŸ‡‡fd†}tjˆd|d|ƒ}q·tjˆd|ƒ}nˆd krëˆjƒrët|ƒdtˆƒdS|rˆd k rˆjdˆƒˆjƒ}nd}‡‡fd†|jƒDƒ}t |ƒ|}|tˆƒdS( sþReturns the local reaching centrality of a node in a directed graph. The *local reaching centrality* of a node in a directed graph is the proportion of other nodes reachable from that node [1]_. Parameters ---------- G : DiGraph A NetworkX DiGraph. v : node A node in the directed graph `G`. paths : dictionary (default=None) If this is not `None` it must be a dictionary representation of single-source shortest paths, as computed by, for example, :func:`networkx.shortest_path` with source node `v`. Use this keyword argument if you intend to invoke this function many times but don't want the paths to be recomputed each time. weight : None or string, optional (default=None) Attribute to use for edge weights. If `None`, each edge weight is assumed to be one. A higher weight implies a stronger connection between nodes and a *shorter* path length. normalized : bool, optional (default=True) Whether to normalize the edge weights by the total sum of edge weights. Returns ------- h : float The local reaching centrality of the node ``v`` in the graph ``G``. Examples -------- >>> import networkx as nx >>> G = nx.DiGraph() >>> G.add_edges_from([(1, 2), (1, 3)]) >>> nx.local_reaching_centrality(G, 3) 0.0 >>> G.add_edge(3, 2) >>> nx.local_reaching_centrality(G, 3) 0.5 See also -------- global_reaching_centrality References ---------- .. [1] Mones, Enys, Lilla Vicsek, and Tamás Vicsek. "Hierarchy Measure for Complex Networks." *PLoS ONE* 7.3 (2012): e33799. https://dx.doi.org/10.1371/journal.pone.0033799 R sedge weights must be positiveisSize of G must be positivec sˆ|jˆdƒS(Ni(R(RRR(RR (s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pyRÂstsourceic3 s$|]}tˆ|dˆƒVqdS(R N(R(RR (RR (s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pys ÏsN( R RRRRRt is_directedR tvaluesR ( RRRR RR!tnormtavgwtsum_avg_weight((RRR s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pyRs$;   ( t__doc__t __future__RtnetworkxRtnetworkx.utilsRt__all__R RtTrueRR(((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/centrality/reaching.pyts   T