Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Uniform Cost Search (UCS) in AI
Next article icon

Uniform Cost Search (UCS) in AI

Last Updated : 23 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Uniform Cost Search (UCS) is a popular search algorithm used in artificial intelligence (AI) for finding the least cost path in a graph. It is a variant of Dijkstra's algorithm and is particularly useful when all edges of the graph have different weights, and the goal is to find the path with the minimum total cost from a start node to a goal node.

In this article, we will explore the fundamentals of UCS, its working mechanism, and its applications in AI.

Introduction to Uniform Cost Search (UCS)

Uniform Cost Search is a pathfinding algorithm that expands the least cost node first, ensuring that the path to the goal node has the minimum cost. Unlike other search algorithms like Breadth-First Search (BFS), UCS takes into account the cost of each path, making it suitable for weighted graphs where each edge has a different cost.

Key Concepts of Uniform Cost Search

  1. Priority Queue: UCS uses a priority queue to store nodes. The node with the lowest cumulative cost is expanded first. This ensures that the search explores the most promising paths first.
  2. Path Cost: The cost associated with reaching a particular node from the start node. UCS calculates the cumulative cost from the start node to the current node and prioritizes nodes with lower costs.
  3. Exploration: UCS explores nodes by expanding the least costly node first, continuing this process until the goal node is reached. The path to the goal node is guaranteed to be the least costly one.
  4. Termination: The algorithm terminates when the goal node is expanded, ensuring that the first time the goal node is reached, the path is the optimal one.

How Does Uniform Cost Search Work?

UCS operates under a simple principle: among all possible expansions, pick the path that has the smallest total cost from the start node. This is implemented using a priority queue to keep the partial paths in order, based on the total cost from the root node.

Here’s the step-by-step process of how UCS works:

  1. Initialization: UCS starts with the root node. It is added to the priority queue with a cumulative cost of zero since no steps have been taken yet.
  2. Node Expansion: The node with the lowest path cost is removed from the priority queue. This node is then expanded, and its neighbors are explored.
  3. Exploring Neighbors: For each neighbor of the expanded node, the algorithm calculates the total cost from the start node to the neighbor through the current node. If a neighbor node is not in the priority queue, it is added to the queue with the calculated cost. If the neighbor is already in the queue but a lower cost path to this neighbor is found, the cost is updated in the queue.
  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.

Implementing Uniform Cost Search to Solve Pathfinding Problem

Step 1: Import Required Libraries

This step imports the necessary libraries for implementing Uniform Cost Search (UCS) and visualizing the graph.

import heapq
import networkx as nx
import matplotlib.pyplot as plt

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.

def uniform_cost_search(graph, start, goal):
# Priority queue to store the frontier nodes, initialized with the start node
priority_queue = [(0, start)]
# Dictionary to store the cost of the shortest path to each node
visited = {start: (0, None)}

while priority_queue:
# Pop the node with the lowest cost from the priority queue
current_cost, current_node = heapq.heappop(priority_queue)

# If we reached the goal, return the total cost and the path
if current_node == goal:
return current_cost, reconstruct_path(visited, start, goal)

# Explore the neighbors
for neighbor, cost in graph[current_node]:
total_cost = current_cost + cost
# Check if this path to the neighbor is better than any previously found
if neighbor not in visited or total_cost < visited[neighbor][0]:
visited[neighbor] = (total_cost, current_node)
heapq.heappush(priority_queue, (total_cost, neighbor))

# If the goal is not reachable, return None
return None

Step 3: Define the Path Reconstruction Function

This function reconstructs the path from the start node to the goal node by tracing back through the visited nodes.

def reconstruct_path(visited, start, goal):
# Reconstruct the path from start to goal by following the visited nodes
path = []
current = goal
while current is not None:
path.append(current)
current = visited[current][1] # Get the parent node
path.reverse()
return path

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.

def visualize_graph(graph, path=None):
G = nx.DiGraph()

# Adding nodes and edges to the graph
for node, edges in graph.items():
for neighbor, cost in edges:
G.add_edge(node, neighbor, weight=cost)

pos = nx.spring_layout(G) # Positioning the nodes

# Drawing the graph
plt.figure(figsize=(8, 6))
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=2000, font_size=15, font_weight='bold', edge_color='gray')
labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, font_size=12)

if path:
# Highlight the path in red
path_edges = list(zip(path, path[1:]))
nx.draw_networkx_edges(G, pos, edgelist=path_edges, edge_color='red', width=2.5)

plt.title("Uniform Cost Search Path Visualization")
plt.show()

Step 5: Define the Graph and Execute UCS

This step defines a sample graph as an adjacency list, sets the start and goal nodes, and runs the UCS algorithm. It then visualizes the graph and the path found.

# Example graph represented as an adjacency list
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('D', 1), ('E', 3)],
'C': [('F', 5)],
'D': [('G', 2)],
'E': [('G', 1)],
'F': [('G', 2)],
'G': []
}

# Example usage of the UCS function
start_node = 'A'
goal_node = 'G'
result = uniform_cost_search(graph, start_node, goal_node)

if result:
total_cost, path = result
print(f"Least cost path from {start_node} to {goal_node}: {' -> '.join(path)} with total cost {total_cost}")
visualize_graph(graph, path)
else:
print(f"No path found from {start_node} to {goal_node}")

