
    wgpv                     <   d dl mZmZ d dlmZ d dlZd dlmZ d dl	m
Z
mZ g dZej                  d        Zej                  dd       Zej                  dd	       Zd
 Z e
d       ej                  d      dd              Z G d d      Z	 ddZddZ	 ddZy)    )heappopheappush)countN)_weight_function)not_implemented_forpairwise)all_simple_pathsis_simple_pathshortest_simple_pathsall_simple_edge_pathsc                      t        |      dk(  ryt        |      dk(  r|d    v S t         fd|D              syt        t        |            t        |      k7  ryt         fdt        |      D              S )a,  Returns True if and only if `nodes` form a simple path in `G`.

    A *simple path* in a graph is a nonempty sequence of nodes in which
    no node appears more than once in the sequence, and each adjacent
    pair of nodes in the sequence is adjacent in the graph.

    Parameters
    ----------
    G : graph
        A NetworkX graph.
    nodes : list
        A list of one or more nodes in the graph `G`.

    Returns
    -------
    bool
        Whether the given list of nodes represents a simple path in `G`.

    Notes
    -----
    An empty list of nodes is not a path but a list of one node is a
    path. Here's an explanation why.

    This function operates on *node paths*. One could also consider
    *edge paths*. There is a bijection between node paths and edge
    paths.

    The *length of a path* is the number of edges in the path, so a list
    of nodes of length *n* corresponds to a path of length *n* - 1.
    Thus the smallest edge path would be a list of zero edges, the empty
    path. This corresponds to a list of one node.

    To convert between a node path and an edge path, you can use code
    like the following::

        >>> from networkx.utils import pairwise
        >>> nodes = [0, 1, 2, 3]
        >>> edges = list(pairwise(nodes))
        >>> edges
        [(0, 1), (1, 2), (2, 3)]
        >>> nodes = [edges[0][0]] + [v for u, v in edges]
        >>> nodes
        [0, 1, 2, 3]

    Examples
    --------
    >>> G = nx.cycle_graph(4)
    >>> nx.is_simple_path(G, [2, 3, 0])
    True
    >>> nx.is_simple_path(G, [0, 2])
    False

    r   F   c              3   &   K   | ]  }|v  
 y wN ).0nGs     e/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/simple_paths.py	<genexpr>z!is_simple_path.<locals>.<genexpr>S   s     %!qAv%s   c              3   2   K   | ]  \  }}||   v   y wr   r   )r   uvr   s      r   r   z!is_simple_path.<locals>.<genexpr>[   s     5TQqAaDy5s   )lenallsetr   )r   nodess   ` r   r
   r
      sr    r 5zQ 5zQQx1} %u%% 3u:#e*$ 5Xe_555    c              #   n   K   t        | |||      D ]  }|g|D cg c]  }|d   	 c}z     yc c}w w)al  Generate all simple paths in the graph G from source to target.

    A simple path is a path with no repeated nodes.

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

    source : node
       Starting node for path

    target : nodes
       Single node or iterable of nodes at which to end path

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

    Returns
    -------
    path_generator: generator
       A generator that produces lists of simple paths.  If there are no paths
       between the source and target within the given cutoff the generator
       produces no output. If it is possible to traverse the same sequence of
       nodes in multiple ways, namely through parallel edges, then it will be
       returned multiple times (once for each viable edge combination).

    Examples
    --------
    This iterator generates lists of nodes::

        >>> G = nx.complete_graph(4)
        >>> for path in nx.all_simple_paths(G, source=0, target=3):
        ...     print(path)
        ...
        [0, 1, 2, 3]
        [0, 1, 3]
        [0, 2, 1, 3]
        [0, 2, 3]
        [0, 3]

    You can generate only those paths that are shorter than a certain
    length by using the `cutoff` keyword argument::

        >>> paths = nx.all_simple_paths(G, source=0, target=3, cutoff=2)
        >>> print(list(paths))
        [[0, 1, 3], [0, 2, 3], [0, 3]]

    To get each path as the corresponding list of edges, you can use the
    :func:`networkx.utils.pairwise` helper function::

        >>> paths = nx.all_simple_paths(G, source=0, target=3)
        >>> for path in map(nx.utils.pairwise, paths):
        ...     print(list(path))
        [(0, 1), (1, 2), (2, 3)]
        [(0, 1), (1, 3)]
        [(0, 2), (2, 1), (1, 3)]
        [(0, 2), (2, 3)]
        [(0, 3)]

    Pass an iterable of nodes as target to generate all paths ending in any of several nodes::

        >>> G = nx.complete_graph(4)
        >>> for path in nx.all_simple_paths(G, source=0, target=[3, 2]):
        ...     print(path)
        ...
        [0, 1, 2]
        [0, 1, 2, 3]
        [0, 1, 3]
        [0, 1, 3, 2]
        [0, 2]
        [0, 2, 1, 3]
        [0, 2, 3]
        [0, 3]
        [0, 3, 1, 2]
        [0, 3, 2]

    The singleton path from ``source`` to itself is considered a simple path and is
    included in the results:

        >>> G = nx.empty_graph(5)
        >>> list(nx.all_simple_paths(G, source=0, target=0))
        [[0]]

        >>> G = nx.path_graph(3)
        >>> list(nx.all_simple_paths(G, source=0, target={0, 1, 2}))
        [[0], [0, 1], [0, 1, 2]]

    Iterate over each path from the root nodes to the leaf nodes in a
    directed acyclic graph using a functional programming approach::

        >>> from itertools import chain
        >>> from itertools import product
        >>> from itertools import starmap
        >>> from functools import partial
        >>>
        >>> chaini = chain.from_iterable
        >>>
        >>> G = nx.DiGraph([(0, 1), (1, 2), (0, 3), (3, 2)])
        >>> roots = (v for v, d in G.in_degree() if d == 0)
        >>> leaves = (v for v, d in G.out_degree() if d == 0)
        >>> all_paths = partial(nx.all_simple_paths, G)
        >>> list(chaini(starmap(all_paths, product(roots, leaves))))
        [[0, 1, 2], [0, 3, 2]]

    The same list computed using an iterative approach::

        >>> G = nx.DiGraph([(0, 1), (1, 2), (0, 3), (3, 2)])
        >>> roots = (v for v, d in G.in_degree() if d == 0)
        >>> leaves = (v for v, d in G.out_degree() if d == 0)
        >>> all_paths = []
        >>> for root in roots:
        ...     for leaf in leaves:
        ...         paths = nx.all_simple_paths(G, root, leaf)
        ...         all_paths.extend(paths)
        >>> all_paths
        [[0, 1, 2], [0, 3, 2]]

    Iterate over each path from the root nodes to the leaf nodes in a
    directed acyclic graph passing all leaves together to avoid unnecessary
    compute::

        >>> G = nx.DiGraph([(0, 1), (2, 1), (1, 3), (1, 4)])
        >>> roots = (v for v, d in G.in_degree() if d == 0)
        >>> leaves = [v for v, d in G.out_degree() if d == 0]
        >>> all_paths = []
        >>> for root in roots:
        ...     paths = nx.all_simple_paths(G, root, leaves)
        ...     all_paths.extend(paths)
        >>> all_paths
        [[0, 1, 3], [0, 1, 4], [2, 1, 3], [2, 1, 4]]

    If parallel edges offer multiple ways to traverse a given sequence of
    nodes, this sequence of nodes will be returned multiple times:

        >>> G = nx.MultiDiGraph([(0, 1), (0, 1), (1, 2)])
        >>> list(nx.all_simple_paths(G, 0, 2))
        [[0, 1, 2], [0, 1, 2]]

    Notes
    -----
    This algorithm uses a modified depth-first search to generate the
    paths [1]_.  A single path can be found in $O(V+E)$ time but the
    number of simple paths in a graph can be very large, e.g. $O(n!)$ in
    the complete graph of order $n$.

    This function does not check that a path exists between `source` and
    `target`. For large graphs, this may result in very long runtimes.
    Consider using `has_path` to check that a path exists between `source` and
    `target` before calling this function on large graphs.

    References
    ----------
    .. [1] R. Sedgewick, "Algorithms in C, Part 5: Graph Algorithms",
       Addison Wesley Professional, 3rd ed., 2001.

    See Also
    --------
    all_shortest_paths, shortest_path, has_path

    r   N)r   )r   sourcetargetcutoff	edge_pathedges         r   r	   r	   ^   sB     D +1fffE 9	hi8d$q'88898s   505c              #   *  K   || vrt        j                  d| d      || v r|h}n	 t        |      }||nt	        |       dz
  }|dk\  r|rt        | |||      E d{    yyy# t        $ r}t        j                  d| d      |d}~ww xY w7 2w)a	  Generate lists of edges for all simple paths in G from source to target.

    A simple path is a path with no repeated nodes.

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

    source : node
       Starting node for path

    target : nodes
       Single node or iterable of nodes at which to end path

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

    Returns
    -------
    path_generator: generator
       A generator that produces lists of simple paths.  If there are no paths
       between the source and target within the given cutoff the generator
       produces no output.
       For multigraphs, the list of edges have elements of the form `(u,v,k)`.
       Where `k` corresponds to the edge key.

    Examples
    --------

    Print the simple path edges of a Graph::

        >>> g = nx.Graph([(1, 2), (2, 4), (1, 3), (3, 4)])
        >>> for path in sorted(nx.all_simple_edge_paths(g, 1, 4)):
        ...     print(path)
        [(1, 2), (2, 4)]
        [(1, 3), (3, 4)]

    Print the simple path edges of a MultiGraph. Returned edges come with
    their associated keys::

        >>> mg = nx.MultiGraph()
        >>> mg.add_edge(1, 2, key="k0")
        'k0'
        >>> mg.add_edge(1, 2, key="k1")
        'k1'
        >>> mg.add_edge(2, 3, key="k0")
        'k0'
        >>> for path in sorted(nx.all_simple_edge_paths(mg, 1, 3)):
        ...     print(path)
        [(1, 2, 'k0'), (2, 3, 'k0')]
        [(1, 2, 'k1'), (2, 3, 'k0')]

    When ``source`` is one of the targets, the empty path starting and ending at
    ``source`` without traversing any edge is considered a valid simple edge path
    and is included in the results:

        >>> G = nx.Graph()
        >>> G.add_node(0)
        >>> paths = list(nx.all_simple_edge_paths(G, 0, 0))
        >>> for path in paths:
        ...     print(path)
        []
        >>> len(paths)
        1


    Notes
    -----
    This algorithm uses a modified depth-first search to generate the
    paths [1]_.  A single path can be found in $O(V+E)$ time but the
    number of simple paths in a graph can be very large, e.g. $O(n!)$ in
    the complete graph of order $n$.

    References
    ----------
    .. [1] R. Sedgewick, "Algorithms in C, Part 5: Graph Algorithms",
       Addison Wesley Professional, 3rd ed., 2001.

    See Also
    --------
    all_shortest_paths, shortest_path, all_simple_paths

    source node  not in graphtarget node Nr   r   )nxNodeNotFoundr   	TypeErrorr   _all_simple_edge_paths)r   r    r!   r"   targetserrs         r   r   r     s     j QooVHMBCC{(	Q&kG )Vs1vzF{w)!VWfEEE {  	Q//L"FGSP	Q 	Fs3   &BA& *BBB&	B/B		BBc              #      
K    j                         r fdn fd}d d i
t        d |fg      g}|rt        
fd|d   D        d       }|!|j                          
j	                          =|^}}}	||v r"t        
j                               |gz   dd   t        
      dz
  |k  r<|
j                         z
  |hz
  r%|
|<   |j                  t         ||                   |ry y w)Nc                 *    j                  | d      S )NT)keysedgesnoder   s    r   <lambda>z(_all_simple_edge_paths.<locals>.<lambda>s  s    aggdg. r   c                 &    j                  |       S r   r2   r4   s    r   r6   z(_all_simple_edge_paths.<locals>.<lambda>u  s    1774= r   c              3   2   K   | ]  }|d    vs|  yw)r   Nr   )r   ecurrent_paths     r   r   z)_all_simple_edge_paths.<locals>.<genexpr>  s     K!A$l2J!Ks      r   )
is_multigraphiternextpoppopitemlistvaluesr   r1   append)r   r    r-   r"   	get_edgesstack	next_edgeprevious_node	next_node_r:   s   `         @r   r,   r,   j  s      ?? 
/(  $<LD&>"#$E
KU2YKTR	IIK  "'0$y1 ++-.)<abAA |q 6)l''))YK7&/L#LLi	234' s   C%C,*C,
multigraphweight)
edge_attrsc           	   #      K   | vrt        j                  d| d      | vrt        j                  d| d      |t        }t        }nt	         |       fd}t
        }g }t               }d}	 |s" | |||      \  }	}
