
    hK                         S r SSKrSSKJr  SS/rSS jr\R                  " SS9SS	 j5       r\R                  " SS9SS
 j5       r	g)zAFunctions for computing reaching centrality of a node or a graph.    N)pairwiseglobal_reaching_centralitylocal_reaching_centralityc                    ^ ^ [        U5      S-
  nUS::  a  gTc  SU-  $ [        U U4S j[        U5       5       5      nXC-  $ )a  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.
   r   c              3   N   >#    U  H  u  pTR                   X4   T   v   M     g 7fN)edges).0ijGweights      Y/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/centrality/reaching.py	<genexpr>"_average_weight.<locals>.<genexpr>   s"     Hqwwqt}V,s   "%)lensumr   )r   pathr   path_lengthtotal_weights   ` `  r   _average_weightr   	   sI      d)a-Ka~;H$HHL%%    r   )
edge_attrsc                   ^^	^
 [         R                  " U TS9(       a  [         R                  " S5      eU R                  TS9m
T
S::  a  [         R                  " S5      eTb  U
U4S jn[         R                  " XS9nO[         R                  " U 5      n[
        nUR                  5        VVs/ s H  u  pgU" XUTUS9PM     nnn[        U5      m	[        U	4S jU 5       5      [        U 5      S-
  -  $ s  snnf )	u  Returns 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
--------
>>> 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://doi.org/10.1371/journal.pone.0033799
r   edge weights must be positiver   Size of G must be positivec                 .   > TUR                  TS5      -  $ Nr   getuvdr   r   s      r   as_distance/global_reaching_centrality.<locals>.as_distanceg   s    !%%"222r   )pathsr   
normalizedc              3   .   >#    U  H
  nTU-
  v   M     g 7fr	    )r   cmax_lrcs     r   r   -global_reaching_centrality.<locals>.<genexpr>v   s     (Cqw{Cs   r   )
nxis_negatively_weightedNetworkXErrorsizeshortest_pathr   itemsmaxr   r   )r   r   r*   r'   shortest_paths
centralitynoder)   lrcr.   r   s    `       @@r   r   r   "   s    j 
  62>??666(Lq;<< 	3 ))!@))!,*J *//11KD 	1%:N1  
 #hG(C((CFQJ77s   -C4c                   ^ ^^	 T R                  TS9=m	S:  a%  [        T 5      S:X  a  [        R                  " S5      eUc  [        R                  " T TS9(       a  [        R                  " S5      eT	S::  a  [        R                  " S5      eTb  U	U4S jn[        R
                  " T XS9nO[        R
                  " T US	9nTc2  T R                  5       (       a  [        U5      S-
  [        T 5      S-
  -  $ U(       a$  Tb!  T R                  TS9T R                  5       -  nOSnU U4S
 jUR                  5        5       n[        U5      U-  nU[        T 5      S-
  -  $ )u%  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
--------
>>> 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://doi.org/10.1371/journal.pone.0033799
r   r   r   zJlocal_reaching_centrality of a single node with self-loop not well-definedr   r   c                 .   > TUR                  TS5      -  $ r    r!   r#   s      r   r'   .local_reaching_centrality.<locals>.as_distance   s    #aeeFA&666r   )sourcer   )r>   c              3   :   >#    U  H  n[        TUTS 9v   M     g7f)r   N)r   )r   r   r   r   s     r   r   ,local_reaching_centrality.<locals>.<genexpr>   s     OOAtF3s   )	r3   r   r0   r2   r1   r4   is_directedvaluesr   )
r   r%   r)   r   r*   r'   normavgwsum_avg_weightr   s
   `  `     @r   r   r   y   s7   x f--2s1v{X
 	
 }$$Qv6""#BCC1""#?@@7 $$QqEE$$Qq1E ~!--//E
Q3q6A:..f(vvVv$qvvx/OODY%NSVaZ((r   r	   )NT)NNT)
__doc__networkxr0   networkx.utilsr   __all__r   _dispatchabler   r   r,   r   r   <module>rK      sa    G  #')D
E&2 X&S8 'S8l X&W) 'W)r   