An priority queue built with an in-place modifiable binary heap (decrease-key,increase-key).
Operations have the following algorithmic complexities:
pop-O(log(n))push-O(log(n))(average:O(1))peek-O(1)remove-O(log(n))
Useful for:
- Dijkstra's shortest path algorithm
- A*
- Rolling median
Install
pip install heapy
Examples
The following is an example implementation of Dijkstra's algorithm, a method
for finding the shortest path in a weighted graph. The heap-based
priority queue reduces the complexity of the basic algorithm from O(n^2)
to O(m*log(n)), for n nodes and m edges.
from heapy import pqueue
def dijkstra(G, r):
'''
Find the shortest paths from a given node in a weighted graph
to every other (connected) node.
'''
todo = pqueue() # this is the heap-based priority queue
distance = {} # distances to all nodes on shortest paths
parents = { r: None } # parent pointers for all items on known shortest paths
todo.push((r, 0)) # alternate syntax: todo[r] = 0
while todo: # automatically checks len(todo) > 0
(n, d) = todo.pop() # get the minimum element; O(log(n))
distance[n] = d # save shortest distance
# update position of children in priority queue, if less
children = G.get(n, {})
for (c, w) in children.items():
if c not in distance and (c not in todo or d + w < todo[c]):
todo[c] = d + w # add or update distance ~ O(1)
parents[c] = n # update parent in minimum path distance spanning tree
return distance
Note that G is a dictionary of dictionaries.
{
'a' : {'b' : 2, 'c' : 1},
'b' : {'a' : 2, 'd' : 3},
'c' : {'a' : 1},
'd' : {'b' : 3}
}
Metadata
Release files for heapy 0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| heapy-0.7.tar.gz | 4.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| heapy-0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.9 kB
Release files / heapy-0.7.tar.gz
| Download URL | heapy-0.7.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1687b6941b3a07bd1e4dd3a3efb30f468fcc9daaef34bfabfbfe423684ded004
|
|
BLAKE2b-256 checksum How to use checksums |
81f20ec1b0be0f036ae8e237850a3d0f010b4546ddb178ab1e765b0720643481
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10
|
Release files / heapy-0.7-py3-none-any.whl
| Download URL | heapy-0.7-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cffb4d3266a6dc1b20ec541b0aa18f6ee00627fd12c6467b2d896e7ab212c131
|
|
BLAKE2b-256 checksum How to use checksums |
14ded9ed7eeb3e01aea6ece9304c280eed8951e80ffb709937f192984d839f23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10
|
Security for heapy 0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.