3
[¾ hæ	  ã               @   s>   d Z ddlmZ ddlmZ ddlmZ eddœdd	„ƒZdS )
zy
Graph utilities and algorithms

Graphs are represented with their adjacency matrices, preferably using
sparse matrices.
é    )Úsparseé   )Úgraph_shortest_path)Ú_deprecate_positional_argsN)Úcutoffc            C   sŒ   t j| ƒr| jƒ } n
t j| ƒ} i }d}|g}xZ|r†|}tƒ }x,|D ]$}||krB|||< |j| 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, ndarray} of shape (n, n)
        Adjacency matrix of the graph. Sparse matrix of format LIL is
        preferred.

    source : int
       Starting node for path.

    cutoff : int, default=None
        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)ÚgraphÚsourcer   ÚseenÚlevelZ
next_levelZ
this_levelÚv© r   ú;/tmp/pip-build-zwgx3nbq/scikit-learn/sklearn/utils/graph.pyÚ"single_source_shortest_path_length   s"     



r   )Ú__doc__Zscipyr   r   Z
validationr   r   r   r   r   r   Ú<module>   s
   