Uniform-Cost Search Algorithm in Artificial Intelligence
Introduction
Uniform-Cost Search (UCS) is a pivotal algorithm that finds its application in the realm of artificial intelligence and computer science. With its unique focus on cost optimization, the Uniform-Cost Search algorithm offers a systematic approach to navigating through complex scenarios, seeking the most economical path from a starting point to a desired goal within a weighted graph or tree. In simple words, this algorithm moves towards the goal state in the cheapest way possible. Hence, this algorithmic gem is particularly important in solving real-world challenges that demand precision and efficiency. It travels with the lowest cumulative cost and reaches the goal.
At its core, UCS operates with cost efficiency as its guiding principle. It employs a priority queue, often realized through a data structure like a heap, to manage nodes yet to be explored. Each node in this queue carries a cost value representing the total expense incurred in reaching that state from the initial one. What sets UCS apart is its unwavering commitment to selecting and expanding nodes based on their cost priority, always opting for the node with the lowest cumulative cost. This intrinsic feature forms the crux of UCS's optimality.
By design, the algorithm assures that once the goal state is achieved, the traversed path is not only successful but also the most cost-effective among all alternatives evaluated. UCS's singular focus on cost renders it especially suitable for scenarios where identifying the path with the minimum cost is of utmost importance, relegating other considerations to a secondary role.
The UCS journey commences with the initial state and progresses by evaluating neighboring states. Through meticulous calculations, it derives the costs associated with transitioning from the starting state to these neighbors. The algorithm then orchestrates the addition of these neighbors to the priority queue based on their cumulative costs. Iteratively, UCS selects nodes from the priority queue, expanding their horizons by examining adjacent states and updating cumulative costs as required. This cyclic process persists until the goal state is reached or until the priority queue is depleted—a telltale sign of an insurmountable obstacle in the quest for a viable path.
Nonetheless, the pursuit of optimality comes at a computational cost. UCS may not be the swiftest solution in terms of time and memory utilization, especially when dealing with intricate solution spaces laden with numerous nodes and edges. The comprehensive exploration that UCS undertakes can potentially strain computational resources. This particular attribute underscores its suitability for situations that demand precise, cost-optimal solutions while also highlighting its potential limitations in terms of efficiency.
While UCS shines for its cost-centric approach, it diverges from other search algorithms in its minimal reliance on heuristics. In contrast to informed search strategies that leverage domain-specific knowledge to guide exploration, UCS's decisions are exclusively driven by cumulative costs. This trait proves invaluable when heuristic insights are either unavailable or when a bias-free, cost-driven solution is the prime objective.
In the context of AIML (Artificial Intelligence Markup Language), which facilitates the creation of conversational agents and chatbots, delving into UCS concepts can unlock new dimensions in crafting sophisticated, human-like interactions. Integrating UCS principles into AIML-powered systems holds the promise of refining these agents' prowess in traversing potential conversational trajectories, enhancing their proficiency in deciphering user input, generating contextually apt responses, and, by extension, delivering a profoundly engaging user experience.
Working of Uniform Cost Search Algorithm
- A UCS is implemented using a priority queue.
- It gives the highest priority to the lowest cumulative cost.
- It gives maximum priority to BFS if the path cost of all the edges is the same.
Uniform-Cost Search is the extension of Dijikstra's algorithm. Instead of adding all vertices into a priority queue, we insert only the source and then insert one item at a time as needed. We verify whether the item is already in the priority queue (using the visited array) at each stage. If yes, we execute the decrease key; otherwise, we insert it.
This Dijkstra version is beneficial for infinite graphs and graphs that are too vast to store in memory. Uniform-Cost Search is mostly employed in Artificial Intelligence.
Algorithm for Uniform Cost Search
Step 1: The first element that should be entered into the priority queue is the root node.
Step 2: In the next step, remove the element with the highest priority.
Step 3: If the node that is removed is the destination, print the total cost and stop the iterations in the algorithm.
Step 4: Else, Check whether the node is already in the visited list
Step 5: Else, add children of the current node to the priority queue. Add these children by considering two major points:
- Consider the distance or cost from the root as the priority while adding it to the priority queue
- Also, ensure that the node is not in the visited list.
Working with an Example
Dijikstra's approach is related to Uniform-Cost Search. In this algorithm, we will visit the adjacent states and choose the least costly state, then we will choose the next least costly state from all un-visited and adjacent states of the visited states, and so on until we reach the goal state (note that we will not continue the path through a goal state), and even if we reach the goal state, we will continue searching for other possible paths (if there are multiple goals). We will maintain a priority queue to select the least expensive next state from all surrounding states of visited states.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence1.png)
Example: Consider the above tree representation. Using Uniform Cost Search Algorithm find out the minimum cost to reach the goal node (G).
Solution: The initial node is A. Hence, the algorithm starts from A.
It has chances of moving to B as well as C. When the costs are compared, the cost to reach B is lesser than the cost to reach node C. Hence, the Uniform cost Search algorithm chooses B as its next node and places A in the visited list.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexmple.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence2.png)
Visited List:
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence3.png)
Now, the algorithm has two nodes to move on. It can reach C with a cost of 3 and reach D with a cost of 5. Hence, it chooses C as it is less costly.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence4.png)
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence5.png)
Next, there is no other option. Hence the next node is F, and it is reached with the cost of 2.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence6.png)
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence7.png)
The next node it moves is the Goal node with the cost of 1 unit. Finally, it reaches the goal state with the minimum cost possible.
The total cost to reach the goal node from the initial node is 7.
Example 2: Consider the following graph and find the minimum cost using a uniform cost search algorithm.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence8.png)
Solution: S is the initial state, and G is the Goal state. The corresponding weights are mentioned at the edges of the graph. The problem aims to find out the minimum cost to reach the goal state from the initial state using a uniform cost search algorithm.
S is the initial node. Hence the traverse starts from S. From S; we can reach either A or B. 3 and 7 are the weights to reach A and B from S, respectively. As the cost to reach A is minimum, the next node will be A, and S will be placed in the visited list.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence9.png)
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence10.png)
From A, we can move to D and C. The cost to reach B is lesser than the cost to reach D. Hence the next node will be B, and A is moved to the visited list.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence11.png)
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence12.png)
The next nodes that can be reached from B are S and C. As S is already in the visited list, the next node will become C.
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence13.png)
![Uniform-Cost Search Algorithm in Artificial Intelligence](https://static.tutorialandexample.com/ai/uniform-cost-search-algorithm-in-artificial-intelligence14.png)
From C, we can reach E and G. The cost to reach G is lesser when compared to the cost to reach E. Therefore, the next node is G. Finally; we reached the goal state.
The cost to reach the goal state from the initial state = 3+2+6+1=12
Advantages of Uniform cost search
- It aids in determining the path with the lowest cumulative cost inside a weighted graph with a distinct cost associated with each edge from the root node to the destination node.
- It is thought to be an optimum solution since the shortest path is taken at each state.
Disadvantages of Uniform Cost Search
As it evaluates every potential path from the root node to the target node, the algorithm may become trapped in an infinite loop.
As priorities in the priority queue must be maintained, the open list must be kept sorted.
The amount of storage required grows dramatically.