B
    Nô`™)  ã               @   sŠ   d Z ddlZddlmZ ddddgZedƒdd
d„ƒZedƒddd„ƒZedƒedƒddd„ƒƒZedƒedƒddd„ƒƒZ	ddd„Z
dS )zLaplacian matrix of graphs.
é    N)Únot_implemented_forÚlaplacian_matrixÚnormalized_laplacian_matrixÚdirected_laplacian_matrixÚ'directed_combinatorial_laplacian_matrixZdirectedÚweightc       	      C   sd   ddl }|dkrt| ƒ}tj| ||dd�}|j\}}|jdd�}|jj| ¡ dg||dd�}|| S )a\  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 matrix
      The Laplacian matrix of G.

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

    See Also
    --------
    to_numpy_array
    normalized_laplacian_matrix
    laplacian_spectrum
    r   NÚcsr)Únodelistr   Úformaté   )Úaxis)r
   )	Úscipy.sparseÚlistÚnxÚto_scipy_sparse_matrixÚshapeÚsumÚsparseÚspdiagsÚflatten)	ÚGr	   r   ÚscipyÚAÚnÚmÚdiagsÚD© r   úC/tmp/pip-unpacked-wheel-fc63gmtm/networkx/linalg/laplacianmatrix.pyr      s    #
c          	   C   sÐ   ddl }ddl}ddl}|dkr(t| ƒ}tj| ||dd�}|j\}}|jdd� ¡ }|j	j
|dg||dd�}	|	| }
|jdd	�� d
| |¡ }W dQ R X d|| |¡< |j	j
|dg||dd�}| |
 |¡¡S )a{  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.

    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 matrix
      The normalized Laplacian matrix of G.

    Notes
    -----
    For MultiGraph/MultiDiGraph, the edges weights are summed.
    See 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]_.

    See Also
    --------
    laplacian_matrix
    normalized_laplacian_spectrum

    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.
    r   Nr   )r	   r   r
   r   )r   )r
   Úignore)Údivideg      ð?)Únumpyr   r   r   r   r   r   r   r   r   r   ZerrstateÚsqrtÚisinfÚdot)r   r	   r   Únpr   r   r   r   r   r   ÚLZ
diags_sqrtZDHr   r   r   r   <   s    4
Z
undirectedZ
multigraphçffffffî?c             C   s°   ddl }ddlm}m} t| ||||d�}|j\}	}
|j|jdd�\}}| ¡ j	}|| 
¡  }| |¡}||dg|	|	ƒ| |d| dg|	|	ƒ }| t| ƒ¡}|||j d  S )	a9  Returns the directed Laplacian matrix of G.

    The graph directed Laplacian is the matrix

    .. math::

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

    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.

    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)
       If None, `P` is selected depending on the properties of the
       graph. Otherwise is one of 'random', 'lazy', or 'pagerank'

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

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

    Notes
    -----
    Only implemented for DiGraphs

    See Also
    --------
    laplacian_matrix

    References
    ----------
    .. [1] Fan Chung (2005).
       Laplacians and the Cheeger inequality for directed graphs.
       Annals of Combinatorics, 9(1), 2005
    r   N)r   Úlinalg)r	   r   Ú	walk_typeÚalphar   )Úkg      ð?g       @)r!   r   r   r(   Ú_transition_matrixr   ÚeigsÚTr   Úrealr   r"   ÚidentityÚlen)r   r	   r   r)   r*   r%   r   r(   ÚPr   r   ÚevalsÚevecsÚvÚpZsqrtpÚQÚIr   r   r   r   ‡   s    <


(c             C   sˆ   ddl m}m} t| ||||d�}|j\}}	|j|jdd�\}
}| ¡ j}|| 	¡  }||dg||ƒ}| 
¡ }||| |j|  d  S )a  Return the directed combinatorial Laplacian matrix of G.

    The graph directed combinatorial Laplacian is the matrix

    .. math::

        L = \Phi - (\Phi P + P^T \Phi) / 2

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

    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)
       If None, `P` is selected depending on the properties of the
       graph. Otherwise is one of 'random', 'lazy', or 'pagerank'

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

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

    Notes
    -----
    Only implemented for DiGraphs

    See Also
    --------
    laplacian_matrix

    References
    ----------
    .. [1] Fan Chung (2005).
       Laplacians and the Cheeger inequality for directed graphs.
       Annals of Combinatorics, 9(1), 2005
    r   )r   r(   )r	   r   r)   r*   r   )r+   g       @)r   r   r(   r,   r   r-   r.   r   r/   r   Útodense)r   r	   r   r)   r*   r   r(   r2   r   r   r3   r4   r5   r6   ÚPhir   r   r   r   Ö   s    ;

c             C   sT  ddl }ddlm}m} |dkrDt | ¡r@t | ¡r:d}qDd}nd}tj| ||td�}|j	\}	}
|dkr¸|d	| 
|jd
d�j¡ dg|	|	ƒ}|dkrž|| }n||	ƒ}|||  d }n˜|dk�rFd|  k rÖd
k sân t d¡‚| ¡ }| |jd
d�dk¡}x|d D ]}d	|	 ||< �q
W ||jd
d� }|| d
| |	  }n
t d¡‚|S )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)
       If None, `P` is selected depending on the properties of the
       graph. Otherwise is one of 'random', 'lazy', or 'pagerank'

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

    Returns
    -------
    P : NumPy matrix
      transition matrix of G.

    Raises
    ------
    NetworkXError
        If walk_type not specified or alpha not in valid range
    r   N)r0   r   ÚrandomÚlazyZpagerank)r	   r   Zdtype)r;   r<   g      ð?r   )r   g       @zalpha must be between 0 and 1z+walk_type must be random, lazy, or pagerank)r!   r   r0   r   r   Zis_strongly_connectedZis_aperiodicr   Úfloatr   Úarrayr   ZflatZNetworkXErrorr9   Úwhere)r   r	   r   r)   r*   r%   r0   r   ÚMr   r   ZDIr2   r8   ZdanglingÚdr   r   r   r,   #  s6    &


$



r,   )Nr   )Nr   )Nr   Nr'   )Nr   Nr'   )Nr   Nr'   )Ú__doc__Znetworkxr   Znetworkx.utilsr   Ú__all__r   r   r   r   r,   r   r   r   r   Ú<module>   s"   -JLJ