
    hpv                     B   S SK JrJr  S SKJr  S SKrS SKJr  S SK	J
r
Jr  / SQr\R                  S 5       r\R                  SS j5       r\R                  SS	 j5       rS
 r\
" S5      \R                  " SS9SS j5       5       r " S S5      r SS jrSS jr SS jrg)    )heappopheappush)countN)_weight_function)not_implemented_forpairwise)all_simple_pathsis_simple_pathshortest_simple_pathsall_simple_edge_pathsc                   ^  [        U5      S:X  a  g[        U5      S:X  a  US   T ;   $ [        U 4S jU 5       5      (       d  g[        [        U5      5      [        U5      :w  a  g[        U 4S j[        U5       5       5      $ )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   ,   >#    U  H	  oT;   v   M     g 7fN ).0nGs     R/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/simple_paths.py	<genexpr>!is_simple_path.<locals>.<genexpr>S   s     %u!Avus   c              3   8   >#    U  H  u  pUTU   ;   v   M     g 7fr   r   )r   uvr   s      r   r   r   [   s     5_TQqAaDy_s   )lenallsetr   )r   nodess   ` r   r
   r
      su    r 5zQ 5zQQx1} %u%%% 3u:#e*$ 5Xe_555    c              #   r   #    [        XX#5       H  nU/U Vs/ s H  oUS   PM	     sn-   v   M      gs  snf 7f)ah  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	   ^   s;     D +1fE	hi8idq'i888 F8s   727c              #   >  #    X;  a  [         R                  " SU S35      eX ;   a  U1nO [        U5      nUb  UO[	        U 5      S-
  nUS:  a  U(       a  [        XXC5       Sh  vN   ggg! [         a   n[         R                  " SU S35      UeSnAff = f N47f)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 ooVHMBCC{(	Q&kG )Vs1vzF{w)!WEEE {  	Q//L"FGSP	Q 	Fs3   )BA. /B&B'B.
B8BBBc              #     ^ ^
#    T R                  5       (       a  U 4S jOU 4S jnS S 0m
[        S U4/5      /nU(       a  [        U
4S jUS    5       S 5      nUc"  UR                  5         T
R	                  5         ME  Utpxn	X;   a"  [        T
R                  5       5      U/-   SS  v   [        T
5      S-
  U:  aA  UT
R                  5       -
  U1-
  (       a%  UT
U'   UR                  [        U" U5      5      5        U(       a  M  g g 7f)Nc                 $   > TR                  U SS9$ )NT)keysedgesnoder   s    r   <lambda>(_all_simple_edge_paths.<locals>.<lambda>s  s    aggdg.r   c                 &   > TR                  U 5      $ r   r3   r5   s    r   r7   r8   u  s    1774=r   c              3   <   >#    U  H  oS    T;  d  M  Uv   M     g7f)r   Nr   )r   ecurrent_paths     r   r   )_all_simple_edge_paths.<locals>.<genexpr>  s     KYA$l2J!!Ys   	   r   )
is_multigraphiternextpoppopitemlistvaluesr   r2   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$1 ++-.)<abAA |q 6)l''))YK7&/L#LLi	234' %s   C?DD
multigraphweight)
edge_attrsc           
   #   8  ^ ^#    UT ;  a  [         R                  " SU S35      eUT ;  a  [         R                  " SU S35      eUc  [        n[        nO[	        T U5      mU U4S jn[
        n/ n[        5       nSn U(       d  U" T XUS9u  pUR                  X5        O[        5       n[        5       n[        S[        U5      5       H  nUSU nU" U5      nU H(  n
U
SU U:X  d  M  UR                  XS-
     X   45        M*      U" T US   UUUUS	9u  n	nUSS U-   n
UR                  X-   U
5        UR                  US   5        M     U(       a(  UR                  5       n
U
v   UR                  U
5        U
nOgGM  ! [         R                   a     Naf = f7f)
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   > [        UU4S j[        X SS  5       5       5      $ )Nc           	   3   X   >#    U  H  u  pT" XTR                  X5      5      v   M!     g 7fr   )get_edge_data)r   r   r   r   wts      r   r   =shortest_simple_paths.<locals>.length_func.<locals>.<genexpr>  s*      >QFQ1.//>Qs   '*r   )sumzip)pathr   rU   s    r   length_func*shortest_simple_paths.<locals>.length_func  s(     >A$QR>Q  r   )rO   r   r>   )ignore_nodesignore_edgesrO   )r*   r+   r   _bidirectional_shortest_pathr   _bidirectional_dijkstra
PathBufferpushr   rangeaddNetworkXNoPathrC   rG   )r   r!   r"   rO   rZ   shortest_path_funclistAlistB	prev_pathlengthrY   r\   r]   irootroot_lengthspurrU   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OLFJJv$5L5L1c)n- !})$/!DBQx4'$(($1u+tw)?@ "#5R%1%1%$LFD  9t+DJJ{3T:   b*' .* 99;DJLLIE 0 (( s1   C!F)F-F 5AF FFFFc                   ,    \ rS rSrS rS rS rS rSrg)r`   i+  c                 N    [        5       U l        / U l        [        5       U l        g r   )r   pathssortedpathsr   counterselfs    r   __init__PathBuffer.__init__,  s    U
wr   c                 ,    [        U R                  5      $ r   )r   rq   rs   s    r   __len__PathBuffer.__len__1  s    4##$$r   c                     [        U5      nX0R                  ;  aH  [        U R                  U[	        U R
                  5      U45        U R                  R                  U5        g g r   )tuplerp   r   rq   rB   rr   rc   )rt   costrY   hashable_paths       r   ra   PathBuffer.push4  sJ    d

*T%%d4<<.@$'GHJJNN=) +r   c                     [        U R                  5      u  pn[        U5      nU R                  R	                  U5        U$ r   )r   rq   r{   rp   remove)rt   r|   numrY   r}   s        r   rC   PathBuffer.pop:  s7    #D$4$45Dd

-(r   )rr   rp   rq   N)	__name__
__module____qualname____firstlineno__ru   rx   ra   rC   __static_attributes__r   r   r   r`   r`   +  s    
%*r   r`   c                     [        XX#U5      nUu  pxn	/ n
U	b  U
R                  U	5        X   n	U	b  M  XzS      n	U	b  U
R                  SU	5        Xy   n	U	b  M  [        U
5      U
4$ )a9  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_succrG   insertr   )r   r!   r"   r\   r]   rO   resultspredsuccwrY   s              r   r^   r^   A  s    Z 'q&UGMD D
-AG - 	!WA
-AqG - t9d?r   c                   ^^ T(       a)  UT;   d  UT;   a  [         R                  " SU SU S35      eX!:X  a	  US0US0U4$ U R                  5       (       a  U R                  nU R                  nOU R
                  nU R
                  nT(       a  U4S jnU" U5      nU" U5      nT(       aH  U R                  5       (       a  U4S jnU4S jn	U" U5      nU	" U5      nOU4S jnU" U5      nU" U5      nUS0n
US0nU/nU/nU(       a  U(       a  [        U5      [        U5      ::  aF  Un/ nU H;  nU" U5       H,  nUU
;  a  UR                  U5        XU'   UU;   d  M&  XU4s  s  $    M=     OEUn/ nU H;  nU" U5       H,  nUU;  a  XU'   UR                  U5        UU
;   d  M&  XU4s  s  $    M=     U(       a	  U(       a  M  [         R                  " SU SU S35      e)	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                    >^  UU 4S jnU$ )Nc              3   D   >#    T" U 5       H  nUT;  d  M  Uv   M     g 7fr   r   r   r   r\   r   s     r   iterate>_bidirectional_pred_succ.<locals>.filter_iter.<locals>.iterate  !     qA, "    	 r   r   r   r\   s   ` r   filter_iter-_bidirectional_pred_succ.<locals>.filter_iter       
 Nr   c                    >^  UU 4S jnU$ )Nc              3   F   >#    T" U 5       H  nX4T;  d  M  Uv   M     g 7fr   r   r   r   r]   	pred_iters     r   r   C_bidirectional_pred_succ.<locals>.filter_pred_iter.<locals>.iterate  #     &q\65"#G *   !	!r   r   r   r]   s   ` r   filter_pred_iter2_bidirectional_pred_succ.<locals>.filter_pred_iter      $
 r   c                    >^  UU 4S jnU$ )Nc              3   F   >#    T" U 5       H  nX4T;  d  M  Uv   M     g 7fr   r   r   r   r]   	succ_iters     r   r   C_bidirectional_pred_succ.<locals>.filter_succ_iter.<locals>.iterate  r   r   r   r   r   r]   s   ` r   filter_succ_iter2_bidirectional_pred_succ.<locals>.filter_succ_iter  r   r   c                    >^  UU 4S jnU$ )Nc              3   X   >#    T" U 5       H  nX4T;  d  M  X4T;  d  M  Uv   M     g 7fr   r   r   r   r]   r   s     r   r   r     ,     "1X651&:T"#G &   **	*r   r   r   r]   s   ` r   r   r     r   r   )r*   rd   is_directedpredecessors
successors	neighborsr   rG   )r   r!   r"   r\   r]   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#1}, "   (JNqA}"#Q&--a0Dy#1}, "   ^^0 

.vheF81E
FFr   c           	      R  ^^ T(       a)  UT;   d  UT;   a  [         R                  " SU SU S35      eX:X  a$  X;  a  [         R                  " SU S35      eSU/4$ U R                  5       (       a  U R                  nU R
                  nOU R                  nU R                  nT(       a  U4S jnU" U5      nU" U5      nT(       aH  U R                  5       (       a  U4S jn	U4S	 jn
U	" U5      nU
" U5      nOU4S
 jnU" U5      nU" U5      n[        X5      n[        n[        n0 0 /nX/0X"/0/n/ / /nUS0US0/n[        5       nU" US   S[        U5      U45        U" US   S[        U5      U45        Xv/n/ nSnUS   (       Ga  US   (       Gax  SU-
  nU" UU   5      u  nnnUUU   ;   a  M5  UUU   U'   UUSU-
     ;   a  WU4$ UU   " U5       GH  nUS:X  a  U" UUU R                  UU5      5      nOU" UUU R                  UU5      5      nUc  MD  UU   U   U-   nUUU   ;   a  UUU   U   :  a  [        S5      eMq  UUU   ;  d  UUU   U   :  d  M  UUU   U'   U" UU   U[        U5      U45        UU   U   U/-   UU   U'   UUS   ;   d  M  UUS   ;   d  M  US   U   US   U   -   nU/ :X  d  WU:  d  M  UnUS   U   SS nUR                  5         US   U   USS -   nGM     US   (       a  US   (       a  GMx  [         R                  " SU SU S35      e)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                    >^  UU 4S jnU$ )Nc              3   D   >#    T" U 5       H  nUT;  d  M  Uv   M     g 7fr   r   r   s     r   r   =_bidirectional_dijkstra.<locals>.filter_iter.<locals>.iterateJ  r   r   r   r   s   ` r   r   ,_bidirectional_dijkstra.<locals>.filter_iterI  r   r   c                    >^  UU 4S jnU$ )Nc              3   F   >#    T" U 5       H  nX4T;  d  M  Uv   M     g 7fr   r   r   s     r   r   B_bidirectional_dijkstra.<locals>.filter_pred_iter.<locals>.iterateY  r   r   r   r   s   ` r   r   1_bidirectional_dijkstra.<locals>.filter_pred_iterX  r   r   c                    >^  UU 4S jnU$ )Nc              3   F   >#    T" U 5       H  nX4T;  d  M  Uv   M     g 7fr   r   r   s     r   r   B_bidirectional_dijkstra.<locals>.filter_succ_iter.<locals>.iteratea  r   r   r   r   s   ` r   r   1_bidirectional_dijkstra.<locals>.filter_succ_iter`  r   r   c                    >^  UU 4S jnU$ )Nc              3   X   >#    T" U 5       H  nX4T;  d  M  X4T;  d  M  Uv   M     g 7fr   r   r   s     r   r   r   n  r   r   r   r   s   ` r   r   r   m  r   r   r   Nz,Contradictory paths found: negative weights?)r*   rd   r+   r   r   r   r   r   r   r   r   rB   rT   
ValueErrorreverse)r   r!   r"   rO   r\   r]   r   r   r   r   r   rU   ra   rC   distsrp   fringeseencneighs	finalpathdirdistrM   r   	finaldistr   	minweightvwLength	totaldistrevpaths       ``                         r   r_   r_     s   \ </6\3I"26(%xq IJJ?//E&"?@@F8} 	}} 	 E"E" ==?? %U+E$U+E  &E&E	!	$BD
CHEh&(!34E"XFQK&!%DAQQ()QQ()^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)rO   NN)heapqr   r   	itertoolsr   networkxr*   +networkx.algorithms.shortest_paths.weightedr   networkx.utilsr   r   __all___dispatchabler
   r	   r   r-   r   r`   r^   r   r_   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   