3
Md)                 @   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|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   Q/var/www/html/virt/lib/python3.6/site-packages/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	j
|dg||dd}	|	| }
|jdd	 d
|j| }W dQ R X d||j|< |j	j
|dg||dd}|j|
j|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
multigraphffffff?c             C   s   ddl }ddlm}m} t| ||||d}|j\}	}
|j|jdd\}}|j j	}||j
  }|j|}||dg|	|	| |d| dg|	|	 }|j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 j}||j	  }||dg||}|j
 }||| |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   sV  ddl }ddlm}m} |dkrDtj| r@tj| r:d}qDd}nd}tj| ||td}|j	\}	}
|dkr|d|j
|jd	d
j dg|	|	}|dkr|| }n||	}|||  d }n|dkrHd|  k od	k n  stjd|j }|j|jd	d
dk}x|d D ]}d|	 ||< qW ||jd	d
 }|| d	| |	  }n
tj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dtypeg      ?r   )r   g       @zalpha must be between 0 and 1z+walk_type must be random, lazy, or pagerank)r;   r<   )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