Skip to content
StackDecode
AI & ML Atlas
Artificial IntelligencealgorithmInteractive simulation

Uniform Cost Search

Expands the cheapest-so-far node first; Dijkstra for general search problems.

The idea

It uses a priority queue ordered by g(n), the exact cost from the start. Because it always extends the cheapest frontier node, the first time it pops the goal that path is optimal. It uses no heuristic, so it explores evenly in all directions like a growing ripple.

priority = g(n)

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

SG
  • current
  • frontier
  • explored
  • path

Walls are dark, brown cells are mud (cost 3), everything else costs 1. Find a route from S to G.

1/89

Compare modes on the same map: A* and UCS find the cheapest route, greedy is fast but can be fooled.

Step by step

  1. 1Put the start on a priority queue with cost 0.
  2. 2Pop the node with the lowest cost g.
  3. 3If it is the goal, return the path.
  4. 4Push each neighbour with cost g + step cost (keep only the cheapest route to each node).

Where it is used

Optimal search when step costs differ and no good heuristic exists.

Watch out for

Explores far more nodes than A* when a decent heuristic is available.

Related