Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Always the cheapest frontier first
Expands the frontier vertex with the lowest path cost g first and tests the goal on expansion, so it finds the cheapest path from the start to the target
Select an algorithm and generate steps to begin visualization
Uniform cost search (UCS) is the uninformed search algorithm for finding the cheapest path from a start to a goal when steps have different costs. It is the version of Dijkstra's algorithm taught in artificial intelligence courses: it grows a frontier of vertices that have been reached but not expanded, and always expands the one with the lowest path cost g. Unlike breadth-first search it does not count steps but adds up their costs, so a long route of cheap roads can beat a short route of expensive ones.
The frontier is a priority queue ordered by g, the cost of the best route found so far, and starts with the start vertex at g = 0. Each step removes the cheapest vertex from the frontier. If it is the goal, the search stops, since no other frontier vertex can lead to a cheaper route. Otherwise the vertex joins the explored set, and each neighbour joins the frontier, or gets a lower g when this route to it is cheaper. The goal test happens on expansion, not when a vertex is first reached, and that is what makes the result optimal. With a binary heap the search takes O((V + E) log V) time. The example is the road map of Romania from Russell and Norvig's textbook, from Arad to Bucharest.
Uniform cost search plans routes on road networks and game maps, finds the cheapest sequence of actions in AI planning, and solves puzzles where moves have different costs. It is the baseline that A* improves on: A* adds a heuristic to the same frontier, and with h = 0 it is exactly uniform cost search. In an AI course UCS sits between breadth-first search and A*, which makes it a standard exam question.
Uniform cost search is graph search with a priority queue ordered by path cost. Two details make it optimal: the goal test at expansion, and lowering a frontier entry when a cheaper route to it appears.
UniformCostSearch(start, goal):
frontier = priority queue ordered by g
frontier.add(start, g = 0)
explored = {}
while frontier is not empty:
n = frontier.popLowestG()
if n == goal: return the path to n
explored.add(n)
for each neighbour m of n:
g2 = g(n) + cost(n, m)
if m not in explored and m not in frontier:
frontier.add(m, g2)
else if m in frontier and g2 < g(m):
frontier.lower(m, g2)
return failureWhen a vertex leaves the frontier its g is final: every other frontier vertex has a g at least as large, and with costs of 0 or more no route through them can come back cheaper. That is why the goal test waits for expansion. Testing the goal when it first joins the frontier would accept the first route found, not the cheapest.
Find the cheapest route from Arad (A) to Bucharest (B) on the road map of Romania this page loads, with road lengths in km.
Example graph: Roads from Arad lead to Zerind (75), Timisoara (118) and Sibiu (140). From Sibiu, roads lead to Fagaras (99) and Rimnicu Vilcea (80). Fagaras reaches Bucharest directly (211), Rimnicu Vilcea through Pitesti (97, then 101).
The cheapest route is Arad, Sibiu, Rimnicu Vilcea, Pitesti, Bucharest, 418 km, and uniform cost search expanded 12 of the 20 cities. Had it stopped when Bucharest first appeared, it would have returned the 450 km route through Fagaras.
Time: O((V + E) log V) · Space: O(V)
Each vertex is expanded at most once and each edge is examined at most once from each end, and every frontier operation costs O(log V) with a binary heap, so a full run takes O((V + E) log V). AI textbooks state the cost for implicit search trees instead: O(b^(1 + ⌊C*/ε⌋)), where b is the branching factor, C* the cost of the best solution and ε the cheapest step cost. That bound shows the weakness of uniform cost search: with many cheap steps it explores a large region before it can reach an expensive goal.
Uniform cost search is one of four best-first searches that differ only in the priority they give the frontier.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Breadth-first search | All steps cost the same: the fewest steps is the cheapest path, and a plain queue is enough. | O(V + E) |
| Dijkstra's algorithm | The cheapest route from the start to every vertex is needed, not to one goal. | O((V + E) log V) |
| A* search | A good admissible heuristic is available: g + h steers the search toward the goal and expands fewer vertices. | O((V + E) log V) |
| Greedy best-first search | A fast route matters more than the cheapest one: rank the frontier by h only. | O((V + E) log V) |
Read the full article: A* Search Algorithm in AI: The Complete Guide
Read the full article: Dijkstra's Algorithm Explained Step by Step
Related algorithms: Dijkstra's Algorithm, A* Search Algorithm, Greedy Best-First Search, Breadth-First Search