Dijkstra Algorithm
Dijkstra Algorithm
SOLUTION: Dijkstra’s Algorithm is used to find the shortest length (distance) between two vertices
of a given weighted graph. Let G = (V, E) be a weighted graph with w(e) representing the weight of
the edge e. We proceed as per the steps given below to find the shortest path from a vertex s to
another vertex t.
For the given graph G = (V, E), we have V = { s, a, b, c, d, t}, E = {{s, a}, {s, c}, {a, b}, {a, c}, {b, c}, {b,
d}, {b, t}, {c, d}, {d, t}}, w({s, a}) = 18, w({s, c}) = 15, w({a, b})= 9, w({a, c}) = 6, w({b, c}) = 14, w({b,
d}) = 10, w({b, t}) = 28, w({c, d}) = 7, w({d, t}) = 36. We proceed as per the following steps to find the
shortest path between s and t.
Step 4. There are two edges incident with s, namely {s, a} and {s, c}. Both a and c are in T.
λ(a) = min(λ(a) , λ(s) + w({s, a})) = min( ∞, 0 + 18) = min( ∞, 18) = 18.
λ(c) = min(λ(c) , λ(s) + w({s, c})) = min( ∞, 0 + 15) = min( ∞, 15) = 15.
Step 4. There are three edges incident with c, namely {c, a}, {c, b}, and {c, d}. Each of a, b,
and d is in T.
λ(a) = min(λ(a) , λ(c) + w({c, a})) = min( 18, 15 + 6) = min( 18, 21) = 18.
λ(b) = min(λ(b) , λ(c) + w({c, b})) = min( ∞, 15 + 14) = min( ∞, 29) = 29.
λ(d) = min(λ(d) , λ(c) + w({c, d})) = min( ∞, 15 + 7) = min( ∞, 22) = 22.
Step 4. There are two edges incident with d, namely {d, b} and {d, t} with b, t are in T.
λ(b) = min(λ(b) , λ(d) + w({d, b})) = min( 27, 22 + 10) = min( 27, 32) = 27.
λ(t) = min(λ(t) , λ(d) + w({d, t})) = min(∞, 22 + 36) = min(∞, 58) = 58.
SOLUTION: Dijkstra’s Algorithm is used to find the shortest length (distance) between two vertices
of a given weighted graph. Let G = (V, E) be a weighted graph with w(e) representing the weight of
the edge e. We proceed as per the steps given below to find the shortest path from a vertex s to
another vertex t.
For the given graph G = (V, E) we have V = { a, b, c, d, e, f, z}, E = {{a, b}, {a, c}, {a, d}, {b, c}, {b, f},
{c, d}, {c, e}, {c, f}, {d, e}, {e, z}, {f, z}}, w({a, b}) = 4, w({a, c}) = 1, w({a, d})= 2, w({b, c}) = 2, w({b, f})
= 4, w({c, d}) = 2, w({c, e}) = 5, w({c, f}) = 7, w({d, e}) = 3, w({e, z}) = 1, w({f, z}) = 3. We proceed as
per the following steps to find the shortest path between a and z.
Step 1. Initial labeling is given by
Vertex v a b c d e f z
λ(v) 0 ∞ ∞ ∞ ∞ ∞ ∞
T { a, b, c, d, e, f, z}
Step 4. There are three edges incident with a, namely {a, b}, {a, c}, and {a, d}. Each of a, b,
c, d is in T.
λ(b) = min(λ(b) , λ(a) + w({a, b})) = min( ∞, 0 + 4) = min( ∞, 4) = 4.
λ(c) = min(λ(c) , λ(s) + w({a, c})) = min( ∞, 0 + 1) = min( ∞, 1) = 1.
λ(d) = min(λ(d) , λ(s) + w({a, d})) = min( ∞, 0 + 2) = min( ∞, 2) = 2.
Step 4. There are four edges incident with c, namely {c, b}, {c, d}, {c, e}, and {c, f}. Each of
b, d, e, f is in T.
λ(b) = min(λ(b) , λ(c) + w({c, b})) = min( 4, 1 + 2) = min( 4, 3) = 3.
λ(d) = min(λ(d) , λ(c) + w({c, d})) = min( 2, 1 + 2) = min( 2, 3) = 2.
λ(e) = min(λ(e) , λ(c) + w({c, e})) = min( ∞, 1 + 5) = min( ∞, 6) = 6.
λ(f) = min(λ(f) , λ(c) + w({c, f})) = min( ∞, 1 + 7) = min( ∞, 8) = 8.
LQ24. State Dijkstra’s Algorithm. Using Dijkstra’s Algorithm find the shortest path and the length of
the shortest path from the vertex a to the vertex z of the following weighted graph.
SOLUTION: Dijkstra’s Algorithm is used to find the shortest length (distance) between two vertices
of a given weighted graph. Let G = (V, E) be a weighted graph with w(e) representing the weight of
the edge e. We proceed as per the steps given below to find the shortest path from a vertex s to
another vertex t.
For the given graph G = (V, E) we have V = { a, b, c, d, e, f, g, z}, E = {{a, b}, {a, c}, {b, c}, {b, d}, {c,
d}, {c, e}, {d, e}, {d, f}, {e, g}, {f, g}, {f, z}, {g, z}}, w({a, b}) = 4, w({a, c}) = 3, w({b, c}) = 2, w({b, d}) =
5, w( {c, d}) = 3, w({c, e}) = 6, w({d, e}) = 1, w({d, f}) = 5, w({e, g}) = 5, w({f, g}) = 2, w({f, z}) = 7,
w({g, z}) = 4. We proceed as per the following steps to find the shortest path between a and z.
Vertex v a b c d e f g z
λ(v) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞
T { a, b, c, d, e, f, g, z}
Step 4. There are two edges incident with a, namely {a, b} and {a, c} with a, b, c ∈T.
λ(b) = min(λ(b) , λ(a) + w({a, b})) = min( ∞, 0 + 4) = min( ∞, 4) = 4.
λ(c) = min(λ(c) , λ(s) + w({a, c})) = min( ∞, 0 + 3) = min( ∞, 3) = 3.
Step 4. There are two edges incident with d, namely {d, e} , {d, f} with e, f ∈T.
λ(e) = min(λ(e) , λ(d) + w({d, e})) = min(9, 6 + 1) = min(9, 7) = 7.
λ(f) = min(λ(f) , λ(d) + w({d, f})) = min(∞, 6 + 5) = min(∞, 11) = 11.
Step 4. There are two edges incident with g, namely {g, f} and {g, z} with f, z ∈T.
λ(f) = min(λ(f) , λ(g) + w({g, f})) = min(11, 10 + 2) = min(11, 12) = 11.
λ(z) = min(λ(z) , λ(g) + w({g, z})) = min(∞, 10 + 4) = min(∞, 14) = 14.
Step 4. There is one edge incident with f, namely {f, z} with z ∈T.
λ(z) = min(λ(z) , λ(f) + w({f, z})) = min(14, 11 + 7) = min(14, 18) = 14.
Step 5. Reset T = T – {f} = { z}. Thus, we have
Vertex v a b c d e f g z
λ(v) 0 4 3 6 7 11 10 14
T {z}
Step 2. u = z is the only choice.
Step 3. Stop.
The shortest distance of the vertex z from the vertex a is λ(z) = 14.
LQ25. State Dijkstra’s Algorithm. Using Dijkstra’s Algorithm find the shortest path and the length of
the shortest path of the following weighted graphs. [ASSIGNMENT PROBLEMS]
(a) (b)