You are on page 1of 33

A.R. Hadaegh California State University San Marcos (CSUSM) Page

1
Finding
Shortest
Path
A.R. Hadaegh California State University San Marcos (CSUSM) Page

2
Finding Shortest Path

Finding the shortest path is a classical problem in graph theory

Edges of a graph are assigned certain weights representing for
example,
Distance between cities
Times separating the execution of programs
Costs of transmitting data from one site to another

When determining the shortest path from vertex v to vertex u,
information about distances between intermediate vertices w, has
to be recorded

Several algorithms have been proposed to solve the shortest path
problem
A.R. Hadaegh California State University San Marcos (CSUSM) Page

3

In this course we discuss 3 algorithms commonly used to
find the shortest path:
Dijkstra Algorithm
Ford Algorithm
WFI Algorithm (created by R. Floyed and P. Ingerman)

Dijkstra and Ford algorithms find shortest path from vertex
v to any other vertex in the graph.

WFI Algorithm finds shortest path from any vertex to any
other vertices
A.R. Hadaegh California State University San Marcos (CSUSM) Page

4
Dijkstra Algorithm: Finding Shortest Path

This algorithm find the shortest path from vertex called first to
any other vertex in a directed graph

DijkstraAlgorith(weighted simple diagraph, vertex first)
for all vertices v
currDist(v) = infinity;
currDist(first) = 0;
toBeChecked = all vertices;

while toBeChecked is not empty
v = a vertex in toBeChecked with minimal currDist(v);
remove v from toBeChecked;

