
    wg                     L    d Z ddlZdgZ ej                  d      	 dd       Zy)z 
Laplacian centrality measures.
    Nlaplacian_centralityweight)
edge_attrsc                    ddl }ddl}t        |       dk(  rt        j                  d      | j                  |      dk(  r|rt        d      | D ci c]  }|d c}S |_t        | j                  |            }	t        |	      t        |      k7  rt        j                  d      || D cg c]	  }||	vs| c}z   }
nt        |       x}}
| j                         rt        j                  | |
|||      }n%t        j                  | |
|      j                         }|j                  |j                   j#                  |d      d	      j%                         }i }t'        |      D ]  \  }}t        |j)                  |j*                  d               }|j-                  |       ||ddf   dd|f   }|j/                         t1        |dd|f         z
  }|j3                  |||          t        |      dkD  r<|j                  |j                   j#                  |d      d	      j%                         }nd
}||z
  }|r||z  }t5        |      ||<    |S c c}w c c}w )a  Compute the Laplacian centrality for nodes in the graph `G`.

    The Laplacian Centrality of a node ``i`` is measured by the drop in the
    Laplacian Energy after deleting node ``i`` from the graph. The Laplacian Energy
    is the sum of the squared eigenvalues of a graph's Laplacian matrix.

    .. math::

        C_L(u_i,G) = \frac{(\Delta E)_i}{E_L (G)} = \frac{E_L (G)-E_L (G_i)}{E_L (G)}

        E_L (G) = \sum_{i=0}^n \lambda_i^2

    Where $E_L (G)$ is the Laplacian energy of graph `G`,
    E_L (G_i) is the Laplacian energy of graph `G` after deleting node ``i``
    and $\lambda_i$ are the eigenvalues of `G`'s Laplacian matrix.
    This formula shows the normalized value. Without normalization,
    the numerator on the right side is returned.

    Parameters
    ----------
    G : graph
        A networkx graph

    normalized : bool (default = True)
        If True the centrality score is scaled so the sum over all nodes is 1.
        If False the centrality score for each node is the drop in Laplacian
        energy when that node is removed.

    nodelist : list, optional (default = None)
        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`)
        Optional parameter `weight` to compute the Laplacian matrix.
        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)
        Optional parameter `walk_type` used when calling
        :func:`directed_laplacian_matrix <networkx.directed_laplacian_matrix>`.
        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 (default = 0.95)
        Optional parameter `alpha` used when calling
        :func:`directed_laplacian_matrix <networkx.directed_laplacian_matrix>`.
        (1 - alpha) is the teleportation probability used with pagerank.

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

    Examples
    --------
    >>> G = nx.Graph()
    >>> edges = [(0, 1, 4), (0, 2, 2), (2, 1, 1), (1, 3, 2), (1, 4, 2), (4, 5, 1)]
    >>> G.add_weighted_edges_from(edges)
    >>> sorted((v, f"{c:0.2f}") for v, c in laplacian_centrality(G).items())
    [(0, '0.70'), (1, '0.90'), (2, '0.28'), (3, '0.22'), (4, '0.26'), (5, '0.04')]

    Notes
    -----
    The algorithm is implemented based on [1]_ with an extension to directed graphs
    using the ``directed_laplacian_matrix`` function.

    Raises
    ------
    NetworkXPointlessConcept
        If the graph `G` is the null graph.
    ZeroDivisionError
        If the graph `G` has no edges (is empty) and normalization is requested.

    References
    ----------
    .. [1] Qi, X., Fuller, E., Wu, Q., Wu, Y., and Zhang, C.-Q. (2012).
       Laplacian centrality: A new centrality measure for weighted networks.
       Information Sciences, 194:240-253.
       https://math.wvu.edu/~cqzhang/Publication-files/my-paper/INS-2012-Laplacian-W.pdf

    See Also
    --------
    :func:`~networkx.linalg.laplacianmatrix.directed_laplacian_matrix`
    :func:`~networkx.linalg.laplacianmatrix.laplacian_matrix`
    r   Nz$null graph has no centrality defined)r   z(graph with no edges has zero full energyz.nodelist has duplicate nodes or nodes not in GT)eigvals_only   g        )numpyscipylennxNetworkXPointlessConceptsizeZeroDivisionErrorsetnbunch_iterNetworkXErrorlistis_directeddirected_laplacian_matrixlaplacian_matrixtoarraypowerlinalgeighsum	enumeratearangeshaperemovediagonalabsfill_diagonalfloat)G
normalizednodelistr   	walk_typealphanpspnnodesetnodes
lap_matrixfull_energylaplace_centralities_dictinode	all_but_iA_2new_diag
new_energy	lapl_cents                        m/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/centrality/laplacian.pyr   r   
   s<   x 
1v{))*PQQvvVv!#$NOO 1  ammH-.w<3x=(""#STTq=!AW,<A==7"5}}11!UFIuU
((E6:BBD
((299>>*4>H!LPPRK !#X& ;4:#3#3A#678	A&q)|4 &&(3z!Q$/?+@@
hy12y>A"))..4."H!LPPRJJ*,	!K/I*/	*:!$'';* %$Q ! >s   
I,	I6I)TNr   Ngffffff?)__doc__networkxr   __all___dispatchabler        r8   <module>r?      s=    !
" X&NRK% 'K%r>   