
    Vh<                     X   d Z ddlZg dZej                  dd       Zd Zej                  dd       Zej                  dd       Zej                  d        Z	d	 Z
ej                  dd
       Zd Zej                  dd       Zej                  dd       Zej                  dd       Zy)z1
Shortest path algorithms for unweighted graphs.
    N)bidirectional_shortest_pathsingle_source_shortest_path"single_source_shortest_path_lengthsingle_target_shortest_path"single_target_shortest_path_lengthall_pairs_shortest_pathall_pairs_shortest_path_lengthpredecessorc                     || vrt        j                  d| d      |t        d      }|g}t        t	        | j
                  ||            S )a  Compute the shortest path lengths from `source` to all reachable nodes in `G`.

    Parameters
    ----------
    G : NetworkX graph

    source : node
       Starting node for path

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= `cutoff` are returned.

    Returns
    -------
    lengths : dict
        Dict keyed by node to shortest path length to `source`.

    Examples
    --------
    >>> G = nx.path_graph(5)
    >>> nx.single_source_shortest_path_length(G, 0)
    {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}

    See Also
    --------
    :any:`shortest_path_length` :
       Shortest path length with specifiable source, target, and weight.
    :any:`single_source_dijkstra_path_length` :
       Shortest weighted path length from source with Dijkstra algorithm.
    :any:`single_source_bellman_ford_path_length` :
       Shortest weighted path length from source with Bellman-Ford algorithm.
    Source  is not in Ginf)nxNodeNotFoundfloatdict_single_shortest_path_length_adj)Gsourcecutoff	nextlevels       ]/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/shortest_paths/unweighted.pyr   r      sR    D Qoox|<==~uI,QVVYGHH    c              #   ,  K   t        |      }|}d}t        |       }|D ]  }||f 
 |rg||kD  ra|dz  }|}g }|D ]I  }| |   D ]/  }	|	|vs|j                  |	       |j                  |	       |	|f 1 t        |      |k(  sI y |r||kD  r_yyyyw)aN  Yields (node, level) in a breadth first search

    Shortest Path Length helper function
    Parameters
    ----------
        adj : dict
            Adjacency dict or view
        firstlevel : list
            starting nodes, e.g. [source] or [target]
        cutoff : int or float
            level at which we stop the process
    r      N)setlenaddappend)
adj
firstlevelr   seenr   levelnv	thislevelws
             r   r   r   =   s      z?DIECA %j

		 	AV %D=HHQK$$Q'e*$	%
 4yA~		 ))s   A	B8B
BBc                     || vrt        j                  d| d      |t        d      }| j                         r| j                  n| j
                  }|g}t        t        |||            S )a  Compute the shortest path lengths to target from all reachable nodes.

    Parameters
    ----------
    G : NetworkX graph

    target : node
       Target node for path

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    lengths : dictionary
        Dictionary, keyed by source, of shortest path lengths.

    Examples
    --------
    >>> G = nx.path_graph(5, create_using=nx.DiGraph())
    >>> length = nx.single_target_shortest_path_length(G, 4)
    >>> length[0]
    4
    >>> for node in range(5):
    ...     print(f"{node}: {length[node]}")
    0: 4
    1: 3
    2: 2
    3: 1
    4: 0

    See Also
    --------
    single_source_shortest_path_length, shortest_path_length
    Target r   r   )r   r   r   is_directed_predr   r   r   )r   targetr   r!   r   s        r   r   r   ^   se    J Qoox|<==~u]]_!''!&&CI,S)VDEEr   c              #   D   K   t         }| D ]  }| || ||      f  yw)a:  Computes the shortest path lengths between all nodes in `G`.

    Parameters
    ----------
    G : NetworkX graph

    cutoff : integer, optional
        Depth at which to stop the search. Only paths of length at most
        `cutoff` are returned.

    Returns
    -------
    lengths : iterator
        (source, dictionary) iterator with dictionary keyed by target and
        shortest path length as the key value.

    Notes
    -----
    The iterator returned only has reachable node pairs.

    Examples
    --------
    >>> G = nx.path_graph(5)
    >>> length = dict(nx.all_pairs_shortest_path_length(G))
    >>> for node in [0, 1, 2, 3, 4]:
    ...     print(f"1 - {node}: {length[1][node]}")
    1 - 0: 1
    1 - 1: 0
    1 - 2: 1
    1 - 3: 2
    1 - 4: 3
    >>> length[3][2]
    1
    >>> length[2][2]
    0

    r   N)r   )r   r   lengthr%   s       r   r	   r	      s1     N 0F /&Af-../s    c                 @   || vrt        j                  d| d      || vrt        j                  d| d      t        | ||      }|\  }}}g }||j                  |       ||   }||j	                          ||d      }||j                  |       ||   }||S )a  Returns a list of nodes in a shortest path between source and target.

    Parameters
    ----------
    G : NetworkX graph

    source : node label
       starting node for path

    target : node label
       ending node for path

    Returns
    -------
    path: list
       List of nodes in a path from source to target.

    Raises
    ------
    NetworkXNoPath
       If no path exists between source and target.

    Examples
    --------
    >>> G = nx.Graph()
    >>> nx.add_path(G, [0, 1, 2, 3, 0, 4, 5, 6, 7, 4])
    >>> nx.bidirectional_shortest_path(G, 2, 6)
    [2, 1, 0, 4, 5, 6]

    See Also
    --------
    shortest_path

    Notes
    -----
    This algorithm is used by shortest_path(G, source, target).
    r   r   r*   )r   r   _bidirectional_pred_succr    reverse)r   r   r-   resultspredsuccr(   paths           r   r   r      s    P Qoox|<==Qoox|<== 'q&&9GMD$ D
-AG - 	LLNT"XA
-AG - Kr   c                 &   ||k(  r	|di|di|fS | j                         r| j                  }| j                  }n| j                  }| j                  }|di}|di}|g}|g}|r|rt	        |      t	        |      k  r=|}	g }|	D ]3  }
||
   D ])  }||vr|j                  |       |
