Greedy Best-First Search Visualization with Heuristics

Greedy best-first search expands the node with the smallest heuristic h(n), the estimated distance to the goal, ignoring the cost already paid. It is often fast but can be led down expensive routes.

About this visualization

Greedy best-first search expands the node that looks closest to the goal according to a heuristic h(n), ignoring the cost already paid. It is often very fast but can follow an expensive route. Set your own heuristic values and compare the result with A*.

How it works

  1. Give every node a heuristic h(n) estimating its distance to the goal.
  2. Expand the frontier node with the smallest h.
  3. Because it ignores g, the path it finds can cost more than the best path.

Formulas used

  • f(n)=h(n)f(n) = h(n)

Worked example

On the Romania map with straight-line distances to Bucharest as the heuristic, greedy goes Arad → Sibiu (h = 253) → Fagaras (176) → Bucharest, cost 450, expanding only 3 nodes. The optimal route costs 418, so greedy is quick but not optimal.

Enter these numbers in the simulator above to see every step.

Complexity and properties

Worst caseO(bm)O(b^{m})
SpaceO(bm)O(b^{m})

Not optimal. Complete in a finite graph when it remembers explored nodes (as here).

When to use it

  • Quick, approximate path-finding when the heuristic is good.
  • Understanding why a heuristic alone is not enough for optimality.
  • AI course assignments comparing search strategies.

Frequently asked questions

Is greedy best-first search optimal?

No. It ignores the cost so far, so it can return a path that costs more than the cheapest one.

Is greedy best-first search complete?

Not in general; with an explored set in a finite graph it will find a path if one exists.

How is greedy search different from A*?

Greedy uses f(n) = h(n); A* uses f(n) = g(n) + h(n), adding the cost already paid, which makes A* optimal with an admissible heuristic.