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.
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 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
- Start with the initial state in the priority queue at cost 0.
- Pop the frontier node with the smallest cumulative cost.
- Test for the goal only after popping that node, not when generating it.
- If this popped node is the goal, return its path and total cost.
- Otherwise, generate each neighbor and compute its new path cost.
- 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.
- 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.
Why not stop as soon as the goal is first generated?
Dry run on the same graph
The slides below apply the algorithm step by step to the A-to-G example.
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 | No | Costs are non-negative and at least | 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 | 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.
When does uniform-cost search guarantee the cheapest path to the goal?
When the goal is first generated
When all edges have the same cost
When the goal is popped from the priority queue and costs are non-negative with a positive minimum step cost
When the branching factor is small
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