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.
Initialization: The algorithm begins at the starting node and assigns it a cost of 0 because no movement has been made yet.
Node Expansion: The node with the lowest path cost is selected from the priority queue, and its neighboring nodes are explored.
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.
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.
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