What is uniform-cost search?

Uniform-cost search is an uninformed graph-search algorithm that always expands the frontier node with the lowest total path cost so far. Use it when edge costs differ and you need a guaranteed cheapest path with non-negative costs.

Imagine a GPS app choosing between a direct toll road that costs 10 minutes plus a fee and a slightly longer free route that costs 8 minutes overall. The fewest turns do not necessarily give the cheapest route. This is exactly the kind of weighted-path problem uniform-cost search is designed to solve.

Key takeaways:

  • Expansion rule: Uniform-cost search always expands the path with the smallest accumulated cost so far.
  • Best use case: It solves shortest-path problems on weighted graphs when costs are non-negative.
  • Optimality condition: It is optimal only if the goal is tested on expansion and step costs stay above a positive minimum.
  • Special case: With equal edge costs, it reduces to breadth-first search.
  • Practical view: In practice, it is Dijkstra’s algorithm viewed as a search method toward a goal.

Uniform cost search algorithm definition

Uniform-Cost Search, or UCS, is an uninformed searchA search method that uses no heuristic estimate and relies only on the problem definition. that expands states in increasing path cost g(n) rather than increasing depth. It is also called lowest-cost-first search. The core shortest-path idea was published by Edsger W. Dijkstra in 1959, and in AI UCS is the same method framed as goal-directed search.

Why it matters and the problem it solves

Uniform-cost search matters because breadth-first search can return the wrong answer when edge costs differ. UCS expands states in increasing path cost g(n), not in increasing depth.

In the diagram below, the direct edge from the start A to the goal B costs 5, while the two-edge route through C costs 2 + 2 = 4. BFS returns the one-edge path because it has fewer steps; UCS returns the cheaper route through C.

The dry run later in this Answer uses this weighted graph from A to G: A→B costs 5, A→D costs 3, D→E costs 2, D→F costs 2, B→C costs 1, F→G costs 3, C→G costs 8, and C→E costs 6. The cheapest route is A→D→F→G with total cost 8, even though another route may reach intermediate nodes in fewer steps.

This is the same kind of choice a routing system makes when roads have different travel times or toll costs.

How the algorithm works

Uniform-cost search uses a priority queueA data structure that always removes the item with the highest priority, such as the lowest path cost. to keep the frontier ordered by current path cost.

  1. Start with the initial state in the priority queue at cost 0.
  2. Pop the frontier node with the smallest cumulative cost.
  3. Test for the goal only after popping that node, not when generating it.
  4. If this popped node is the goal, return its path and total cost.
  5. Otherwise, generate each neighbor and compute its new path cost.
  6. If the new cost is lower than the best known cost for that neighbor, record it and push the new entry into the priority queue.
  7. Repeat until the goal is popped or the frontier becomes empty.

Practical implementations in coding interviews usually use lazy insertion: they push improved entries into the heap and later skip stale entries when popped. This is common because many heap libraries do not support clean decrease-key updates.

If all edge costs are equal, UCS behaves like BFS. Even then, the correct goal test is still when a node is expanded, not when it is generated.

Question

Why not stop as soon as the goal is first generated?

Show Answer

Dry run on the same graph

The slides below apply the algorithm step by step to the A-to-G example.

1 of 5

After F is expanded, the frontier holds C at cost 6 and G at cost 8. UCS expands C first because it is cheaper, but its edges reach G at 14 and E at 12, which are no improvement. G is then popped with total cost 8, so UCS returns A→D→F→G.

Complexity and limits

Uniform-cost search is complete and optimal when every step cost is at least ε > 0. Its guarantee does not hold with negative costs, because a later path could always become cheaper; for such graphs, shortest-path methods designed for negative weights are needed instead.

The standard worst-case time and space bounds are both based on how many paths UCS may expand before reaching the optimal solution cost C*.

Here, b is the branching factor, C* is the optimal solution cost, and ε is the minimum positive step cost. The bound arises because UCS may expand every path whose cost is less than C*.

