Edit the graph, pick a start and a goal, then press Play. Each node has a heuristic h, its straight-line distance to the goal on the canvas. Greedy best-first search always expands the node that looks closest to the goal and ignores what the path has cost so far. Drag nodes around to change h.
Not found yetIn the queueExpanding nowExpandedPath found"h" under a node is its distance to the goal. The number on a link is its cost, which greedy search ignores.
Step 0 of 0
Order of expansion
Nodes appear here in the order they leave the queue. The small number beside each one is its h value.
Adjacency table
Every node with its links and their costs, plus its h value. It updates as you edit the graph. Click a row to select that node.
Node
Connected to (cost)
h
Status
The algorithm
The highlighted lines are the ones running in the current step.
Greedy Best-First Search (GBFS) is an informed search algorithm. It uses a heuristic h(n), an estimate of how far each node is from the goal, and always expands the node in its priority queue with the smallest h. It only asks "which node looks closest to the goal?" and never considers how much the path has cost so far. That makes it fast and often direct, but it can be led into dead ends or onto expensive routes.
O((V + E) log V)
Time complexity with a heap on a finite graph. It can be exponential on trees with a poor heuristic
O(V)
Space complexity — the queue and the discovered set can hold every node
Not optimal
The first path it finds is not guaranteed to be the cheapest
Heuristic h(n)
Priority is the estimated distance to the goal, nothing else
How it works
Put the start node in the priority queue and mark it as discovered.
Take out the entry with the smallest h. This is the node that looks closest to the goal.
If it is the goal, stop. Follow the parent links back to the start to read off the path.
Otherwise look at each of its neighbours. Any neighbour that has not been discovered is marked, given this node as its parent, and added to the queue with priority h.
Neighbours that are already discovered are skipped. Costs are never used to update or replace an entry.
Repeat from step 2 until the goal leaves the queue or the queue is empty.
Key properties
The heuristic drives everything: here h is the straight-line distance to the goal on the canvas, so dragging a node changes the search. A better estimate means a better search.
Ignores path cost: two nodes with the same h are treated alike even if one is far more expensive to reach. Try the "Fast but not cheapest" example.
Can chase dead ends: a node close to the goal in a straight line may have no route onward. Try the "Dead-end trap" example.
Complete on finite graphs: with a discovered set it never loops, so it finds a path whenever one exists.
Related algorithms: Uniform Cost Search orders by the cost so far, g(n). Greedy orders by h(n). A* orders by g(n) + h(n) and is optimal when h never overestimates.
Worked example
Same graph as the default example in the Visualizer. Go from A to G. Each node shows h, its straight-line distance to G on the canvas (in units of 40 px, rounded down). Link costs are ignored when choosing.
Default map with h values.
Queue A (h=14).
Expand A. Add B (h=10), C (h=10), D (h=11).
Smallest h is a tie between B and C; B comes first alphabetically. Expand B: add E (h=5). C is already queued.
Smallest h is now E (5). Expand it: add G (h=0).
Smallest h is G (0). It is the goal, so stop.
Path found: A → B → E → G, cost 4 + 5 + 3 = 12. It was found fast, but the cheapest path costs 11 (A → C → B → E → G), because greedy never compares costs.
Where GBFS is used
Game pathfinding
Getting a character across a map quickly when a fast, good-enough path is fine.
Quick navigation
Heading toward a destination first and refining the route later.
Puzzle solving
Sliding puzzles and other problems where "how far from solved" is easy to estimate.
Focused web crawling
Following the links that look most relevant to a topic before all others.
Robot motion
Moving toward a target while steering around obstacles as they appear.
Large search spaces
Cases where exploring everything is too slow and speed matters more than the best answer.
Switch back to the Visualizer tab to build a graph and watch the search chase the goal step by step.