Greedy Best-First Search vs. Uniform Cost Search

Last Updated July 20, 2026

Greedy Best-First Search ranks frontier nodes only by h(n)h(n), the estimated cost remaining to the goal. Uniform Cost Search ranks them only by g(n)g(n), the accumulated path cost from the start.

  • Use Greedy Best-First Search when reaching a usable route quickly matters more than proving that the route has the lowest total cost.
  • Use Uniform Cost Search when the cheapest route must be guaranteed under the required nonnegative-cost and graph-search conditions.
Greedy follows estimated closeness; UCS follows accumulated cost.

Head-to-Head Showdown

Frontier Priority

Greedy Best-First Search: Lowest h(n)h(n)

Uniform Cost Search: Lowest g(n)g(n)

The Implication: Greedy selects the node estimated closest to the goal, even when reaching that node has already been expensive. Uniform Cost Search selects the cheapest discovered route so far, even when that node appears farther from the goal.

Accumulated Cost Effect

Greedy Best-First Search: Ignored for priority

Uniform Cost Search: Controls priority

The Implication: Greedy may return an expensive route because previously paid path cost never changes its node ordering. Uniform Cost Search continually compares accumulated costs and relaxes a destination whenever a cheaper route is discovered.

Solution Guarantee

Greedy Best-First Search: No cheapest-path guarantee

Uniform Cost Search: Conditional cheapest path

The Implication: Even a perfect heuristic cannot make Greedy optimal because g(n)g(n) remains excluded from its priority. Uniform Cost Search protects the cheapest-path result when edge costs and graph-search handling satisfy the required conditions.

Selection Criteria

Scenario:Choosing any safe escape route for a game character under a strict frame-time budget where path cost is secondary.

Choose Greedy Best-First Search:Greedy ranks frontier nodes only by h(n)h(n), so an informative estimate can direct the search toward an exit without evaluating every cheaper side route. Uniform Cost Search may perform additional work to protect a cheapest-path guarantee that this scenario does not require.

Scenario:Finding the lowest-fuel route between delivery depots where every unit of transportation cost affects the operating budget.

Choose Uniform Cost Search:Uniform Cost Search ranks nodes by accumulated cost and relaxes a route whenever a cheaper alternative appears. Greedy Best-First Search ignores fuel already spent when selecting the next node, so it can return a more expensive route.

Scenario:Finding the lowest-cost sequence through an API-dependency graph when no reliable estimate predicts closeness to the final endpoint.

Choose Uniform Cost Search:Without a trustworthy h(n)h(n), Greedy has no dependable basis for directing the search toward the endpoint. Uniform Cost Search needs no heuristic and uses known path costs to identify the cheapest sequence under the required conditions.

Side By Side Trace

A directed graph contains two competing routes from SS to GG. Edge labels show route cost, while every displayed h(n)h(n) equals the exact cheapest remaining cost to GG. Greedy Best-First Search orders its Open List by h(n)h(n) alone, while Uniform Cost Search ignores h(n)h(n) and ranks nodes by accumulated cost g(n)g(n).

5
3
2
4
1
7
S
3
A
5
B
1
C
0
G

Step 1: Evaluating the Start Node

Greedy Best-First Search

Evaluates SS and adds both neighbors to the Open List: A(3)A(3) and B(5)B(5), ordered by h(n)h(n). AA leads because h(A)=3<h(B)=5h(A)=3<h(B)=5, and SS moves to the Closed List.

Uniform Cost Search

Expands SS with tentative distance 00 and relaxes both outgoing edges: AA receives tentative distance 55 and BB receives tentative distance 22. The priority queue holds B(2)B(2) ahead of A(5)A(5) since BB's tentative distance is smaller.

Step 2: Point of Divergence

Greedy Best-First Search

Compares h(A)=3h(A)=3 with h(B)=5h(B)=5 and selects AA from the Open List. Although reaching AA costs 55 while reaching BB costs 22, those accumulated costs do not affect Greedy's priority. AA moves to the Closed List, and GG enters with h(G)=0h(G)=0.

Uniform Cost Search

Compares tentative distance 22 for BB against tentative distance 55 for AA and selects BB since 2<52<5. BB becomes closed, and BB's edge to CC is relaxed to tentative distance 66.

Step 3: Greedy Reaches the Goal

Greedy Best-First Search

Compares h(G)=0h(G)=0 with h(B)=5h(B)=5 and selects GG. The goal is removed from the Open List for evaluation, so Greedy returns SAGS \rightarrow A \rightarrow G with total cost 88.

Uniform Cost Search

Compares tentative distance 55 for AA against tentative distance 66 for CC and selects AA since 5<65<6. AA becomes closed, and its edge to GG is relaxed to a first recorded tentative distance of 88.