|j                  |	|
       nt               }t               }t        dt        |            D ]  }|d| } ||      }|D ]'  }
|
d| |k(  s|j                  |
|dz
     |
|   f       ) 	  | |d   ||||	      \  }	}|dd |z   }
|j                  ||	z   |
       |j                  |d           |r(|j                         }
|
 |j                  |
       |
}ny# t         j                  $ r Y Ww xY ww)
a	  Generate all simple paths in the graph G from source to target,
       starting from shortest ones.

    A simple path is a path with no repeated nodes.

    If a weighted shortest path search is to be used, no negative weights
    are allowed.

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

    source : node
       Starting node for path

    target : node
       Ending node for path

    weight : string or function
        If it is a string, it is the name of the edge attribute to be
        used as a weight.

        If it is a function, the weight of an edge is the value returned
        by the function. The function must accept exactly three positional
        arguments: the two endpoints of an edge and the dictionary of edge
        attributes for that edge. The function must return a number or None.
        The weight function can be used to hide edges by returning None.
        So ``weight = lambda u, v, d: 1 if d['color']=="red" else None``
        will find the shortest red path.

        If None all edges are considered to have unit weight. Default
        value None.

    Returns
    -------
    path_generator: generator
       A generator that produces lists of simple paths, in order from
       shortest to longest.

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

    NetworkXError
       If source or target nodes are not in the input graph.

    NetworkXNotImplemented
       If the input graph is a Multi[Di]Graph.

    Examples
    --------

    >>> G = nx.cycle_graph(7)
    >>> paths = list(nx.shortest_simple_paths(G, 0, 3))
    >>> print(paths)
    [[0, 1, 2, 3], [0, 6, 5, 4, 3]]

    You can use this function to efficiently compute the k shortest/best
    paths between two nodes.

    >>> from itertools import islice
    >>> def k_shortest_paths(G, source, target, k, weight=None):
    ...     return list(
    ...         islice(nx.shortest_simple_paths(G, source, target, weight=weight), k)
    ...     )
    >>> for path in k_shortest_paths(G, 0, 3, 2):
    ...     print(path)
    [0, 1, 2, 3]
    [0, 6, 5, 4, 3]

    Notes
    -----
    This procedure is based on algorithm by Jin Y. Yen [1]_.  Finding
    the first $K$ paths requires $O(KN^3)$ operations.

    See Also
    --------
    all_shortest_paths
    shortest_path
    all_simple_paths

    References
    ----------
    .. [1] Jin Y. Yen, "Finding the K Shortest Loopless Paths in a
       Network", Management Science, Vol. 17, No. 11, Theory Series
       (Jul., 1971), pp. 712-716.

    r&   r'   r(   Nc           	      H    t        fdt        | | dd        D              S )Nc           	   3   X   K   | ]!  \  }} ||j                  ||             # y wr   )get_edge_data)r   r   r   r   wts      r   r   z=shortest_simple_paths.<locals>.length_func.<locals>.<genexpr>  s.      4:Q1aA./s   '*r   )sumzip)pathr   rR   s    r   length_funcz*shortest_simple_paths.<locals>.length_func  s*     >A$QR>Q  r   )rL   r   r;   )ignore_nodesignore_edgesrL   )r)   r*   r   _bidirectional_shortest_pathr   _bidirectional_dijkstra
