Interactive A* & Greedy Best-First Search Demo

GATE Relevance: Medium

Prerequisites

🎯 Learning Objectives

  • Visualise how A* and Greedy Best-First Search explore a 2D grid
  • Compare the number of nodes expanded and path optimality between algorithms
  • Observe the effect of different heuristics (Manhattan vs Euclidean) on search behaviour
  • Build intuition for why A* finds optimal paths while Greedy does not always
  • Understand how obstacles and maze structures affect search performance

1. How to Use This Demo

The interactive grid visualisation below lets you watch informed search algorithms find a path from a start cell (top-left, green) to a goal cell (bottom-right, red). Here’s how to use it:

  1. Select an algorithm from the dropdown: A* or Greedy Best-First Search.
  2. Select a heuristic: Manhattan distance (|Δx| + |Δy|) or Euclidean distance (√(Δx² + Δy²)).
  3. Toggle Draw Walls to click/drag on the grid and place obstacles, or use Generate Maze for a random maze.
  4. Click Step to advance one expansion at a time, or Play to auto-advance at the selected speed.
  5. Adjust the Speed slider to control auto-play speed (slow for learning, fast for comparison).
  6. Click Reset to clear the search state (walls are preserved) or Clear All to remove walls too.
  7. Watch the colour coding:
    • Green = Start cell
    • Red = Goal cell
    • Blue = In open list (frontier)
    • Orange = Currently being expanded
    • Grey = Closed (already expanded)
    • Dark = Wall (impassable)
    • Yellow = Final path found

2. Interactive Grid Visualisation

Nodes Expanded
0
Open List Size
0
Path Cost
—
Path Length
—
Time (ms)
—

Initialising grid… (requires JavaScript)

■ Start ■ Goal ■ Open (Frontier) ■ Closed (Explored) ■ Wall ■ Path

3. A* Search — How It Works

A* Search evaluates each node using the evaluation function:

Evaluation Function

f(n) = g(n) + h(n)

  • g(n) = actual cost from the start node to node n (path cost so far)
  • h(n) = heuristic estimate of the cost from node n to the goal
  • f(n) = estimated total cost of the cheapest solution through n

A* always expands the node in the open list with the lowest f(n). This combines the “cheapest path so far” (g) with the “estimated remaining cost” (h), balancing exploration and exploitation.

Key Properties

  • Complete: Yes (if branching factor is finite and step costs ≥ ε > 0)
  • Optimal: Yes, when h(n) is admissible (never overestimates) — and with graph search, when h(n) is also consistent
  • Time & Space: O(bd) in the worst case, but with a good heuristic, dramatically fewer nodes are expanded

On the Grid

In this demo, each cell is a node and each move to an adjacent cell has cost 1 (horizontal/vertical). The heuristic estimates the distance to the goal cell:

  • Manhattan: h(n) = |xn − xgoal| + |yn − ygoal| — admissible and consistent for 4-directional movement
  • Euclidean: h(n) = √((xn − xgoal)² + (yn − ygoal)²) — admissible and consistent, but less “informed” than Manhattan for grid movement

Watch how A* opens fewer nodes with Manhattan (a tighter, more informed heuristic) compared to Euclidean.

4. Greedy Best-First Search — How It Works

Greedy Best-First Search uses only the heuristic for its evaluation:

Evaluation Function

f(n) = h(n)

It always expands the node that appears closest to the goal, ignoring the cost already incurred.

Key Properties

  • Complete: No in general (can get stuck in loops without cycle checking); Yes with repeated-state checking in finite spaces
  • Optimal: No — it may find a path that “looks close” to the goal but is actually longer
  • Time & Space: O(bm) worst case, but a good heuristic can provide dramatic improvement

Comparison Exercise

Try this experiment with the demo:

  1. Generate a maze, then run A* with Manhattan. Note the nodes expanded and path cost.
  2. Reset the search (keep walls), switch to Greedy, and run again.
  3. Compare: Greedy usually expands fewer nodes but may find a longer path. A* expands more nodes but guarantees the shortest path.
💡 Key Insight

A* = “guaranteed optimal, usually efficient”. Greedy = “faster on average, but path may be suboptimal.” The g(n) component in A* is what ensures optimality — it prevents the algorithm from being “seduced” by nodes that are close to the goal but reached via an expensive path.

5. Manhattan vs Euclidean Heuristic

Both heuristics are admissible for 4-directional grid movement, but they lead to different search behaviour:

PropertyManhattanEuclidean
Formula|Δx| + |Δy|√(Δx² + Δy²)
Admissible?Yes (4-dir)Yes (4-dir)
Consistent?YesYes
DominancehM ≥ hE alwaysWeaker estimate
Nodes Expanded (A*)FewerMore
Best for4-directional grids8-directional or continuous

Since Manhattan dominates Euclidean (hM(n) ≥ hE(n) for all n), A* with Manhattan will always expand a subset of the nodes that A* with Euclidean expands. Verify this in the demo!

6. Guided Exercises

📝 Exercise 1: Open Space Comparison

Clear all walls. Run A* (Manhattan), then Greedy (Manhattan) on the empty grid. Compare:

  • Does A* expand more or fewer nodes than Greedy?
  • Are the final paths the same? Why or why not?
  • Predict: On an open grid with no obstacles, should both algorithms find the same path?
📝 Exercise 2: Maze Trap

Generate a maze. Observe whether Greedy gets “trapped” exploring dead ends that appear close to the goal. Then run A* on the same maze and note how g(n) helps it avoid those traps.

📝 Exercise 3: Heuristic Impact

With the same maze, run A* first with Manhattan, then with Euclidean. Record the nodes expanded for each. Verify that Manhattan always results in fewer or equal node expansions.

📝 Exercise 4: Wall Placement Strategy

Manually draw a “U-shape” wall around the goal, forcing a long detour. Observe how Greedy expands many nodes inside the U (drawn toward the goal) while A* more quickly finds the route around.

7. Interpreting the Results

After running multiple experiments, you should observe:

  • A* (Manhattan) consistently finds the shortest path with the fewest expanded nodes among the tested configurations.
  • A* (Euclidean) also finds the optimal path but typically expands a few more nodes since the heuristic is less tight.
  • Greedy (Manhattan or Euclidean) often runs faster (fewer expansions) but the path it finds can be significantly suboptimal, especially in mazes with many dead ends.
  • The stats panel makes the trade-offs quantifiable: optimal path cost vs. computational effort (nodes expanded).
  • These observations directly correspond to the theoretical guarantees proved in the A* Search and Admissibility & Consistency pages.

Summary

  • A* evaluates nodes by f(n) = g(n) + h(n); Greedy uses f(n) = h(n).
  • A* guarantees an optimal path when h is admissible (and consistent for graph search).
  • Greedy is faster on average but provides no optimality guarantee.
  • Manhattan distance dominates Euclidean on 4-directional grids, leading to fewer node expansions.
  • Use this demo to build visual intuition before solving GATE problems on paper.
  • Practice predicting which node expands next before clicking Step — this is the exact skill tested in GATE.

References

  • Russell & Norvig, AIMA, Chapter 3.5 — Informed (Heuristic) Search Strategies.
  • Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). “A Formal Basis for the Heuristic Determination of Minimum Cost Paths.” IEEE Transactions on Systems Science and Cybernetics.