ARTIN1 WK 3 Lecture 3 - Dijkstra and A-Star Algorithms
ARTIN1 WK 3 Lecture 3 - Dijkstra and A-Star Algorithms
Week 3: Lecture 3
Path Planning – Dijikstra and A* Algorithms
Hello and welcome to Week 3, Path Planning Algorithms. In the previous session, we
learned about uninformed search, which was BFS. Now you will learn the informed search
algorithm, Dijkstra and A* in this lecture.
Let's call the previous graph concept. As you can see in the graph, there are nodes called s,
u, x, v. If you remember, we discussed about the directed graph and undirected graph. So
here, as you can see from the figure, it's a directed graph with some weight on it, on the
edges. For example from s to u. it’s directed, with the weight of 1 on the edges.
Let's take an example, that if I want to traverse the graph from s to x, which one would be
the shortest part? So if I go through from s to u and then x, which is 1 + 2 = 3, so that means
total cost from s to x via u is 3. Now as s is also directly connected with the x, the total cost
from s to x is 5 as it is directly connected. And similarly via v, it will cost us 3 + 4 = 7.
From all the calculations it will be easy to finalise that from s to x. the shortest path with the
least cost is s->u->x, with the cost total 3. I hope you remember and recall all the previous
concepts.
Now let's start to understand how the Dijkstra algorithm works, which is the shortest path
algorithm. A digital algorithm finds the shortest path between a source node and all of the
nodes in a graph. Dijkstra algorithm use the weights of the edges to find the path that
minimises the total distance, or you can say weight between the source node and all other
connected nodes. The algorithm keeps track of this shortest path that are currently known
to exist between each node and the source node, and it upgrades these values when a
shorter path is discovered. The other node is added to the path and identied as ‘visited’,
once the algorithm has determined the shortest path between the source node and that
other node.
The process is repeated until every node in the graph has been added to the path. In this
manner, a path that connects the source node to every other node and takes the shortest
route to each node is created. So this is the process used in Dijkstra algorithm.
This is the Dijkstra algorithm pseudocode presentation. This is what we have just learned
from the previous slide, as I explained, and this is in addition format. You can see the
distance to source. The vertex is 0 til the process, until we reach to our goal node, from the
source.
Let's now examine the operation of algorithms that we have learned from the previous
slides. As shown in the figure, the figure depicts six nodes, each of which is connected to an
edge with our weight or cost mentioned on it. Our node A is our source node and the goal
node is node C. The shortest path must be taken when navigating the graph to get from one
node to another, with the least amount of cost. We've already seen that.
As a first step, we mark all six nodes as unvisited and create a table, as shown here, as you
can see. We write down all the nodes with the shortest distance of value from that previous
node as well. As you can see, the column starts from our source node A. Initially we know
nothing about the cost value or distance value, because we haven't visited any node yet, so
we set it to A to A is 0, as you can see on the table, which is our source node. And from the
other nodes we set all the values to infinite as we haven't visited those. We put the
maximum value infinity for each node.
The last column is initially empty, but we store the previous nodes that lead us to the
shortest distance. I will cover and I'll explain it later. I hope so far it clear about the graph
and the tables to you.
Now let's start with the node A: A is connected by two neighbour nodes B and D, as you can
see from the figure. The distance from A -> B is 2 and A -> D the is 8. We update our table
for B, the previous visited node is always stored in the previous node section, and update
value 2 from infinity, because previously the value was infinite. Similarly for D as well, which
update the value. Now we mark it as a visited node, as you can see from the visit node list.
Well, based on the algorithm, check the shortest path value and select the node as a
starting node. In this case, A has the lowest value but is already visited. So then we can
continue with B as it has the lowest value compared to other nodes. We visited B and D, but
B’s value is 2, and D’s value is 8, so we start with the node B. As on this figure, visited
neighbours to node B are D and E unvisited and E with the distance of 6, and D with the
distance of 5, as you can see from the figures, respectively.
Now we can update the distance in the table for E and D, from node A, because we know A
is our resource node. The distance from A -> E, so as you can see via B is 2 + 6 = 8. And so in
the table for node E, the value we will update from infinity to 8 and the previous node for E
is now the same for the node D as well.
We have to do the same procedure for the algorithm. The distance value from A to D over B
is 2 + five, which is 7. So we update to table again. Make sure every time, whenever we
change, we have to update the table. Now with node B all the neighbours have visited. so
2
we mark our node B as a visited and remove it from the unvisited list. as you can see, it's
mentioned on the slide.
Let's move to the next step. Now the same process, we choose our next node from the table
with the minimal distance, which is node D, as you can see, because A and B are already
visited. Now we calculate the distance of these neighbour nodes, which are E and F. The
distance from A -> E over D is 7 + 3, which is now 10, but we are not updating the table as
we have the shortest distance to E is 8 over B, which we already covered, which is less than
10, so we are not updating now.
For node F over D from A, the distance is 7 + two, which is 9, so we update our table with
the shortest distance. And in the last column for node F, the previous node is now D. Now
we mark node D as visited, and for the next move we continue with node E, with the lowest
distance value. Now the distance from E -> C is 8 + nine, which is 17, and we have this value
in the table as 17, which is less than infinity. The distance from E -> F is 8 + 1, which is 9,
which is precisely the existing distance from node D, so we do not need to update it. Now
node E has become visited, so we move it from unvisited node list to the visited node list.
As you can see in the figure, node F and node C are left as the unvisited nodes. We start
with node F, as it has the lowest distance value, which is 9. Now the distance from A -> C
over node F is 9 + three, which is 12. We update the value for node C, which was 17, and
now update it with 12, with the previous node F, so C has no visited neighbours, so we are
done here, and we have also done with the algorithm as we reach our goal node in the
graph. As a result, the table now shows the shortest distance between A and each
subsequent node.
At this point, so far, if we use the table value along with the nodes, we can find the shortest
path from A to C. Let's start the other way around – not going from source to our goal; we
are traversing from goal to source. Notice the previous node is node F, as you can see in the
table, with the minimal distance value. Node F’s previous node is node D, and node D’s
previous node is node B. Finally, node B’s previous node found node A, so the shortest path
from A -> C is A -> b, B -> d, D -> F and F to finally C, with the cost or distance of travel.
This is the Dijkstra algorithm, I hope you understand, and it help us to find a soft path with
the help of weight on the edges. And it's easy to understand, as they describe in a word, in a
pseudocode, and with the practical example of the graph. I hope you understand. And you
will use the Dijkstra algorithm and explore by yourself with the help of Python code in your
challenge activity or lab session.
This is another example. Please use the Dijkstra algorithm to try to solve this and implement
the same mechanism to solve this problem. You can use the previous example for you to
understand.
Let's move on to the next algorithm, which is the A*. let's investigate the A* search
algorithm. As we learned from the Dijkstra algorithm, it explored the entire space rather
3
than our goal position, in order to achieve the optimality and the completeness. A* relies on
heuristic techniques with the advantage of better memory usage. As you've seen in the
Dijkstra algorithm, it explores all the possible nodes. That's why it consumes a lot of
memory. The term optimality refers to the search algorithm’s ability to always identify the
best visible solution – well in this case, the shortest part to the goal state. If a search
algorithm possesses the completeness property, it guarantees to find a solution to given
issues, if one exists, with the help of the given equation, as you can see: f(n)=g(n)+h(n), we
calculate the total estimated cost of the bot through neighbours, neighbour nodes,
neighbouring nodes n, where g(n) is the value of the shortest path from the start node to
node n, which might be the neighbour or any other node, which is connected to the source
one. And the edge of n is the heuristic approximation of the node’s value.
Depending on the type of problem we might need to apply different heuristic functions for
A*, in order to identify the best solution, because its effectiveness is dependent and
dependent mainly on the heuristic value of h(n). Heuristic functions like Manhattan distance
or Euclidean distance. With the help of any of this matter, you can find the heuristic
function. Well, I'm assuming that you know what is the meaning of heuristic in AI, but if you
don't know, let me explain in brief. Heuristic is an approach to problem solving in which the
objective is to produce a working solution within a reasonable time frame, instead of
looking for a perfect solution. Heuristic strategies look for a quick solution, which we are
looking for, and that falls within an acceptable range of accuracy. It means and algorithm
should give a proper solution with the limited time frame.
The previous slides explanation of the A* algorithm is presented in a pseudo form. Well, I
know there is a so many words going on, but what I explain here is written in a pseudo
format, so if you want to understand, you can go through by each line, but as I have already
covered in the wordings in a previous slide.
Now let's start with the example, which give you a better explanation of AI algorithm. As
you can see, we have problem statement of our agent wants to reach to their destination
from source node and there are some obstacle, as well, plotted. Let's start with the A*
algorithm. Define an openlist which has only node S, which is a resource node, as I said, as
shown in the figure. And our target goal, node D, denoted as D in this case. The S has a no
parent node, as you can see there isn't one so g(s) is obvious, we assume, is equal to 0 and
the Euclidean distance we calculated and assume that has already been given, which is […] is
equal to 10.
So f(s) is equal to 0 + 10 = 10. The f value is updated at each step by combining the g and h
values, as the algorithm stated. To get the goal node, the least f value node is chosen. It
means that whenever we get the lowest value, we're going to choose that node’s f value.
Now let's take the neighbour of node S, and we've found that there are two nodes which
are connected with node S, so we added N1 and N2 to our openlist and N1 and N2 have a
parent node S, as you can see from the figure. And based on that g(n) + f(n) value, which is
4
already mentioned on this slide, we calculated the f(N1), which is 7.4 and f(N2), which is 7.0
fi. It's clear we have found that N2 has the lowest value, lowest cost f value, so we begin to
say proceed with the N2 with the closelist.
In our closelist now we have S and N2. Similar process as the next step. N2 has the
neighbour node and N3, N4, N5 and N6, as you can see from the figure. So based on the g
and h value, we can calculate the f value for all nodes, the table nodes of N2. So in the
openlist, nodes are N1, N3, N4, N5 and N6. The openlist is keeping temporary nodes which
are the neighbour of the current […]. which we are processing. Here we do not have to
calculate N1 again as this value has already been calculated and will not change in future.
After the calculation, we found that N4 has the lowest value. We move N4 to the closelist
with S and N2 and what we have done previously. So we are moving now.
Next step: again the same process, the same method is used to determine f value for N4’s
new neighbours, which we discovered as N7, N8 and N9. Here we discovered that N9 has
the lowest value based on the calculation, as you can see on your screen, which is the 12.4.
Therefore we added and 9 to our near least and will next study its neighbour nodes. Clearly
it is like a repeating process. If you have a g(n) value and a h(n) value, you calculate f and
whenever you find f value, the lowest one, you consider that node, put it into the closelist
and move to explore the neighbour nodes.
Now, N9 nodes, neighbour nodes, are N10, N11, N12 and N13. These have been explored
and again, based on the formula calculation, which is the f(N)=g(N)+h(N) that you have
already seen and calculated with the previous nodes. N12 has the lowest value, so we can
move N12 now to our closelist. I’m not going through all the calculation – it’s already there.
You can see here it's clear we found that our N12 has the lowest value. We continue with
the N12 and explore its neighbours. As the next step found that N14 and N15 are the
neighbours of N12 and among them, N14 comes up with the lowest value with the same
formula, which is 19. And compared to the and f(N15) we put N14 into over closelist.
Finally nodes N16, N17 and N19 and D, as you can see, we explored which are near our
target node, including D, we discovered as a neighbour or N14. Here we discovered that in
comparison to other nodes, destination node D has the minimal value of 20.4. That means
that we reached our destination node. So the closelist has the finally been completed with
S, N2, N4, N9, N12, N14 and D. This is the optimal path from S -> D, with the help of the A*
algorithm, which is the heuristic approach we have applied.
You can see it's a little bit complicated, but not as you thought, it is just a use the formula,
which I repeat again: f(n)= g(n)=h(n). So then, if you have all the values, it will be easy for
you to find and find the shortest path with the minimal cost. In this case, we found from S ->
D, again I'm repeating, is S -> N2, N4, N9, N12 and N14 and we finally reached our
destination node D.
5
This is the A* algorithm. You will implement this algorithm, with the Python code, as your
challenge activity. So far we covered Dijkstra algorithm and A* algorithm. The Dijkstra
algorithm, we are just doing the summary now, the Dijkstra algorithm is the shortest path
between the start node and the target node. It is useful to find it, but the challenge is this
could be inefficient as well as is a waste of time, as we know that it explores all the nodes
and it consumes lots of memory evaluating a route that could be ignored. It means it will be
easy for us to find the shortest path, but it explores all the nodes, that's why it consumes a
lot of memory; a lot of time.
The A* algorithm is more efficient. They heuristic function to provides an estimate of the
cost of the path between each node. So in a way it makes the algorithm faster and it can
make a better choice about the next path to take and will find the shortest path. As you see
from the example, once we get the closelist neighbour, closelist node, we don't have to
explore further what we have, which is the next neighbour having the lowest f(N) value. We
consider that and we carry forward with that node.
But the challenge with the A* algorithm is the performance depends upon the accuracy of
the heuristic algorithm, which is used to compute the function h(n). It means if we have a
problem with the heuristic algorithm, we might come up with the wrong solution. But there
is a very, very less chances to come up with the wrong solution. The use of memory is more
as the Dijkstra algorithm, as it keeps all of the nodes in the memory.
In a way, both the algorithms are efficient, but both algorithms have their own cons. It
depends upon the application – when you apply any of these algorithms.
Thank you.