ó
    †~iRA  ã                   óJ  • S r SSKrSSKJr  / SQr\R                  " SS9SS j5       r\R                  " SS9SS j5       r\" S	5      \" S
5      \R                  " SS9 SS j5       5       5       r	\" S	5      \" S
5      \R                  " SS9 SS j5       5       5       r
SS jrg)a{  Laplacian matrix of graphs.

All calculations here are done using the out-degree. For Laplacians using
in-degree, use `G.reverse(copy=False)` instead of `G` and take the transpose.

The `laplacian_matrix` function provides an unnormalized matrix,
while `normalized_laplacian_matrix`, `directed_laplacian_matrix`,
and `directed_combinatorial_laplacian_matrix` are all normalized.
é    N)Únot_implemented_for)Úlaplacian_matrixÚnormalized_laplacian_matrixÚdirected_laplacian_matrixÚ'directed_combinatorial_laplacian_matrixÚweight)Ú
edge_attrsc                 óæ   • SSK nUc  [        U 5      n[        R                  " XUSS9nUR                  u  pVUR
                  R                  UR                  SS9S4Xe4S9R                  5       nXt-
  $ )už
  Returns the Laplacian matrix of G.

The graph Laplacian is the matrix L = D - A, where
A is the adjacency matrix and D is the diagonal matrix of node degrees.

Parameters
----------
G : graph
   A NetworkX graph

nodelist : list, optional
   The rows and columns are ordered according to the nodes in nodelist.
   If nodelist is None, then the ordering is produced by G.nodes().

weight : string or None, optional (default='weight')
   The edge data key used to compute each value in the matrix.
   If None, then each edge has weight 1.

Returns
-------
L : SciPy sparse array
  The Laplacian matrix of G.

Notes
-----
For MultiGraph, the edges weights are summed.

This returns an unnormalized matrix. For a normalized output,
use `normalized_laplacian_matrix`, `directed_laplacian_matrix`,
or `directed_combinatorial_laplacian_matrix`.

This calculation uses the out-degree of the graph `G`. To use the
in-degree for calculations instead, use `G.reverse(copy=False)` and
take the transpose.

See Also
--------
:func:`~networkx.convert_matrix.to_numpy_array`
normalized_laplacian_matrix
directed_laplacian_matrix
directed_combinatorial_laplacian_matrix
:func:`~networkx.linalg.spectrum.laplacian_spectrum`

Examples
--------
For graphs with multiple connected components, L is permutation-similar
to a block diagonal matrix where each block is the respective Laplacian
matrix for each component.

>>> G = nx.Graph([(1, 2), (2, 3), (4, 5)])
>>> print(nx.laplacian_matrix(G).toarray())
[[ 1 -1  0  0  0]
 [-1  2 -1  0  0]
 [ 0 -1  1  0  0]
 [ 0  0  0  1 -1]
 [ 0  0  0 -1  1]]

>>> edges = [
...     (1, 2),
...     (2, 1),
...     (2, 4),
...     (4, 3),
...     (3, 4),
... ]
>>> DiG = nx.DiGraph(edges)
>>> print(nx.laplacian_matrix(DiG).toarray())
[[ 1 -1  0  0]
 [-1  2 -1  0]
 [ 0  0  1 -1]
 [ 0  0 -1  1]]

Notice that node 4 is represented by the third column and row. This is because
by default the row/column order is the order of `G.nodes` (i.e. the node added
order -- in the edgelist, 4 first appears in (2, 4), before node 3 in edge (4, 3).)
To control the node order of the matrix, use the `nodelist` argument.

>>> print(nx.laplacian_matrix(DiG, nodelist=[1, 2, 3, 4]).toarray())
[[ 1 -1  0  0]
 [-1  2  0 -1]
 [ 0  0  1 -1]
 [ 0  0 -1  1]]

This calculation uses the out-degree of the graph `G`. To use the
in-degree for calculations instead, use `G.reverse(copy=False)` and
take the transpose.

>>> print(nx.laplacian_matrix(DiG.reverse(copy=False)).toarray().T)
[[ 1 -1  0  0]
 [-1  1 -1  0]
 [ 0  0  2 -1]
 [ 0  0 -1  1]]

References
----------
.. [1] Langville, Amy N., and Carl D. Meyer. Googleâ€™s PageRank and Beyond:
   The Science of Search Engine Rankings. Princeton University Press, 2006.

r   NÚcsr©Únodelistr   Úformaté   ©Úaxis©Úshape)	ÚscipyÚlistÚnxÚto_scipy_sparse_arrayr   ÚsparseÚ	dia_arrayÚsumÚtocsr)ÚGr   r   ÚspÚAÚnÚmÚDs           Ú\/home/mande/repo/quber/.venv/lib/python3.13/site-packages/networkx/linalg/laplacianmatrix.pyr   r      sp   € óH àÑÜ˜“7ˆÜ
× Ò  ¸fÈUÑS€AØ�7‰7�D€AØ
�	‰	×Ñ˜QŸU™U¨˜U˜]¨AÐ.°q°fÐÐ=×CÑCÓE€AØ‰5€Ló    c                 óø  • SSK nSSKnUc  [        U 5      n[        R                  " XUSS9nUR
                  u  pgUR                  SS9nUR                  R                  US4Xf4S9R                  5       n	X•-
  n
UR                  SS	9   S
UR                  U5      -  nSSS5        SWUR                  U5      '   UR                  R                  US4Xf4S9R                  5       nXÊU-  -  $ ! , (       d  f       NT= f)uº  Returns the normalized Laplacian matrix of G.

The normalized graph Laplacian is the matrix

.. math::

    N = D^{-1/2} L D^{-1/2}

where `L` is the graph Laplacian and `D` is the diagonal matrix of
node degrees [1]_.

Parameters
----------
G : graph
   A NetworkX graph

nodelist : list, optional
   The rows and columns are ordered according to the nodes in nodelist.
   If nodelist is None, then the ordering is produced by G.nodes().

weight : string or None, optional (default='weight')
   The edge data key used to compute each value in the matrix.
   If None, then each edge has weight 1.

Returns
-------
N : SciPy sparse array
  The normalized Laplacian matrix of G.

Notes
-----
For MultiGraph, the edges weights are summed.
See :func:`to_numpy_array` for other options.

If the Graph contains selfloops, D is defined as ``diag(sum(A, 1))``, where A is
the adjacency matrix [2]_.

This calculation uses the out-degree of the graph `G`. To use the
in-degree for calculations instead, use `G.reverse(copy=False)` and
take the transpose.

For an unnormalized output, use `laplacian_matrix`.

Examples
--------

>>> import numpy as np
>>> edges = [
...     (1, 2),
...     (2, 1),
...     (2, 4),
...     (4, 3),
...     (3, 4),
... ]
>>> DiG = nx.DiGraph(edges)
>>> print(nx.normalized_laplacian_matrix(DiG).toarray())
[[ 1.         -0.70710678  0.          0.        ]
 [-0.70710678  1.         -0.70710678  0.        ]
 [ 0.          0.          1.         -1.        ]
 [ 0.          0.         -1.          1.        ]]

Notice that node 4 is represented by the third column and row. This is because
by default the row/column order is the order of `G.nodes` (i.e. the node added
order -- in the edgelist, 4 first appears in (2, 4), before node 3 in edge (4, 3).)
To control the node order of the matrix, use the `nodelist` argument.

>>> print(nx.normalized_laplacian_matrix(DiG, nodelist=[1, 2, 3, 4]).toarray())
[[ 1.         -0.70710678  0.          0.        ]
 [-0.70710678  1.          0.         -0.70710678]
 [ 0.          0.          1.         -1.        ]
 [ 0.          0.         -1.          1.        ]]
>>> G = nx.Graph(edges)
>>> print(nx.normalized_laplacian_matrix(G).toarray())
[[ 1.         -0.70710678  0.          0.        ]
 [-0.70710678  1.         -0.5         0.        ]
 [ 0.         -0.5         1.         -0.70710678]
 [ 0.          0.         -0.70710678  1.        ]]

See Also
--------
laplacian_matrix
normalized_laplacian_spectrum
directed_laplacian_matrix
directed_combinatorial_laplacian_matrix

References
----------
.. [1] Fan Chung-Graham, Spectral Graph Theory,
   CBMS Regional Conference Series in Mathematics, Number 92, 1997.
.. [2] Steve Butler, Interlacing For Weighted Graphs Using The Normalized
   Laplacian, Electronic Journal of Linear Algebra, Volume 16, pp. 90-98,
   March 2007.
.. [3] Langville, Amy N., and Carl D. Meyer. Googleâ€™s PageRank and Beyond:
   The Science of Search Engine Rankings. Princeton University Press, 2006.
r   Nr   r   r   r   r   Úignore)Údivideç      ð?)Únumpyr   r   r   r   r   r   r   r   r   ÚerrstateÚsqrtÚisinf)r   r   r   Únpr   r   r   Ú_Údiagsr!   ÚLÚ
diags_sqrtÚDHs                r"   r   r   „   sì   € óB ÛàÑÜ˜“7ˆÜ
× Ò  ¸fÈUÑS€AØ�7‰7�D€AØ�E‰E�qˆEˆM€EØ
�	‰	×Ñ˜U A˜J¨q¨fÐÐ5×;Ñ;Ó=€AØ	‰€AØ	�‰˜HˆÒ	%Ø˜2Ÿ7™7 5›>Ñ)ˆ
÷ 
&à'(€Jˆr�x‰x˜
Ó#Ñ$Ø	�‰×	Ñ	˜j¨!˜_°Q°FÐ	Ð	;×	AÑ	AÓ	C€BØ�R‘‰=Ð÷	 
&Õ	%ús   ÂC+Ã+
C9Ú
undirectedÚ
multigraphc                 óR  • SSK nSSKn[        XX#US9nUR                  u  p‰UR                  R
                  R                  UR                  SS9u  p«UR                  5       R                  nXÌR                  5       -  nUR                  UR                  U5      5      nUR                  R                  US4Xˆ4S9R                  5       U-  UR                  R                  SU-  S4Xˆ4S9R                  5       -  nUR                  [!        U 5      5      nUXÿR                  -   S-  -
  $ )	až  Returns the directed Laplacian matrix of G.

The graph directed Laplacian is the matrix

.. math::

    L = I - \frac{1}{2} \left (\Phi^{1/2} P \Phi^{-1/2} + \Phi^{-1/2} P^T \Phi^{1/2} \right )

where `I` is the identity matrix, `P` is the transition matrix of the
graph, and `\Phi` a matrix with the Perron vector of `P` in the diagonal and
zeros elsewhere [1]_.

Depending on the value of walk_type, `P` can be the transition matrix
induced by a random walk, a lazy random walk, or a random walk with
teleportation (PageRank).

Parameters
----------
G : DiGraph
   A NetworkX graph

nodelist : list, optional
   The rows and columns are ordered according to the nodes in nodelist.
   If nodelist is None, then the ordering is produced by G.nodes().

weight : string or None, optional (default='weight')
   The edge data key used to compute each value in the matrix.
   If None, then each edge has weight 1.

walk_type : string or None, optional (default=None)
   One of ``"random"``, ``"lazy"``, or ``"pagerank"``. If ``walk_type=None``
   (the default), then a value is selected according to the properties of `G`:
   - ``walk_type="random"`` if `G` is strongly connected and aperiodic
   - ``walk_type="lazy"`` if `G` is strongly connected but not aperiodic
   - ``walk_type="pagerank"`` for all other cases.

alpha : real
   (1 - alpha) is the teleportation probability used with pagerank

Returns
-------
L : NumPy matrix
  Normalized Laplacian of G.

Notes
-----
Only implemented for DiGraphs

The result is always a symmetric matrix.

This calculation uses the out-degree of the graph `G`. To use the
in-degree for calculations instead, use `G.reverse(copy=False)` and
take the transpose.

See Also
--------
laplacian_matrix
normalized_laplacian_matrix
directed_combinatorial_laplacian_matrix

References
----------
.. [1] Fan Chung (2005).
   Laplacians and the Cheeger inequality for directed graphs.
   Annals of Combinatorics, 9(1), 2005
r   N©r   r   Ú	walk_typeÚalphar   ©Úkr   r'   ç       @)r(   r   Ú_transition_matrixr   r   ÚlinalgÚeigsÚTÚflattenÚrealr   r*   Úabsr   r   ÚidentityÚlen)r   r   r   r6   r7   r,   r   ÚPr   r    ÚevalsÚevecsÚvÚpÚsqrtpÚQÚIs                    r"   r   r   ú   s  € óP Ûô 	Ø	 VÈñ	€Að �7‰7�D€Aà—9‘9×#Ñ#×(Ñ(¨¯©°Ð(Ð2�L€EØ�‰‹×Ñ€AØ	�E‰E‹G‰€Aà�G‰G�B—F‘F˜1“IÓ€Eà
�	‰	×Ñ˜U A˜J¨q¨fÐÐ5×;Ñ;Ó=Ø
ñ	à
�)‰)×
Ñ
˜s U™{¨AÐ.°q°fÐ
Ð
=×
CÑ
CÓ
Eñ	Fð ð 	�‰”C˜“FÓ€Aà�—C‘C‘˜3‰ÑÐr#   c                 ó~  • SSK n[        XX#US9nUR                  u  pxUR                  R                  R                  UR                  SS9u  pšU
R                  5       R                  nX»R                  5       -  nUR                  R                  US4Xw4S9R                  5       nXÝU-  UR                  U-  -   S-  -
  $ )at  Return the directed combinatorial Laplacian matrix of G.

