Greedy Best-First Search Theory Guide
Try the Greedy Best-First Search Solver →Picture this: you are driving through an unfamiliar city and you can see a tall skyscraper in the distance — that is your destination. Instead of checking the map, you just keep turning down whichever street points most directly at the building. Toll road? Take it. Dead end? Back up and try the next closest street. Traffic jam? Irrelevant — it still looks close. That is Greedy Best-First Search in a nutshell: pure tunnel vision on the destination, zero awareness of the cost of getting there.
- It makes every decision using exactly one number: At every node, Greedy Search looks only at — the estimated distance to the goal. No history, no accumulated cost, no context. It just asks 'which neighbour looks closest to the finish line?' and sprints toward it.
- It has complete amnesia for the past: Greedy Search does not track how expensive the journey has been. It has no term keeping score — if it just crossed a brutally expensive edge to get here, that cost is already forgotten. The past is completely irrelevant.
- It is fast, reckless, and dangerously easy to trap: Greedy Search is genuinely quick on simple graphs, but on any graph with a detour or dead end, it will confidently take the bait and return a path far more expensive than optimal. It is not guaranteed to find the shortest path.
On an exam, Greedy Search is almost always tested on a graph deliberately designed to make it fail — so you can see exactly where it goes wrong and understand why A* was invented to fix it.
How to Trace Greedy Search by Hand
Initialize your two lists before touching anything else. Draw two columns on your exam paper and label them `openList` and `closedList`. Place the start node in the `openList` and write its value next to it — read this directly from the heuristic table provided in the question. There is no calculation here. Every value in Greedy Search is a static number handed to you on the exam paper.
Select the node with the lowest from the `openList`. Scan every node currently sitting in the `openList` and pick the one with the smallest value. That is the node Greedy Search believes is closest to the goal. If two nodes share the exact same , break the tie alphabetically — pick whichever comes first in the alphabet to match standard grading keys.
Move the selected node to the `closedList` and expand its neighbours. Cross it off the `openList` and write it into the `closedList` — it is permanently locked and will never be revisited. Now look at every direct neighbour of this node. For each one, simply look up its value from the heuristic table. No addition, no multiplication — just a table lookup.
Process each neighbour using three simple rules — and notice the massive shortcut. If a neighbour is already in the `closedList`, skip it completely. If a neighbour is brand new, add it to the `openList` with its value. If a neighbour is already in the `openList`, do absolutely nothing — this is the key difference from A* and UCS. Since is a fixed property of the node itself and not affected by how you arrived there, there is never anything to update.
Keep going until the goal node is selected from the `openList` — not just discovered. Repeat the process row by row: select the lowest , lock it, expand its neighbours. The classic exam trap is stopping the moment the goal node appears as a neighbour in the `openList`. That is only discovery. The algorithm only terminates when the goal node is formally selected as the lowest entry and moved to the `closedList`.
The Greedy Evaluation Function
Breaking Down the Formula & The Fatal Flaw
- — The Decider: Just like A*, Greedy Search always selects the node with the lowest from the `openList`. This is the single number that drives every decision. The critical difference: in Greedy Search, is not a sum of two values — it is a direct, unfiltered mirror of the heuristic.
- — The Only Number That Matters: This is the estimated remaining distance from the current node to the goal — a pure prediction, like a straight-line crow-flies distance on a map. In Greedy Search, this single estimate holds 100% of the decision-making power. There is nothing else in the formula to balance it out.
- The Missing — The Fatal Flaw: The actual cost traveled so far, , is completely absent. Greedy Search is literally blind to edge weights — a path that cost 1,000 to reach and a path that cost 1 look absolutely identical to it. As long as both nodes have the same , Greedy Search treats them as equals.
- The Optimality Trap — Write this on your exam: If asked 'why is Greedy Search not optimal?', the answer is always this: it ignores . Because it cannot see accumulated travel cost, it will happily take a massively expensive detour as long as the intermediate nodes have low values and appear close to the goal.
Solved Example: Tracing Greedy Search on Paper
Draw this graph on your paper before reading the steps. Nodes: S (Start), A, B, G (Goal). Edges and their costs: S→A = 10, S→B = 2, A→G = 10, B→G = 3. Heuristic values (given, not calculated): , , , . Write the edge weights on your graph — you are about to ignore them completely, but having them visible will make the final reveal hit harder. Draw two columns and label them `openList` and `closedList`.
Step 1: Initialize the Start Node S
Place S in the `openList` using only its heuristic value. Read directly from the table and write S(6) in the `openList`. No addition, no edge weights, no calculation. The `closedList` is empty. This initialization step is always worth marks on its own — never skip it.
Step 2: Pop S — Expand Neighbours A and B
S has the only score in the `openList`, so it is selected automatically. Move S to the `closedList`. Look at S's direct neighbours: A and B. Add them to the `openList` using only their heuristic values — A(2) and B(4). Do not look at the edge weights of 10 and 2. Greedy Search does not see them. Your `openList` now holds A(2) and B(4).
Step 3: Pop A — Taking the Bait
Scan the `openList`: A(2) is lower than B(4), so A is selected. Move A to the `closedList`. Expand A's only neighbour: G. Add G to the `openList` with . Here is the trap: Greedy Search just chose the path through A — which sits behind a cost-10 edge — over the path through B, which only costs 2 to reach. It did this purely because looked more promising than . The algorithm is already walking into disaster.
Step 4: Pop G — Terminate and Review the Damage
G has the lowest in the `openList` at 0, so it is selected and moved to the `closedList`. The algorithm terminates — the path found is S → A → G. Now look at the actual edge weights on your graph. S → A → G costs . The path Greedy Search completely ignored, S → B → G, only costs . Greedy Search returned a path that is four times more expensive than optimal. This is exactly the trap professors build into exam graphs — a tempting low heuristic hiding behind a brutal edge weight.
See the Interactive Solver in Action
Now that you know how to trace it by hand, use the solver to watch Greedy Search confidently walk into traps in real time. See the `openList` build using only values, watch edge weights get ignored entirely, and follow the algorithm as it charges toward the goal — straight past the optimal path.
Your Turn to Practice
Trace a full solved exam question by hand, or build your own Greedy Best-First Search question in the interactive solver.
Rules & Common Mistakes
- Exam Trap: Never Update Nodes Already in the Open ListThis is the biggest time-saver in a Greedy Search trace and the clearest sign that you understand the algorithm. If a neighbour is already sitting in the `openList`, ignore it completely and move on. Unlike A* or UCS, there is no to recalculate and no cheaper path to compare. The value is a fixed property of the node itself — it does not change based on how you arrived there. Attempting to update it wastes time and signals to the examiner that you are mixing up algorithms.
- Exam Trap: Do Not Panic When Greedy Search Hits a Dead EndWhen Greedy Search takes the bait and reaches a node with no unvisited neighbours, students often freeze, cross out their work, or try to backtrack manually. Do none of those things. Simply move the dead-end node to the `closedList` as normal and scan the `openList` for the next lowest . The algorithm has not broken — it just hit an expensive corner. The `openList` is still alive, and the trace continues exactly as before from whatever candidate is left.
- Exam Trap: Do Not Stop When the Goal Enters the Open ListSeeing the goal node appear as a neighbour and get added to the `openList` is just discovery — it means nothing about termination. Greedy Search only stops when the goal node is formally selected as the lowest entry in the `openList` and moved to the `closedList`. Until that pop happens, a different node with a lower could still be sitting in the `openList` and would be expanded first. Terminating on discovery is the most common single-point mark loss in a Greedy Search trace.
- Pro Tip: Tied Values — Break Alphabetically and Keep MovingIf two nodes in the `openList` share the exact same score, do not freeze and do not guess randomly. Break the tie alphabetically — select the node whose label comes first in the alphabet. This keeps the trace consistent with standard grading rubrics and makes it straightforward for the examiner to follow the logic. Both choices lead to the same final outcome in terms of path cost, but only one will match the expected answer key.
Strengths, Weaknesses & When To Use It
When to use it:In the real world, Greedy Search gets used when speed matters far more than perfection. Think of basic NPC movement in a video game — the enemy character just needs to start charging toward the player immediately, and a slightly inefficient path is completely acceptable. Pixel-perfect routing would be overkill. On an exam, however, Greedy Search is almost never the right tool for finding a good path. It will be tested specifically so students can watch it fall into a heuristic trap and explain exactly why A* was built to fix the problem.
Advantages
- Blindingly Fast — The Heat-Seeking Missile: Because Greedy Search completely ignores , it never wastes time comparing alternate routes or recalculating accumulated costs. It locks onto the lowest in the `openList` and sprints directly toward the goal. On simple graphs without walls or obstacles, it finds the destination faster than any other informed search algorithm with minimal memory overhead.
- Incredibly Simple to Trace and Implement: The total absence of edge weight arithmetic makes Greedy Search the easiest informed search algorithm to run by hand. There is no to track, no to calculate, and no existing nodes to update. Every decision reduces to one question: which node in the `openList` has the lowest ? Read the table, pick the lowest, and move.
Disadvantages
- Not Optimal — The Fatal Flaw: Because Greedy Search is completely blind to , it will confidently commit to a path costing 1,000 if the intermediate nodes have slightly better values than a path costing 2. Speed and accuracy are in direct conflict here, and Greedy Search always chooses speed. It will return an answer, but there is zero guarantee that answer is the cheapest one.
- Easily Fooled by Obstacles and Not Always Complete: A misleading heuristic or a physical wall in the graph can drag Greedy Search deep into a dead end just because those nodes appear geographically close to the goal. Since it refuses to reconsider previous decisions, it has no way to recover intelligently — it just keeps chasing the lowest until it runs out of options. Without a `closedList` to catch already-visited nodes, it can even get trapped in an infinite loop cycling between the same nodes forever.
Greedy Search vs. A* Search
Greedy Search and A* both use a heuristic to steer the search toward the goal — but they have fundamentally different philosophies about what else matters. A* balances the future estimate against the real cost of the journey so far , keeping the algorithm honest about expensive detours. Greedy Search strips the past away completely. It is A* with amnesia — same compass, no memory.
- The Formula — Half of A*'s Brain: Greedy Search uses , which is exactly half of A*'s formula . The term is gone entirely. That single missing variable is the entire reason Greedy Search is fast, reckless, and non-optimal — all at the same time.
- The Mindset — Taking the Bait Every Time: When A* evaluates a node with a tempting low but a brutal accumulated cost, the penalty inflates the total score and pushes that node down the priority queue. Greedy Search cannot see the penalty at all. The tempting node looks just as attractive as a cheap one, and Greedy takes the bait without hesitation every single time.
- The Optimality Guarantee — Night and Day: A* with an admissible heuristic is mathematically guaranteed to find the absolute shortest path. Greedy Search guarantees exactly one thing: it will try to get to the goal quickly. The path it returns could be optimal by coincidence, or it could be four times more expensive than necessary. There is no way to know without checking independently.
- What Your Exam Is Actually Testing: The classic exam setup is to ask for both A* and Greedy traced on the exact same graph. That graph was deliberately designed so Greedy walks into a trap and A* avoids it. If both traces return the same path, double-check the working — the question was almost certainly built so they diverge, and matching results usually means a calculation was skipped somewhere in the A* trace.
Detailed Comparisons & Guides
Greedy Search vs. A*: Which Finds the Optimal Path?
Greedy is fast but reckless. A* is slower but guaranteed optimal. See both traced on the exact same graph to spot the trap.
Greedy Search vs. Uniform Cost Search (Dijkstra)
One uses only the past, the other uses only the future. See why neither is perfect on its own.
Implementation Pseudocode
// Greedy Search maintains two lists throughout the entire search
// openList = discovered nodes not yet finalized — sorted purely by h(n), nothing else
// closedList = fully processed nodes — never revisited, never updated
// Notice what is missing: there is no g(n) anywhere in this algorithm
function greedySearch(startNode, goalNode):
// ── INITIALIZATION ──────────────────────────────────────────────────
// Read h(n) directly from the heuristic table — no calculation required
// There is no g(n) to initialize, no f(n) to compute — just one static number
startNode.h = heuristic(startNode)
startNode.parent = null
// Add the start node to the openList as the only initial candidate
openList = [startNode]
closedList = []
// ── MAIN LOOP ────────────────────────────────────────────────────────
while openList is not empty:
// Always pop the node with the lowest h(n) — this is the only metric Greedy trusts
// No g(n) penalty, no accumulated cost — just the raw heuristic estimate
current = node in openList with the lowest h(n)
remove current from openList
// EXAM TRAP — Goal check happens HERE, after popping, not when generating neighbours
// Seeing the goal node appear as a neighbour does NOT terminate the algorithm
if current == goalNode:
return reconstruct_path(current) // trace parent pointers back to start
// Move current to the closedList — it has been fully processed
add current to closedList
// ── EXPAND NEIGHBOURS ───────────────────────────────────────────
for each neighbour in getNeighbours(current):
// MASSIVE SHORTCUT — Skip the neighbour if it lives in EITHER list
// In A* and UCS, you must check if a cheaper g(n) path exists for openList nodes
// In Greedy Search, h(n) is a fixed property of the node — it never changes
// A discovered node will never get a better score, so skip it immediately
if neighbour is in closedList or neighbour is in openList:
continue
// Brand new node — read its h(n) from the table and add it to the openList
// This is the entire 'update' step — one lookup, one insertion, nothing more
neighbour.h = heuristic(neighbour)
neighbour.parent = current
add neighbour to openList
// openList is empty and goal was never popped — no path exists in this graph
return failure
// ── INITIAL CALL ─────────────────────────────────────────────────────
// greedySearch(startNode, goalNode)Time & Space Complexity
| Scenario | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Worst Case (Misleading Heuristic) | Here is the branching factor and is the maximum depth of the entire graph. This is the exam trap: a misleading can drag Greedy Search down the longest, deepest wrong path in the graph before it hits a dead end. Unlike A*, which scales with (the optimal solution depth), Greedy has no cost-awareness to bail it out early. | ||
| Best Case (Perfect Heuristic) | If perfectly points toward the goal at every step, Greedy Search behaves like a heat-seeking missile. It expands exactly nodes — one per level down to the goal — and ignores the rest of the graph entirely. Both time and space scale only with the depth of the solution, making this the most efficient possible outcome. | ||
| Average Case (Realistic Heuristic) | reduced in practice | reduced in practice | In practice, Greedy Search is often significantly faster than A* and uses far less memory, because it sprints toward the goal instead of carefully balancing costs. The trade-off is that its performance is completely unpredictable — a slightly misleading can send it on a catastrophically expensive detour with no warning. |
Summary
Greedy Search is A* with amnesia — it has the compass but lost the map. Every decision reduces to a single number: , the estimated distance to the goal. The actual cost of the journey, , is completely invisible to it, which is why it can confidently return a path four times more expensive than optimal without any indication that something went wrong. On an exam, the survival rules are simple: initialize the `openList` with values only, never waste time updating nodes already discovered, move dead-end nodes to the `closedList` and keep going, and wait for the goal to be formally popped before terminating. Master those four rules and Greedy Search becomes one of the fastest algorithms to trace on any exam — because you will never confuse it with A* again.
Greedy Search Exam Questions Students Always Get Wrong
Why can't I stop the algorithm the second the Goal node appears in my openList?
Generating the goal just means it was discovered — not that it is next in line. A different node with a lower might still be sitting in the `openList`, and Greedy Search must always expand the absolute lowest first. The algorithm only terminates when the Goal node is formally popped and moved to the `closedList`.
I found a neighbour that is already sitting in my openList. Should I update its score?
No — skip it completely and move on. Unlike A*, Greedy Search has no to recalculate. The value is a static number pulled directly from the heuristic table and will never change regardless of the path taken to reach it. Attempting to update it wastes exam time and signals to the grader that you are mixing up algorithms.
All of my current node's neighbours are already in the closedList. Did I make a math error?
No error was made — Greedy Search just walked into a dead end by chasing a tempting heuristic. Do not panic, do not erase the work, and do not attempt to trace backwards manually. Simply move the dead-end node to the `closedList` as normal and pop the next lowest from whatever remains in the `openList`.
Two nodes in the openList have the exact same score. Which one do I pop first?
Break the tie alphabetically — pick whichever node comes first in the alphabet. This keeps the trace consistent with standard grading rubrics and makes it straightforward for the examiner to follow the working. Both choices lead to the same final path cost, but only one will match the expected answer key.
My Greedy Search and A* traces returned the exact same path. Is this normal?
That is a major red flag — treat it as a sign to double-check the A* working immediately. Exam questions asking for both algorithms on the same graph are almost always designed so Greedy falls into a heuristic trap that A* avoids. If both paths match, a calculation was almost certainly skipped or miscalculated somewhere in the A* trace.
Core University Curriculum
This algorithm and its manual calculation methods are foundational requirements in leading Computer Science and Software Engineering programs worldwide. You will find this topic heavily featured in the syllabi of these standard AI courses:
Explore Related Algorithms
Try the A* Algorithm Calculator
Run the exact same graph through A* and watch it reject every trap Greedy Search fell for. See how adding to the decision changes everything.
A* Algorithm Theory
A* is Greedy Search with its memory restored. See exactly how adding transforms a reckless sprint into a mathematically guaranteed optimal path.