Graph Theory III
Graph Theory III
Theory-III
DR. ASHESH PAUL
Distance Between Two Vertices:
If 𝐺 be a connected graph then the diameter of the graph is the length of the
maximum distance between any two vertices 𝒖 and 𝒗 of 𝑮 and is denoted by dim 𝐺 .
This algorithm determines the shortest path between two given vertices.
Suppose 𝑣𝑖 and 𝑣𝑗 be two vertices which are connected by an edge 𝑒𝑖𝑗 . Then
the weight associated to 𝑒𝑖𝑗 will be denoted by 𝑤𝑖𝑗 .
The algorithm is an iterative process and involve some steps.
Steps of Dijkstra’s Algorithm
Step – I
If the given graph is not simple, turn it to a simple graph by discarding all the self loops
and parallel edges.
Note: While discarding the parallel edges keep the edge which has the smallest weight.
Let us describe the steps by an example. Suppose we are asked to find the shortest path
between 𝒗𝟐 and 𝒗𝟓 of the following graph 𝑮:
Step - I
Step-II
If we are finding the shortest distance between the two vertices 𝑣𝑖 and 𝑣𝑘 , we start with any one
of 𝑣𝑖 or 𝑣𝑘 . Let we have started with 𝑣𝑖 .
Now assign labels to the vertices by the following rule:
The starting vertex 𝒗𝒊 is permanently labelled as 𝟎.
All other vertices are temporarily labelled as ∞.
The minimum label of this step is 𝒅 = 𝟎
In the given example we start with vertex 𝑣2 . So 𝑣2 is permanently labelled as 0. All other
Vertices are temporarily labelled as ∞.
𝑣1 𝑣2 𝑣3 𝑣4 𝑣5 𝑣6 𝑣7
Step 1 ∞ 0 ∞ ∞ ∞ ∞ ∞
Step-III
Find the minimum label
Every vertex 𝒗𝒋 which are not yet permanently labelled gets a new temporary label
𝑣1 𝑣2 𝑣3 𝑣4 𝑣5 𝑣6 𝑣7
Row 1 ∞ 0 ∞ ∞ ∞ ∞ ∞
Row 2 min ∞, 1 + 0 × min ∞, 7 + 0 min ∞, ∞ + 0 min ∞, ∞ + 0 min ∞, ∞ + 0 min ∞, ∞ + 0
=1 =7 =∞ =∞ =∞ =∞
Row 3 × × min 7, 3 + 1 min ∞, ∞ + 1 min ∞, ∞ + 1 min ∞, 3 + 1 min ∞, 4 + 1
=4 =∞ =∞ =4 =5
Row 4 × × × min ∞, 8 + 4 min ∞, ∞ + 4 min 4, ∞ + 4 min 5, ∞ + 4
= 12 =∞ =4 =5
Row 5 × × × min 12, ∞ + 4 min ∞, 7 + 4 × min 5, 4 + 4
= 12 = 11 =5
Row 6 × × × min 12, 3 + 5 min 11, 2 + 5 × ×
=8 =7
Step-IV
As 𝑣5 is permanently labelled now look straight along the column of 𝑣5 .
Search where the value got changed. Here the value changed immediately in
the previous row, i.e., in row 𝟓.
Now, look for the vertex in this row which is permanently labelled. Here it is 𝒗𝟕 .
Next look upwards through this column and repeat the process until you reach the
initial vertex 𝒗𝟐 .
Thus we get the desired shortest path as 𝒗𝟓 → 𝒗𝟕 → 𝒗𝟏 → 𝒗𝟐
Weight for this path is 2+4+1=7
𝑣1 𝑣2 𝑣3 𝑣4 𝑣5 𝑣6 𝑣7
Row 1 ∞ 0 ∞ ∞ ∞ ∞ ∞
Row 2 min ∞, 1 + 0 × min ∞, 7 + 0 min ∞, ∞ + 0 min ∞, ∞ + 0 min ∞, ∞ + 0 min ∞, ∞ + 0
=1 =7 =∞ =∞ =∞ =∞
Row 3 × × min 7, 3 + 1 min ∞, ∞ + 1 min ∞, ∞ + 1 min ∞, 3 + 1 min ∞, 4 + 1
=4 =∞ =∞ =4 =5
Row 4 × × × min ∞, 8 + 4 min ∞, ∞ + 4 min 4, ∞ + 4 min 5, ∞ + 4
= 12 =∞ =4 =5
Row 5 × × × min 12, ∞ + 4 min ∞, 7 + 4 × min 5, 4 + 4
= 12 = 11 =5
Row 6 × × × min 12, 3 + 5 min 11, 2 + 5 × ×
=8 =7