Shortest Path Algorithms
Shortest Path Algorithms
SHORTEST PATH
Introduction
20
E G
inf
inf
inf inf
15
B D
5
0 3
4 12
A
8 C
inf 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
inf
inf inf
15
B D
5
0 3
4 12
A
8 C
inf 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
inf
Node B: decide what is smaller 0+5 or inf ... 5 is smaller so UPDATE
+ we have to track predecessor when we update ( if we do not update, we don’t )
inf inf
15
B D
5
0 3
4 12
A
8 C predecessor of B
inf 7
is A
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
inf
5 inf
15
B D
5
0 3
4 12
A
8 C
inf 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
inf
Node H: decide what is smaller 0+8 or inf ... 8 is smaller so UPDATE
5 inf
15
B D
5
0 3
4 12
A
C predecessor of H
8
7 is A
8
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
inf
Node E: decide what is smaller 0+9 or inf ... 9 is smaller so UPDATE
5 inf
15
B D
5
0 3
4 12
A
C predecessor of E
8
7 is A
8
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
B–5 ; H–8; E-9
5 inf
15
B D
5
0 3
4 12
A
8 C
8 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
B–5 ; H–8; E-9
5 inf
15
B D
5
0 3
4 12
A
8 C
8 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
H–8; E-9
5 inf
15
B D
5
0 3
4 12
A
8 C
8 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
H–8; E-9
5 inf
15
B D
5
0 3
4 12
A
8 C
8 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node D: decide what is smaller 5+15 or inf ... 20 is smaller so UPDATE
H–8; E-9
5 inf
15
B D
5
0 3
4 12
A
8 C
8 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node D: decide what is smaller 5+15 or inf ... 20 is smaller so UPDATE
H–8; E-9
5 20
15
B D
5
0 3
4 12
A
C predecessor of D
8
7 is B
8
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node C: decide what is smaller 5+12 or inf ... 17 is smaller so UPDATE
H–8; E-9
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
inf
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node C: decide what is smaller 5+12 or inf ... 17 is smaller so UPDATE
H–8; E-9
5 20
15
B D
5
0 3
4 12
A
C predecessor of C
8
7 is B
8
17
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node H: decide what is smaller 5+4 or 8 ... 8 is smaller so DO NOT UPDATE
H–8; E-9
5 20
15
B D
5
0 3
4 12
A
C predecessor of H
8
7 remanins A because
8
17 we do not update !!!
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node H: decide what is smaller 5+4 or 8 ... 8 is smaller so DO NOT UPDATE
H–8; E-9
5 20
15
B D
5
0 3
4 12
A
C predecessor of H
8
7 remanins A because
8
17 we do not update !!!
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
H – 8 ; E – 9 ; C – 17 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
17
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
H – 8 ; E – 9 ; C – 17 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
17
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
E – 9 ; C – 17 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
17
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
E – 9 ; C – 17 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
17
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node C: decide what is smaller 8+7 or 17 ... 15 is smaller so UPDATE
E – 9 ; C – 17 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
17
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node C: decide what is smaller 8+7 or 17 ... 15 is smaller so UPDATE
E – 9 ; C – 15 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node F: decide what is smaller 8+6 or inf ... 14 is smaller so UPDATE
E – 9 ; C – 15 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 inf 11
6
5
F
13
4
20
E G
inf
9
Node F: decide what is smaller 8+6 or inf ... 14 is smaller so UPDATE
E – 9 ; C – 15 ; D – 20
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
E – 9 ; C – 15 ; D – 20 ; F – 14
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
E – 9 ; C – 15 ; D – 20 ; F – 14
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
C – 15 ; D – 20 ; F – 14
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
C – 15 ; D – 20 ; F – 14
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
Node H: decide what is smaller 9+5 or 8 ... 8 is smaller so DO NOT UPDATE
C – 15 ; D – 20 ; F – 14
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
Node F: decide what is smaller 9+4 or 14 ... 13 is smaller so UPDATE
C – 15 ; D – 20 ; F – 14
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 14
5
F
13
4
20
E G
inf
9
Node F: decide what is smaller 9+4 or 14 ... 13 is smaller so UPDATE
C – 15 ; D – 20 ; F – 13
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
inf
9
Node G: decide what is smaller 9+20 or inf ... 29 is smaller so UPDATE
C – 15 ; D – 20 ; F – 13
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
inf
9
Node G: decide what is smaller 9+20 or inf ... 29 is smaller so UPDATE
C – 15 ; D – 20 ; F – 13 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
C – 15 ; D – 20 ; F – 13 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
C – 15 ; D – 20 ; F – 13 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
C – 15 ; D – 20 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
Node C: decide what is smaller 13+1 or 15 ... 14 is smaller so UPDATE
C – 15 ; D – 20 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
15
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
Node C: decide what is smaller 13+1 or 15 ... 14 is smaller so UPDATE
C – 14 ; D – 20 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
Node G: decide what is smaller 13+13 or 29 ... 26 is smaller so UPDATE
C – 14 ; D – 20 ; G – 29
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
29
9
Node G: decide what is smaller 13+13 or 29 ... 26 is smaller so UPDATE
C – 14 ; D – 20 ; G – 26
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
C – 14 ; D – 20 ; G – 26
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
C – 14 ; D – 20 ; G – 26
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
D – 20 ; G – 26
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
Node D: decide what is smaller 14+3 or 20 ... 17 is smaller so UPDATE
D – 20 ; G – 26
5 20
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
Node D: decide what is smaller 14+3 or 20 ... 17 is smaller so UPDATE
D – 17 ; G – 26
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
Node G: decide what is smaller 14+11 or 26 ... 25 is smaller so UPDATE
D – 17 ; G – 26
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
26
9
Node G: decide what is smaller 14+11 or 26 ... 25 is smaller so UPDATE
D – 17 ; G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
D – 17 ; G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
D – 17 ; G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
Node G: decide what is smaller 15+17 or 25 ... 25 is smaller so DO NOT UPDATE
G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
G – 25
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
We have constructed the shortest path tree: we just have to calculated once, than reuse it
as many times as we want !!!
5 17
15
B D
5
0 3
4 12
A
8 C
8 7
14
H 9
1
9 11
6 13
5
F
13
4
20
E G
25
9
SHORTEST PATH
BELLMAN-FORD ALGORITHM
Bellman Ford Algorithm
C B
-10
Real life scenarios: no negative cycles at all ... but sometimes we transform a problem into a graph
with positive / negative edge weights and looking for some negative cycles !!!
1970: Yen optimization
If the graph is a DAG, so there is no directed cycles, it is easier to find the shortest
path
We sort the vertices into topological order: we iterate throught the topological
order relaxing all edges from the actual vertex
Topological sort algorithm computes shortest path tree in any edge weighted (can
be negative!!!) DAG in time O(E+V)
It is much faster than Bellman-Ford or Dijkstra
Applications: solving Knapsack problem
Example
SHORTEST PATH
ALL PAIRS SHORTEST PATH(FLOYD WARSHALL ALGORITHM)
Floyd Warshall Algorithm
Each cell A[i][j] is filled with the distance from the ith vertex to the jth vertex. If there is
no path from ith vertex to jth vertex, the cell is left as infinity.
2- Now, create a matrix A1 using matrix A0. The elements in the first column and the first
row are left as they are. The remaining cells are filled in the following way.
Let k be the intermediate vertex in the shortest path from source to destination. In this
step, k is the first vertex. A[i][j] is filled with (A[i][k] + A[k][j]) if (A[i][j] > A[i][k] +
A[k][j]).
That is, if the direct distance from the source to the destination is greater than the path
through the vertex k, then the cell is filled with A[i][k] + A[k][j].
In this step, k is vertex 1. We calculate the distance from source vertex to destination
vertex through this vertex k.
Calculate the distance from the source vertex to destination vertex through this vertex k
For example: For A1[2, 4], the direct distance from vertex 2 to 4 is 4 and the sum of the
distance from vertex 2 to 4 through vertex (ie. from vertex 2 to 1 and from vertex 1 to 4)
is 7. Since 4 < 7, A0[2, 4] is filled with 4.
3- Similarly, A2 is created using A1. The elements in the second column and the second
row are left as they are.
In this step, k is the second vertex (i.e. vertex 2). The remaining steps are the same as in
step 2.
4- Similarly, A3 is also created.
4- Similarly, A4 is also created.
5- A4 gives the shortest path between each pair of vertices.
TRAVELLING
SALESMAN
PROBLEM
TSP
Given a list of cities and the distances between each pair of cities, what is the
shortest possible route that visits each city exactly once and returns to the origin
city
NP-hard problem in combinatorial optimization
N cities -> N! permutations, so brute force methods cannot be used
It is possible that the worst-case running time for any algorithm for the TSP
increases exponentially with the number of cities
Hamilton path: a path in an undirected or directed graph that visits
each vertex exactly once
Hamiltonian cycle: Hamiltonian path that is a cycle
HERE: we are looking for the shortest path Hamiltonian cycle basically !!!
We are looking for a Hamiltonian cycle but with the shortest
overall cost possible !!!
1
A B
2
2
5 3
C D
1
We are looking for a Hamiltonian cycle but with the shortest
overall cost possible !!!
1
A B
2
2
5 3
C D
1
Total cost: 10
We are looking for a Hamiltonian cycle but with the shortest
overall cost possible !!!
1
A B
2
2
5 3
C D
1
The most direct solution would be to try all permutations, this is the brute-force
search
O(n!) running time: impractical even for 20 cities
Dynamic programing approach, Held-Karp algorithm solve the problem
Not so fast too
Principle: we just want to get an approximate solution, it does not need to be the
best
It can be done with Monte-Carlo based simulations
Methods for solving TSP
Choose a random tour: not so efficient
Greedy search: we always choose the next nearest city
2-opt-solution: generate a random tour take a route that crosses over itself and
reorder it so that it does not
If the tour overall distance can not be enhanced with swap of edges then terminate
Simulated annealing: it can be good !!!
It can avoid local minimums
It is like Metropolis-Hastings algorithm: we allow non-optimal subsolutions too with
given probability ( this probability is constant !!! )
Simulated annealing
Nearest Neighbor Method
TRAVELLING
SALESMAN
PROBLEM
NEAREST NEIGHBOR METHOD
This procedure gives reasonably good results for the travelling salesman problem. The
method is as follows:
Step 1: Select an arbitrary vertex and find the vertex that is nearest to this starting
vertex to form an initial path of one edge.
Step 2: Let v denote the latest vertex that was added to the path. Now, among the
result of the vertices that are not in the path, select the closest one to v and add the
path, the edge-connecting v and this vertex. Repeat this step until all the vertices
of graph G are included in the path.
Step 3: Join starting vertex and the last vertex added by an edge and form the
circuit.
Let the problem be the given graph
1-Select edge V1-V3
2-Select edge V3-V2
3-Select edge V2-V4
4-Select edge V4-V5
5-Select edge V5-V1