Uniform Cost Search: A Complete Solved Numerical Example

Scenario: City Transit Route Optimization

The Objective: Find the most cost-effective transit route from the starting station (S) to the final destination (G). Observe how Uniform Cost Search systematically explores paths based on exact ticket costs, updating its knowledge whenever a cheaper detour is discovered.

Core Mechanics
  • Always Pop the Cheapest: Dijkstra runs on a Priority Queue. Instead of popping the oldest or newest node, you must always pop the unvisited node with the lowest cumulative cost, g(n)g(n).
  • Edge Relaxation: When evaluating neighbors, calculate the running total from the start. If this new total is cheaper than a neighbor's currently known cost, you "relax" the edge by updating it with the better score.
  • Settled Nodes Are Locked: Once a node is popped as the absolute cheapest option, its shortest path is mathematically guaranteed. Lock it in the Closed List and never evaluate it again.
  • The Goal Test (Pop, Don't Peek): Do not declare victory the moment the goal node appears in the queue! You must wait until the goal is officially popped to guarantee no cheaper path to it exists.

Step 1: Initial Map Configuration

Before we begin evaluating, here is the full structure of the spatial graph. Notice that Dijkstra's algorithm relies solely on exact edge weights, with no heuristics involved.

Start Node: SGoal Node: G
2
5
3
7
2
9
8
3
S
A
C
B
D
G

Step 2: Step-by-Step Resolution Trace

Watch the algorithm maintain the priority queue matrix and continuously relax edges until the optimal path is locked in.

Priority Queue Matrix

n= Edge Relaxed
n= Next Node
n= Relaxed & Next Node!
= Closed Node
Evaluating Node ↓/Target →
SABCDG
S25
A559
B5714
C713
D10
G

Calculation Breakdown

1

Evaluating Neighbors of S

Path to A (via SA)✨ Updated: Initial path established.
0+2=20 + 2 = 2
Path to B (via SB)✨ Updated: Initial path established.
0+5=50 + 5 = 5
2

Evaluating Neighbors of A

Path to C (via AC)✨ Updated: Initial path established.
2+3=52 + 3 = 5
Path to D (via AD)✨ Updated: Initial path established.
2+7=92 + 7 = 9
3

Evaluating Neighbors of B

Path to D (via BD)✨ Updated: Cheaper route found (Previous: 9).
5+2=75 + 2 = 7
Path to G (via BG)✨ Updated: Initial path established.
5+9=145 + 9 = 14
4

Evaluating Neighbors of C

Path to G (via CG)✨ Updated: Cheaper route found (Previous: 14).
5+8=135 + 8 = 13
5

Evaluating Neighbors of D

Path to G (via DG)✨ Updated: Cheaper route found (Previous: 13).
7+3=107 + 3 = 10
6

Goal Reached at G

No valid unvisited neighbors to explore.

Step 3: Final Computed Path

Cheapest Path Found

S → B → D → G
Total Path Cost:10

Shortest Path to All Nodes

SS
0
SA
2
SB
5
SC
5
SD
7
SG
10

Final Takeaway

Look at the 'G' column in the matrix to see 'Edge Relaxation' in action as the algorithm repeatedly overwrites its own notes! It initially finds paths costing 14 and 13 before finally locking in the optimal 10-cost route, proving Dijkstra never settles until it mathematically guarantees the cheapest path.