Inferensys

Glossary

Uniform-Cost Search

Uniform-Cost Search is a graph search algorithm that expands the node with the lowest cumulative path cost from the start, guaranteeing an optimal solution when all step costs are non-negative.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
HEURISTIC SEARCH ALGORITHMS

What is Uniform-Cost Search?

Uniform-Cost Search (UCS) is a foundational graph traversal algorithm for finding the lowest-cost path between nodes in a weighted graph, forming a core component of agentic planning and decision-making systems.

Uniform-Cost Search (UCS) is a best-first search algorithm that systematically explores a weighted graph by always expanding the node with the lowest cumulative path cost from the start node. It uses a priority queue, typically ordered by the g(n) function representing the exact cost from the start to node n, to manage its frontier. This strategy guarantees an optimal solution—the cheapest path—provided all step costs are non-negative, making it a direct generalization of Breadth-First Search (BFS) for graphs with varying edge weights.

The algorithm's guarantee of optimality is its primary strength, but it can be computationally expensive as it may explore all nodes with a path cost less than the optimal solution's cost. In agentic cognitive architectures, UCS provides a reliable, uninformed planning baseline for agents navigating environments with known cost maps. It is a special case of the more general Dijkstra's Algorithm, which finds shortest paths to all nodes, and is often superseded by A Search* when a suitable heuristic function is available to guide exploration more efficiently toward the goal.

ALGORITHM MECHANICS

Key Features and Properties

Uniform-Cost Search (UCS) is defined by its systematic, cost-driven exploration of a weighted graph. These cards detail its core operational properties, guarantees, and practical considerations.

01

Optimality Guarantee

Uniform-Cost Search is guaranteed to find an optimal (lowest-cost) path from the start node to a goal node, provided all step costs are non-negative. This guarantee stems from its expansion order: it always expands the node with the smallest cumulative path cost (g(n)) first. If a cheaper path to a node is discovered later, the algorithm updates its cost and parent, ensuring the final solution is the absolute minimum cost.

  • Key Condition: Requires non-negative edge weights. A negative cost could create a cycle that reduces total path cost indefinitely, breaking the algorithm's logic.
  • Contrast with BFS: Breadth-First Search finds the shortest path in terms of the number of steps in an unweighted graph. UCS generalizes this to find the lowest cumulative cost in a weighted graph.
02

Priority Queue Frontier

The algorithm's behavior is governed by its use of a priority queue (often a min-heap) to manage the search frontier (or open list). The priority of a node is its current path cost from the start, g(n).

  • Operation: When a node is expanded, its successors are generated. Each successor's path cost is calculated (g(parent) + cost(action)). The successor is then inserted into the priority queue with this cost as its priority.
  • Node Selection: The next node to expand is always the one at the front of the priority queue—the node with the smallest g(n). This greedy selection on cumulative cost is what ensures optimality.
  • Efficiency: Using a min-heap allows for O(log n) insertions and O(1) access to the minimum element, making the frontier management efficient.
03

Completeness

Uniform-Cost Search is complete, meaning it will always find a solution if one exists, given that the graph is finite and all step costs are ≥ ε > 0 (a small positive number).

  • Finite Graphs: In a finite state space, UCS will systematically explore all reachable nodes in order of increasing cost, eventually finding a goal if it is reachable.
  • Infinite Graphs & Zero Costs: The algorithm remains complete for infinite graphs if all step costs are ≥ ε > 0. This prevents infinite sequences of zero-cost actions that would be expanded before exploring potentially cheaper paths with positive costs. If zero-cost actions are allowed, completeness is not guaranteed without additional mechanisms like graph search with a closed set.
04

Path Cost vs. Heuristic

A defining characteristic of UCS is that it uses only the incurred path cost (g(n)) to guide its search. It does not employ a heuristic function (h(n)) to estimate the remaining cost to the goal.

  • Blind Search: This makes UCS a form of blind (uninformed) search. It expands nodes based solely on the known cost from the start, with no look-ahead information about the goal's proximity.
  • Contrast with A*: A* Search enhances UCS by adding a heuristic h(n) to form the evaluation function f(n) = g(n) + h(n). This allows A* to be optimally efficient under certain conditions, often exploring far fewer nodes than UCS by directing the search toward the goal.
  • When to Use: UCS is ideal when no good heuristic is available, but an optimal solution is required on a weighted graph with non-negative costs.
05

Graph Search Implementation