||<   ||v s"|||fc c S  5 n<|}	g }|	D ]3  }
||
   D ])  }||vr|
||<   |j                  |       ||v s"|||fc c S  5 |r|rt        j                  d| d| d      )zBidirectional shortest path helper.

    Returns (pred, succ, w) where
    pred is a dictionary of predecessors from w to the source, and
    succ is a dictionary of successors from w to the target.
    NzNo path between z and .)r+   r6   r7   r!   r   r    r   NetworkXNoPath)r   r   r-   GpredGsuccr6   r7   forward_fringereverse_fringe
this_levelr&   r(   s               r   r3   r3      sq    77 	}} D>DD>D XNXN
^~#n"55'JN -q -A}&--a0"#QDy#T1},-- (JN -q -A}"#Q&--a0Dy#T1},-- ^, 

.vheF81E
FFr   c           	          || vrt        j                  d| d      d }|t        d      }|di}||gi}t        t	        | j
                  ||||            S )af  Compute shortest path between source
    and all other nodes reachable from source.

    Parameters
    ----------
    G : NetworkX graph

    source : node label
       Starting node for path

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    paths : dictionary
        Dictionary, keyed by target, of shortest paths.

    Examples
    --------
    >>> G = nx.path_graph(5)
    >>> path = nx.single_source_shortest_path(G, 0)
    >>> path[4]
    [0, 1, 2, 3, 4]

    Notes
    -----
    The shortest path is not necessarily unique. So there can be multiple
    paths between the source and each target node, all of which have the
    same 'shortest' length. For each target node, this function returns
    only one of those paths.

    See Also
    --------
    shortest_path
    r   	 not in Gc                     | |z   S N p1p2s     r   joinz)single_source_shortest_path.<locals>.joinY      Bwr   r   r   )r   r   r   r   _single_shortest_pathr!   )r   r   r   rI   r   pathss         r   r   r   0  sk    L Qooxy9:: ~uIfXE%aeeYvtLMMr   c                     d}|}|rA||kD  r<|}i }|D ]&  }| |   D ]  }	|	|vs |||   |	g      ||	<   d||	<    ( |dz  }|r||kD  r<|S )a  Returns shortest paths

    Shortest Path helper function
    Parameters
    ----------
        adj : dict
            Adjacency dict or view
        firstlevel : dict
            starting nodes, e.g. {source: 1} or {target: 1}
        paths : dict
            paths for starting nodes, e.g. {source: [source]}
        cutoff : int or float
            level at which we stop the process
        join : function
            function to construct a path from two partial paths. Requires two
            list inputs `p1` and `p2`, and returns a list. Usually returns
            `p1 + p2` (forward from source) or `p2 + p1` (backward from target)
    r   r   rE   )
