
    h                         S r SSKJr  SSKrSS/r\R                  " SS9SS j5       rSS	 jr\r	\R                  SS
 j5       r
SS jrg)zLoad centrality.    )
itemgetterNload_centralityedge_load_centralityweight)
edge_attrsc                    UbY  SnU  H  n[        XUSU5      nXQU;   a  Xq   OS-  nM!     U(       a)  U R                  5       nUS::  a  U$ USUS-
  US-
  -  -  -  nU$ 0 R                  U S5      nU H(  n[        XUSU5      nU H  n	XY==   Xy   -  ss'   M     M*     U(       a;  U R                  5       nUS::  a  U$ SUS-
  US-
  -  -  n
U H  nXQ==   U
-  ss'   M     U$ )u~  Compute load centrality for nodes.

The load centrality of a node is the fraction of all shortest
paths that pass through that node.

Parameters
----------
G : graph
  A networkx graph.

normalized : bool, optional (default=True)
  If True the betweenness values are normalized by b=b/(n-1)(n-2) where
  n is the number of nodes in G.

weight : None or string, optional (default=None)
  If None, edge weights are ignored.
  Otherwise holds the name of the edge attribute used as weight.
  The weight of an edge is treated as the length or distance between the two sides.

cutoff : bool, optional (default=None)
  If specified, only consider paths of length <= cutoff.

Returns
-------
nodes : dictionary
   Dictionary of nodes with centrality as the value.

See Also
--------
betweenness_centrality

Notes
-----
Load centrality is slightly different than betweenness. It was originally
introduced by [2]_. For this load algorithm see [1]_.

References
----------
.. [1] Mark E. J. Newman:
   Scientific collaboration networks. II.
   Shortest paths, weighted networks, and centrality.
   Physical Review E 64, 016132, 2001.
   http://journals.aps.org/pre/abstract/10.1103/PhysRevE.64.016132
.. [2] Kwang-Il Goh, Byungnam Kahng and Doochul Kim
   Universal behavior of Load Distribution in Scale-Free Networks.
   Physical Review Letters 87(27):1–4, 2001.
   https://doi.org/10.1103/PhysRevLett.87.278701
        Fr            ?   )_node_betweennessorderfromkeys)Gvcutoff
normalizedr   betweennesssourceubetweenr   vkscales              U/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/centrality/load.pynewman_betweenness_centralityr   
   s   d 	}F(FE6JHx-8;Q>K  GGIEz""3519";<<K  kk!S)!F(FE6JH8</  " GGIEz""EAI%!)45E %' !    c                    Uc  [         R                  " XUSS9u  pVO[         R                  " XX$5      u  pVUR                  5        VVs/ s H  u  pxX4PM
     n	nnU	R	                  5         U	 VVs/ s H  u  pUS:  d  M  UPM     snnU	SS& 0 R                  US5      n
U	(       aN  U	R                  5       nX;   a0  [        X[   5      nX[    H  nX:X  a    OX==   X   U-  -  ss'   M     U	(       a  MN  U
 H  nX==   S-  ss'   M     U(       a4  [        U
5      nUS:  a#  SUS-
  US-
  -  -  nU
 H  nX==   U-  ss'   M     U
$ s  snnf s  snnf )a  Node betweenness_centrality helper:

See betweenness_centrality for what you probably want.
This actually computes "load" and not betweenness.
See https://networkx.lanl.gov/ticket/103

This calculates the load of each node for paths from a single source.
(The fraction of number of shortests paths from source that go
through each node.)

To get the load for a node you need to do all-pairs shortest paths.

If weight is not None then use Dijkstra for finding shortest paths.
NTr   return_seenr   r   r   r
   )nxpredecessor!dijkstra_predecessor_and_distanceitemssortr   poplen)r   r   r   r   r   predlengthvertlonodesbetweenr   	num_pathsxr   s                  r   r   r   V   s@     ~&dSv==aX *08ITqiF8
KKM'-7v)1Qv7F1I kk&#&G
JJL9DGIW;
gj944
 	 & 
a
  Lq5!a%AE*+E
e#
 N5 97s   	E0E Ec                     0 nU R                  5        H  u  p4SX#U4'   SX$U4'   M     U  H2  n[        XUS9nUR                  5        H  u  pxX'==   U-  ss'   M     M4     U$ )a{  Compute edge load.

WARNING: This concept of edge load has not been analysed
or discussed outside of NetworkX that we know of.
It is based loosely on load_centrality in the sense that
it counts the number of shortest paths which cross each edge.
This function is for demonstration and testing purposes.

Parameters
----------
G : graph
    A networkx graph

cutoff : bool, optional (default=False)
    If specified, only consider paths of length <= cutoff.

Returns
-------
A dict keyed by edge 2-tuple to the number of shortest paths
which use that edge. Where more than one path is shortest
the count is divided equally among paths.
r	   )r   )edges_edge_betweennessr"   )	r   r   r   ur   r   r   e	ubetweenvs	            r   r   r      sr    0 K	!F!F  $Qv>$NN,LANi'N -  r   c                    [         R                  " XUSS9u  pE[        UR                  5       [	        S5      S9 VVs/ s H  u  pgUPM	     nnn0 n	U R                  U5       H  u  pSXU4'   SXU
4'   M     U(       a  UR                  5       nX;   aa  [        XK   5      nXK    HL  nX;   d  M
  [        XM   5      nXM    H-  nXU4==   XU4   U-  -  ss'   XU4==   XU4   U-  -  ss'   M/     MN     U(       a  M  U	$ s  snnf )zEdge betweenness helper.Tr   r   )keyr   )r   r    sortedr"   r   r/   r$   r%   )r   r   nodesr   r&   r'   ndr*   r+   r1   r   r,   wr-   s                  r   r0   r0      s    ^^Af$ONT"6<<>z!}EFEDAaEFFGAA  JJL9DGIW9 #DGI!WA7q6?Y+FFA7q6?Y+FF %	  & N' Gs   C>)NNTN)FTN)F)NF)__doc__operatorr   networkxr   __all___dispatchabler   r   r   r   r0    r   r   <module>rA      sh      4
5 X&H 'HV0f 0    Fr   