Complete Code

Python
import heapq import networkx as nx import matplotlib.pyplot as plt  def uniform_cost_search(graph, start, goal):     # Priority queue to store the frontier nodes, initialized with the start node     priority_queue = [(0, start)]     # Dictionary to store the cost of the shortest path to each node     visited = {start: (0, None)}          while priority_queue:         # Pop the node with the lowest cost from the priority queue         current_cost, current_node = heapq.heappop(priority_queue)                  # If we reached the goal, return the total cost and the path         if current_node == goal:             return current_cost, reconstruct_path(visited, start, goal)                  # Explore the neighbors         for neighbor, cost in graph[current_node]:             total_cost = current_cost + cost             # Check if this path to the neighbor is better than any previously found             if neighbor not in visited or total_cost < visited[neighbor][0]:                 visited[neighbor] = (total_cost, current_node)                 heapq.heappush(priority_queue, (total_cost, neighbor))          # If the goal is not reachable, return None     return None  def reconstruct_path(visited, start, goal):     # Reconstruct the path from start to goal by following the visited nodes     path = []     current = goal     while current is not None:         path.append(current)         current = visited[current][1]  # Get the parent node     path.reverse()     return path  def visualize_graph(graph, path=None):     G = nx.DiGraph()      # Adding nodes and edges to the graph     for node, edges in graph.items():         for neighbor, cost in edges:             G.add_edge(node, neighbor, weight=cost)      pos = nx.spring_layout(G)  # Positioning the nodes      # Drawing the graph     plt.figure(figsize=(8, 6))     nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=2000, font_size=15, font_weight='bold', edge_color='gray')     labels = nx.get_edge_attributes(G, 'weight')     nx.draw_networkx_edge_labels(G, pos, edge_labels=labels, font_size=12)      if path:         # Highlight the path in red         path_edges = list(zip(path, path[1:]))         nx.draw_networkx_edges(G, pos, edgelist=path_edges, edge_color='red', width=2.5)      plt.title("Uniform Cost Search Path Visualization")     plt.show()  # Example graph represented as an adjacency list graph = {     'A': [('B', 1), ('C', 4)],     'B': [('D', 1), ('E', 3)],     'C': [('F', 5)],     'D': [('G', 2)],     'E': [('G', 1)],     'F': [('G', 2)],     'G': [] }  # Example usage of the UCS function start_node = 'A' goal_node = 'G' result = uniform_cost_search(graph, start_node, goal_node)  if result:     total_cost, path = result     print(f"Least cost path from {start_node} to {goal_node}: {' -> '.join(path)} with total cost {total_cost}")     visualize_graph(graph, path) else:     print(f"No path found from {start_node} to {goal_node}") 

Output:

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

Applications of UCS in AI

Uniform Cost Search is widely applicable in various fields within AI:

  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 of Uniform Cost Search

  • Optimality: UCS is guaranteed to find the least cost path to the goal state if the cost of each step exceeds zero.
  • Completeness: This algorithm is complete; it will find a solution if one exists.

Challenges with UCS

  • Space Complexity: The main drawback of UCS is its space complexity. The priority queue can grow significantly, especially if many nodes are being expanded.
  • Time Complexity: The time it takes to find the least cost path can be considerable, especially if the state space is large.

Conclusion

Uniform Cost Search is a powerful algorithm in AI for situations where paths have different costs and the goal is to minimize the total cost. Its application across various domains showcases its versatility and effectiveness. However, understanding its computational requirements is crucial for practical implementations, especially in scenarios with large data sets or limited computational resources


Next Article
Uniform Cost Search (UCS) in AI

A

alka1974
Improve
Article Tags :
  • Artificial Intelligence
  • AI-ML-DS
  • AI-ML-DS With Python

Similar Reads

    Uninformed Search Algorithms in AI
    Uninformed search algorithms is also known as blind search algorithms, are a class of search algorithms that do not use any domain-specific knowledge about the problem being solved. Uninformed search algorithms rely on the information provided in the problem definition, such as the initial state, ac
    8 min read
    Heuristic Search Techniques in AI
    Heuristic search techniques are used for problem-solving in AI systems. These techniques help find the most efficient path from a starting point to a goal, making them essential for applications such as navigation systems, game playing, and optimization problems.Heuristic search operates within the
    5 min read
    Breadth-first Search is a special case of Uniform-cost search
    In AI there are mainly two types of search techniques: Un-informed/blind search techniquesInformed search techniques Search algorithms under the Uninformed category are: Breadth-first searchUniform cost searchDepth-first searchDepth limited searchIterative deepening depth-first searchBidirectional s
    6 min read
    State Space Search in AI
    State Space Search is used to solve problems by finding different possible states and their transitions. In simple terms it’s like finding the best route to goal by trying different paths. In this article, we will understand it and implement Breadth-First Search (BFS) to solve the 8-puzzle problem.T
    9 min read
    Uniform-Cost Search (Dijkstra for large Graphs)
    Uniform-Cost Search is a variant of Dijikstra's algorithm. Here, instead of inserting all vertices into a priority queue, we insert only the source, then one by one insert when needed. In every step, we check if the item is already in the priority queue (using the visited array). If yes, we perform
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences