VOOZH about

URL: https://www.geeksforgeeks.org/artificial-intelligence/uniform-cost-search-ucs-in-ai/

⇱ Uniform Cost Search (UCS) in AI - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Uniform Cost Search (UCS) in AI

Last Updated : 8 Jun, 2026

Uniform Cost Search (UCS) is an uninformed search algorithm used to find the lowest-cost path between a start node and a goal node in a weighted graph. It systematically explores paths based on their cumulative cost, making it suitable for cost-sensitive pathfinding problems.

  • Considers the total path cost rather than the number of steps taken.
  • Guarantees the optimal solution when all edge costs are non-negative.
  • Uses a priority queue to efficiently select the next node with the lowest cost.

Key Concepts

  • Priority Queue: UCS uses a priority queue to stores nodes and always selects the node with the lowest total cost for exploration.
  • Path Cost: Calculates the cumulative cost from the start node to the current node and prioritizes nodes with lower costs.
  • Node Expansion: Explores nodes inRepresents the total cost required to reach a node from the start node. order of increasing path cost, giving preference to cheaper paths first.
  • Optimal Solution: Stops when the goal node is reached through the lowest-cost path, ensuring the best solution when all edge costs are non-negative.

Working

Uniform Cost Search (UCS) operates by always exploring the path with the lowest cumulative cost first. It uses a priority queue to keep track of nodes and their path costs.

  1. Initialization: The algorithm begins at the starting node and assigns it a cost of 0 because no movement has been made yet.
  2. Node Expansion: The node with the lowest path cost is selected from the priority queue, and its neighboring nodes are explored.
  3. Exploring Neighbors: For each neighboring node, UCS calculates the total cost to reach it from the start node. The node is then added to the priority queue, or its cost is updated if a cheaper path is found.
  4. Goal Check: After expanding a node, the algorithm checks if it has reached the goal node. If the goal is reached, the algorithm returns the total cost to reach this node and the path taken.
  5. Repetition: This process repeats until the priority queue is empty or the goal is reached.

Implementation

Step 1: Import Required Libraries

Importing the required libraries for implementing Uniform Cost Search and visualizing the graph.

  • heapq: Implements a priority queue for selecting the lowest-cost node.
  • networkx: Used to create and manage graph structures.
  • matplotlib.pyplot: Used to visualize the graph and highlight the shortest path.

Step 2: Define the Uniform Cost Search Function

This function implements the UCS algorithm to find the least cost path from a start node to a goal node in a weighted graph.

Step 3: Define the Path Reconstruction Function

Creating a helper function to reconstruct the final path by tracing parent nodes from the goal back to the start.

Step 4: Define the Visualization Function

This function visualizes the graph and the path found by UCS, using networkx for graph creation and matplotlib for visualization.

Step 5: Define the Graph and Execute UCS

Defining a sample graph as an adjacency list, set the start and goal nodes, and run the UCS algorithm. It then visualizes the graph and the path found.

Output:

Least cost path from A to G: A -> B -> D -> G with total cost 4

👁 pathfinding-uniform-cost-search
Output

You can download the complete code from here.

Applications

  1. Pathfinding in Maps: Determining the shortest route between two locations on a map, considering different costs for different paths.
  2. Network Routing: Finding the least-cost route in a communication or data network.
  3. Puzzle Solving: Solving puzzles where each move has a cost associated with it, such as the sliding tiles puzzle.
  4. Resource Allocation: Tasks that involve distributing resources efficiently, where costs are associated with different allocation strategies.

Advantages

  • Optimality: Guarantees the optimal (minimum-cost) solution when all edge costs are non-negative.
  • Completeness: Ensures a solution is found if a valid path to the goal exists.
  • Cost-Based Exploration: Expands nodes based on cumulative path cost rather than depth, making it suitable for weighted graphs.
  • Applicable to Variable Costs: Works effectively when actions or edges have different traversal costs.

Challenges

  • High Time Complexity: May explore a large number of nodes before reaching the goal, especially in large search spaces.
  • High Space Complexity: Requires storing all generated frontier nodes in the priority queue, leading to significant memory consumption.
  • Repeated Node Expansions: Nodes may be revisited and updated when a lower-cost path is discovered.
  • Performance Limitations: Can become computationally expensive for large graphs with many possible paths.
Comment

Explore