for all vertices u adjacent to v and in toBeChecked
if (currDist(u) > currDist(v) + weight(edeg(vu) )
currDist(u) = currDist(v) + weight(edge(vu));
Predecessor(u) = v;
A.R. Hadaegh California State University San Marcos (CSUSM) Page

5
In this algorithm, we can create two arrays:
predecessor array to keep track of the predecessor of
the vertices
For example if the shortest path from
vertex a to vertex b is: [a, v
1
, v
2
, .. v
n
, b],
then the predecessor of b is v
n
.
CurrDist array to keep track of the current distances of
each vertex to vertex first
To start, based on the algorithm, we start from the source
(vertex that is called first in the algorithm)

Then using the weigh of the edges with adjacent vertices,
we update the current Distance of adjacent vertices

Then we choose the vertex with smallest current Distance
and repeat the process

We continue this process until all vertices are checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

6
We try to find the shorted path from any vertex to vertex d in this example

The adjacent vertices to d are a and h.
Moving from d to a makes the current distance of a to be 0+4=4
This is accepted because 4 is less than infinity (current distance of a)
This makes the predecessor of a to be d
Also, moving from d to h makes the distance of h to be 0+1=1
This is accepted because 1 is less than infinity (current distance of h)
This makes the predecessor of h to be d
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
Null
Null
Null
Null
Null
Null
Null
Null
Null
Null
A.R. Hadaegh California State University San Marcos (CSUSM) Page

7
Next we pick the vertex with the smallest current distance that is not checked
As shown in the array, this vertex is h
The adjacent vertices to h are e and i.
Moving from h to e makes the current distance of e to be 1+5=6
This is accepted because 6 is less than infinity (current distance of e)
This makes the predecessor of e to be h
Also, moving from h to i makes the distance of i to be 1+9=10
This is accepted because 10 is less than infinity (current distance of i)
This makes the predecessor of i to be h
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
Null
Null
d
Null
Null
Null
Null
Null
Null
4
1
Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

8
Next we pick the vertex with the smallest current distance that is not checked
As shown in the array, this vertex is a
The adjacent vertices to a are h and e.
Moving from a to e makes the current distance of e to be 4+1=5
This is accepted because 5 is less than 6 (current distance of e)
This makes the predecessor of e to be a
Also, moving from a to h makes the distance of h to be 4+10=14
This is not accepted because 14 is greater than 1 (current distance of h)
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
Null
Null
d
h
Null
Null
Null
h
Null
4
1
6
10
Checked
Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

9
Next we pick the vertex with the smallest current distance that is not checked

As shown in the array, this vertex is e

The only adjacent vertex to e is f.

Moving from e to f makes the current distance of f to be 5+3=8
This is accepted because 8 is less than infinity (current distance of f)
This makes the predecessor of f to be e
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
Null
Null
d
h
Null
Null
Null
a
Null
4
1
5
10
Checked
Checked
Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

10
Next we pick the vertex with the smallest current distance that is not checked
As shown in the array, this vertex is f
The adjacent vertices to f are b, c, g, and i.
Moving from f to b makes the current distance of b to be 8+1=9
This is accepted because 9 is less than infinity (current distance of b)
This makes the predecessor of b to be f
Moving from f to i makes the current distance of i to be 8+1=9
This is accepted because 9 is less than 10 (current distance of i)
This makes the predecessor of i to be f
Moving from f to g makes the current distance of g to be 8+7=15
This is accepted because 15 is less than infinity (current distance of g)
This makes the predecessor of g to be f
Moving from f to c makes the current distance of c to be 8+3=11
This is accepted because 11 is less than infinity (current distance of c)
This makes the predecessor of c to be f
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
Null
d
h
Null
Null
Null
a
Null
4
1
5
10
Checked
Checked
Checked
Checked
8
A.R. Hadaegh California State University San Marcos (CSUSM) Page

11
Next we pick the vertex with the smallest current distance that is not checked

As shown in the array, this vertex is either b or i. We can pick any of them. So
we select b.

The only adjacent vertex to b is c.

Moving from b to c makes the current distance of c to be 9+2=11
This is not accepted because 11 is not less than 11 (current distance of c)
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
f
d
f
Null
Null
f
a
f
4
1
5
9
Checked
Checked
Checked
Checked
8
Checked
15
11
9
A.R. Hadaegh California State University San Marcos (CSUSM) Page

12
Next we pick the vertex with the smallest current distance that is not checked

As shown in the array, this vertex is i.

The only adjacent vertex to i is j.

Moving from i to j makes the current distance of j to be 9+2=11
This is accepted because 11 is less than infinity (current distance of j)
This makes the predecessor of j to be i
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
f
d
f
Null
Null
f
a
f
4
1
5
9
Checked
Checked
Checked
Checked
8
Checked
15
11
9 Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

13
Next we pick the vertex with the smallest current distance that is not checked

As shown in the array, this vertex is either c or j. We select c

There is no adjacent vertex to c
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
f
d
f
i
Null
f
a
f
4
1
5
9
Checked
Checked
Checked
Checked
8
Checked
15
11
9 Checked
Checked
11
A.R. Hadaegh California State University San Marcos (CSUSM) Page

14
Next we pick the vertex with the smallest current distance that is not checked

As shown in the array, this vertex is j.

The only adjacent vertex to j is g.

Moving from j to g makes the current distance of g to be 11+1=12
This is accepted because 12 is less than 15 (current distance of g)
This makes the predecessor of g to be j
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
f
d
f
i
Null
f
a
f
4
1
5
9
Checked
Checked
Checked
Checked
8
Checked
15
11
9 Checked
Checked
11
Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

15
Next we pick the vertex with the smallest current distance that is not checked

As shown in the array, this vertex is g.

There is no adjacent vertex to g
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
j
d
f
i
Null
f
a
f
4
1
5
9
Checked
Checked
Checked
Checked
8
Checked
12
11
9 Checked
Checked
11
Checked
Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

16
All vertices are now checked. There is nothing left and we are done.
The CurrDist array shows us the minimum distance from d to any vertex
The predecessor array show us how to find the path from d to any vertex. For
example to find the path from d to j,
We start from j and find the predecessor of j which is i
then find the predecessor of i which f
then find the predecessor of f which is e
then find the predecessor e which is a
and finally the predecessor of a which is d
Therefore the path from d to j is
d a e f i j
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
a
b
c
d
e
f
g
h
i
j
a
b
c
d
e
f
g
h
i
j
Predecessor CurrDist
0
d
e
j
d
f
i
Null
f
a
f
4
1
5
9
Checked
Checked
Checked
Checked
8
Checked
12
11
9 Checked
Checked
11
Checked
Checked
Checked
A.R. Hadaegh California State University San Marcos (CSUSM) Page

17
You can show the steps of
Dijkstra Algorithm all in one
page as shown below
4 4
9
11 11 11
0
6 5
8
15 15 15 15 12
1
10 10 10 9 9
11 11
d h a e f b i c j g
a
b
c
d
e
f
g
h
i
j
init 1 2 3 4 5 6 7 8 9 10
d
a
h
e
c
f
b
g
i j
4
1
10
1
5
9
3
1
2
3
7
1
2
1
A.R. Hadaegh California State University San Marcos (CSUSM) Page

18
Complexity of Dijkstra Algorithm

The complexity of Dijkstra algorithm is O(|V
2
|)

The first for loop and the while loop are executed |V| times

For each iteration of the while loop
A vertex in toBeChecked list with minimal current
distance has to be found which requires O(|V|) steps
The for loop iterates deg(v) times which is also O(|V|)

Problem:
Dijkstra algorithm only works for positive weights and
it fails when negative weights are used in graphs
A.R. Hadaegh California State University San Marcos (CSUSM) Page

19
Ford Algorithm

Like Dijkstra Algorithm, Ford Algorithm uses the same method for
setting the current distances

However, Ford Algorithm does not permanently determine the
shortest distance for any vertex until it processes the entire graph

Ford Algorithm, accepts both positive and negative weights but does
not accept graphs with negative cycles

The pseudocode is:

FordAlgorithm(weighted simple diagraph, vertex first)
for all vertices v
currDist(v) = infinity;
currDist(first) = 0;
while there is an edge(vu) such that currDist(u) > currDist(v) + weight(edge(vu))
currDist(u) = currDist(v) + weight(edge(vu));
A.R. Hadaegh California State University San Marcos (CSUSM) Page

20
c
h
d
b
e
a
f
g
i
-1
1
1
4
-1
1
-5
2
1
4
1
1
The edges are:
ab, be, cd, cg, ch, da,
de, di, ef, gd, hg, if
a
b
c
d
e
f
g
h
i
0
Apply ab
a
b
c
d
e
f
g
h
i
0
Apply be
a
b
c
d
e
f
g
h
i
0
Apply cd
a
b
c
d
e
f
g
h
i
0
Apply cg
a
b
c
d
e
f
g
h
i
0
Apply ch
a
b
c
d
e
f
g
h
i
0
Apply da
a
b
c
d
e
f
g
h
i
0
Apply de
a
b
c
d
e
f
g
h
i
0
Apply di
a
b
c
d
e
f
g
h
i
0
Apply ef
a
b
c
d
e
f
g
h
i
0
Apply gd
a
b
c
d
e
f
g
h
i
0
Apply hg
a
b
c
d
e
f
g
h
i
0
Apply if
1
1
1
1
1
3
1
1
5
3
1
1
2
3
1
5
1
9
3
1
5
1
2
1 1 1 1 1 1
3
0
5
9
1
2
3
0
0
5
9
1
2
0
5
3
1
2
0
3
First Iteration
A.R. Hadaegh California State University San Marcos (CSUSM) Page

21
c
h
d
b
e
a
f
g
i
-1
1
1
4
-1
1
-5
2
1
4
1
1
The edges are:
ab, be, cd, cg, ch, da,
de, di, ef, gd, hg, if
Second Iteration
a
b
c
d
e
f
g
h
i
0
Apply ab
0
5
3
1
2
0
3
4
a
b
c
d
e
f
g
h
i
0
Apply be
0
-1
3
1
2
0
3
4
a
b
c
d
e
f
g
h
i
0
Apply cd
0
-1
3
1
2
0
3
4
a
b
c
d
e
f
g
h
i
0
Apply cg
0
-1
3
1
2
0
3
4
a
b
c
d
e
f
g
h
i
0
Apply ch
0
-1
3
1
2
0
3
4
a
b
c
d
e
f
g
h
i
0
Apply da
0
-1
3
1
2
0
2
4
a
b
c
d
e
f
g
h
i
0
Apply de
0
-1
3
1
2
0
2
4
a
b
c
d
e
f
g
h
i
0
Apply di
0
-1
3
1
1
0
2
4
a
b
c
d
e
f
g
h
i
0
Apply ef
0
-1
3
1
1
0
2
4
a
b
c
d
e
f
g
h
i
0
Apply gd
0
-1
3
1
1
-1
2
4
a
b
c
d
e
f
g
h
i
0
Apply hg
0
-1
3
1
1
-1
2
4
a
b
c
d
e
f
g
h
i
0
Apply if
0
-1
2
1
1
-1
2
4
A.R. Hadaegh California State University San Marcos (CSUSM) Page

22
c
h
d
b
e
a
f
g
i
-1
1
1
4
-1
1
-5
2
1
4
1
1
The edges are:
ab, be, cd, cg, ch, da,
de, di, ef, gd, hg, if
Third Iteration
a
b
c
d
e
f
g
h
i
0
Apply ab
0
-1
2
1
1
-1
2
3
a
b
c
d
e
f
g
h
i
0
Apply be
0
-2
2
1
1
-1
2
3
a
b
c
d
e
f
g
h
i
0
Apply cd
0
-2
2
1
1
-1
2
3
a
b
c
d
e
f
g
h
i
0
Apply cg
0
-2
2
1
1
-1
2
3
a
b
c
d
e
f
g
h
i
0
Apply ch
0
-2
2
1
1
-1
2
3
a
b
c
d
e
f
g
h
i
0
Apply da
0
-2
2
1
1
-1
1
3
a
b
c
d
e
f
g
h
i
0
Apply de
0
-2
2
1
1
-1
1
3
a
b
c
d
e
f
g
h
i
0
Apply di
0
-2
2
1
0
-1
1
3
a
b
c
d
e
f
g
h
i
0
Apply ef
0
-2
2
1
0
-1
1
3
a
b
c
d
e
f
g
h
i
0
Apply gd
0
-2
2
1
0
-1
1
3
a
b
c
d
e
f
g
h
i
0
Apply hg
0
-2
2
1
0
-1
1
3
a
b
c
d
e
f
g
h
i
0
Apply if
0
-2
1
1
0
-1
1
3
A.R. Hadaegh California State University San Marcos (CSUSM) Page

23
c
h
d
b
e
a
f
g
i
-1
1
1
4
-1
1
-5
2
1
4
1
1
The edges are:
ab, be, cd, cg, ch, da,
de, di, ef, gd, hg, if
Fourth Iteration
a
b
c
d
e
f
g
h
i
0
Apply da
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply ab
0
-2
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply be
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply cd
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply cg
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply ch
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply de
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply di
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply ef
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply gd
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply hg
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply if
0
-3
1
1
0
-1
1
2
A.R. Hadaegh California State University San Marcos (CSUSM) Page

24
c
h
d
b
e
a
f
g
i
-1
1
1
4
-1
1
-5
2
1
4
1
1
The edges are:
ab, be, cd, cg, ch, da,
de, di, ef, gd, hg, if
Fifth Iteration
a
b
c
d
e
f
g
h
i
0
Apply ch
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply if
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply ab
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply be
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply cd
0
-3
1
1
0
-1
1
2
a
b
c
d
e
f
g
h
i
0
Apply cg
0
-3
1
1
0
-1
1
2
.. .
Note: In this iteration nothing changes. This is the sign that
shows we are done and have found the minimum distances
A.R. Hadaegh California State University San Marcos (CSUSM) Page

25
In the example of the Ford algorithm, we ordered the edges
alphabetically. However, the order of the edges should have no
effect on the final result

In this example, we showed that we do as many as iterations as
required in order to find the minimum distance from the source
vertex (called first in the algorithm) to any other vertex.

Note that the current distance of some vertices changed more
than once even within the same iteration
A.R. Hadaegh California State University San Marcos (CSUSM) Page

26
Complexity of Ford Algorithm

The computational complexity of this algorithm is O(|V|*|E|)
because

there would be at most |V| - 1 passes through the sequence
of |E| edges since |V| -1 is the largest number of edges in
any path

in the first pass, at least one-edge paths are determined

in the second pass, all two-edges paths are determined

and so on
A.R. Hadaegh California State University San Marcos (CSUSM) Page

27
You can show the steps of
Ford Algorithm all in one
page as shown below
3 3 2 1
4 3 2
0
1 0 -1
5 5 -1 -2 -3
9 3 2 1
1 0
1
2 2 1 0
a
b
c
d
e
f
g
h
i
Iterations
init 1 2 3 4
c
h
d
b
e
a
f
g
i
-1
1
1
4
-1
1
-5
2
1
4
1
1
The edges are:
ab, be, cd, cg, ch, da,
de, di, ef, gd, hg, if
A.R. Hadaegh California State University San Marcos (CSUSM) Page

28
All to All Shortest Path

Dijkstra and Ford Algorithms only determine the shortest
path from a particular vertex to other vertices in the graph

Robert W, Floyed and P.Z Ingerman introduced an
algorithm called WFIAlgorithm that determine the shortest
path from any vertex v
i
to any other vertex v
j

The pseudocode is:

WFIAlgorithm (matrix weight)
for i =1 to |V|
for j = 1 to |V|
for k = 1 to |V|
if weight[j][k] > weight[j][i] + weigh[i][k]
weight[j][k] = weight[j][i] + weigh[i][k];
A.R. Hadaegh California State University San Marcos (CSUSM) Page

29

In the WFI algorithm, the outer loop refers to the vertices
which may be on a path between the vertex with the index j
and the vertex with index k

For example, in the first iteration, when i=1, all paths
v
j
. v
1
, .., v
k
are considered, and if there is currently no
path from v
j
to v
k
and v
k
is reachable from v
j
, the path is
established with its weight equal to p where p is:

p = weight(path(v
j
, v
1
)) + weight (path(v
1
, v
k
))

or current weight of this path, weight(v
j
,v
k
) is changed to p
if p is less than weight(path(v
j
,v
k
))
A.R. Hadaegh California State University San Marcos (CSUSM) Page

30
b
d
a
e
c -4
2
1
4
1
3
-2
Step 1:

We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex a
0 2 -4
0 -2 1 3
0 1
0 4
0
a
b
c
d
e
a b c d e
b
d
a
e
c
A.R. Hadaegh California State University San Marcos (CSUSM) Page

31
b
d
a
e
c -4
2
1
4
1
3
-2
Step 2:

We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex b
0 2 0 -4 5
0 -2 1 3
0 1
0 4
0
a
b
c
d
e
a b c d e
b
d
a
e
c
The red arrows mean:
- it is cheaper to go from a to c via b
- Also it is cheaper to go from a to e via b
A.R. Hadaegh California State University San Marcos (CSUSM) Page

32
b
d
a
e
c -4
2
1
4
1
3
-2
Step 3:

We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex c
0 2 0 -4 1
0 -2 1 -1
0 1
0 4
0
a
b
c
d
e
a b c d e
b
d
a
e
c
The red arrows mean:
- it is cheaper to go from a to c via b
- Also it is cheaper to go from b to e via c
- and so far cheapest path from a to e is
a b c e
A.R. Hadaegh California State University San Marcos (CSUSM) Page

33
b
d
a
e
c -4
2
1
4
1
3
-2
Step 4:

We show how to go from a vertex specified in a row to a vertex specified in a
column via vertex d
0 2 0 -4 0
0 -2 1 -1
0 1
0 4
0
a
b
c
d
e
a b c d e
b
d
a
e
c
The red arrows mean:
- it is cheaper to go from a to c via b
- Also it is cheaper to go from b to e via c
- Also it is cheaper to go from a to e via d