Note: Zero-cost cycles are a pitfall. Without a positive lower bound on action cost, UCS can keep revisiting equal-cost paths and may not make progress as expected. Implementations should also use strict improvement checks, so a path is updated only when the new cost is smaller, not equal.

Uniform-cost search vs BFS, Dijkstra, and A*

Uniform-cost search is closest to BFS, Dijkstra, and A*, but they solve slightly different versions of the shortest-path problem. UCS and Dijkstra use the same core rule; A* adds a heuristic, while BFS assumes all step costs are equal.

Comparing UCS with BFS, Dijkstra, and A*

Method

Expansion order

Uses heuristic?

Optimal when

Best fit

BFS

Smallest depth

No

All edge costs are equal

Unweighted or unit-cost graphs

UCS

Smallest path cost g(n)

No

Costs are non-negative and at least ε > 0; goal tested on pop

Weighted shortest path to a goal

Dijkstra

Smallest distance from source

No

Edge weights are non-negative

Shortest paths from one source to many nodes

A*

Smallest g(n) + h(n)

Yes

Heuristic is admissible and consistent

Faster goal-directed search when a good heuristic exists

In AI, UCS is effectively Dijkstra’s algorithm with an early stop when the goal is popped from the queue. Unlike A*, UCS does not use a heuristic estimate, so it is simpler but often explores more states.

Tools that implement it

Most libraries expose UCS under shortest-path or Dijkstra APIs rather than the name “UCS.” Graph tools and pathfinding libraries typically provide Dijkstra-style functions, which are the practical implementation of this search on weighted graphs with non-negative costs.

Check your understanding with these quick questions.

Quiz
1

When does uniform-cost search guarantee the cheapest path to the goal?

A)

When the goal is first generated

B)

When all edges have the same cost

C)

When the goal is popped from the priority queue and costs are non-negative with a positive minimum step cost

D)

When the branching factor is small

Question 1 of 30 attempted

When to use uniform-cost search

Choose uniform-cost search when path costs differ and correctness matters more than exploring the fewest nodes. If you have a good heuristic, A* is usually more efficient; if all costs are equal, BFS is enough.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is uniform cost search used for?

Uniform-cost search is used to find the cheapest path in a weighted graph when step costs are non-negative. Typical uses include route planning, robot navigation, and action planning where moves have different costs.


Is uniform cost search just Dijkstra?

Yes. In practice, UCS is Dijkstra’s algorithm viewed as a goal-directed search on non-negative weighted graphs, often stopping when the goal is popped.


What is the difference between uniform cost search and A* search?

Uniform-cost search orders nodes only by path cost so far, while A* orders them by path cost plus a heuristic estimate to the goal. A* can expand fewer nodes when the heuristic is admissible.


Does uniform cost search use a heuristic?

No. Uniform-cost search is an uninformed search method that uses only the accumulated path cost.


What is uniform search in AI and how is it used?

In AI, this usually refers to uniform-cost search. It expands the lowest-cost frontier path first and is used for shortest-path or least-cost planning problems with different non-negative action costs.


Which is better, A* or Dijkstra?

A* is usually better for a single known goal when a reliable heuristic is available. Dijkstra, and therefore UCS, is better when no heuristic is available or when shortest paths to many nodes are needed.


Is BFS just Dijkstra?

Yes, when every edge has the same cost. Once edge weights differ, BFS can return a more expensive path.


When is uniform-cost search optimal?

Uniform-cost search is optimal when every step cost is at least ε > 0 and the goal is tested on expansion. Negative costs break this guarantee.


What are the complexity/time and space bounds of uniform-cost search?

The standard worst-case time and space bounds are both O(b^(1 + floor(C*/ε))). UCS may need to expand all paths cheaper than the optimal solution cost before it can safely stop.


Why can't we test for goal when generating a node in UCS?

Because the first generated goal might not be the cheapest goal path. UCS is only safe to stop when the goal is removed from the priority queue as the current lowest-cost frontier node.


Free Resources