
    h
                     v    S r SSKrSSKJr  S/r\" S5      \" S5      \R                  SS j5       5       5       rg)	z5Bethe Hessian or deformed Laplacian matrix of graphs.    N)not_implemented_forbethe_hessian_matrixdirected
multigraphc           
      "   SSK nUc  [        U 5      nUcP  [        S [        R                  " U 5       5       5      [        S [        R                  " U 5       5       5      -  S-
  n[        R
                  " XSS9nUR                  u  pVUR                  R                  UR                  R                  UR                  SS9SXeSS	95      nUR                  R                  UR                  R                  XeSS	95      nUS
-  S-
  U-  X-  -
  U-   $ )u8  Returns the Bethe Hessian matrix of G.

The Bethe Hessian is a family of matrices parametrized by r, defined as
H(r) = (r^2 - 1) I - r A + D where A is the adjacency matrix, D is the
diagonal matrix of node degrees, and I is the identify matrix. It is equal
to the graph laplacian when the regularizer r = 1.

The default choice of regularizer should be the ratio [2]_

.. math::
  r_m = \left(\sum k_i \right)^{-1}\left(\sum k_i^2 \right) - 1

Parameters
----------
G : Graph
   A NetworkX graph
r : float
   Regularizer parameter
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()``.

Returns
-------
H : scipy.sparse.csr_array
  The Bethe Hessian matrix of `G`, with parameter `r`.

Examples
--------
>>> k = [3, 2, 2, 1, 0]
>>> G = nx.havel_hakimi_graph(k)
>>> H = nx.bethe_hessian_matrix(G)
>>> H.toarray()
array([[ 3.5625, -1.25  , -1.25  , -1.25  ,  0.    ],
       [-1.25  ,  2.5625, -1.25  ,  0.    ,  0.    ],
       [-1.25  , -1.25  ,  2.5625,  0.    ,  0.    ],
       [-1.25  ,  0.    ,  0.    ,  1.5625,  0.    ],
       [ 0.    ,  0.    ,  0.    ,  0.    ,  0.5625]])

See Also
--------
bethe_hessian_spectrum
adjacency_matrix
laplacian_matrix

References
----------
.. [1] A. Saade, F. Krzakala and L. Zdeborová
   "Spectral Clustering of Graphs with the Bethe Hessian",
   Advances in Neural Information Processing Systems, 2014.
.. [2] C. M. Le, E. Levina
   "Estimating the number of communities in networks by spectral methods"
   arXiv:1507.00827, 2015.
r   Nc              3   0   #    U  H  u  pUS -  v   M     g7f)   N .0vds      T/var/www/html/env/lib/python3.13/site-packages/networkx/linalg/bethehessianmatrix.py	<genexpr>'bethe_hessian_matrix.<locals>.<genexpr>H   s     .1s   c              3   *   #    U  H	  u  pUv   M     g 7f)Nr
   r   s      r   r   r   H   s     4P<41Q<s      csr)nodelistformat)axis)r   r	   )scipylistsumnxdegreeto_scipy_sparse_arrayshapesparse	csr_arrayspdiagseye)	Grr   spAnmDIs	            r   r   r   	   s    t 7y.1..4P299Q<4P1PPSTT
  eDA77DA
		BII--aeeemQU-STA
		BIIMM!uM=>AqD1H>AE!A%%    )NN)__doc__networkxr   networkx.utilsr   __all___dispatchabler   r
   r+   r   <module>r1      sL    ;  .!
" Z \"C&  # !C&r+   