ó
    …~i)  ã                   óH  • S r SSKrSSKJr  / SQr\" S5      \" S5      \R                  SS.S	 j5       5       5       r\" S5      \" S5      \R                  SS.S
 j5       5       5       r\" S5      \" S5      \R                  S 5       5       5       r	\R                  S 5       r
g)z5
Subraph centrality and communicability betweenness.
é    N)Únot_implemented_for)Úsubgraph_centrality_expÚsubgraph_centralityÚ&communicability_betweenness_centralityÚestrada_indexÚdirectedÚ
multigraphF)Ú
normalizedc                ó*  • SSK n[        U 5      n[        R                  " X5      nSXDS:g  '   UR                  R                  U5      n[        [        UR                  5       5      nU(       a  XfR                  5       -  n[        [        X65      5      nU$ )a„  Returns the subgraph centrality for each node of G.

Subgraph centrality  of a node `n` is the sum of weighted closed
walks of all lengths starting and ending at node `n`. The weights
decrease with path length. Each closed walk is associated with a
connected subgraph ([1]_).

Parameters
----------
G: graph
normalized : bool
    If True, normalize the centrality values using the largest eigenvalue of the
    adjacency matrix so that the centrality values are generally between 0 and 1.

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

Raises
------
NetworkXError
    If the graph is not undirected and simple.

See Also
--------
subgraph_centrality:
    Alternative algorithm of the subgraph centrality for each node of G.

Notes
-----
This version of the algorithm exponentiates the adjacency matrix.

The subgraph centrality of a node `u` in G can be found using
the matrix exponential of the adjacency matrix of G [1]_,

.. math::

    SC(u)=(e^A)_{uu} .

Examples
--------
(Example from [1]_)

>>> G = nx.Graph(
...     [
...         (1, 2),
...         (1, 5),
...         (1, 8),
...         (2, 3),
...         (2, 8),
...         (3, 4),
...         (3, 6),
...         (4, 5),
...         (4, 7),
...         (5, 6),
...         (6, 7),
...         (7, 8),
...     ]
... )
>>> sc = nx.subgraph_centrality_exp(G)
>>> print([f"{node} {sc[node]:0.2f}" for node in sorted(sc)])
['1 3.90', '2 3.90', '3 3.64', '4 3.71', '5 3.64', '6 3.71', '7 3.64', '8 3.90']
>>> sc = nx.subgraph_centrality(G, normalized=True)
>>> print([f"{node} {sc[node]:0.3f}" for node in sorted(sc)])
['1 0.194', '2 0.194', '3 0.181', '4 0.184', '5 0.181', '6 0.184', '7 0.181', '8 0.194']

References
----------
.. [1] Ernesto Estrada, Juan A. Rodriguez-Velazquez,
   "Subgraph centrality in complex networks",
   Physical Review E 71, 056103 (2005).
   https://arxiv.org/abs/cond-mat/0504730

r   Né   g        )ÚscipyÚlistÚnxÚto_numpy_arrayÚlinalgÚexpmÚmapÚfloatÚdiagonalÚmaxÚdictÚzip)ÚGr
   ÚspÚnodelistÚAÚexpAÚvaluesÚscs           Úh/home/mande/repo/quber/.venv/lib/python3.13/site-packages/networkx/algorithms/centrality/subgraph_alg.pyr   r      su   € ó` ä�A‹w€HÜ
×Ò˜!Ó&€Aà€Aˆ3�h�KØ�9‰9�>‰>˜!Ó€DÜ”˜Ÿ™›Ó(€FÞØŸ*™*›,Ñ&ˆä	Œc�(Ó#Ó	$€BØ€Ió    c          	      óš  • SSK n[        U 5      n[        R                  " X5      nSXBR	                  U5      '   UR
                  R                  U5      u  pVUR                  U5      S-  nU(       a"  UR                  XUR                  5       -
  5      nOUR                  U5      nXx-  n	[        [        U[        [        U	5      5      5      n
U
$ )a   Returns subgraph centrality for each node in G.

Subgraph centrality  of a node `n` is the sum of weighted closed
walks of all lengths starting and ending at node `n`. The weights
decrease with path length. Each closed walk is associated with a
connected subgraph ([1]_).

Parameters
----------
G: Graph
normalized : bool
    If True, normalize the centrality values using the largest eigenvalue of the
    adjacency matrix so that the centrality values are generally between 0 and 1.

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

Raises
------
NetworkXError
   If the graph is not undirected and simple.

See Also
--------
subgraph_centrality_exp:
    Alternative algorithm of the subgraph centrality for each node of G.

Notes
-----
This version of the algorithm computes eigenvalues and eigenvectors
of the adjacency matrix.

Subgraph centrality of a node `u` in G can be found using
a spectral decomposition of the adjacency matrix [1]_,

.. math::

   SC(u)=\sum_{j=1}^{N}(v_{j}^{u})^2 e^{\lambda_{j}},

where `v_j` is an eigenvector of the adjacency matrix `A` of G
corresponding to the eigenvalue `\lambda_j`.

Examples
--------
(Example from [1]_)

>>> G = nx.Graph(
...     [
...         (1, 2),
...         (1, 5),
...         (1, 8),
...         (2, 3),
...         (2, 8),
...         (3, 4),
...         (3, 6),
...         (4, 5),
...         (4, 7),
...         (5, 6),
...         (6, 7),
...         (7, 8),
...     ]
... )
>>> sc = nx.subgraph_centrality(G)
>>> print([f"{node} {sc[node]:0.2f}" for node in sorted(sc)])
['1 3.90', '2 3.90', '3 3.64', '4 3.71', '5 3.64', '6 3.71', '7 3.64', '8 3.90']
>>> sc = nx.subgraph_centrality(G, normalized=True)
>>> print([f"{node} {sc[node]:0.3f}" for node in sorted(sc)])
['1 0.194', '2 0.194', '3 0.181', '4 0.184', '5 0.181', '6 0.184', '7 0.181', '8 0.194']

References
----------
.. [1] Ernesto Estrada, Juan A. Rodriguez-Velazquez,
   "Subgraph centrality in complex networks",
   Physical Review E 71, 056103 (2005).
   https://arxiv.org/abs/cond-mat/0504730

r   Nr   é   )Únumpyr   r   r   Únonzeror   ÚeighÚarrayÚexpr   r   r   r   r   )r   r
   Únpr   r   ÚwÚvÚvsquareÚexpwÚxgr   s              r    r   r   o   s    € óf ä�A‹w€HÜ
×Ò˜!Ó&€Aà€A‡j�j�ƒmÑØ�9‰9�>‰>˜!Ó�D€AØ�h‰h�q‹k˜QÑ€GÞØ�v‰v�aŸ%™%›'‘kÓ"‰à�v‰v�a‹yˆØ	‰€Bä	Œc�(œC¤ r›NÓ+Ó	,€BØ€Ir!   c                 ó>  • SSK nSSKn[        U 5      n[        U5      n[        R
                  " X5      nSXQR                  U5      '   UR                  R                  U5      n[        [        U[        U5      5      5      n0 nU  HÃ  n	Xy   n
XZSS24   R                  5       nUSS2U
4   R                  5       nSXZSS24'   SUSS2U
4'   XbR                  R                  U5      -
  U-  nSXÚSS24'   SUSS2U
4'   XÑR                  UR                  U5      5      -  n[        UR                  5       5      X‰'   XµU
SS24'   XÅSS2U
4'   MÅ     [        U5      nUS:”  a:  SUS-
  S-  US-
  -
  -  nUR!                  5        VVs0 s H  u  nnUUU-  _M     nnnU$ s  snnf )aQ  Returns subgraph communicability for all pairs of nodes in G.

Communicability betweenness measure makes use of the number of walks
connecting every pair of nodes as the basis of a betweenness centrality
measure.

Parameters
----------
G: graph

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

Raises
------
NetworkXError
    If the graph is not undirected and simple.

Notes
-----
Let `G=(V,E)` be a simple undirected graph with `n` nodes and `m` edges,
and `A` denote the adjacency matrix of `G`.

Let `G(r)=(V,E(r))` be the graph resulting from
removing all edges connected to node `r` but not the node itself.

The adjacency matrix for `G(r)` is `A+E(r)`,  where `E(r)` has nonzeros
only in row and column `r`.

The subraph betweenness of a node `r`  is [1]_

.. math::

     \omega_{r} = \frac{1}{C}\sum_{p}\sum_{q}\frac{G_{prq}}{G_{pq}},
     p\neq q, q\neq r,

where
`G_{prq}=(e^{A}_{pq} - (e^{A+E(r)})_{pq}`  is the number of walks
involving node r,
`G_{pq}=(e^{A})_{pq}` is the number of closed walks starting
at node `p` and ending at node `q`,
and `C=(n-1)^{2}-(n-1)` is a normalization factor equal to the
number of terms in the sum.

The resulting `\omega_{r}` takes values between zero and one.
The lower bound cannot be attained for a connected
graph, and the upper bound is attained in the star graph.

References
----------
.. [1] Ernesto Estrada, Desmond J. Higham, Naomichi Hatano,
   "Communicability Betweenness in Complex Networks"
   Physica A 388 (2009) 764-774.
   https://arxiv.org/abs/0905.4102

Examples
--------
>>> G = nx.Graph([(0, 1), (1, 2), (1, 5), (5, 4), (2, 4), (2, 3), (4, 3), (3, 6)])
>>> cbc = nx.communicability_betweenness_centrality(G)
>>> print([f"{node} {cbc[node]:0.2f}" for node in sorted(cbc)])
['0 0.03', '1 0.45', '2 0.51', '3 0.45', '4 0.40', '5 0.19', '6 0.03']
r   Nr   r#   g      ð?)r$   r   r   Úlenr   r   r%   r   r   r   r   ÚrangeÚcopyÚdiagr   ÚsumÚitems)r   r)   r   r   Únr   r   ÚmappingÚcbcr+   ÚiÚrowÚcolÚBÚorderÚscaleÚnodeÚvalues                     r    r   r   Ô   sˆ  € óH Ûä�A‹w€HÜˆH‹€AÜ
×Ò˜!Ó&€Aà€A‡j�j�ƒmÑØ�9‰9�>‰>˜!Ó€DÜ”3�x¤ q£Ó*Ó+€GØ
€CÛˆà‰JˆØ’1�‰g�l‰l‹nˆØ’�1�‰g�l‰l‹nˆØˆŠQˆ$‰ØˆŠ!ˆQˆ$‰Ø—I‘I—N‘N 1Ó%Ñ%¨Ñ-ˆàˆŠQˆ$‰ØˆŠ!ˆQˆ$‰Ø	�W‰W�R—W‘W˜Q“ZÓ Ñ ˆÜ�q—u‘u“w“ˆ‰àˆ!ŠQˆ$‰ØŠ!ˆQˆ$‹ñ ô" �‹H€EØˆqƒyØ˜ ™¨Ñ)¨U°S©[Ñ9Ñ:ˆØ69·i±i´kÔB²k¡{ t¨Uˆt�U˜U‘]Ò"±kˆÑBØ€Jùó Cs   ÆFc                 óF   • [        [        U 5      R                  5       5      $ )uC  Returns the Estrada index of a the graph G.

The Estrada Index is a topological index of folding or 3D "compactness" ([1]_).

Parameters
----------
G: graph

Returns
-------
estrada index: float

Raises
------
NetworkXError
    If the graph is not undirected and simple.

Notes
-----
Let `G=(V,E)` be a simple undirected graph with `n` nodes  and let
`\lambda_{1}\leq\lambda_{2}\leq\cdots\lambda_{n}`
be a non-increasing ordering of the eigenvalues of its adjacency
matrix `A`. The Estrada index is ([1]_, [2]_)

.. math::
    EE(G)=\sum_{j=1}^n e^{\lambda _j}.

References
----------
.. [1] E. Estrada, "Characterization of 3D molecular structure",
   Chem. Phys. Lett. 319, 713 (2000).
   https://doi.org/10.1016/S0009-2614(00)00158-5
.. [2] JosÃ© Antonio de la PeÃ±aa, Ivan Gutman, Juan Rada,
   "Estimating the Estrada index",
   Linear Algebra and its Applications. 427, 1 (2007).
   https://doi.org/10.1016/j.laa.2007.06.020

Examples
--------
>>> G = nx.Graph([(0, 1), (1, 2), (1, 5), (5, 4), (2, 4), (2, 3), (4, 3), (3, 6)])
>>> ei = nx.estrada_index(G)
>>> print(f"{ei:0.5}")
20.55
)r4   r   r   )r   s    r    r   r   ;  s   € ô\ Ô" 1Ó%×,Ñ,Ó.Ó/Ð/r!   )Ú__doc__Únetworkxr   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r   r   r   © r!   r    Ú<module>rH      sÞ   ðñó Ý .ò€ñ �ZÓ Ù�\Ó"Ø×ÑØ-2ô Yó ó #ó !ðYñx �ZÓ Ù�\Ó"Ø×ÑØ).ô _ó ó #ó !ð_ñD �ZÓ Ù�\Ó"Ø×Ññaó ó #ó !ðaðH ×Ññ-0ó ñ-0r!   