Professional Documents
Culture Documents
A2 Dijkstras Algorithm Shortest Path AI
A2 Dijkstras Algorithm Shortest Path AI
Dijkstra’s algorithm (pronounced dyke – strah) is a method of finding the shortest path
between two points on a graph. Each point on the graph is called a node or a vertex. It is the
basis of technology such as GPS tracking and, therefore, is an important part of Artificial
Intelligence.
This set of instructions briefly describes how it works.
Give the start vertex a final value of 0.
Calculate the distance of each neighbor from the start vertex
B
If the calculated distance of a vertex is less than the known distance, update the shortest
IS
distance
Update the previous vertex for each of the updated distances. For example, B and D are
updated via A
ol
Visit the unvisited vertex with the smallest known distance from the start vertex
ho
Sc
rt
sa
ad
He
-
Visited = [A, D, E, B, C]
d
oo
Unvisited =[A, B, C, D, E]
qs
A 0
B E63 AD
M.
C E7 E
D E1 A
s
E E2 D
au
Gh
We have the shortest total distance from the starting vertex to all the other vertices.
We also have the shortest of sequence of vertices from A to every other vertex, in other words
the shortest path. For example, to get from A to C notice that we arrived C via E this is shown
in the previous vertex column. when we examine the information of E we can see that we
arrived at E via D and we examine the information for D we can see that we arrived at D via A.
Thus, the previous vertex column actually gives us the shortest path from A to every
other vertex
REPEAT
Visit the unvisited vertex with the smallest known distance from the
start vertex
For the current vertex, examine its unvisited neighbors
For the current vertex, calculate distance of each neighbor from
B
start vertex
IS
If the calculated distance of a vertex is less than the known
distance, update the shortest distance
ol
Update the previous vertex for each of the update distances
Add the current vertex to the list of visited vertices
ho
UNTIL all vertices visited
Sc
It’s worth pointing out that Dijkstra’s shortest path algorithm is an example of a greedy
algorithm:
rt
sa
As we are selecting the next vertex to visit; the algorithm chooses the unvisited vertex with
the smallest known distance from the start vertex. The truth is we could select next unvisited
ad
vertex using pretty much any criteria we like but the assumption here is that if we select a
He
closest one to start every time, we will get to the end more quickly.
To define a greedy algorithm more, we say that it makes locally optimal choices at each
-
stage in the hope of finding the global optimum. With some graphs this greedy approach is
d
desirable particularly if we want to find the shortest path from a starting vertex to all of the
oo
others but what if we simply wanted to find the shortest path from A to E in the following
graph.
qs
Ma
M.
s
au
Exercise:
The following graph shows the routes through a large park.
Use Dijkstra’s algorithm to find the shortest path from point A to point I.
B
IS
ol
ho
Sc
rt
sa
ad
He
Unvisited [A B C D E F G]
-
Visited [A C B E D F G]
d
Shortest Pervious
oo
Vertex
distance from A vertex
qs
A 0
B ∞5 A
Ma
C ∞6 A
D ∞9 B
M.
E ∞ 11 CB
F ∞ 12 C
G ∞ 17 E
s
au
Gh
We have reached G via E, E via B and B via A (Notice previous vertex column)
A→B→E→G