🧭 A* Pathfinding Algorithm

Informed search, heuristics, and grid-based pathfinding

Overview

A generic A* search implementation with multiple heuristics, grid-based pathfinding with obstacles, and ASCII visualization. A* combines Dijkstra's completeness with greedy best-first speed using an admissible heuristic.

Concepts Demonstrated

Key Functions

FunctionDescription
PQ.createCreate a min-heap priority queue
PQ.push / PQ.popInsert and extract-min operations
astarGeneric A* search over any graph
grid_astarGrid-specific A* with obstacle support
manhattan / euclidean / chebyshevDistance heuristic functions
visualize_gridASCII rendering of grid, path, and explored cells

How A* Works

(* A* maintains two costs per node:
   g(n) = actual cost from start to n
   h(n) = heuristic estimate from n to goal
   f(n) = g(n) + h(n) — nodes expanded in f-order *)

(* 1. Push start with f = h(start, goal) *)
(* 2. Pop node with lowest f from priority queue *)
(* 3. If it's the goal, reconstruct path *)
(* 4. Expand neighbors: if new g < known g, update *)
(* 5. Repeat until goal found or queue empty *)

let path = grid_astar grid start goal Manhattan in
visualize_grid grid path

Heuristics Comparison

HeuristicFormulaBest For
Manhattan|dx| + |dy|4-directional grids
Euclidean√(dx² + dy²)Any-angle movement
Chebyshevmax(|dx|, |dy|)8-directional grids