The graph directed combinatorial Laplacian is the matrix

.. math::

    L = \Phi - \frac{1}{2} \left (\Phi P + P^T \Phi \right)

where `P` is the transition matrix of the graph and `\Phi` a matrix
with the Perron vector of `P` in the diagonal and zeros elsewhere [1]_.

Depending on the value of walk_type, `P` can be the transition matrix
induced by a random walk, a lazy random walk, or a random walk with
teleportation (PageRank).

Parameters
----------
G : DiGraph
   A NetworkX graph

nodelist : list, optional
   The rows and columns are ordered according to the nodes in nodelist.
   If nodelist is None, then the ordering is produced by G.nodes().

weight : string or None, optional (default='weight')
   The edge data key used to compute each value in the matrix.
   If None, then each edge has weight 1.

walk_type : string or None, optional (default=None)
    One of ``"random"``, ``"lazy"``, or ``"pagerank"``. If ``walk_type=None``
    (the default), then a value is selected according to the properties of `G`:
    - ``walk_type="random"`` if `G` is strongly connected and aperiodic
    - ``walk_type="lazy"`` if `G` is strongly connected but not aperiodic
    - ``walk_type="pagerank"`` for all other cases.

alpha : real
   (1 - alpha) is the teleportation probability used with pagerank

Returns
-------
L : NumPy matrix
  Combinatorial Laplacian of G.