PathBufferpushr   rangeaddNetworkXNoPathr@   rD   )r   r    r!   rL   rV   shortest_path_funclistAlistB	prev_pathlengthrU   rW   rX   irootroot_lengthspurrR   s   `                @r   r   r     s    x QooVHMBCCQooVHMBCC~9a(	
 5ELEI
-aOLFDJJvt$5L5L1c)n- + !})$/! ADBQx4'$(($q1u+tAw)?@A#5R%1%1%$LFD  9t+DJJ{V3T:   b*'+* 99;DJLLIE 0 (( s1   CF F?1E20AF2FFFFc                   $    e Zd Zd Zd Zd Zd Zy)r[   c                 N    t               | _        g | _        t               | _        y r   )r   pathssortedpathsr   counterselfs    r   __init__zPathBuffer.__init__,  s    U
wr   c                 ,    t        | j                        S r   )r   rl   rn   s    r   __len__zPathBuffer.__len__1  s    4##$$r   c                     t        |      }|| j                  vrHt        | j                  |t	        | j
                        |f       | j                  j                  |       y y r   )tuplerk   r   rl   r?   rm   r^   )ro   costrU   hashable_paths       r   r\   zPathBuffer.push4  sL    d

*T%%d4<<.@$'GHJJNN=) +r   c                     t        | j                        \  }}}t        |      }| j                  j	                  |       |S r   )r   rl   rt   rk   remove)ro   ru   numrU   rv   s        r   r@   zPathBuffer.pop:  s9    #D$4$45sDd

-(r   N)__name__
__module____qualname__rp   rr   r\   r@   r   r   r   r[   r[   +  s    
%*r   r[   c                     t        | ||||      }|\  }}}	g }
|	|
j                  |	       ||	   }	|	||
d      }	|	|
j                  d|	       ||	   }	|	t        |
      |
fS )a  Returns the shortest path between source and target ignoring
       nodes and edges in the containers ignore_nodes and ignore_edges.

    This is a custom modification of the standard bidirectional shortest
    path implementation at networkx.algorithms.unweighted

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

    source : node
       starting node for path

    target : node
       ending node for path

    ignore_nodes : container of nodes
       nodes to ignore, optional

    ignore_edges : container of edges
       edges to ignore, optional

    weight : None
       This function accepts a weight argument for convenience of
       shortest_simple_paths function. It will be ignored.

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

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

    See Also
    --------
    shortest_path

    r   )_bidirectional_pred_succrD   insertr   )r   r    r!   rW   rX   rL   resultspredsuccwrU   s              r   rY   rY   A  s    Z 'q&&,UGMD$ D
-AG - 	T!WA
-AqG - t9d?r   c                 4   r$|v s|v rt        j                  d| d| d      ||k(  r	|di|di|fS | j                         r| j                  }| j                  }n| j
                  }| j
                  }rfd} ||      } ||      }r@| j                         rfd}fd}	 ||      } |	|      }nfd} ||      } ||      }|di}
|di}|g}|g}|r|rt        |      t        |      k  r@|}g }|D ]6  } ||      D ])  }||
vr|j                  |       ||
|<   ||v s"|
||fc c S  8 n?|}g }|D ]6  } ||      D ])  }||vr|||<   |j                  |       ||
v s"|
||fc c S  8 |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.
    No path between  and .Nc                       fd}|S )Nc              3   :   K    |       D ]  }|vs|  y wr   r   r   r   rW   r   s     r   iteratez>_bidirectional_pred_succ.<locals>.filter_iter.<locals>.iterate  '     q  A,    r   r   r   rW   s   ` r   filter_iterz-_bidirectional_pred_succ.<locals>.filter_iter       
 Nr   c                       fd}|S )Nc              3   >   K    |       D ]  }|| fvs
