;i @dZddlmZddZdZGddeZdS) zM altgraph.GraphAlgo - Graph algorithms ===================================== ) GraphErrorNcti}i}t}d||<|D]}||||<||krn||D]p}|||}||||z} ||vr| ||krt dV||vs | ||kr | ||<|||<q||fS)a` Dijkstra's algorithm for shortest paths `David Eppstein, UC Irvine, 4 April 2002 `_ `Python Cookbook Recipe `_ 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 rz3Dijkstra: found better path to already-final vertex)_priorityDictionaryout_nbrs edge_by_node edge_datar) graphstartendDPQvwedge_idvwLengths FC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\altgraph/GraphAlgo.pydijkstrars. A AAAeH t! 88 E""  A((A..Gteoog666HAvvad??$M#!x!A$!!  q6Mct|||\}}g} ||||krn ||}%||S)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** )rappendreverse)r r r r r Paths r shortest_pathr8sc E5# & &DAq D C %<< f   LLNNN Krc0eZdZdZdZdZdZdZdZdS)ra 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. cHg|_t|dS)a  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__heapdict__init__)selfs rrz_priorityDictionary.__init__bs#  drcDt|dkrtd|j}|dd|vs$||dd|ddkr|}d} d|zdz}|dzt|kr||||dzkr|dz }|t|ks |||kr|||<n||||<|}k|dd|v||dd|ddk|ddS)zU Find smallest item after removing deleted items from front of heap. rz$smallest of empty priorityDictionary)len IndexErrorrpop)r heaplastIteminsertionPoint smallChilds rsmallestz_priorityDictionary.smallestlsB t99>>CDD D{1gaj$$T!WQZ(8DGAJ(F(FxxzzHN ,/!3 NSYY..Z(4 Q+???!OJT**h$z:J.J.J+3D('+J'7^$!+ ,1gaj$$T!WQZ(8DGAJ(F(FAwqzrc"fd}|S)zK Create destructive sorted iterator of priorityDictionary. c3Ktdkr0}|V|=tdk.dSdS)Nr)r$r+)xr s riterfnz,_priorityDictionary.__iter__..iterfnsLd))a--MMOOGd))a------r)r r/s` r__iter__z_priorityDictionary.__iter__s)       vxxrct||||j}t|dt|zkr>d|D|_|jdS||f}t|}|d|dkrC|||dz dzkr1||dz dz||<|dz dz}|dkr|||dz dzk1|||<dS)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#cg|] \}}||f Sr0r0).0krs r z3_priorityDictionary.__setitem__..s ;;;daAq6;;;rNrr")r __setitem__rr$itemssortr)r keyvalr'newPairr)s rr7z_priorityDictionary.__setitem__s! sC((({ t99q3t99} $ $;;djjll;;;DK K       CjG YYN KK    1$$4!9KPQ8Q3R)R)R'+^a-?A,E'F^$"01"4!:!1$$4!9KPQ8Q3R)R)R$+D rc$||vr|||<||S)zT Reimplement setdefault to pass through our customized __setitem__. r0)r r:r;s r setdefaultz_priorityDictionary.setdefaults  d??DICyrN) __name__ __module__ __qualname____doc__rr+r1r7r>r0rrrrOsi$0   +++(rr)N)rBaltgraphrrrrrr0rrrDs ----`.\\\\\$\\\\\r