Notes
-----
Only implemented for DiGraphs

The result is always a symmetric matrix.

This calculation uses the out-degree of the graph `G`. To use the
in-degree for calculations instead, use `G.reverse(copy=False)` and
take the transpose.

See Also
--------
laplacian_matrix
normalized_laplacian_matrix
directed_laplacian_matrix

References
----------
.. [1] Fan Chung (2005).
   Laplacians and the Cheeger inequality for directed graphs.
   Annals of Combinatorics, 9(1), 2005
r   Nr5   r   r8   r   r:   )r   r;   r   r   r<   r=   r>   r?   r@   r   r   Útoarray)r   r   r   r6   r7   r   rD   r   r    rE   rF   rG   rH   ÚPhis                 r"   r   r   \  s°   € óN äØ	 VÈñ	€Að �7‰7�D€Aà—9‘9×#Ñ#×(Ñ(¨¯©°Ð(Ð2�L€EØ�‰‹×Ñ€AØ	�E‰E‹G‰€Aà
�)‰)×
Ñ
˜q !˜f¨Q¨FÐ
Ð
3×
;Ñ
;Ó
=€Cà˜‘'˜AŸC™C #™IÑ%¨Ñ,Ñ,Ð,r#   c                 ó  • SSK nSSKnUc>  [        R                  " U 5      (       a!  [        R                  " U 5      (       a  SnOSnOSn[        R
                  " XU[        S9nUR                  u  p‰US;   an  UR                  R                  SUR                  S	S
