◈ MAZE SIGNAL

FIELD NOTE 01 · REPRODUCIBLE SEARCH

Same cost.
Less searching?

A* expands 23 cells where Dijkstra expands 140 on one open grid. On a different map, both expand nine. The map and the heuristic explain the difference.

1. First choose the right objective.

Our search minimizes the cost to enter cells. Moves are up, down, left or right. Ordinary cells and the goal cost one, terrain costs 1–9, and the starting cell costs zero.

3 5
S999G
.###.
.....

The direct route takes four moves but costs 9 + 9 + 9 + 1 = 28. The route around the walls takes eight moves and costs eight. Both algorithms choose the detour. A shortest-in-moves algorithm would answer a different question.

2. A* adds a lower bound.

Dijkstra prioritizes the known cost g. A* prioritizes g + h. Here h is Manhattan distance to the goal: |row − goal row| + |column − goal column|.

Each legal move changes this distance by at most one and costs at least one. Therefore h never overestimates the remaining cost and is consistent: h(u) ≤ cost(u,v) + h(v). Walls and expensive terrain can make the real route longer or costlier than that bound.

3. Reproduce the comparison.

In the demo choose Open grid, choose Instant, then Run comparison. It is a 10 × 14 map with S in the upper left and G in the lower right. Both minimum-cost routes take 9 + 13 = 22 moves.

ExampleCost · bothDijkstra expandedA* expanded
Weighted detour · 3 × 5899
Open grid · 10 × 142214023
No route · 3 × 5No route66

On the open grid, the Manhattan bound equals the true remaining cost. Many nodes tie on g + h; this implementation breaks ties by larger g, then smaller row-major index, favoring progress along a route. A different tie rule can expand more cells. Expanded includes the popped goal.

The detour illustrates the limit: Manhattan distance initially ignores the walls and weights, and both searches expand nine cells. With no route, both exhaust the same six reachable cells. A* is not guaranteed to save work on every map.

4. Do not turn this into a speed claim.

140 / 23 ≈ 6.09 times fewer expanded cells on this map. This is not a 6.09× runtime speedup.

Heuristic evaluation, heap operations, language, hardware and rendering have costs. Animation speed only controls presentation. The Python/C timing experiments are separate and have their own workloads and environment metadata.

These counts use the committed JavaScript solver and fixtures from the v1.1.0 source release. They were also checked against Python/C. No user ratings are involved.

5. Check the evidence yourself.

node docs/verify.cjs

This checks both algorithms on 505 committed maps: five fixed cases and 500 maps from seed 357. For Python/C regeneration, follow the verification instructions. Read the versioned solver and separate Python/C measurements.

Further reading: the NetworkX A* documentation explains the heuristic admissibility requirement. This project uses its own implementation.

فارسی — همین مثال چه می‌گوید؟

در نقشهٔ وزن‌دار، مسیر چهارحرکتی هزینهٔ ۲۸ دارد؛ مسیر هشت‌حرکتی هزینهٔ ۸ دارد. هدف ما کمترین هزینه است. در شبکهٔ باز ۱۰×۱۴، Dijkstra تعداد ۱۴۰ و A* تعداد ۲۳ خانه را گسترش می‌دهند و هر دو هزینهٔ ۲۲ دارند.

A* فاصلهٔ منهتن را به هزینهٔ طی‌شده اضافه می‌کند. این تخمین در شبکهٔ باز دقیق است ولی دیوارها و وزن‌ها را نمی‌بیند. در نمونهٔ وزن‌دار هر دو ۹ خانه را بررسی می‌کنند؛ پس کاهش جست‌وجو همیشگی نیست. تعداد خانه کمتر نیز الزاماً زمان کمتر نیست. از مثال Open grid و سرعت Instant برای بازتولید استفاده کن.