§
    bŠtjÑ	  ã                   óŠ   — d Z ddlZddlmZ dgZ ed¦  «         ed¦  «        ej        dd„¦   «         ¦   «         ¦   «         ZdS )	z5Bethe Hessian or deformed Laplacian matrix of graphs.é    N)Únot_implemented_forÚbethe_hessian_matrixÚdirectedÚ
multigraphc                 ó  — ddl }|€t          | ¦  «        }|€Zt          d„ t          j        | ¦  «        D ¦   «         ¦  «        t          d„ t          j        | ¦  «        D ¦   «         ¦  «        z  dz
  }t          j        | |d¬¦  «        }|j        \  }}|j                             |                     d¬¦  «        df||f¬	¦  «         	                    ¦   «         }|j         
                    ||d¬
¦  «        }|dz  dz
  |z  ||z  z
  |z   S )uì  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   ó&   K  — | ]\  }}|d z  V — ŒdS )é   N© ©Ú.0ÚvÚds      ú`/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/linalg/bethehessianmatrix.pyú	<genexpr>z'bethe_hessian_matrix.<locals>.<genexpr>H   s*   è è € Ð.Ð.™˜˜A��1‘Ð.Ð.Ð.Ð.Ð.Ð.ó    c              3   ó    K  — | ]	\  }}|V — Œ
d S )Nr
   r   s      r   r   z'bethe_hessian_matrix.<locals>.<genexpr>H   s&   è è € Ð4PÐ4P¹4¸1¸a°QÐ4PÐ4PÐ4PÐ4PÐ4PÐ4Pr   é   Úcsr)ÚnodelistÚformat)Úaxis)Úshape)r   r	   )ÚscipyÚlistÚsumÚnxÚdegreeÚto_scipy_sparse_arrayr   ÚsparseÚ	dia_arrayÚtocsrÚ	eye_array)	ÚGÚrr   ÚspÚAÚnÚmÚDÚIs	            r   r   r   	   s  € ðt ÐÐÐàÐÝ˜‘7”7ˆØ€yÝÐ.Ð.¥¤¨1¡¤Ð.Ñ.Ô.Ñ.Ô.µÐ4PÐ4PÅ2Ä9ÈQÁ<Ä<Ð4PÑ4PÔ4PÑ1PÔ1PÑPÐSTÑTˆÝ
Ô  ¨X¸eÐDÑDÔD€AØŒ7�D€A€qØ
Œ	×Ò˜QŸUšU¨˜U™]œ]¨AÐ.°q¸!°fÐÑ=Ô=×CÒCÑEÔE€AØ
Œ	×Ò˜A˜q¨ÐÑ/Ô/€AØˆq‰D�1‰H˜‰>˜A ™EÑ! AÑ%Ð%r   )NN)Ú__doc__Únetworkxr   Únetworkx.utilsr   Ú__all__Ú_dispatchabler   r
   r   r   ú<module>r0      s‘   ðØ ;Ð ;à Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .à!Ð
"€ð Ð�ZÑ Ô ØÐ�\Ñ"Ô"ØÔðA&ð A&ð A&ñ Ôñ #Ô"ñ !Ô ðA&ð A&ð A&r   