U
    9%eD                     @   s    d Z ddlZdgZdddZdS )	z 
Laplacian centrality measures.
    Nlaplacian_centralityTweightffffff?c                    s  ddl }ddl}ddl}t| dkr.td|dkrvt| | t t|kr^td| fdd| D  }	nt	|  }}	| 
 rt| |	|||}
nt| |	| }
||jj|
ddd	 }i }t|D ]\}}t	||
jd }|| |
|ddf dd|f }|
 t|
dd|f  }||||  ||jj|ddd	 }|| }|rx|| }|||< q|S )
av  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>`.
        If None, the transition matrix is selected depending on the properties
        of the graph. Otherwise can be `random`, `lazy`, or `pagerank`.

    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.

    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
    --------
    directed_laplacian_matrix
    laplacian_matrix
    r   Nz$null graph has no centrality definedz.nodelist has duplicate nodes or nodes not in Gc                    s   g | ]}| kr|qS  r   ).0nZnodesetr   g/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/networkx/algorithms/centrality/laplacian.py
<listcomp>j   s      z(laplacian_centrality.<locals>.<listcomp>T)Zeigvals_only   )numpyscipyZscipy.linalglennxZNetworkXPointlessConceptsetZnbunch_iterZNetworkXErrorlistZis_directedZdirected_laplacian_matrixZlaplacian_matrixZtoarraypowerZlinalgZeighsum	enumerateZarangeshaperemoveZdiagonalabsZfill_diagonal)G
normalizedZnodelistr   Z	walk_typealphanpspr   ZnodesZ
lap_matrixZfull_energyZlaplace_centralities_dictinodeZ	all_but_iZA_2Znew_diagZ
new_energyZ	lapl_centr   r   r	   r   	   s8    V



)TNr   Nr   )__doc__Znetworkxr   __all__r   r   r   r   r	   <module>   s            