9-  S4Xˆ4S9R                  5       n
US:X  a  X§-  nU$ UR                  R                  USS9nXÊU-  -   S-  n U$ US:X  a‹  SUs=:  a  S	:  d  O  [        R                  " S5      eUR                  5       nS	U-  XwR                  S	S
9S:H  SS24'   XwR                  S	S
9UR                  SS24   R                   -  nXG-  S	U-
  U-  -   nU$ [        R                  " S5      e)a*  Returns the transition matrix of G.

This is a row stochastic giving the transition probabilities while
performing a random walk on the graph. Depending on the value of walk_type,
P can be the transition matrix induced by a random walk, a lazy random walk,
or a random walk with teleportation (PageRank).

Parameters
----------
G : DiGraph
   A NetworkX graph

nodelist : list, optional
   The rows and columns are ordered according to the nodes in nodelist.
   If nodelist is None, then the ordering is produced by G.nodes().

weight : string or None, optional (default='weight')
   The edge data key used to compute each value in the matrix.
   If None, then each edge has weight 1.

walk_type : string or None, optional (default=None)
   One of ``"random"``, ``"lazy"``, or ``"pagerank"``. If ``walk_type=None``
   (the default), then a value is selected according to the properties of `G`:
    - ``walk_type="random"`` if `G` is strongly connected and aperiodic
    - ``walk_type="lazy"`` if `G` is strongly connected but not aperiodic
    - ``walk_type="pagerank"`` for all other cases.

alpha : real
   (1 - alpha) is the teleportation probability used with pagerank

Returns
-------
P : numpy.ndarray
  transition matrix of G.

Raises
------
NetworkXError
    If walk_type not specified or alpha not in valid range
r   NÚrandomÚlazyÚpagerank)r   r   Údtype)rP   rQ   r'   r   r   r   r   )r   r:   zalpha must be between 0 and 1z+walk_type must be random, lazy, or pagerank)r(   r   r   Úis_strongly_connectedÚis_aperiodicr   Úfloatr   r   r   r   r   Ú	eye_arrayÚNetworkXErrorrM   Únewaxisr>   )r   r   r   r6   r7   r,   r   r   r   r    ÚDIrD   rK   s                r"   r;   r;   ´  sŽ  € óR ÛàÑÜ×#Ò# A×&Ñ&Ü�Š˜q×!Ñ!Ø$‘	à"‘	à"ˆIä
× Ò  ¸fÌEÑR€AØ�7‰7�D€AØÐ&Ó&Ø�Y‰Y× Ñ  #¨¯©°1¨¨Ñ"5°qÐ!9À!ÀÐ ÐH×NÑNÓPˆØ˜Ó Ø‘ˆAð$ €Hð! —	‘	×#Ñ# A¨eÐ#Ð4ˆAØ˜!‘V‘˜sÑ"‰Að €Hð 
�jÓ	 Ø�E•˜A•Ü×"Ò"Ð#BÓCÐCà�I‰I‹Kˆà#$ q¡5ˆ�%‰%�Qˆ%ˆ-˜1Ñ
šaÐ
Ñ à—‘˜1��˜bŸj™jª!˜mÑ,×.Ñ.Ñ.ˆØ‰I˜˜U™ a™Ñ'ˆð €Hô ×ÒÐLÓMÐMr#   )Nr   )Nr   Ngffffffî?)Ú__doc__Únetworkxr   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r   r   r   r;   © r#   r"   Ú<module>ra      sá   ðñó Ý .ò€ð ×Ò˜XÑ&ójó 'ðjðZ ×Ò˜XÑ&ónó 'ðnñj �\Ó"Ù�\Ó"Ø×Ò˜XÑ&à=Aó\ó 'ó #ó #ð\ñ~ �\Ó"Ù�\Ó"Ø×Ò˜XÑ&à=AóR-ó 'ó #ó #ðR-õjLr#   