You are on page 1of 3

Dijkstra’s shortest path algorithm:

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

Vertex Shortest Previous


distance from A vertex
Ma

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.

The total shortest distance from A to B is 3


The total shortest distance from A to C is 7
The total shortest distance from A to D is 1
The total shortest distance from A to E is 2

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

The shortest path is A → D → E → C

Page 1 of 3 - AI - Dijkstras shortest path algorithm - AL CS


Algorithm:

Let distance of start vertex from start vertex = 0


Let distance of all over vertices from start = infinity

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

Here the short-sighted greedy approach would result in unnecessary processing.


Gh

Refined algorithm that we can implement:

Let the distance of start vertex from start vertex = 0


Let the distance of all other vertices from start = infinity
WHILE vertices remain unvisited
Visit unvisited vertex with smallest known distance from start
vertex (call this 'current vertex')
FOR each unvisited neighbor of the current vertex
Calculate the distance from start vertex
IF the calculated distance of this vertex is less than the known
distance
Update shortest distance to this vertex
Update the previous vertex with the current vertex
ENDIF

Page 2 of 3 - AI - Dijkstras shortest path algorithm - AL CS


NEXT unvisited neighbor
Add the current vertex to the list of visited vertices
ENDWHILE

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)

So, the shortest path from A to G is A to B to E to G

A→B→E→G

Page 3 of 3 - AI - Dijkstras shortest path algorithm - AL CS

You might also like