Skip to main content

A professional, extensible search algorithm framework for discrete spaces (graphs and grids). Implements A* with pluggable heuristics.

Project description

search-library

CI Python 3.11+ License: MIT Ruff MyPy Coverage

A professional, extensible search algorithm framework for discrete spaces (graphs and 2D grids).

Features

  • A* Search Algorithm — optimal pathfinding with f(n) = g(n) + h(n)
  • Graph support — weighted directed/undirected graphs with adjacency lists
  • Grid support — 2D pathfinding with obstacles, variable costs, 4/8-directional movement
  • Pluggable heuristics — Manhattan, Euclidean, or bring your own
  • Extensible architecture — designed for adding BFS, DFS, Dijkstra without modifying base code
  • Strict typing — full mypy --strict compliance with Generics and Protocols
  • Zero runtime dependencies — pure Python standard library only

Installation

pip install search-library

Or with uv:

uv add search-library

Quick Start

Graph Search

from search_library import Graph, AStarSearch

# Create an undirected weighted graph
graph = Graph[str](directed=False)
graph.add_edge("A", "B", 1.0)
graph.add_edge("B", "C", 2.0)
graph.add_edge("A", "C", 5.0)

# Solve with A*
problem = graph.to_search_problem("A", "C")
solver = AStarSearch(problem)
result = solver.search()

print(result.path)           # ['A', 'B', 'C']
print(result.total_cost)     # 3.0
print(result.nodes_explored) # 3

Grid Pathfinding (Maze)

from search_library import Grid, AStarSearch
from search_library.grid import GridSearchProblem

# 0 = walkable, 1 = obstacle
matrix = [
    [0, 0, 0, 0, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 1, 0],
    [0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0],
]

grid = Grid.from_matrix(matrix)
problem = GridSearchProblem(grid, start=(0, 0), goal=(4, 4))
solver = AStarSearch(problem)
result = solver.search()

print(result.success)         # True
print(result.total_cost)      # 8.0
print(result.nodes_explored)  # Number of states explored

Custom Heuristic

from search_library.heuristics.base import Heuristic

class ChebyshevHeuristic(Heuristic[tuple[int, int]]):
    """Chebyshev distance — useful for 8-directional grids."""
    def estimate(self, state: tuple[int, int], goal: tuple[int, int]) -> float:
        return float(max(abs(state[0] - goal[0]), abs(state[1] - goal[1])))

Custom Search Problem

from search_library.core.problem import SearchProblem
from search_library.algorithms.astar import AStarSearch

class EightPuzzle(SearchProblem[tuple[int, ...]]):
    """Example: define any discrete search problem."""
    def initial_state(self) -> tuple[int, ...]:
        return (1, 2, 3, 4, 0, 5, 6, 7, 8)

    def is_goal(self, state: tuple[int, ...]) -> bool:
        return state == (1, 2, 3, 4, 5, 6, 7, 8, 0)

    def successors(self, state: tuple[int, ...]) -> list[tuple[tuple[int, ...], float]]:
        # Return list of (next_state, cost) tuples
        ...

Architecture

src/search_library/
├── core/           # Node, State (Protocol), SearchProblem (ABC), SearchResult
├── algorithms/     # A* implementation (extensible for BFS, DFS, Dijkstra)
├── heuristics/     # Heuristic ABC + Manhattan + Euclidean
├── graph/          # Graph + Edge + GraphSearchProblem adapter
├── grid/           # Grid + GridSearchProblem adapter (4/8 directions)
├── utils/          # Formatting helpers
└── exceptions/     # SearchError hierarchy

Design Principles

  • SOLID — each class has a single responsibility; open for extension, closed for modification
  • Strategy Pattern — heuristics are interchangeable at runtime
  • Adapter Pattern — Graph and Grid adapt to the unified SearchProblem interface
  • Generic Types — algorithms work with any hashable state type

Development

# Install all dependencies (including dev tools)
uv sync

# Run tests with coverage
uv run pytest

# Lint
uv run ruff check src/ tests/

# Format
uv run ruff format src/ tests/

# Type check (strict mode)
uv run mypy src/

# Build wheel + sdist
uv build

CI/CD

Pipeline Trigger What it does
CI push, PR Ruff + MyPy + Pytest (matrix: 3.11, 3.12, 3.13, 3.14)
CD push to main CI + Semantic Release + PyPI publish

Versioning follows Conventional Commits:

  • feat: → minor version bump
  • fix: → patch version bump
  • feat!: / BREAKING CHANGE: → major version bump

Roadmap

  • A* Search Algorithm
  • Manhattan & Euclidean heuristics
  • Graph support (directed/undirected, weighted)
  • Grid support (4/8 directions, obstacles)
  • BFS (Breadth-First Search)
  • DFS (Depth-First Search)
  • Dijkstra's Algorithm
  • Bidirectional Search
  • Visualization utilities

License

MIT — free for academic and commercial use.

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

search_library-0.1.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

search_library-0.1.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file search_library-0.1.0.tar.gz.

File metadata

  • Download URL: search_library-0.1.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for search_library-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5493717ded3475b7c1f58f50bdd235c27772c1ccb9b35fb7dfaa415c4dd72de0
MD5 651d6c9750125ab6063c7e4f732195f3
BLAKE2b-256 edaa8e4eed1ec50e08c443077c2c7b2a8f4db92e81813221de3048d6286b3f94

See more details on using hashes here.

Provenance

The following attestation bundles were made for search_library-0.1.0.tar.gz:

Publisher: cd.yml on ahincho/search-library

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file search_library-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: search_library-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for search_library-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2531ee9d9319ddf8069ea657d770a2293b3a34c53f647506bcf5821d0dc1db51
MD5 40cdacad2f3d831509b69eb525403663
BLAKE2b-256 3e180f25b1f0c50a950e0a79b67ea42683e024fcac360aa645d47c98930d2a41

See more details on using hashes here.

Provenance

The following attestation bundles were made for search_library-0.1.0-py3-none-any.whl:

Publisher: cd.yml on ahincho/search-library

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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