Skip to main content

FastHeapDict

FastHeapDict is a high-performance indexed priority queue for Python designed for algorithms that repeatedly update priorities. Unlike duplicate-entry priority queues, FastHeapDict maintains a single active heap entry for each key, providing efficient decrease_key operations while avoiding stale entries and unnecessary heap growth. It is particularly well suited for graph algorithms such as Dijkstra's shortest-path algorithm, A*, Prim's algorithm, event simulation, and scheduling applications.

Requirements

  • Python 3.10 or newer

Installation

Install from PyPI:

python -m pip install fastheapdict

Install from a local source checkout:

python -m pip install -e .

A Complete Example: Dijkstra's Shortest-Path Algorithm

The example below demonstrates the intended use of FastHeapDict in Dijkstra's algorithm.

from fastheapdict import FastHeapDict

def dijkstra(graph, source):
    dist = {v: float("inf") for v in graph}
    dist[source] = 0

    pq = FastHeapDict()
    pq[source] = 0

    while pq:
        u, du = pq.popitem()

        if du > dist[u]:
            continue

        for v, w in graph[u]:
            nd = du + w

            if nd < dist[v]:
                dist[v] = nd
                pq.decrease_key(v, nd)

    return dist


graph = {
    "A": [("B", 4), ("C", 2)],
    "B": [("C", 5), ("D", 10)],
    "C": [("E", 3)],
    "D": [("F", 11)],
    "E": [("D", 4)],
    "F": []
}

print(dijkstra(graph, "A"))

Output:

{'A': 0, 'B': 4, 'C': 2, 'D': 9, 'E': 5, 'F': 20}

Core Operations

from fastheapdict import FastHeapDict

pq = FastHeapDict()

pq["alice"] = 8
pq["bob"] = 3

pq.decrease_key("alice", 2)

key, priority = pq.popitem()

print(key, priority)
Operation Complexity
Insert / Update O(log n)
decrease_key O(log n)
popitem O(log n)
Key lookup O(1)

When should I use FastHeapDict?

FastHeapDict is intended for problems in which priorities change frequently. Typical applications include shortest-path algorithms, minimum spanning trees, informed graph search, discrete-event simulation, and scheduling. If your application repeatedly inserts duplicate entries into a standard heap to emulate decrease-key, FastHeapDict can simplify the implementation and reduce unnecessary heap growth.

Running the bundled example from source

From the project root directory, install the package in editable mode first and then run the example:

python -m pip install -e .
python examples/basic_usage.py

Running the test suite from source

From the project root directory:

python -m pip install -e ".[test]"
python -m pytest

Benchmarks

The source distribution includes the benchmark programs used to evaluate the implementation. They reproduce the experiments described in the accompanying project report and compare FastHeapDict against alternative priority-queue implementations.

To install the optional benchmark dependencies from a source checkout, run:

python -m pip install -e ".[benchmark]"

Then run the benchmark command-line interface:

fastheapdict-benchmark --self-test --experiments all

You can also run the package module directly:

python -m fastheapdict --self-test --experiments all

Building and checking the distribution

From the project root directory:

python -m pip install --upgrade build twine
rm -rf dist build src/*.egg-info
python -m build
python -m twine check dist/*

License

MIT License.

Metadata

Release files for fastheapdict 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastheapdict 0.1.0
File Size Uploaded Provenance Actions
fastheapdict-0.1.0.tar.gz 17.5 kB Without provenance Details for fastheapdict-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastheapdict 0.1.0
File Interpreter ABI Platform Actions
fastheapdict-0.1.0-py3-none-any.whl Python 3 none any Details for fastheapdict-0.1.0-py3-none-any.whl

Total release size: 36.9 kB

Release files / fastheapdict-0.1.0.tar.gz

Download URL fastheapdict-0.1.0.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f2ba727dfd7914368b3f4a71f33f01e920e10477737758800c8a459110a504e2
BLAKE2b-256 checksum
How to use checksums
ff01ad20a129ea91a480b0ef05dad4b000fa5f900196bc6c325e7644d2d61539
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / fastheapdict-0.1.0-py3-none-any.whl

Download URL fastheapdict-0.1.0-py3-none-any.whl
Size 19.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
304c004827c10c08daffb90ad21e1d47b315e4f4dbee381dcd31f41dae23e9c6
BLAKE2b-256 checksum
How to use checksums
e0e69249cdb8b67e3ab73a944ed80f9745816310031012cfbbdd03f3498a86df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for fastheapdict 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page