zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
U
K±d ã@s6dZddlmZd
dd„Zdd„ZGdd    „d    eƒZdS) zM
altgraph.GraphAlgo - Graph algorithms
=====================================
é)Ú
GraphErrorNc
Cs°i}i}tƒ}d||<|D]Œ}||||<||kr6q¨| |¡D]d}| ||¡}||| |¡}    ||kr€|    ||kr¤tdƒ‚q@||ks”|    ||kr@|    ||<|||<q@q||fS)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
    rz3Dijkstra: found better path to already-final vertex)Ú_priorityDictionaryZout_nbrsZ edge_by_nodeZ    edge_datar)
ÚgraphÚstartÚendÚDÚPÚQÚvÚwZedge_idZvwLength©r úId:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\altgraph/GraphAlgo.pyÚdijkstras(   ÿ rcCs>t|||ƒ\}}g}| |¡||kr(q2||}q| ¡|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**
    )rÚappendÚreverse)rrrrrÚPathr r r Ú shortest_path8s    
 
rc@s8eZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd S) 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.
    cCsg|_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__heapÚdictÚ__init__©Úselfr r r rbsz_priorityDictionary.__init__cCsÌt|ƒdkrtdƒ‚|j}|dd|ksF||dd|ddkrÀ| ¡}d}d|d}|dt|ƒkrŠ||||dkrŠ|d7}|t|ƒks¢|||kr¬|||<q||||<|}qRq|ddS)zU
        Find smallest item after removing deleted items from front of heap.
        rz$smallest of empty priorityDictionaryéé)ÚlenÚ
IndexErrorrÚpop)rÚheapZlastItemÚinsertionPointZ
smallChildr r r Úsmallestls$ , ÿþ z_priorityDictionary.smallestcs‡fdd„}|ƒS)zK
        Create destructive sorted iterator of priorityDictionary.
        c3s&tˆƒdkr"ˆ ¡}|Vˆ|=qdS)Nr)rr)Úxrr r Úiterfn‰s z,_priorityDictionary.__iter__.<locals>.iterfnr )rr!r rr Ú__iter__„s z_priorityDictionary.__iter__cCs¬t |||¡|j}t|ƒdt|ƒkrHdd„| ¡Dƒ|_|j ¡n`||f}t|ƒ}| d¡|dkr |||ddkr ||dd||<|dd}qb|||<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.
        rcSsg|]\}}||f‘qSr r )Ú.0Úkr
r r r Ú
<listcomp>šsz3_priorityDictionary.__setitem__.<locals>.<listcomp>Nrr)rÚ __setitem__rrÚitemsÚsortr)rÚkeyÚvalrZnewPairrr r r r&‘s 
z_priorityDictionary.__setitem__cCs||kr|||<||S)zT
        Reimplement setdefault to pass through our customized __setitem__.
        r )rr)r*r r r Ú
setdefault¥sz_priorityDictionary.setdefaultN)    Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrr"r&r+r r r r rOs 
 r)N)r/Zaltgraphrrrrrr r r r Ú<module>s 
0