A* Pathfinding Algorithm
Also Known As
A-star
A* search
heuristic pathfinding
TL;DR
Heuristic search algorithm that finds the lowest-cost path using f(n)=g(n)+h(n), widely used in maps and game AI.
Explanation
A* is a best-first graph search algorithm that extends Dijkstra’s algorithm by adding a heuristic function h(n), which estimates the remaining cost to the goal. Nodes are prioritised using f(n) = g(n) + h(n), where g(n) is the known cost from the start. With an admissible and consistent heuristic, A* is both complete and optimal. Its efficiency depends heavily on heuristic quality — a good heuristic drastically reduces explored nodes compared to Dijkstra. Common heuristics include Manhattan distance for grid movement and Euclidean distance for continuous space. In PHP, A* is most applicable in routing systems, game AI, logistics optimisation, and path-based simulations.
Common Misconception
✗ A* is always faster than Dijkstra. In reality, performance depends entirely on the heuristic — with h(n)=0, A* becomes Dijkstra and loses its advantage.
Why It Matters
Poor pathfinding scales badly — A* reduces search space dramatically, enabling real-time routing, AI movement, and logistics optimisation without brute-force traversal.
Common Mistakes
- Using a heuristic that overestimates — breaks optimality guarantees.
- Using arrays instead of a priority queue — leads to O(n) extraction instead of O(log n).
- Revisiting nodes without checking for better gScore — causes unnecessary expansions.
- Not separating g(n) and f(n) — mixing them leads to incorrect path selection.
Avoid When
- Graph has no meaningful heuristic — use Dijkstra instead.
- All edge costs are equal and graph is small — BFS may be simpler.
When To Use
- Finding shortest paths in weighted graphs with spatial structure.
- Game AI movement or map routing with clear distance heuristics.
Code Examples
💡 Note
BFS explores uniformly, while A* prioritises promising paths using a heuristic, reducing explored nodes significantly.
✗ Vulnerable
<?php
// ❌ BFS — ignores edge weights and heuristic guidance
function bfsPath(array $grid, array $start, array $goal): array
{
$queue = [[$start, [$start]]];
$visited = [];
while (!empty($queue)) {
[$node, $path] = array_shift($queue); // O(n)
if ($node === $goal) {
return $path;
}
foreach (neighbours($grid, $node) as $next) {
if (!in_array($next, $visited, true)) {
$visited[] = $next;
$queue[] = [$next, [...$path, $next]];
}
}
}
return [];
}
✓ Fixed
<?php
// ✅ A* with proper priority queue and scoring
function astar(array $grid, array $start, array $goal): array
{
$open = new SplMinHeap();
$gScore = [];
$cameFrom = [];
$key = fn($n) => $n[0] . ',' . $n[1];
$h = fn($n) => abs($goal[0] - $n[0]) + abs($goal[1] - $n[1]);
$startKey = $key($start);
$gScore[$startKey] = 0;
$open->insert([0 + $h($start), $start]);
while (!$open->isEmpty()) {
[, $current] = $open->extract();
if ($current === $goal) {
return reconstructPath($cameFrom, $current);
}
foreach (neighbours($grid, $current) as $next) {
$currentKey = $key($current);
$nextKey = $key($next);
$tentativeG = ($gScore[$currentKey] ?? INF) + cost($current, $next);
if ($tentativeG < ($gScore[$nextKey] ?? INF)) {
$cameFrom[$nextKey] = $current;
$gScore[$nextKey] = $tentativeG;
$fScore = $tentativeG + $h($next);
$open->insert([$fScore, $next]);
}
}
}
return []; // no path
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Edited
27 Mar 2026
Views
64
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 16
Perplexity 15
ChatGPT 5
Ahrefs 3
Google 2
SEMrush 2
Majestic 1
Meta AI 1
Also referenced
How they use it
crawler 44
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: High
⚡ Quick Fix
Use SplMinHeap and Manhattan heuristic: f = g + abs(dx) + abs(dy).
📦 Applies To
cli
web
simulation
🔍 Detection Hints
array_shift($queue) && no heuristic function
Auto-detectable:
✓ Yes
semgrep
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✗ Manual fix
Fix: High
Context: Function
Tests: Update