|  y wr   r   r   r   rX   	pred_iters     r   r   zC_bidirectional_pred_succ.<locals>.filter_pred_iter.<locals>.iterate  +     &q\ $q65"#G$   r   r   r   rX   s   ` r   filter_pred_iterz2_bidirectional_pred_succ.<locals>.filter_pred_iter      $
 r   c                       fd}|S )Nc              3   >   K    |       D ]  }| |fvs
|  y wr   r   r   r   rX   	succ_iters     r   r   zC_bidirectional_pred_succ.<locals>.filter_succ_iter.<locals>.iterate  r   r   r   r   r   rX   s   ` r   filter_succ_iterz2_bidirectional_pred_succ.<locals>.filter_succ_iter  r   r   c                       fd}|S )Nc              3   L   K    |       D ]  }| |fvs
|| fvs|  y wr   r   r   r   rX   r   s     r   r   z>_bidirectional_pred_succ.<locals>.filter_iter.<locals>.iterate  6     "1X $q651a&:T"#G$   $$$r   r   r   rX   s   ` r   r   z-_bidirectional_pred_succ.<locals>.filter_iter  r   r   )r)   r_   is_directedpredecessors
successors	neighborsr   rD   )r   r    r!   rW   rX   GpredGsuccr   r   r   r   r   forward_fringereverse_fringe
this_levelr   r   s      ``            r   r~   r~     s    </6\3I"26(%xq IJJ77 	}} 	 E"E" ==? %U+E$U+E  &E&E 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},-- ^0 

