learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Greedy Best-First Search Visualizer

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

Time: O((V + E) log V)
Space: O(V)
Use Case: Game pathfinding, robot navigation, fast first solutions
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Greedy Best-First Search

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.

How it works

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.

Applications

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.

Pseudocode

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 failure

Nothing 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.

Worked example, step by step

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.

  1. Expand Arad. Its neighbours Zerind (h 357), Sibiu (233) and Timisoara (317) join the frontier. Sibiu looks closest.
  2. Expand Sibiu. Fagaras (155), Rimnicu Vilcea (187) and Oradea (364) join. Fagaras looks closest, although the road through it is longer.
  3. Expand Fagaras. Bucharest joins the frontier with h = 0, so it is expanded next and the search ends.
  4. Compare. The route Arad, Sibiu, Fagaras, Bucharest costs 140 + 99 + 211 = 450 km. Uniform cost search and A* find 418 km through Rimnicu Vilcea and Pitesti.

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.

Complexity, and where it comes from

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.

When to use Greedy Best-First Search, and when not to

Greedy best-first search, uniform cost search and A* are one algorithm with three different priorities.

AlternativePrefer it whenCost
Uniform cost searchOrder by g: the cheapest path without any heuristic, but it explores in every direction.O((V + E) log V)
A* searchOrder 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 climbingKeep only the best neighbour and no frontier: faster still, but it can get stuck with nowhere better to go.O(depth)

Common pitfalls

  • Expecting the shortest path. Greedy best-first returns the first route that reaches the goal, not the cheapest one. Use A* when the cost matters.
  • Dead ends. A heuristic that points into a dead end, such as a lake between the start and the goal, leads the search into it. With an explored set it backs out eventually, but only after expanding the dead end.
  • No explored set. As a tree search, without an explored set, greedy best-first can loop forever between two vertices that each look closer to the goal than anything else on offer.
  • Mixing up the scales. Greedy best-first only compares h values, so their scale does not matter. A* adds h to g, so for A* the heuristic must be in the same units as the costs and must never overestimate.

Frequently asked questions

What is greedy best-first search?
An informed search algorithm that always expands the frontier node with the lowest heuristic value h, the estimated distance to the goal, ignoring the cost already paid.
Is greedy best-first search optimal?
No. It can return a path that is not the cheapest, as on the Romania map, where it finds 450 km instead of 418 km. It is complete on finite graphs when it keeps an explored set.
What is the difference between greedy best-first search and A*?
Greedy best-first orders the frontier by h alone, A* by g + h. Adding the cost so far is what makes A* optimal with an admissible heuristic.
What heuristic does greedy best-first search use?
Any estimate of the remaining distance. On maps the standard choice is the straight-line distance to the goal, used on this page.
When should I use greedy best-first search?
When a reasonable answer quickly is worth more than the best answer, for example in games, or for a first solution that another method improves later.

Related algorithms: A* Search Algorithm, Uniform Cost Search (UCS), Breadth-First Search, Dijkstra's Algorithm

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 20
Edges: 23