Uniform-Cost Search Algorithm

Uniform Cost Search (UCS) in AI is a fundamental search algorithm used to find the least-cost path in a state space where actions have different costs. It is widely taught in artificial intelligence courses and frequently appears in university exams and technical interviews.
Uniform Cost Search belongs to the family of uninformed search strategies, meaning it does not use heuristics and relies only on path cost.

What is Uniform Cost Search (UCS)?
Uniform Cost Search is an uninformed search algorithm that expands the node with the lowest cumulative path cost using a priority queue, guaranteeing an optimal solution when all step costs are non-negative.
This definition is central to understanding the uniform cost search algorithm and is commonly used in exams and AI theory explanations.
How Uniform Cost Search Works
Uniform Cost Search explores the search space by always choosing the cheapest path so far, rather than the shallowest path.
Step-by-Step UCS Algorithm
-
Insert the start node into the priority queue with path cost = 0
-
Use a priority queue (min-heap) ordered by cumulative path cost
-
Remove the node with the lowest total cost
-
Expand the node and generate its successors
-
Add successors to the queue with updated path costs
-
Repeat until the goal node is dequeued
The algorithm stops only when the goal node is removed from the priority queue, ensuring optimality.
Master AI Search Algorithms
Learn the math and logic behind UCS, A*, and other advanced algorithms in Scaler's Data Science & Machine Learning Course.
Data Structure Used in Uniform Cost Search
Priority Queue (Min-Heap)
Uniform Cost Search uses a priority queue, where nodes are ordered based on their cumulative path cost, not insertion order.
This is why UCS works correctly on weighted graphs.
Why BFS Fails on Weighted Graphs
Breadth-First Search (BFS) uses a FIFO queue and assumes all edges have equal cost. In weighted graphs:
-
BFS may find a shorter path in terms of steps
-
But not the minimum-cost path
UCS fixes this limitation by always expanding the lowest-cost path first.
Uniform Cost Search Example (Solved)
Weighted Graph Example
Assume the following graph:
-
Start node: A
-
Goal node: G
Edges and costs:
-
A → B (1)
-
A → C (4)
-
B → D (2)
-
B → G (6)
-
C → G (1)
-
D → G (3)
Step-by-Step UCS Execution (Queue State)
| Step | Expanded Node | Priority Queue (Node, Cost) |
|---|---|---|
| 1 | A | (B,1), (C,4) |
| 2 | B | (D,3), (C,4), (G,7) |
| 3 | D | (C,4), (G,6), (G,7) |
| 4 | C | (G,5), (G,6), (G,7) |
| 5 | G | Goal reached (Cost = 5) |
Optimal Path: A → C → G Total Cost: 5
This example clearly shows how uniform cost search prioritizes cost over depth.
Properties of Uniform Cost Search
| Property | UCS |
|---|---|
| Complete | Yes |
| Optimal | Yes |
| Informed | No |
| Uses heuristic | No |
Uniform Cost Search is both complete and optimal, provided all step costs are non-negative.
Time and Space Complexity of UCS
Time Complexity
O(b^(C/ε))*
Where:
-
b = branching factor
-
C** = cost of the optimal solution
-
ε = minimum step cost
This means UCS may explore many nodes with cost less than the optimal solution. You can learn more about time complexity calculations here.
Space Complexity
Same as time complexity
UCS stores all generated nodes in memory because it keeps the entire frontier in the priority queue.
In simple terms: Uniform Cost Search is memory-intensive.
Uniform Cost Search vs Other Algorithms
| Feature | UCS | BFS | A* |
|---|---|---|---|
| Graph type | Weighted | Unweighted | Weighted |
| Optimal | Yes | No | Yes |
| Uses heuristic | No | No | Yes |
| Data structure | Priority Queue | FIFO Queue | Priority Queue |
This comparison is frequently tested in AI exams and interviews.
When Should You Use Uniform Cost Search?
Use uniform cost search in AI when:
-
The graph is weighted
-
No heuristic information is available
-
Finding the least-cost path is more important than speed
-
You need a guaranteed optimal solution
Avoid UCS when memory is limited or when a good heuristic exists (A* is better then).
Applications of Uniform Cost Search
Uniform Cost Search is used in:
-
Route planning with varying travel costs
-
AI planning problems
-
Pathfinding in weighted environments
-
Cost-based decision-making systems
It forms the conceptual foundation for advanced algorithms like A*.
FAQs
Is uniform cost search optimal?
Yes. Uniform Cost Search is optimal as long as all action costs are non-negative. It always expands the node with the lowest cumulative path cost.
Is uniform cost search complete?
Yes. UCS is complete because it will eventually explore all paths with increasing cost until the goal is found.
What is the difference between BFS and UCS?
BFS expands nodes level by level, while UCS expands nodes based on cumulative cost. BFS fails on weighted graphs, but UCS handles them correctly.
Does UCS use heuristics?
No. Uniform Cost Search is an uninformed search algorithm and does not use heuristics.
Why is a priority queue used in UCS?
A priority queue ensures that the node with the lowest path cost is expanded first, which guarantees optimality.