Step 4: Uniform-Cost Search Improves the Route to G

Greedy Best-First Search

Performs no further work. The search already terminated one step earlier when GG was removed from the Open List with a total path cost of 88.

Uniform Cost Search

Compares tentative distance 66 for CC against tentative distance 88 for GG and selects CC since 6<86<8. CC's edge to GG is relaxed: the candidate distance 6+1=76+1=7 is smaller than the recorded 88, so the distance matrix updates GG to 77 and its predecessor changes from AA to CC.

Step 5: Uniform-Cost Search Reaches the Goal

Greedy Best-First Search

Performs no further work. Greedy's result and total evaluated nodes remain unchanged from the previous step.

Uniform Cost Search

Selects GG from the priority queue with tentative distance 77, the smallest remaining value. GG becomes closed, so the goal test succeeds and Uniform-Cost Search terminates with the cheaper route through CC.

Final Result

Greedy Best-First Search:Greedy Best-First Search returns SAGS \rightarrow A \rightarrow G with total cost 88 after evaluating SS, AA, and GG. Every heuristic equals the true remaining cost, yet Greedy still chooses the costlier route because accumulated path cost never affects its priority.

Uniform Cost Search:Uniform-Cost Search returns SBCGS \rightarrow B \rightarrow C \rightarrow G with total cost 77 after closing 55 nodes: SS, BB, AA, CC, and GG. Its distance matrix first recorded G=8G=8 through AA, then relaxation through CC lowered it to 77, producing the cheaper route after two additional node selections.

Common Pitfalls & Exam Mistakes

  • Ranking Greedy nodes by accumulated path cost.

    The Mistake: Students calculate the cost from the start and use it to reorder Greedy's frontier instead of selecting the smallest heuristic value.

    Why It's Wrong: Greedy Best-First Search ranks nodes only by h(n)h(n). Using accumulated cost g(n)g(n) instead changes the search into Uniform Cost Search rather than preserving Greedy's heuristic-only priority.

  • Adding h(n)h(n) to Uniform Cost Search.

    The Mistake: Students include the displayed heuristic values in UCS priorities because both algorithms use the same graph.

    Why It's Wrong: Uniform Cost Search ranks nodes only by accumulated path cost g(n)g(n) and updates that cost through relaxation. Adding h(n)h(n) changes its priority into g(n)+h(n)g(n)+h(n), producing A* rather than UCS.

  • Assuming a perfect heuristic makes Greedy as reliable as Uniform Cost Search.

    The Mistake: Students believe exact remaining-cost estimates must make Greedy return the cheapest route.

    Why It's Wrong: The shared trace uses exact remaining costs, yet Greedy still returns cost 88 because it ignores accumulated cost. Uniform Cost Search returns cost 77 by ranking nodes through g(n)g(n) and relaxing the goal when a cheaper route appears.

Comparative Analysis

AttributeGreedy Best-First SearchUniform Cost Search
Evaluation Functionh(n)h(n)g(n)g(n)
Accumulated Path CostIgnored for priorityControls priority
Goal EstimateControls priorityNot used
Cheapest-Path GuaranteeNo guaranteeConditional guarantee
CompletenessNot guaranteed generallyConditional guarantee
Typical Trade-OffDirect goal pursuitCheapest-route protection

Common Questions & Edge Cases

  • Do Greedy Best-First Search and Uniform Cost Search use different priority values?

    Yes. Greedy Best-First Search ranks frontier nodes by h(n)h(n), the estimated remaining cost to the goal. Uniform Cost Search ranks them by g(n)g(n), the accumulated path cost from the start.

  • Can Greedy Best-First Search replace Uniform Cost Search when path cost does not matter?

    Yes. Greedy can be appropriate when any valid route is acceptable and an informative heuristic can guide the search toward the goal. The returned route may still cost more because accumulated path cost does not control Greedy's priority.

  • Can Uniform Cost Search evaluate fewer nodes than Greedy Best-First Search?

    Yes. A misleading heuristic can send Greedy through unhelpful regions, while low accumulated costs may direct UCS efficiently toward the goal. Expanded-node counts depend on the graph and heuristic, so neither algorithm universally performs less work.

  • Does a perfect heuristic make Greedy Best-First Search as reliable as Uniform Cost Search?

    No. Even exact remaining-cost estimates cannot protect optimality when Greedy ignores the accumulated cost already paid. In the shared trace, Greedy returns cost 88 while Uniform Cost Search returns cost 77 through accumulated-cost ordering and relaxation.

Explore the Algorithms in Action

Open the theory pages or try the interactive solvers for the algorithms compared above.