B
    ¦	ˆ\÷  ã               @   sb   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddd	„Zed
ƒdd„ ƒZ	edƒdd„ ƒZ
dS )zy
Graph utilities and algorithms

Graphs are represented with their adjacency matrices, preferably using
sparse matrices.
é    )Úsparse)Úcsgraphé   )Úgraph_shortest_path)Ú
deprecatedNc             C   sŒ   t  | ¡r|  ¡ } n
t  | ¡} i }d}|g}xZ|r†|}tƒ }x,|D ]$}||krB|||< | | j| ¡ qBW |dk	r|||kr|P |d7 }q.W |S )aï  Return the shortest path length from source to all reachable nodes.

    Returns a dictionary of shortest path lengths keyed by target.

    Parameters
    ----------
    graph : sparse matrix or 2D array (preferably LIL matrix)
        Adjacency matrix of the graph
    source : integer
       Starting node for path
    cutoff : integer, optional
        Depth to stop the search - only
        paths of length <= cutoff are returned.

    Examples
    --------
    >>> from sklearn.utils.graph import single_source_shortest_path_length
    >>> import numpy as np
    >>> graph = np.array([[ 0, 1, 0, 0],
    ...                   [ 1, 0, 1, 0],
    ...                   [ 0, 1, 0, 1],
    ...                   [ 0, 0, 1, 0]])
    >>> list(sorted(single_source_shortest_path_length(graph, 0).items()))
    [(0, 0), (1, 1), (2, 2), (3, 3)]
    >>> graph = np.ones((6, 6))
    >>> list(sorted(single_source_shortest_path_length(graph, 2).items()))
    [(0, 1), (1, 1), (2, 0), (3, 1), (4, 1), (5, 1)]
    r   Nr   )r   Z
isspmatrixZtolilZ
lil_matrixÚsetÚupdateZrows)ZgraphÚsourceÚcutoffÚseenÚlevelZ
next_levelZ
this_levelÚv© r   ú2lib/python3.7/site-packages/sklearn/utils/graph.pyÚ"single_source_shortest_path_length   s"    



r   z›sklearn.utils.graph.connected_components was deprecated in version 0.19 and will be removed in 0.21. Use scipy.sparse.csgraph.connected_components instead.c              O   s   t j| |ŽS )N)r   Úconnected_components)ÚargsÚkwargsr   r   r   r   I   s    r   z‹sklearn.utils.graph.graph_laplacian was deprecated in version 0.19 and will be removed in 0.21. Use scipy.sparse.csgraph.laplacian instead.c              O   s   t j| |ŽS )N)r   Z	laplacian)r   r   r   r   r   Úgraph_laplacianP   s    r   )N)Ú__doc__Zscipyr   Zscipy.sparser   r   Zdeprecationr   r   r   r   r   r   r   r   Ú<module>   s   
1