
    h/8                     D   S r SSKJr  SSKJrJr  SSKJr  SSKr	SSK
Jr  SSKJr  SSKJr  S	S
/r\" S5      \	R"                  " SS9 SS j5       5       r\" S5      \	R"                  " SS9SS j5       5       rS rS rS rS rS rSS jrSS jr\" S5      SS j5       rg)z Betweenness centrality measures.    )deque)heappopheappush)countN)_weight_function)py_random_state)not_implemented_forbetweenness_centralityedge_betweenness_centrality   weight)
edge_attrsc           	      |   [         R                  U S5      nUc  U nO)UR                  [        U R	                  5       5      U5      nU HK  nUc  [        X5      u  ppO[        XU5      u  ppU(       a  [        XiXU5      u  plM<  [        XiXU5      u  plMM     [        U[        U 5      UU R                  5       UUS9nU$ )u  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.
  Weights are used to calculate weighted shortest paths, so they are
  interpreted as distances.

endpoints : bool, optional
  If True include the endpoints in the shortest path counts.

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
    Note that this is only used if k is not None.

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.

The total number of paths between source and target is counted
differently for directed and undirected graphs. Directed paths
are easy to count. Undirected paths are tricky: should a path
from "u" to "v" count as 1 undirected path or as 2 directed paths?

For betweenness_centrality we report the number of undirected
paths when G is undirected.

For betweenness_centrality_subset the reporting is different.
If the source and target subsets are the same, then we want
to count undirected paths. But if the source and target subsets
differ -- for example, if sources is {0} and targets is {1},
then we are only counting the paths in one direction. They are
undirected paths but we are counting them in a directed way.
To count them as undirected paths, each should count as half a path.

This algorithm is not guaranteed to be correct if edge weights
are floating point numbers. As a workaround you can use integer
numbers by multiplying the relevant edge attributes by a convenient
constant factor (eg 100) and converting to integers.

References
----------
.. [1] Ulrik Brandes:
   A Faster Algorithm for Betweenness Centrality.
   Journal of Mathematical Sociology 25(2):163-177, 2001.
   https://doi.org/10.1080/0022250X.2001.9990249
.. [2] Ulrik Brandes:
   On Variants of Shortest-Path Betweenness
   Centrality and their Generic Computation.
   Social Networks 30(2):136-145, 2008.
   https://doi.org/10.1016/j.socnet.2007.11.001
.. [3] Ulrik Brandes and Christian Pich:
   Centrality Estimation in Large Networks.
   International Journal of Bifurcation and Chaos 17(7):2303-2318, 2007.
   https://dx.doi.org/10.1142/S0218127407018403
.. [4] Linton C. Freeman:
   A set of measures of centrality based on betweenness.
   Sociometry 40: 35–41, 1977
   https://doi.org/10.2307/3033543
        )
normalizeddirectedk	endpoints)dictfromkeyssamplelistnodes"_single_source_shortest_path_basic"_single_source_dijkstra_path_basic_accumulate_endpoints_accumulate_basic_rescalelenis_directed)Gr   r   r   r   seedbetweennessr   sSPsigma_s                \/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/centrality/betweenness.pyr
   r
      s    d --3'KyDOQ/>?ENA%?fMNA%2;1QONK.{qKNK  A
K        c                    [         R                  U S5      nUR                  [         R                  U R                  5       S5      5        Uc  U nO)UR	                  [        U R                  5       5      U5      nU H1  nUc  [        X5      u  ppO[        XU5      u  pp[        XXXU5      nM3     U  H  nX\	 M     [        U[        U 5      X R                  5       S9nU R                  5       (       a
  [        XUS9nU$ )a  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.
  Weights are used to calculate weighted shortest paths, so they are
  interpreted as distances.

seed : integer, random_state, or None (default)
    Indicator of random number generation state.
    See :ref:`Randomness<randomness>`.
    Note that this is only used if k is not None.

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.
   https://doi.org/10.1080/0022250X.2001.9990249
.. [2] Ulrik Brandes: On Variants of Shortest-Path Betweenness
   Centrality and their Generic Computation.
   Social Networks 30(2):136-145, 2008.
   https://doi.org/10.1016/j.socnet.2007.11.001
r   )r   r   )r   )r   r   updateedgesr   r   r   r   r   _accumulate_edges
_rescale_er   r    is_multigraph_add_edge_keys)r!   r   r   r   r"   r#   r   r$   r%   r&   r'   r(   ns                r)   r   r      s    L --3'Kt}}QWWY45yDOQ/>?ENA%?fMNA%'!D  N SV
]]_K 	$QFCr*   c                    / n0 nU  H  n/ X4'   M	     [         R                  U S5      n0 nSXQ'   SXa'   [        U/5      nU(       a  UR                  5       nUR	                  U5        Xd   nXT   n	X    HL  n
X;  a  UR	                  U
5        US-   Xj'   Xj   US-   :X  d  M-  XZ==   U	-  ss'   X:   R	                  U5        MN     U(       a  M  X#XV4$ )Nr         ?r      )r   r   r   popleftappend)r!   r$   r%   r&   vr'   DQDvsigmavws              r)   r   r      s    
A
A MM!S!E
AEHADqc
A
IIK	TAzAvtrAv~F"A  ! >r*   c                    [        X5      n/ n0 nU  H  n/ XE'   M	     [        R                  U S5      n0 nSXa'   [        n[        n	US0n
[        5       n/ nU" US[        U5      X45        U(       a  U	" U5      u  ppXW;   a  M  Xe==   Xo   -  ss'   UR                  U5        XU'   X   R                  5        H}  u  nnX" UUU5      -   nUU;  a7  UU
;  d	  UU
U   :  a(  UU
U'   U" UU[        U5      UU45        SUU'   U/UU'   MO  UU
U   :X  d  MZ  UU==   Xe   -  ss'   UU   R                  U5        M     U(       a  M  X4Xg4$ )Nr   r5   r   )	r   r   r   r   r   r   nextr8   items)r!   r$   r   r%   r&   r9   r'   r:   pushpopseencr;   distr(   predr>   edgedatavw_dists                      r)   r   r     sc   a(F
A
A MM!S!E
AEHD
Cq6DA
AQQ
 V$6EK	!4::<KAxVAq(33Gzq}$q'0A!QQ$q'1a01as!DG#aEH$!A ( !" >r*   c                     [         R                  US5      nU(       aT  UR                  5       nSXV   -   X6   -  nX&    H  nXX==   X8   U-  -  ss'   M     Xd:w  a  X==   XV   -  ss'   U(       a  MT  X4$ Nr   r6   r   r   rC   	r#   r%   r&   r'   r$   deltar>   coeffr9   s	            r)   r   r   =  sv    MM!QE
EEGUX)AH5((H 6Neh&N ! r*   c                     X==   [        U5      S-
  -  ss'   [        R                  US5      nU(       aW  UR                  5       nSXV   -   X6   -  nX&    H  nXX==   X8   U-  -  ss'   M     Xd:w  a  X==   XV   S-   -  ss'   U(       a  MW  X4$ )Nr6   r   )r   r   r   rC   rM   s	            r)   r   r   I  s    Nc!fqj NMM!QE
EEGUX)AH5((H 6Nehl*N ! r*   c                 4   [         R                  US5      nU(       az  UR                  5       nSXV   -   X6   -  nX&    H:  nX8   U-  n	X4U ;  a  XU4==   U	-  ss'   OXU4==   U	-  ss'   XX==   U	-  ss'   M<     Xd:w  a  X==   XV   -  ss'   U(       a  Mz  U $ rK   rL   )
r#   r%   r&   r'   r$   rN   r>   rO   r9   rE   s
             r)   r/   r/   V  s    MM!QE
EEGUX)A5 Av[(F#q(#F#q(#HMH  6Neh&N ! r*   c                     U(       a3  U(       a  US:  a  S nO/SXS-
  -  -  nO$US::  a  S nOSUS-
  US-
  -  -  nOU(       d  SnOS nUb  Ub  Xa-  U-  nU  H  nX==   U-  ss'   M     U $ )N   r6         ? )r#   r3   r   r   r   r   scaler9   s           r)   r   r   g  s    1u Qa%[)!VE!a%AE*+EEE=IMEANe#N r*   c                     U(       a  US::  a  S nOSXS-
  -  -  nOU(       d  SnOS nUb  Ub  XQ-  U-  nU  H  nX==   U-  ss'   M     U $ )Nr6   rT   rU   )r#   r3   r   r   r   rV   r9   s          r)   r0   r0     s`    6E!e%EEE=IMEANe#N r*   graphc                 &   [        X5      n[        R                  U R                  S5      nU HZ  u  pVX   U   nU" XVU5      nU V	s/ s H  o" XVXU	   05      U:X  d  M  U	PM     n
n	XU4   [	        U
5      -  nU
 H	  n	XXVU	4'   M     M\     U$ s  sn	f )a  Adds the corrected betweenness centrality (BC) values for multigraphs.

Parameters
----------
G : NetworkX graph.

betweenness : dictionary
    Dictionary mapping adjacent node tuples to betweenness centrality values.

weight : string or function
    See `_weight_function` for details. Defaults to `None`.

Returns
-------
edges : dictionary
    The parameter `betweenness` including edges with keys and their
    betweenness centrality values.

The BC value is divided among edges of equal weight.
r   )r   r   r   r.   r   )r!   r#   r   _weightedge_bcur9   dwtr   keysbcs               r)   r2   r2     s    , q)GmmAGGS)GDGQ1?1aqA$i 8B >1?Q 3t9,A!#Q1I   N @s   BB)NTNFN)NTNN)FNF)FN)N)__doc__collectionsr   heapqr   r   	itertoolsr   networkxnx+networkx.algorithms.shortest_paths.weightedr   networkx.utilsr   networkx.utils.decoratorsr	   __all___dispatchabler
   r   r   r   r   r   r/   r   r0   r2   rU   r*   r)   <module>rl      s    &  #   H * 9#%B
C X&CGI ' IX X&[ ' [B2!H	
"2& W   r*   