r!   r"   rL   r   rI   r$   r   r'   r&   r(   s
             r   rK   rK   c  s    & EI
		 	%AV %E>#E!Hqc2E!H#$IaL%	%
 	
  Lr   c           	          || vrt        j                  d| d      d }| j                         r| j                  n| j                  }|t        d      }|di}||gi}t        t        |||||            S )a  Compute shortest path to target from all nodes that reach target.

    Parameters
    ----------
    G : NetworkX graph

    target : node label
       Target node for path

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    Returns
    -------
    paths : dictionary
        Dictionary, keyed by target, of shortest paths.

    Examples
    --------
    >>> G = nx.path_graph(5, create_using=nx.DiGraph())
    >>> path = nx.single_target_shortest_path(G, 4)
    >>> path[0]
    [0, 1, 2, 3, 4]

    Notes
    -----
    The shortest path is not necessarily unique. So there can be multiple
    paths between the source and each target node, all of which have the
    same 'shortest' length. For each target node, this function returns
    only one of those paths.

    See Also
    --------
    shortest_path, single_source_shortest_path
    r*   rB   c                     || z   S rD   rE   rF   s     r   rI   z)single_target_shortest_path.<locals>.join  rJ   r   r   r   )r   r   r+   r6   r!   r   r   rK   )r   r-   r   rI   r!   r   rL   s          r   r   r     s~    J Qooxy9:: MMO!&&C~uIfXE%c9eVTJKKr   c              #   >   K   | D ]  }|t        | ||      f  yw)a  Compute shortest paths between all nodes.

    Parameters
    ----------
    G : NetworkX graph

    cutoff : integer, optional
        Depth at which to stop the search. Only paths of length at most
        `cutoff` are returned.

    Returns
    -------
    paths : iterator
        Dictionary, keyed by source and target, of shortest paths.

    Examples
    --------
    >>> G = nx.path_graph(5)
    >>> path = dict(nx.all_pairs_shortest_path(G))
    >>> print(path[0][4])
    [0, 1, 2, 3, 4]

    Notes
    -----
    There may be multiple shortest paths with the same length between
    two nodes. For each pair, this function returns only one of those paths.

    See Also
    --------
    floyd_warshall
    all_pairs_all_shortest_paths

    r/   N)r   )r   r   r%   s      r   r   r     s/     H  D-a6BCCDs   c                 r   || vrt        j                  d| d      d}|g}||i}|g i}|rc|dz   }|}	g }|	D ]J  }
| |
   D ]@  }||vr|
g||<   |||<   |j                  |       $||   |k(  s-||   j                  |
       B L |r||k  rn|rc||r||vrg dfS ||   ||   fS ||vrg S ||   S |r||fS |S )a  Returns dict of predecessors for the path from source to all nodes in G.

    Parameters
    ----------
    G : NetworkX graph

    source : node label
       Starting node for path

    target : node label, optional
       Ending node for path. If provided only predecessors between
       source and target are returned

    cutoff : integer, optional
        Depth to stop the search. Only paths of length <= cutoff are returned.

    return_seen : bool, optional (default=None)
        Whether to return a dictionary, keyed by node, of the level (number of
        hops) to reach the node (as seen during breadth-first-search).

    Returns
    -------
    pred : dictionary
        Dictionary, keyed by node, of predecessors in the shortest path.


    (pred, seen): tuple of dictionaries
        If `return_seen` argument is set to `True`, then a tuple of dictionaries
        is returned. The first element is the dictionary, keyed by node, of
        predecessors in the shortest path. The second element is the dictionary,
        keyed by node, of the level (number of hops) to reach the node (as seen
        during breadth-first-search).

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> list(G)
    [0, 1, 2, 3]
    >>> nx.predecessor(G, 0)
    {0: [], 1: [0], 2: [1], 3: [2]}
    >>> nx.predecessor(G, 0, return_seen=True)
    ({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3})


    r   rB   r   r   r2   )r   r   r    )r   r   r-   r   return_seenr$   r   r#   r6   r'   r&   r(   s               r   r
   r
     s*   ^ Qooxy9::EIE?DB<D
			 	&AqT &D= cDG#DG$$Q'!W%GNN1%&	& fo  T!BxL$v,//T!	<$<Kr   rD   )NNN)__doc__networkxr   __all___dispatchabler   r   r   r	   r   r3   r   rK   r   r   r
   rE   r   r   <module>rW      s   	 &I &IRB +F +F\ )/ )/X > >B1Gh /N /NdB 0L 0Lf $D $DN Q Qr   