Home/Pathfinding/Greedy best-first
Search algorithmsCTRL K

Greedy best-first

· heuristic only · no cost so far
/pathfinding/greedy-best-first
EXPANDED
0
closed cells
OPEN SET
1
candidates ranked by h
PUSHES
0
cells added to the open set
PATH
—
length once the goal is reached
WALLS
28%
of 880 cells blocked
closedopencurrentpathstart / goal
44×20 · 28% walls · seed 7 · 267 steps
step 0 / 266
Walls28
greedy_best_first.ts
1
function greedyBestFirst(grid: Grid, start: Cell, goal: Cell) {
2
  const open = new MinHeap<Cell>([start]);
3
  const seen = new Set([start]), parent = new Map();
4
  while (open.size > 0) {
5
    const current = open.pop(); // lowest heuristic, cost so far ignored
6
    if (current === goal) return rebuild(parent, current);
7
    for (const neighbor of neighbors(grid, current)) {
8
      if (seen.has(neighbor)) continue;
9
      seen.add(neighbor);
10
      parent.set(neighbor, current);
11
      open.push(neighbor, heuristic(neighbor, goal));
12
    }
13
  }
14
  return null;
15
}
CURRENT STEP
line 2

Start at (12, 1), goal at (5, 42). The open set holds only the start.

// how it works

Understanding Greedy best-first

greedy best-first · always toward the goal, whatever it costs
01

The idea

Greedy best-first search ranks the open cells by the heuristic alone, the estimated distance to the goal, and ignores how far each one is from the start. It therefore runs straight at the goal, expanding very few cells when the way is clear.

The price is the path. Because the cost so far is never considered, a cell reached by a long detour is as good as one reached directly, and the path it returns can be much longer than the shortest. It is A* with g dropped: fast, memory-light, and not optimal.

02

The four stages

1
popthe open cell closest to the goal by h
2
checkis it the goal? then rebuild
3
pushunvisited neighbours, ranked by h alone
4
pathfollow parents back to the start
03

Complexity

best
O(d)a clear line to the goal
average
O(E log V)usually far fewer cells than A*
worst
O(E log V)a dead-end pocket facing the goal
space
O(V)open set and parents
04

Versus its siblings

CELLS EXPANDED · 44×20 MAZE
bfs
612
dijkstra
604
bidirectional
310
a*
188
greedy
96
jps
41
greedy expanded the least and found a path 23% longer than the shortest
05

Pseudocode

GREEDY(start, goal)
  open ← {start}; seen ← {start}
  while open not empty
    current ← the open cell with the lowest h(current, goal)
    if current = goal: return path via parent
    for each unseen neighbour of current: mark seen; parent[neighbour] ← current; add to open
06

When to use

Any path will do and speed matters: crowd simulation, rough routing before a refinement pass.
Open terrain with few obstacles, where the heuristic is nearly exact.
07

Pitfalls

Not optimal: a U-shaped wall in front of the goal makes it explore the whole pocket and return a long path.
It does not revisit: a cell reached the long way first keeps that parent forever.
Weighting the heuristic in A* (f = g + w·h) gives a tunable middle ground; w → ∞ is greedy.
08

History

Best-first search with a heuristic goes back to Judea Pearl's 1984 book Heuristics, where greedy best-first is the simplest member of the family and A* the one with the optimality proof. Game programmers rediscover it every time a map is open enough that the detours do not show.

Back to Pathfinding