.vheF81E
FFr   c           	         r$|v s|v rt        j                  d| d| d      ||k(  r"|| vrt        j                  d| d      d|gfS | j                         r| j                  }| j
                  }n| j                  }| j                  }rfd} ||      } ||      }r@| j                         rfd}	fd	}
 |	|      } |
|      }nfd
} ||      } ||      }t        | |      }t        }t        }i i g}||gi||gig}g g g}|di|dig}t               } ||d   dt        |      |f        ||d   dt        |      |f       ||g}g }d}|d   rX|d   rRd|z
  } |||         \  }}}|||   v r(|||   |<   ||d|z
     v r|fS  ||   |      D ]  }|dk(  r |||| j                  ||            }n |||| j                  ||            }|A||   |   |z   }|||   v r|||   |   k  s_t        d      |||   vs|||   |   k  s}|||   |<    |||   |t        |      |f       ||   |   |gz   ||   |<   ||d   v s||d   v s|d   |   |d   |   z   }|g k(  s|kD  s|}|d   |   dd }|j                          |d   |   |dd z   } |d   r|d   rRt        j                  d| d| d      )a	  Dijkstra's algorithm for shortest paths using bidirectional search.

    This function returns the shortest path between source and target
    ignoring nodes and edges in the containers ignore_nodes and
    ignore_edges.

    This is a custom modification of the standard Dijkstra bidirectional
    shortest path implementation at networkx.algorithms.weighted

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

    source : node
        Starting node.

    target : node
        Ending node.

    weight: string, function, optional (default='weight')
        Edge data key or weight function corresponding to the edge weight
        If this is a function, the weight of an edge is the value
        returned by the function. The function must accept exactly three
        positional arguments: the two endpoints of an edge and the
        dictionary of edge attributes for that edge. The function must
        return a number or None to indicate a hidden edge.

    ignore_nodes : container of nodes
        nodes to ignore, optional

    ignore_edges : container of edges
        edges to ignore, optional

    Returns
    -------
    length : number
        Shortest path length.

    Returns a tuple of two dictionaries keyed by node.
    The first dictionary stores distance from the source.
    The second stores the path from the source to that node.

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

    Notes
    -----
    Edge weight attributes must be numerical.
    Distances are calculated as sums of weighted edges traversed.

    The weight function can be used to hide edges by returning None.
    So ``weight = lambda u, v, d: 1 if d['color']=="red" else None``
    will find the shortest red path.

    In practice  bidirectional Dijkstra is much more than twice as fast as
    ordinary Dijkstra.

    Ordinary Dijkstra expands nodes in a sphere-like manner from the
    source. The radius of this sphere will eventually be the length
    of the shortest path. Bidirectional Dijkstra will expand nodes
    from both the source and the target, making two spheres of half
    this radius. Volume of the first sphere is pi*r*r while the
    others are 2*pi*r/2*r/2, making up half the volume.

    This algorithm is not guaranteed to work if edge weights
    are negative or are floating point numbers
    (overflows and roundoff errors can cause problems).

    See Also
    --------
    shortest_path
    shortest_path_length
    r   r   r   zNode r'   r   c                       fd}|S )Nc              3   :   K    |       D ]  }|vs|  y wr   r   r   s     r   r   z=_bidirectional_dijkstra.<locals>.filter_iter.<locals>.iterateJ  r   r   r   r   s   ` r   r   z,_bidirectional_dijkstra.<locals>.filter_iterI  r   r   c                       fd}|S )Nc              3   >   K    |       D ]  }|| fvs
