Graphworks
A Python module for efficient graph theoretic programming
Quick Start
pip install graphworks
from graphworks import Graph, to_text
from graphworks.algorithms import find_path, is_connected
graph = Graph("roads").add_edge("A", "B", weight=2).add_edge("B", "C", label="toll")
print(graph.neighbors("B")) # ['A', 'C'] — undirected edges work both ways
print(find_path(graph, "C", "A")) # ['C', 'B', 'A']
print(is_connected(graph)) # True
print(to_text(graph))
Graphs can also be loaded from JSON (Graph(input_file=...) / Graph(input_graph=...)); a
neighbor may be a bare name or an object carrying edge data:
{
"label": "roads",
"directed": false,
"weighted": true,
"vertices": { "A": { "label": "Alpha" } },
"graph": { "A": ["B", { "target": "C", "weight": 2.5, "label": "toll" }], "B": [], "C": [] }
}
save_to_json writes this same format, so weights, labels, and attributes round-trip.
API conventions
Graphis the only class; algorithms (graphworks.algorithms) and exporters (graphworks.export) are free functions that take aGraph.- A vertex name that is not in the graph raises
KeyError(graph[v],neighbors, and every algorithm). Thedict.get-stylegraph.vertex(name)andgraph.edge(u, v)returnNone. - Predicates are vacuously true on an empty graph; metrics that are undefined there
(
min_degree,max_degree,diameter) raiseValueError. - Algorithms whose precondition fails raise
ValueErrorrather than returning a wrong answer (topological_sorton a cyclic graph,find_circuiton a non-Eulerian graph,diameteron a disconnected graph). - Degree counts every incident edge (in + out for directed graphs; a self-loop counts twice).
Optional extras:
pip install graphworks[matrix] # numpy adjacency matrix support
pip install graphworks[viz] # graphviz export
pip install graphworks[docs] # generate documentation
Development
Requirements
- Python 3.13+
- uv (>= 0.10.12)
Setup
uv sync --extra all
Running Tests
# Run all tests (includes coverage; fails under 90%)
uv run pytest
# Run a single test file
uv run pytest tests/test_graph.py
# Run a single test by name
uv run pytest tests/test_graph.py -k "test_method_name"
Linting and Formatting
# Lint
uv run ruff check --fix src/ tests/
# Format
uv run black src/ tests/
uv run isort src/ tests/
# Type checking
uv run ty check
# Code complexity
uv run xenon --max-average=A --max-modules=B --max-absolute=B src/
# Run all pre-commit hooks
prek run --all-files
Publishing
Version is managed automatically via git tags using hatchling-vcs.
- Tag a commit:
git tag -a X.Y.Z -m 'release message' - Push the tag:
git push --tags - The GitHub Actions workflow will build and publish to PyPI automatically.
Roadmap
Tier 1: Data model
-
Vertexclass with name, optional label, and immutable attribute mapping -
Edgedataclass with source, target, directed flag, optional weight, label, and attributes - Both classes are frozen (immutable) with identity-based equality and hashing
Tier 2: Graph refactor
- Internal storage uses
dict[str, Vertex]for vertex lookup - Adjacency structure uses
dict[str, dict[str, Edge]]for O(1) edge access - Edge weights, labels, and attributes survive union, complement (vertices), and JSON/DOT export
-
add_edgesupportsweightandlabelkeyword arguments -
edge()lookup method for direct O(1) edge retrieval
Tier 3: Lossless conversions
-
to_adjacency_matrix()/from_adjacency_matrix()round-trips that preserve vertex names via an index mapping -
complementpreserves original vertex names and thedirectedflag -
to_edge_list()/from_edge_list()conversions - Weighted JSON format (
{"target": ..., "weight": ...}neighbor objects, seeg4.json)
Tier 4: Algorithms
- Dijkstra's shortest path (weighted)
- Prim's / Kruskal's minimum spanning tree
- Strongly connected components (Tarjan or Kosaraju)
- Improved shortest-path implementations leveraging weighted edges
Tier 5: Export and CLI
- JSON export (
save_to_json, lossless round-trip) - Graphviz DOT export (
save_to_dot, with vertex/edge labels and weights) - Text tree and 2-D ASCII rendering (
to_text,to_ascii) - Rich-based demo script (
uv run demo) - CLI application for common graph operations
- Rich rendering integration for interactive graph display
Tier 6: Cross-cutting quality
- Thread safety (immutable graph views or locking around mutations)
- Input validation hardening
- Performance benchmarks
Metadata
Release files for graphworks 0.10.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| graphworks-0.10.0.tar.gz | 34.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| graphworks-0.10.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 77.7 kB
Release files / graphworks-0.10.0.tar.gz
| Download URL | graphworks-0.10.0.tar.gz |
|---|---|
| Size | 34.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
34904a6037230ac9cc1cbdcb95b6d6d938ccfa1ab447d007128f76b6757ad294
|
|
BLAKE2b-256 checksum How to use checksums |
ddd485f58b99de0878283fb0ba0f23cdb14abe06789f3776a4b34ddcdb068f6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.
Transparency logRelease files / graphworks-0.10.0-py3-none-any.whl
| Download URL | graphworks-0.10.0-py3-none-any.whl |
|---|---|
| Size | 43.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea7eb223600fc8f4e377784093df260076b4912b3745cb6e99c0c9ff1bf122d1
|
|
BLAKE2b-256 checksum How to use checksums |
4609605f586968c9ca49e1e2912f5a9a4d89e0182150cef90da7697efd6cb64d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.
Transparency log