Skip to main content

graphs_esroka

A small Python library of graph algorithms. It currently provides Dijkstra's shortest path algorithm, built on a min-heap, for weighted graphs with non-negative edge weights.

Installation

Install from PyPI with pip (requires Python 3.8+):

pip install graphs_esroka

Or install the latest version straight from GitHub (also requires Git). The package lives in the src folder, so the URL points there:

pip install "git+https://github.com/ethancs13/graphs_esroka.git#subdirectory=src"

To uninstall:

pip uninstall graphs_esroka

Quick Start

from graphs_esroka import sp

graph = {
    'A': {'B': 4, 'C': 1},
    'B': {'D': 1},
    'C': {'B': 2, 'D': 5},
    'D': {},
}

dist, path = sp.dijkstra(graph, 'A')

print(dist)  # {'A': 0, 'B': 3, 'C': 1, 'D': 4}
print(path)  # {'A': [], 'B': ['A', 'C'], 'C': ['A'], 'D': ['A', 'C', 'B']}

API

sp.dijkstra(graph, source)

Finds the shortest path from source to every node it can reach.

Parameters

  • graph: a dictionary of dictionaries, where graph[u][v] is the weight of the edge from u to v. Edges are directed, so for an undirected graph, list each edge in both directions.
  • source: the node to start from. It must be a key in graph.

Returns

A tuple (dist, path):

  • dist: a dictionary mapping each node to its shortest distance from source. Nodes that can't be reached keep the value sys.maxsize.
  • path: a dictionary mapping each reachable node to the list of nodes visited before it, starting at source. The destination itself is not included, so path['D'] == ['A', 'C', 'B'] means the route is A → C → B → D. Nodes that can't be reached are left out.

Requirements for the input graph

  • Every node must appear as a key in graph, even nodes with no outgoing edges (give those an empty dictionary, like 'D': {} above). Otherwise dijkstra raises a KeyError.
  • Edge weights must be non-negative numbers. Dijkstra's algorithm does not give correct results with negative weights.

Running the Example Script

test.py reads a graph from a text file and prints the shortest paths from node 0:

python src/test.py path/to/graph.txt

Each line of the file is one directed edge, written as source destination weight (all integers):

0 1 4
0 7 8
1 0 4
1 2 8
...

Example output:

Shortest distances from 0:
{0: 0, 1: 4, 2: 12, 3: 19, 4: 21, 5: 11, 6: 9, 7: 8, 8: 14}
spf to 0: []
spf to 1: [0]
spf to 7: [0]
spf to 2: [0, 1]
...

Project Structure

graphs_esroka/
├── src/
│   ├── graphs_esroka/
│   │   ├── __init__.py
│   │   ├── heapq.py      # min-heap used by Dijkstra's algorithm
│   │   └── sp.py         # shortest path algorithms
│   ├── test.py           # example script
│   ├── README.md         # package description used by pip
│   └── pyproject.toml    # package settings used by pip
├── LICENSE
└── README.md

Development

Clone the repository and install it in editable mode, so your code changes take effect without reinstalling:

git clone https://github.com/ethancs13/graphs_esroka.git
cd graphs_esroka
pip install -e src

To build the distribution files (.whl and .tar.gz) into src/dist:

pip install build
python -m build src

Contributing

Contributions are welcome. The main branch is protected, and all development happens on dev:

  1. Create a branch from dev for your change.
  2. Open a pull request into dev.
  3. Changes are merged from dev into main through a pull request when they are ready for release.

License

This project is licensed under the MIT License. See LICENSE for details.

Metadata

Release files for graphs-esroka 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 graphs-esroka 0.1.0
File Size Uploaded Provenance
graphs_esroka-0.1.0.tar.gz 12.0 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 22.8 kB

Release files / graphs_esroka-0.1.0.tar.gz

Download URL graphs_esroka-0.1.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4624ee752335b0265ce747994668dece3890c13ea7b76627b4db307ec486cc7c
BLAKE2b-256 checksum
How to use checksums
5478083ebc934956689ab37b9aaefaf597171a24e83f1e24fdc2444e89c5bc45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

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

Download URL graphs_esroka-0.1.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
967ef836f8559954b721367a17ba3abf934277f3b65a11dd872f79b1db79133e
BLAKE2b-256 checksum
How to use checksums
32a90cc4faf3d1d835ffaa7ea910fcc0b5244196a5d94d8e5f6e2eda8a49ebb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Security for graphs-esroka 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.

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