| 1 |
heapdict: a heap with decreased-key and increase-key operations |
| 2 |
=============================================================== |
| 3 |
|
| 4 |
heapdict implements the MutableMapping ABC, meaning it works pretty |
| 5 |
much like a regular Python dict. It's designed to be used as a |
| 6 |
priority queue, where items are added and consumed as follows: |
| 7 |
|
| 8 |
:: |
| 9 |
|
| 10 |
hd = heapdict() |
| 11 |
hd[obj1] = priority1 |
| 12 |
hd[obj2] = priority2 |
| 13 |
# ... |
| 14 |
(obj, priority) = hd.popitem() |
| 15 |
|
| 16 |
Compared to an ordinary dict, a heapdict has the following differences: |
| 17 |
|
| 18 |
popitem(): |
| 19 |
Remove and return the (key, priority) pair with the lowest |
| 20 |
priority, instead of a random object. |
| 21 |
|
| 22 |
peekitem(): |
| 23 |
Return the (key, priority) pair with the lowest priority, without |
| 24 |
removing it. |
| 25 |
|
| 26 |
Unlike the Python standard library's heapq module, the heapdict |
| 27 |
supports efficiently changing the priority of an existing object |
| 28 |
(often called "decrease-key" in textbooks). Altering the priority is |
| 29 |
important for many algorithms such as Dijkstra's Algorithm and A*. |
| 30 |
|