Finding the Lowest-Cost Path with Uniform Cost Search

Last updated

Problem Setup

Scenario:Find the lowest-cost path from the start node S to the goal node G in a weighted directed graph. Different routes may reach the same node with different total costs.

Given:A directed graph with S as the start node and G as the goal node. Each connection has a non-negative edge cost, and the search keeps track of the best known cost to reach each node.

Task:Apply Uniform Cost Search step by step, update a node whenever a cheaper route is found, and determine the lowest-cost path from S to G together with its total cost.

Need a quick refresher? Review Uniform Cost theory

Weighted Graph

Arrows show the permitted direction of travel, and edge labels show the cost of each connection. Different routes to the same node may have different total costs.

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

Step-by-Step Priority Queue Matrix

n= Edge Relaxed
n= Next Node
n= Relaxed & Next Node!
—= Closed Node
Evaluating Node ↓/Target →
SABCDG
—0∞∞∞∞∞
S—25∞∞∞
A——559∞
B———5714
C————713
D—————10
G——————

Calculation Breakdown

1

Evaluating Neighbors of S

Path to A (via S → A)Relaxed: Initial path established.
0+2=20 + 2 = 2
Path to B (via S → B)Relaxed: Initial path established.
0+5=50 + 5 = 5
2

Evaluating Neighbors of A

Path to C (via A → C)Relaxed: Initial path established.
2+3=52 + 3 = 5
Path to D (via A → D)Relaxed: Initial path established.
2+7=92 + 7 = 9
3

Evaluating Neighbors of B

Path to D (via B → D)Relaxed: Cheaper route found (Previous: 9).
5+2=75 + 2 = 7
Path to G (via B → G)Relaxed: Initial path established.
5+9=145 + 9 = 14
4

Evaluating Neighbors of C

Path to G (via C → G)Relaxed: Cheaper route found (Previous: 14).
5+8=135 + 8 = 13
5

Evaluating Neighbors of D

Path to G (via D → G)Relaxed: Cheaper route found (Previous: 13).
7+3=107 + 3 = 10
6

Goal Reached at G

No valid unvisited neighbors to explore.

Final Answer

The lowest-cost route from S to G is:

Optimal path: S → B → D → G

Total path cost:10

G was initially reached with a cost of 14, which later improved to 13 and then 10. Uniform Cost Search does not stop when the goal is first discovered; it continues until G has the lowest cost in the priority queue and is selected for expansion, confirming the shortest path.

Final Shortest Costs from S

S → S
0
S → A
2
S → B
5
S → C
5
S → D
7
S → G
10

Exam Takeaway

  • Select the smallest cumulative cost, not the fewest edges: Uniform Cost Search expands the frontier node with the lowest known total cost from the start. A path with more edges can still be cheaper than one with fewer edges.
  • Update tentative distances when a cheaper route is found: A node's first recorded distance may not be its shortest distance. Compare each new path cost with the current best value and replace it if the new route is cheaper.
  • Distinguish discovered nodes from finalized nodes: With nonnegative edge weights, a node's shortest distance is finalized when it is removed as the minimum-cost entry from the priority queue. Merely discovering the goal does not justify stopping the search.