0% found this document useful (0 votes)
31 views

Dijkstra Algorithm

Graph theory unit 5

Uploaded by

21l153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Dijkstra Algorithm

Graph theory unit 5

Uploaded by

21l153
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LQ22. State Dijkstra’s Algorithm.

Using Dijkstra’s Algorithm find the shortest path


and the length of the shortest path from the vertex s to the vertex t 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 = { 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 5. Reset T = T – {s} = {a, b, c, d, t}. Thus, we have

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 5. Reset T = T – {c} = {a, b, d, t}. Thus, we have

Step 4. There is one edge incident with a, namely {a, b} with b is in T.


λ(b) = min(λ(b) , λ(c) + w({a, b})) = min( 29, 18 + 9) = min( 29, 27) = 27.
Step 5. Reset T = T – {a} = { b, d, t}. Thus, we have

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.

Step 5. Reset T = T – {d} = { b, t}. Thus, we have

Step 4. There is one edge incident with b, namely {b, t} with t is in T.


λ(t) = min(λ(t) , λ(b) + w({b, t})) = min(58, 27+ 28) = min(58, 55) = 55.

Step 5. Reset T = T – {b} = { t}. Thus, we have


LQ23. 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, 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 2. u = a has λ(u) a minimum (with value 0).

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 5. Reset T = T – {a} = { b, c, d, e, f, z}. Thus, we have


Vertex v a b c d e f z
λ(v) 0 4 1 2 ∞ ∞ ∞
T { b, c, d, e, f, z}

Step 2. u = c has λ(u) a minimum (with value 1).

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.

Step 5. Reset T = T – {c} = { b, d, e, f, z}. Thus, we have


Vertex v a b c d e f z
λ(v) 0 4 1 2 ∞ ∞ ∞
T { b, d, e, f, z}

Step 2. u = d has λ(u) a minimum (with value 2).

Step 4. There is one edge incident with d, namely {d, e} with e is in T.


λ(e) = min(λ(e) , λ(d) + w({d, e})) = min(∞, 2 + 3) = min(∞, 5) = 5.
Step 5. Reset T = T – {d} = { b, e, f, z}. Thus, we have
Vertex v a b c d e f z
λ(v) 0 4 1 2 5 ∞ ∞
T { b, e, f, z}

Step 2. u = b has λ(u) a minimum (with value 4).

Step 4. There is one edge incident with b, namely {b, f} with f is in T.


λ(f) = min(λ(f) , λ(b) + w({b, f})) = min(∞, 4 + 4) = min(∞, 8) = 8.

Step 5. Reset T = T – {b} = { e, f, z}. Thus, we have


Vertex v a b c d e f z
λ(v) 0 4 1 2 5 8 ∞
T { e, f, z}

Step 2. u = e has λ(u) a minimum (with value 5).

Step 4. There is one edge incident with e, namely {e, z} with z is in T.


λ(z) = min(λ(z) , λ(e) + w({e, z})) = min(∞, 5 + 1) = min(∞, 6) = 6.

Step 5. Reset T = T – {e} = { f, z}. Thus, we have


Vertex v a b c d e f z
λ(v) 0 4 1 2 5 8 6
T {f, z}

Step 2. u = z has λ(u) a minimum (with value 6).


Step 3. Stop.

The shortest distance of the vertex z from the vertex a is λ(z) = 6.

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.

Step 1. Initial labeling is given by

Vertex v a b c d e f g z
λ(v) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞
T { a, b, c, d, e, f, g, z}

Step 2. u = a has λ(u) a minimum (with value 0).

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 5. Reset T = T – {a} = { b, c, d, e, f, g, z}. Thus, we have


Vertex v a b c d e f g z
λ(v) 0 4 3 ∞ ∞ ∞ ∞ ∞
T { b, c, d, e, f, g, z}

Step 2. u = c has λ(u) a minimum (with value 3).


Step 4. There are three edges incident with c, namely {c, b}, {c, d}, and {c, e}. Each of b, d, e
is in T.
λ(b) = min(λ(b) , λ(c) + w({c, b})) = min( 4, 3 + 2) = min( 4, 3) = 3.
λ(d) = min(λ(d) , λ(c) + w({c, d})) = min(∞, 3 + 3) = min(∞, 6) = 6.
λ(e) = min(λ(e) , λ(c) + w({c, e})) = min( ∞, 3 + 6) = min( ∞, 9) = 9.

Step 5. Reset T = T – {c} = { b, d, e, f, g, z}. Thus, we have


Vertex v a b c d e f g z
λ(v) 0 3 3 6 9 ∞ ∞ ∞
T { b, d, e, f, g, z}

Step 2. u = b has λ(u) a minimum (with value 3).

Step 4. There is one edge incident with b, namely {b, d} with d is in T.


λ(d) = min(λ(d) , λ(b) + w({b, d})) = min(6, 3 + 5) = min(6, 8) = 6.

Step 5. Reset T = T – {b} = { d, e, f, g, z}. Thus, we have


Vertex v a b c d e f g z
λ(v) 0 4 3 6 9 ∞ ∞ ∞
T { d, e, f, g, z}

Step 2. u = d has λ(u) a minimum (with value 6).

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 5. Reset T = T – {d} = { e, f, g, z}. Thus, we have


Vertex v a b c d e f g z
λ(v) 0 4 3 6 7 11 ∞ ∞
T { e, f, g, z}

Step 2. u = e has λ(u) a minimum (with value 5).

Step 4. There is one edge incident with e, namely {e, g} with g is in T.


λ(g) = min(λ(g) , λ(e) + w({e, g})) = min(∞, 5 + 5) = min(∞, 10) = 10.
Step 5. Reset T = T – {e} = { f, g, z}. Thus, we have
Vertex v a b c d e f g z
λ(v) 0 4 3 6 7 11 10 ∞
T {f, g, z}
Step 2. u = g has λ(u) a minimum (with value 10).

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 5. Reset T = T – {g} = { 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 {f, z}
Step 2. u = f has λ(u) a minimum (with value 11).

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)

You might also like