|  y wr   r   r   s     r   r   zB_bidirectional_dijkstra.<locals>.filter_pred_iter.<locals>.iterateY  r   r   r   r   s   ` r   r   z1_bidirectional_dijkstra.<locals>.filter_pred_iterX  r   r   c                       fd}|S )Nc              3   >   K    |       D ]  }| |fvs
|  y wr   r   r   s     r   r   zB_bidirectional_dijkstra.<locals>.filter_succ_iter.<locals>.iteratea  r   r   r   r   s   ` r   r   z1_bidirectional_dijkstra.<locals>.filter_succ_iter`  r   r   c                       fd}|S )Nc              3   L   K    |       D ]  }| |fvs
|| fvs|  y wr   r   r   s     r   r   z=_bidirectional_dijkstra.<locals>.filter_iter.<locals>.iteraten  r   r   r   r   s   ` r   r   z,_bidirectional_dijkstra.<locals>.filter_iterm  r   r   r   Nz,Contradictory paths found: negative weights?)r)   r_   r*   r   r   r   r   r   r   r   r   r?   rQ   
ValueErrorreverse)r   r    r!   rL   rW   rX   r   r   r   r   r   rR   r\   r@   distsrk   fringeseencneighs	finalpathdirdistrJ   r   	finaldistr   	minweightvwLength	totaldistrevpaths       ``                         r   rZ   rZ     s   \ </6\3I"26(%xq IJJ?//E&"?@@F8} 	}} 	 E"E" ==? %U+E$U+E  &E&E	!V	$BD
CHEvh&6(!34E"XFQK&!%DAQQ()QQ()U^F I
C
)q	 #g6#;'q!c
?c
1a#g y))Q 	>Aaxq!Q__Q%:;	q!Q__Q%:;	 Sz!}y0HE#JeCjm+$%STT$s)#x$s)A,'>'S	!VC[8T!Wa"89 %c
1 3c
1Q<AaL !%Q
T!WQZ 7I B)i*?$-	"'(1+a.)$)!HQK'!"+$=	3	>! )q	T 

.vheF81E
FFr   r   )NNN)NN)rL   NN)heapqr   r   	itertoolsr   networkxr)   +networkx.algorithms.shortest_paths.weightedr   networkx.utilsr   r   __all___dispatchabler
   r	   r   r,   r   r[   rY   r~   rZ   r   r   r   <module>r      s    #   H 8 J6 J6Z b9 b9J bF bFJ'5T \"X&R ' #Rj . EI<~fGT IMMGr   