Skip to main content

A clean and reusable graph algorithms toolkit

Project description

graphkit

PyPI Python Tests

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 Graph abstraction
  • 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 graph
  • directed=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 → v is 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.


🧪 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygraphkit-0.6.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygraphkit-0.6.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pygraphkit-0.6.0.tar.gz.

File metadata

  • Download URL: pygraphkit-0.6.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pygraphkit-0.6.0.tar.gz
Algorithm Hash digest
SHA256 fb5b7cb10ad2c7d675a2d561cfb7659b2eb7dcfede742e30189d4448a30a3717
MD5 257bec64c83d3df0a9f571cb2256ff78
BLAKE2b-256 63d11d42fd1c2c201b35caa9083adedfe7e3464acc883417e09b6782f756f084

See more details on using hashes here.

File details

Details for the file pygraphkit-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: pygraphkit-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pygraphkit-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12c5af7be2abd2f97adfcaa0d34edf006fdfa6a274fb896827cc8f4fb859af90
MD5 f923cc27b7f37f7cd1fec8dd1aab3d45
BLAKE2b-256 9e5c15a5551d902a3f5bcff2696b3915b30fb32fba94b593a897cb53f5b83bb3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page