To handle repeated states and ensure termination, UCS is typically implemented as a graph search rather than a tree search. This requires maintaining a closed set (or explored set) of already-expanded nodes.

  • Process: When a node is expanded, it is moved from the frontier to the closed set. When generating a successor:
    • If it is already in the closed set, it is ignored (a cheaper path to it cannot be found, as UCS expands in order of increasing cost).
    • If it is already in the frontier with a higher cost, its cost and parent are updated in the priority queue.
  • Preventing Redundancy: This mechanism prevents the algorithm from endlessly revisiting states and re-expanding them, which is critical for efficiency and termination in graphs with cycles.
  • Memory Trade-off: The closed set consumes O(b^d) memory in the worst case, where b is the branching factor and d is the depth of the optimal solution.
06

Time and Space Complexity

The computational demands of UCS are significant, as it explores all nodes with a path cost less than the optimal solution cost.

  • Time Complexity: O(b^(1 + C*/ε)), where b is the branching factor, C* is the cost of the optimal solution, and ε is the minimum step cost. In the worst case, it explores all nodes up to the optimal cost depth, which can be very large if costs are small.
  • Space Complexity: O(b^(1 + C*/ε)). The frontier (priority queue) and the closed set must store all nodes in the explored region. This is the primary limitation of UCS for large or complex state spaces.
  • Practical Consideration: While optimal, UCS can be prohibitively expensive in terms of memory and time for problems with high branching factors or where many paths have similar, low costs. This often necessitates the use of informed search algorithms like A* when a good heuristic is available.
FEATURE COMPARISON

Uniform-Cost Search vs. Other Search Algorithms

A technical comparison of Uniform-Cost Search against other foundational graph traversal and pathfinding algorithms, highlighting key operational characteristics for developers selecting a search strategy.

Algorithmic FeatureUniform-Cost SearchBreadth-First Search (BFS)Depth-First Search (DFS)Greedy Best-First SearchA* Search

Primary Selection Criterion

Lowest path cost (g(n)) from start

Shallowest node (depth)

Deepest node (LIFO stack order)

Lowest heuristic estimate to goal (h(n))

Lowest combined cost (g(n) + h(n))

Graph Type

Weighted (non-negative edges)

Unweighted

Unweighted or Weighted

Unweighted or Weighted

Weighted (non-negative edges)

Optimality Guarantee

✅ Yes (with non-negative costs)

✅ Yes (for unweighted graphs)

❌ No

❌ No

✅ Yes (with admissible heuristic)

Completeness Guarantee

✅ Yes (finite branching)

✅ Yes (finite branching)

❌ No (can loop infinitely)

❌ No (can loop infinitely)

✅ Yes (finite branching)

Time Complexity

O(b^(1 + C*/ε))

O(b^d)

O(b^m)

O(b^m)

O(b^d)

Space Complexity

O(b^(1 + C*/ε))

O(b^d)

O(bm)

O(bm)

O(b^d)

Requires Heuristic Function

❌ No

❌ No

❌ No

✅ Yes

✅ Yes

Data Structure for Frontier

Priority Queue (min-heap)

FIFO Queue

LIFO Stack

Priority Queue (min-heap)

Priority Queue (min-heap)

Effective for Large/Infinite Graphs

❌ No (exponential memory)

❌ No (exponential memory)

✅ Yes (linear memory)

✅ Yes (linear memory)

❌ No (exponential memory)

Generalizes to

Dijkstra's Algorithm (finds all shortest paths)

Uniform-Cost Search (if h(n) = 0)

UNIFORM-COST SEARCH

Frequently Asked Questions

Uniform-Cost Search is a foundational algorithm for finding optimal paths in weighted graphs. These questions address its core mechanics, guarantees, and practical applications in AI and agentic systems.

Uniform-Cost Search (UCS) is a graph traversal and pathfinding algorithm that systematically explores a state space by always expanding the node with the lowest cumulative path cost from the start node. It operates by maintaining a priority queue (the frontier or open list) ordered by the current path cost g(n). The algorithm repeatedly dequeues the lowest-cost node, checks if it is the goal, and if not, expands it by generating its successors. The path cost to each successor is calculated by adding the step cost of the transition to the parent's cost. If a cheaper path to a previously seen node is found, its cost is updated. This process continues until the goal node is dequeued, at which point the algorithm terminates, having found the lowest-cost path.

Key Mechanism: The use of a priority queue ensures a best-first exploration based solely on incurred cost, not on a heuristic estimate to the goal. This makes it a generalization of Breadth-First Search (BFS) for weighted graphs, where BFS assumes uniform step costs.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.