Skip to main content

A clean and reusable graph algorithms toolkit

Project description

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 Graph abstraction
  • Object-oriented API
  • Readable and canonical implementations
  • Fully testable and extensible
  • No external dependencies

Algorithms included

  • Shortest Path

    • Dijkstra’s Algorithm
    • Bellman-Ford Algorithm
  • Minimum Spanning Tree

    • Kruskal’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

📐 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 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

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
  • No premature optimization
  • Code clarity over cleverness
  • Easy to rewrite during competitive programming
  • Reusable in real-world systems

🛣️ Roadmap

Planned additions:

  • Prim’s Algorithm
  • Floyd-Warshall Algorithm
  • Topological Sort
  • Strongly Connected Components
  • Benchmarking utilities

🤝 Contributing

Contributions are welcome.

  • Add algorithms
  • Improve tests
  • Improve documentation

Please keep implementations:

  • Clean
  • Readable
  • Well-tested

📜 License

MIT License


👤 Author

Gaurav Khakse

If you use this library for learning or projects, a ⭐ on GitHub is appreciated.

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.1.1.tar.gz (6.7 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.1.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pygraphkit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 56d93b65096c12d3062a2e125df9a5045e4d68cf9f5335185a7aa3944a767e4b
MD5 617a17a850db8a716ea54d808caa2176
BLAKE2b-256 12bf4ab7b4fdbdb532f0b7f17b5d030ecbdc6ef4f2d076248c52cda71afcaeb1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygraphkit-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 104199f22db70ffd067e7f0abbc9aaa8942b0f52e823a35c63fa4e90f330e4b9
MD5 fb2bf08afb03245bc1df5b5145a72fcf
BLAKE2b-256 91c76e36f733348fa1fcbdb4a9f834a9ffa307c64ee783de23b3187f8f775944

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