Uniform Cost Search (UCS) Algorithm in Python
Uniform Cost Search (UCS) Algorithm in Python
Pseudocode
Using the Uniform Search Algorithm to find the best
Pen and Paper
solution in a graph modeled problem example
python
order to find a path from a starting to a final (target) point. The Greedy algorithm
was the first heuristic algorithm we have talked about. Today, we are going to talk
about another search algorithm, called the *Uniform Cost Search (UCS)
1. Introduction
2. Pseudocode
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 1/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Introduction
As we already mentioned in the past, search algorithms like Breadth-First Search,
Depth First Search, the Greedy algorithm, and of course the UCS algorithm, are
used to find a solution in a graph. The graph represents the given problem, in
which each vertex (node) of the graph represents a state of the problem and each
edge represents a valid action that takes us from one state (vertex) to the other. If
you want to learn more about graphs follow the link below:
In contrast to BFS and DFS algorithms that don't take into consideration neither
the cost between two nodes nor any heuristic value, the Greedy algorithm uses a
hand, the UCS algorithm uses the path's cost from the initial node to the current
node as the extension criterion**. Starting from the initial state (starting node), the
UCS algorithm, in each step chooses the node that is **closer **to the initial node.
When the algorithm finds the solution, returns the path from the initial state to the
final state. The UCS algorithm is characterized as complete, as it always returns
Pseudocode
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 2/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
The UCS algorithm takes as inputs the graph along with the starting and the
destination nodes and returns the optimum path between these nodes if exists.
Similar to the Greedy algorithm, the UCS algorithm uses two lists, the *opened
*and the *closed *list. The first list contains the nodes that are possible to be
selected and the closed list contained the nodes that have already been selected.
Firstly, the first node (initial state) is appended to the opened list (initialization
phase). In each step, the node (selected node)with the smallest distance value is
removed from the opened list and is appended to the closed list. For each child of
the selected node, the algorithm calculates the distance from the first node to this.
If the child does not exist in both lists or is in the opened list but with a bigger
distance value from the initial node, then the child is appended in the opened list
in the position of the corresponding node. The whole process is terminated when
a solution is found, or the opened list be empty. The latter situation means that
there is not a possible solution to the related problem. The pseudocode of the
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 3/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Our target is to go from the city (node) V1 to V6 following the path with the
smallest cost (shortest path). Let's execute the UCS algorithm:
Step 1: Initialization
The first node V1 (initial state) of the graph is appended to the opened list. The
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 4/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
The V1 is selected as it is the only node in the opened list. Its children V2 and V3
are appended in the opened list after the distance calculation from node V1.
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 5/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Node V4 is selected as it has the smallest distance value. In this step, we find a
better distance value for node V5, so we replace the node (V5, 10) with node (V5,
9).
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 6/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Node V2 is selected as it has the smallest distance value. However, none of its
children is appended in the opened list, as nodes V3 and V4 are already inserted
in the closed list and the algorithm doesn't find a better distance value for node
V5.
Node V2 is selected as it has the smallest distance value. A better path to node
V6 is found in this step. So, we replace the old node (V6, 13) with node (V6, 11)
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 7/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Node V6 (target node) is selected. So the algorithm returns the path from node
Python implementation
Having understood the algorithmic procedure of the UCS algorithm is time to
implement it in Python. We are going to extend the code from the Graphs article
First, we create the class *Node *that represents each node of the graph. This
class has a couple of attributes such as the heuristic value that corresponds to
the distance from the initial node. An overview of the *Node *class is the following.
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 8/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
We compare the nodes according to their heuristic value (distance from the initial
node). So, we implement the magic of the dunder method gt(), which defines the
way the nodes are compared to each other.
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 9/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
The main class of the algorithm is the *UCS *class that represents the UCS
algorithm. This class has a couple of attributes, such as the *graph *which
represents the model of the problem, the *opened *and *closed *lists, etc. An
overview of the class is the following:
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 10/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
To calculate the distance from the initial node to the current node we use the
following method:
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 11/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Finally, the core algorithm is the *search() *method that represents the whole
algorithmic procedure.
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 12/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Example
Now, we are ready to test our implementation using the example above as the
input of the algorithm. Remember that the search space of our example is the
following:
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 13/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 14/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
Subscribe
Powered by Formula
YouTube Stackademic Write for IPE Style Guide Gambling US Betting Sites
🇬🇧 🇵🇹 🇳🇬 🇪🇸 🇮🇳
In Plain English Ltd © 2024
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 15/18
12/02/2024, 10:16 Uniform Cost Search (UCS) Algorithm in Python
As we can see, we have the same results. Remember, that the UCS algorithm
always returns the optimum solution, so this path is the shortest path as we also
Conclusion
In this article, we had the opportunity to talk about the UCS Algorithm, to find the
best solution from an initial (starting) node to the target node. In contrast to the
Greedy algorithm that uses a heuristic value, such as the Manhattan distance, to
estimate the distance to the target node, the UCS algorithm (like Dijkstra's
algorithm) uses the distance from the initial node to select the next node in the
path. In the next article, we will talk about the well-known A algorithm to find the
optimum solution to a given graph problem. Until then, keep learning and keep
coding.
If you want to read more articles like this one, regarding Algorithms, Data
Structures, Data Science, and Machine Learning follow me on Medium,
Python
CONTINUE LEARNING
2022-06-23
2021-07-15
2021-11-03
6 Amazing Algorithms to Get the Square Root (and Any Root) of any
Number in Python
2021-04-28
data analysis
2021-09-18
2021-09-15
https://plainenglish.io/blog/uniform-cost-search-ucs-algorithm-in-python-ec3ee03fca9f#pen-and-paper-example 18/18