Uniform-Cost Search
Definition:â
Uniform-Cost Search (UCS) is a search algorithm that expands the node with the lowest cost first, ensuring that it finds the least-cost path in weighted graphs. UCS is similar to Dijkstra's algorithm but is often applied in AI and pathfinding scenarios.
Video Explanation**â

Characteristics:â
- Lowest Cost Path: Always expands the node with the lowest path cost.
- Optimal: Guarantees an optimal solution for paths with non-negative weights.
- Uninformed: Does not use heuristics and is a type of uninformed search.
How Uniform-Cost Search Works:â
- Initialize: Start with the root node.
- Expand Node: Choose the node with the lowest cumulative path cost.
- Add Neighbors: Add unvisited neighbors with updated path costs.
- Repeat: Continue until reaching the goal or emptying the priority queue.
Time Complexity:â
- Time Complexity: (O(b^d)) where
bis the branching factor anddis the depth of the goal.
Space Complexity:â
- Space Complexity: (O(b^d))
Advantages of Uniform-Cost Search:â
- Optimal Path: Finds the least-cost path for weighted graphs.
- No Heuristic Needed: Effective without heuristic guidance.
Disadvantages of Uniform-Cost Search:â
- Inefficient for Large Graphs: Can be slow for large search spaces.
- High Memory Use: Stores all frontier nodes in memory.
Uniform-Cost Search Algorithm (Java Implementation):â
import java.util.*;
class GraphNode {
int id, cost;
List<GraphNode> neighbors;
GraphNode(int id, int cost) {
this.id = id;
this.cost = cost;
neighbors = new ArrayList<>();
}
}
class UniformCostSearch {
public static int ucs(GraphNode start, GraphNode goal) {
PriorityQueue<GraphNode> pq = new PriorityQueue<>(Comparator.comparingInt(n -> n.cost));
Set<GraphNode> visited = new HashSet<>();
pq.add(new GraphNode(start.id, 0));
while (!pq.isEmpty()) {
GraphNode current = pq.poll();
if (current.id == goal.id) return current.cost;
visited.add(current);
for (GraphNode neighbor : current.neighbors) {
if (!visited.contains(neighbor)) {
pq.add(new GraphNode(neighbor.id, current.cost + neighbor.cost));
}
}
}
return -1; // Goal not reachable
}
}
Applications of Uniform-Cost Search:â
Pathfinding in AI: Used for finding the shortest path in robotics. Network Routing: Ensures the lowest cost route in weighted networks. Resource Allocation: Finds optimal paths with resource constraints.
Summary:â
Uniform-Cost Search is optimal and complete for finding the lowest-cost path in weighted graphs, widely used in AI and networking.
Test Your Understanding
Answer these 3 questions to check what you have learned.
What is the main topic of this documentation page?
What should be identified before implementing uniform cost search?
How is understanding of uniform cost search best checked?
Done with this topic? Mark it as complete to track your progress.
đŦ Discuss this page
Have a question or spot something confusing in "Uniform-Cost Search"? Ask below â it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.