Shortest Path Algorithm: Juhi Kesarwani Ashish Kumar Kesarwany
Shortest Path Algorithm: Juhi Kesarwani Ashish Kumar Kesarwany
May 5, 2024
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 1 / 27
Table of Contents
1 Shortest path
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 2 / 27
Shortest paths
Note
δ(u, v ) = ∞ if no path from u to v exists.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 3 / 27
d(u, v) d(u, x) + d(x, v).
Triangle inequality
Let G (V , E ) be a weighted graph. For all u, v , x ∈ V , we have
d(u, v)
u v
d(u, x) d(x, v)
x
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 4 / 27
Algorithms for shortest path
There are many algorithms for finding shortest path between a given pair of
vertices of graph. But, in our course, we’ll discuss following two algorithms
✍ Dijkstra’s Algorithm
✍ Floyd-Warshall Algorithm
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 5 / 27
Dijkstra’s Algorithm
Figure 1: Dr. Edsger Dijkstra at blackboard during a conference at ETH Zurich in 1994.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 6 / 27
The algorithm involves assigning labels to vertices. Let G = (V , E ) be a connected
weighted graph. Let a and z be any two vertices where a be the starting point and
z be the terminal point. Let L(v ) denotes the labels at vertex v . At any point
some vertices have temporary labels and rest have permanent labels. It begins with
starting vertex a by 0 and other vertices with ∞. We next label all neighbours v of
a by L(v ), is the weight of the edge from a to v . Let u be the vertex among those v
for which L(u) is minimum. Now find those neighbours w of u, and for those w not
already permanently labelled, assign the label L(w ) = L(u) + w (e), w (e) being the
weight of the edge from u to w , while for those w already labelled L(w ), change
the label to L(u) + w (e) if this is smaller. Each iteration of the algorithm changes
the status of one label from temporary to permanent, thus we may terminate the
algorithm when z receives a permanent label.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 7 / 27
Input: A connected weighted graph G . If G has self-loops, delete them. If G has
parallel edges between two vertices, delete all except that having least weight.
Step-1 Initially set the starting vertex a permanently with 0, i.e., L(a) = 0 and set
L(v ) = ∞ for all vertices v ̸= a.
Step-2 Let u be a vertex in T (Set of unvisited vertices) for which L(u) is minimum
and hence permanent label of u.
Step-3 If u = z, stop.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 8 / 27
Example
Apply Dijkstra’s algorithm to the graph given below and find the shortest path
from a to f .
b 6 d
1 2
a 2 5 3 f
4 7
c 1 e
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 9 / 27
Solution
The initial labelling is given by
Vertex V a b c d e f
L(v) 0 ∞ ∞ ∞ ∞ ∞
T {a, b, c, d, e, f}
Vertex V a b c d e f
L(v) 0 1 4 ∞ ∞ ∞
T {, b, c, d, e, f}
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 10 / 27
Iteration 2: u = b, the permanent label of b is 1. T becomes T − {b} there are
three vertices connected to b, i.e., c, d, e and c, d, e ∈ T .
L(c) = min{old L(c), L(b) + w (bc)} = min{4, 1 + 2} = 3
L(d) = min{old L(d), L(b) + w (bd)} = min{∞, 1 + 6} = 4
L(e) = min{old L(e), L(b) + w (be)} = min{∞, 1 + 5} = 6
Hence minimum label is L(c) = 3.
Vertex V a b c d e f
L(v) 0 1 3 7 6 ∞
T {, c, d, e, f}
Iteration 3: u = c, the permanent label of c is 3, T becomes T − {c}. There is
one vertex connected to c, i.e., e where e ∈ T .
L(e) = min{old L(e), L(c) + w (ce)}
= min{6, 3 + 1}
The minimum label is L(e) = 4.
Vertex V a b c d e f
L(v) 0 1 3 7 4 ∞
T { d, e, f}
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 11 / 27
Iteration 4: u = e, the permanent label of e is 4, T becomes T − {e}. There
are two vertices connected to e, i.e., d, f where d, f ∈ T .
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 13 / 27
Solution
The initial labelling is given by
Vertex V a b c d e f z
L(v) 0 ∞ ∞ ∞ ∞ ∞ ∞
T {a, b, c, d, e, f, z}
Iteration 1:
Vertex V a b c d e f z
L(v) 0 22 16 8 ∞ ∞ ∞
T { b, c, d, e, f, z}
Iteration 2:
Vertex V a b c d e f z
L(v) 0 22 16 8 ∞ 14 ∞
T { b, c, e, f, z}
Iteration 3:
Vertex V a b c d e f z
L(v) 0 21 16 8 ∞ 14 23
T { b, c, e, z}
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 14 / 27
Iteration 4:
Vertex V a b c d e f z
L(v) 0 21 16 8 20 14 23
T { b, e, z}
Iteration 5:
Vertex V a b c d e f z
L(v) 0 21 16 8 20 14 23
T { b, z}
Iteration 6:
Vertex V a b c d e f z
L(v) 0 21 16 8 20 14 23
T { z}
Since u = z, the only choice, iteration stops. Thus, the length of the shortest
path is 23.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 15 / 27
Exercise 1:
Determine the shortest path between the vertices A to F as shown below.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 16 / 27
Exercise 2:
Determine the shortest path between the vertices 0 to 6 as shown below.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 17 / 27
Floyd-Warshall Algorithm
The Floyd-Warshall Algorithm is used to find shortest path between all pair of
vertices in weighted graph.This algorithm works for both the directed and undirected
weighted graphs. But, it does not work for the graphs with negative cycles (where
the sum of the edges in a cycle is negative). One need to follow the steps below to
find the shortest path between all the pairs of vertices.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 18 / 27
Step 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. aij is filled with (aik + akj ) if (aij > aik + akj ).
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 aik + akj.
Step 3: Similarly, A2 is created using A1 . The elements in the second column and
the second row are left as they are.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 19 / 27
Example
Find the shortest path between all pair of vertices using Floyd-Warshall Algorithm
in the graph below.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 20 / 27
Solution
1 2 3 4
1 0 3 ∞ 5
2 2 0 ∞ 4
0
A =
3 ∞ 1 0 ∞
4 ∞ ∞ 2 0
1 2 3 4 1 2 3 4
1 0 3 ∞ 5 1 0 3 ∞ 5
2 2 0 2 2 0 ∞ 4
1 −→
A =
3 ∞ 0 3 ∞ 1 0 ∞
4 ∞ 0 4 ∞ ∞ 2 0
Next, we will find A with the help of A1 .
2
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 21 / 27
1 2 3 4 1 2 3 4
0 1 3 1 0 3 ∞ 5
2 2 0 ∞ 4 2 2 0 ∞ 4
2 −→
A =
3 1 0 3 3 1 0 5
4 ∞ 0 4 ∞ ∞ 2 0
1 2 3 4 1 2 3 4
0 1 ∞ 5 1 0 3 ∞ 5
2 0 ∞ 2 2 0 ∞ 4
3 −→
A =
3 3 1 0 5 3 3 1 0 5
4 2 0 4 5 3 2 0
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 22 / 27
Finally, we find A4 from A3
1 2 3 4 1 2 3 4
0 1 5 1 0 3 7 5
2 0 4 2 2 0 6 4
4 −→
A =
3 0 5 3 3 1 0 5
4 5 3 2 0 4 5 3 2 0
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 23 / 27
Example
Find the shortest path between all pair of vertices using Floyd-Warshall Algorithm
in the graph below.
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 24 / 27
Solution
A B C D E A B C D E
A 0 4 ∞ 5 ∞ A 0 4 ∞ 5 ∞
B ∞ 0 1 ∞ 6 B ∞ 0 1 ∞ 6
0 1
A = C
2 ∞ 0 3 ∞,
A = C
2 6 0 3 12
D ∞ ∞ 1 0 2 D ∞ ∞ 1 0 2
E 1 ∞ ∞ 4 0 E 1 5 ∞ 4 0
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 25 / 27
A B C D E A B C D E
A 0 4 5 5 10 0 A 4 5 5 10
B ∞ 0 1 ∞ 6 B 3 0 1 4 6
A2 = C
2 6 0 3 12,
A3 = C
2 6 0 3 12
D ∞ ∞ 1 0 2 D 3 7 1 0 2
E 1 5 6 4 0 E 1 5 6 4 0
A B C D E A B C D E
A 0 4 5 5 7 A 0 4 5 5 7
B 3 0 1 4 6 B 3 0 1 4 6
4 5
A = C
2 6 0 3 5 ,
A = C
2 6 0 3 5
D 3 7 1 0 2 D 3 7 1 0 2
E 1 5 5 4 0 E 1 5 5 4 0
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 26 / 27
Exercise 3:
Find the shortest path between all pair of vertices using Floyd-Warshall Algorithm
in the graph below.
0 5 6 6 9
4 0 1 5 7
Ans: A5 =
3 8 0 4 7
5 10 2 0 3
2 7 7 5 0
Juhi Kesarwani & Ashish Kumar Kesarwany (VITB) Shortest path algorithm May 5, 2024 27 / 27