graphkit
graphkit is a clean, reusable Python library providing standard graph algorithms with a unified and intuitive API.
It is designed for:
- Learning and revision of graph algorithms
- Interview and competitive programming preparation
- Real-world projects requiring graph processing
- Avoiding repeated reimplementation of well-known algorithms
✨ Features
- Simple
Graphabstraction - Object-oriented API
- Readable and canonical implementations
- Fully tested with CI
- No external runtime dependencies
Algorithms included
Shortest Path
- Dijkstra’s Algorithm
- Bellman–Ford Algorithm
Minimum Spanning Tree
- Kruskal’s Algorithm
- Prim’s Algorithm
Traversals
- Breadth-First Search (BFS)
- Depth-First Search (DFS)
📦 Installation
pip install pygraphkit
For development:
pip install -e .
🚀 Quick Start
from graphkit import Graph
g = Graph()
g.add_edge(1, 2, 4)
g.add_edge(1, 3, 1)
g.add_edge(3, 2, 2)
print(g.dijkstra(1))
Output
{1: 0, 2: 3, 3: 1}
🧠 Core Concept
Graph Abstraction
All algorithms operate on a single Graph class.
Graph(directed=False)
directed=False→ undirected graphdirected=True→ directed graph
Adding edges
g.add_edge(u, v, weight)
- Default weight is
1 - For undirected graphs, edges are added both ways
Removing edges
Edges can be removed dynamically using remove_edge.
g.remove_edge(u, v)
Remove a specific weighted edge
g.remove_edge(u, v, weight)
Behavior
- For undirected graphs, both directions are removed
- For directed graphs, only
u → vis removed - If the edge does not exist, the operation is a no-op
📐 API Reference
Dijkstra’s Algorithm
Finds shortest paths from a source node (non-negative weights).
g.dijkstra(source)
Returns:
{node: shortest_distance}
Bellman–Ford Algorithm
Supports negative edge weights and detects negative cycles.
g.bellman_ford(source)
Raises:
ValueError: Negative cycle detected
Kruskal’s Algorithm
Computes the Minimum Spanning Tree (undirected graphs only).
mst, total_weight = g.kruskal()
Returns:
mst: list of edges(u, v, w)total_weight: sum of MST edge weights
Prim’s Algorithm
Computes the Minimum Spanning Tree starting from a given node.
mst, total_weight = g.prim(start)
- Works on undirected graphs
- Uses a greedy priority-queue approach
Topological Sort
Returns a topological ordering of a directed acyclic graph (DAG).
order = g.topological_sort()
- Works only on directed graphs
- Raises ValueError if the graph contains a cycle
Floyd–Warshall Algorithm
Computes all-pairs shortest paths.
dist = g.floyd_warshall()
- Supports negative weights
- Raises ValueError if a negative cycle exists
- Returns a distance matrix as a nested dictionary
Strongly Connected Components (SCC)
Computes strongly connected components of a directed graph.
components = g.strongly_connected_components()
- Works only on directed graphs
- Uses kosaraju's algorithm
- Returns a list of node groups
Breadth-First Search (BFS)
g.bfs(source)
Returns traversal order as a list.
Depth-First Search (DFS)
g.dfs(source)
Returns traversal order as a list.
Maximum Flow (Dinic)
Computes the maximum flow in a directed flow network.
from graphkit.flow import FlowGraph
g = FlowGraph()
g.add_edge(0, 1, 3)
g.add_edge(1, 2, 2)
max_flow = g.max_flow(0, 2)
- Uses Dinic's algorithm
- Automatically manages residual edges
- Runs efficiently on large graphs
Minimum Cut (from Max Flow)
Returns the minimum cut after computing maximum flow.
from graphkit.flow import FlowGraph
g = FlowGraph()
g.add_edge(0, 1, 3)
g.add_edge(1, 2, 2)
max_flow, (S, T) = g.max_flow_with_min_cut(0, 2)
- Uses residual graph from Dinic's algorithm
- Returns (S, T) partition of vertices
- S contains the source
🧪 Testing
graphkit uses pytest for testing all core algorithms.
The test suite covers:
- Shortest path correctness
- Negative edge weights
- Negative cycle detection
- Disconnected graphs
- Error handling for invalid usage
Run tests locally:
pip install -e .
pytest -v
All tests must pass before a release is published.
📁 Project Structure
graphkit/
├── graphkit/
│ ├── graph.py
│ ├── algorithms/
│ ├── utils/
│ └── __init__.py
│
├── tests/
│ └── test_*.py
│
├── README.md
├── pyproject.toml
└── LICENSE
🎯 Design Philosophy
- One canonical implementation per algorithm
- Code clarity over cleverness
- No premature optimization
- Easy to rewrite during competitive programming
- Reusable in real-world systems
🛣️ Roadmap
Planned additions:
- Floyd–Warshall Algorithm
- Topological Sort
- Strongly Connected Components (Kosaraju / Tarjan)
- Maximum Flow algorithms (Edmonds–Karp, Dinic)
- Benchmarking utilities
🤝 Contributing
Contributions are welcome.
You can help by:
- Adding algorithms
- Improving test coverage
- Enhancing documentation
Please keep implementations:
- Clean
- Readable
- Well-tested
📜 License
MIT License
📘 Documentation
Metadata
Release files for pygraphkit 1.0.1
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 | Actions |
|---|---|---|---|---|
| pygraphkit-1.0.1.tar.gz | 12.9 kB | Without provenance | Details for pygraphkit-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygraphkit-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for pygraphkit-1.0.1-py3-none-any.whl |
Total release size: 25.0 kB
Release files / pygraphkit-1.0.1.tar.gz
| Download URL | pygraphkit-1.0.1.tar.gz |
|---|---|
| Size | 12.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
56170f011e736cebd715a8ecda6e1cb0b1da24caf895c6f08e0428d7cca525b9
|
|
BLAKE2b-256 checksum How to use checksums |
cc6594881b093421106550fc00b8349ff78f8c4f70e182bdd59af31a3a7bd0f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Release files / pygraphkit-1.0.1-py3-none-any.whl
| Download URL | pygraphkit-1.0.1-py3-none-any.whl |
|---|---|
| Size | 12.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c5b30318b354e271f2de9f61a410e701808275c86b1646beea47cf322d9ba134
|
|
BLAKE2b-256 checksum How to use checksums |
c40a64bf56c0815272c94ce1889db1883a797108cb9cb443efadfff60872b209
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Security for pygraphkit 1.0.1
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.