
    i                     :    S r SSKJr  SS jrS r " S S\5      rg)	zM
altgraph.GraphAlgo - Graph algorithms
=====================================
    )
GraphErrorNc                 <   0 n0 n[        5       nSXQ'   U H  nXV   X6'   Xb:X  a    X44$ U R                  U5       H[  nU R                  Xg5      nX6   U R                  U5      -   n	Xs;   a  XU   :  a  [	        S5      eMD  Xu;  d
  XU   :  d  MS  XU'   XdU'   M]     M     X44$ )a   
Dijkstra's algorithm for shortest paths

`David Eppstein, UC Irvine, 4 April 2002
    <http://www.ics.uci.edu/~eppstein/161/python/>`_

`Python Cookbook Recipe
    <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/119466>`_

Find shortest paths from the  start node to all nodes nearer than or
equal to the end node.

Dijkstra's algorithm is only guaranteed to work correctly when all edge
lengths are positive.  This code does not verify this property for all
edges (only the edges examined until the end vertex is reached), but will
correctly compute shortest paths even for some graphs with negative edges,
and will raise an exception if it discovers that a negative edge has
caused it to make a mistake.

Adapted to altgraph by Istvan Albert, Pennsylvania State University -
June, 9 2004
r   z3Dijkstra: found better path to already-final vertex)_priorityDictionaryout_nbrsedge_by_node	edge_datar   )
graphstartendDPQvwedge_idvwLengths
             ?C:\des-py\RoboSAPF\venv\Lib\site-packages\altgraph/GraphAlgo.pydijkstrar      s    . 	A
AAAHt8 6M "A((.Gteoog66Hvd?$M  # xA$!! # " 6M    c                     [        XU5      u  p4/ n UR                  U5        X!:X  a  OXB   nM  UR                  5         U$ )a	  
Find a single shortest path from the *start* node to the *end* node.
The input has the same conventions as dijkstra(). The output is a list of
the nodes in order along the shortest path.

**Note that the distances must be stored in the edge data as numeric data**
)r   appendreverse)r	   r
   r   r   r   Paths         r   shortest_pathr   8   sJ     E#&DAD
C<f	 
 	LLNKr   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
r   O   a  
Priority dictionary using binary heaps (internal use only)

David Eppstein, UC Irvine, 8 Mar 2002

Implements a data structure that acts almost like a dictionary, with
two modifications:

    1. D.smallest() returns the value x minimizing D[x].  For this to
       work correctly, all values D[x] stored in the dictionary must be
       comparable.

    2. iterating "for x in D" finds and removes the items from D in sorted
       order. Each item is not removed until the next item is requested,
       so D[x] will still return a useful value until the next iteration
       of the for-loop.  Each operation takes logarithmic amortized time.
c                 <    / U l         [        R                  U 5        g)z
Initialize priorityDictionary by creating binary heap of pairs
(value,key).  Note that changing or removing a dict entry will not
remove the old pair from the heap until it is found by smallest()
or until the heap is rebuilt.
N)_priorityDictionary__heapdict__init__)selfs    r   r    _priorityDictionary.__init__b   s     dr   c                    [        U 5      S:X  a  [        S5      eU R                  nUS   S   U ;  d  XS   S      US   S   :w  a  UR                  5       nSn SU-  S-   nUS-   [        U5      :  a  X   XS-      :  a  US-  nU[        U5      :  d  X!U   ::  a  X!U'   O
X   X'   UnMR  US   S   U ;  a  Ms  XS   S      US   S   :w  a  M  US   S   $ )zE
Find smallest item after removing deleted items from front of heap.
r   z$smallest of empty priorityDictionary      )len
IndexErrorr   pop)r!   heaplastIteminsertionPoint
smallChilds        r   smallest_priorityDictionary.smallestl   s    t9>CDD{{1gaj$!WQZ(8DGAJ(FxxzHN/!3
NSY.(4Q+??!OJT*hz:J.J+3('+'7$!+  1gaj$!WQZ(8DGAJ(F Awqzr   c                    ^  U 4S jnU" 5       $ )z;
Create destructive sorted iterator of priorityDictionary.
c               3   ~   >#    [        T5      S:  a)  TR                  5       n U v   TU 	 [        T5      S:  a  M(  g g 7f)Nr   )r&   r-   )xr!   s    r   iterfn,_priorityDictionary.__iter__.<locals>.iterfn   s5     d)a-MMOG d)a-s   6== )r!   r2   s   ` r   __iter___priorityDictionary.__iter__   s    
	 xr   c                    [         R                  XU5        U R                  n[        U5      S[        U 5      -  :  aF  U R	                  5        VVs/ s H  u  pEXT4PM
     snnU l        U R                  R                  5         gX!4n[        U5      nUR                  S5        US:  a8  XcUS-
  S-     :  a*  X7S-
  S-     X7'   US-
  S-  nUS:  a  XcUS-
  S-     :  a  M*  XcU'   gs  snnf )z
Change value stored in dictionary and add corresponding pair to heap.
Rebuilds the heap if the number of deleted items gets large, to avoid
memory leakage.
r%   Nr   r$   )r   __setitem__r   r&   itemssortr   )r!   keyvalr)   kr   newPairr+   s           r   r8   _priorityDictionary.__setitem__   s     	C({{t9q3t9}$.2jjl;ldaA6l;DKKKjG YNKK 1$!9KPQ8Q3R)R'+a-?A,E'F$"01"4!: !1$!9KPQ8Q3R)R $+  <s   C&c                     X;  a  X U'   X   $ )zD
Reimplement setdefault to pass through our customized __setitem__.
r4   )r!   r;   r<   s      r   
setdefault_priorityDictionary.setdefault   s     ?Iyr   )__heapN)__name__
__module____qualname____firstlineno____doc__r    r-   r5   r8   rA   __static_attributes__r4   r   r   r   r   O   s     $0+(r   r   )N)rH   altgraphr   r   r   r   r   r4   r   r   <module>rK      s(     -`.\$ \r   