Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Head straight for the goal
Expands the frontier vertex with the lowest heuristic h, the straight-line distance to the target, first: fast, but its path is not always the cheapest
Select an algorithm and generate steps to begin visualization
Greedy best-first search is an informed search algorithm: at every step it expands the frontier vertex that looks closest to the goal according to a heuristic h, an estimate of the distance still to go. It ignores the cost already paid to reach a vertex, which makes it fast and single-minded. The heuristic here is the straight-line distance to the goal, the classic example from Russell and Norvig's textbook, where the search crosses Romania from Arad to Bucharest.
The frontier is a priority queue ordered by h and starts with the start vertex. Each step removes the vertex with the lowest h, stops if it is the goal, and otherwise adds its unexplored neighbours to the frontier, remembering where each came from. Because only h decides the order, the search rushes toward the goal and often expands very few vertices. The price is that its path is not always the cheapest: on the Romania map it goes through Fagaras, which looks closer to Bucharest, and pays 450 km while the best route costs 418. A* fixes this by ranking the frontier by g + h instead of h alone.
Greedy best-first search is used when a good answer fast matters more than the best answer: pathfinding in games, robot navigation in open space, and first solutions to puzzles and planning problems. It is the standard example in AI courses of what a heuristic does on its own, and it is compared with uniform cost search (cost only) and A* (cost plus heuristic) to show why A* needs both terms.
Greedy best-first search has the same frontier as uniform cost search and A*. Only the priority changes: h, the estimated distance to the goal, and nothing else.
GreedyBestFirst(start, goal, h):
frontier = priority queue ordered by h
frontier.add(start)
explored = {}
while frontier is not empty:
n = frontier.popLowestH()
if n == goal: return the path to n
explored.add(n)
for each neighbour m of n:
if m not in explored and m not in frontier:
parent[m] = n
frontier.add(m)
return failureNothing in the loop looks at the cost of the route so far, so the first route that reaches the goal is kept, whatever it cost. A good heuristic makes the search fast, because it expands almost only vertices on the way to the goal. A misleading one can send it far astray.
Search from Arad (A) to Bucharest (B) on the road map of Romania this page loads, with h the straight-line distance to Bucharest.
Example graph: Road lengths in km as on Russell and Norvig's map. Straight-line distances to Bucharest in this layout: Sibiu about 233, Fagaras 155, Rimnicu Vilcea 187, Timisoara 317, Zerind 357.
Greedy best-first expanded only 3 cities before it reached the goal, against 12 for uniform cost search, but its route is 32 km longer. That trade of optimality for speed is the point of the example.
Time: O((V + E) log V) · Space: O(V)
With an explored set each vertex is expanded at most once, so the worst case equals uniform cost search, O((V + E) log V) with a binary heap. The difference lies in the typical case: with an accurate heuristic the search expands little more than the vertices on its path. AI textbooks give O(b^m) for the worst case on a search tree, with b the branching factor and m the maximum depth, since a misleading h can lead it down the deepest branch.
Greedy best-first search, uniform cost search and A* are one algorithm with three different priorities.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Uniform cost search | Order by g: the cheapest path without any heuristic, but it explores in every direction. | O((V + E) log V) |
| A* search | Order by g + h: the cheapest path with an admissible h, and far fewer expansions than uniform cost search. | O((V + E) log V) |
| Weighted A* | Order by g + w·h with w > 1: between A* and greedy, a path at most w times the optimum for fewer expansions. | O((V + E) log V) |
| Hill climbing | Keep only the best neighbour and no frontier: faster still, but it can get stuck with nowhere better to go. | O(depth) |
Read the full article: A* Search Algorithm in AI: The Complete Guide
Read the full article: A* Search Algorithm: Step-by-Step Guide + Visualizer
Related algorithms: A* Search Algorithm, Uniform Cost Search (UCS), Breadth-First Search, Dijkstra's Algorithm