0% found this document useful (0 votes)
60 views11 pages

Bellman-Ford Algorithm Guide

Uploaded by

ctxxfx5ykz
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)
60 views11 pages

Bellman-Ford Algorithm Guide

Uploaded by

ctxxfx5ykz
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

Design and Analysis of Algorithms

Single Source Shortest Path Problem


(Bellman-Ford Algorithm)

Dr. D. P. Acharjya
Professor, SCOPE
SJT Annex – 201 E

3/9/2024 Dr. D. P. Acharjya 1


Bellman Ford Algorithm
 Let G = (V, E) be a weighted directed graph with
source s and weight function w : E → R .
 The Bellman-Ford algorithm solves the single-source
shortest-path problem in which edge weights may be
negative.
 The algorithm returns a Boolean value indicating
whether or not there is a negative-weight cycle that is
reachable from the source.
 The algorithm fails if there is a negative weight cycle.
 Otherwise, the algorithm produces the shortest path.

3/9/2024 Dr. D. P. Acharjya 2


Bellman-Ford (G, w, s)
1. INITIALIZE – SINGLE SOURCE (G, s)
2. for i ← 1 to n – 1 = |V| - 1
3. do for each edge (u, v)  E
4. do RELAX (u, v, w)
5. for each edge (u, v)  E
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. Return TRUE

3/9/2024 Dr. D. P. Acharjya 3


Initialize-Single Source d (G, s)
1. Initialize – Single Source (G, s)
2. for each vertex v V[G]
3. do d[v] ← ∞
4. π[v] ← NIL
5. d[s] = 0
After initialization
 π[v] = NIL for all v V (Predecessor)
 d[s] = 0, and d[v] = ∞ for v  V -{s}
(Distance of source is 0)
3/9/2024 Dr. D. P. Acharjya 4
Relax (u,v,w)
1. RELAX (u, v, w)
2. if d[v] > d[u] + w(u, v)
3. then d[v] ← d[u] + w(u, v)
4. π[v] ← u d[v] > d[u] + w(u, v)
After Relax
 Relaxing test whether we can
improve the shortest path to v
going through u and so, update
d[v] and π[v].
d[v] < d[u] + w(u, v)

3/9/2024 Dr. D. P. Acharjya 5


Numerical Illustration
 Consider the following graph
and compute the shortest
distance to all the vertices from
the source vertex.

 After Initialization
 d[s] = 0, and d[t] = ∞, d[x] = ∞,
d[y] = ∞, d[z] = ∞
 π[s] = NIL, π[t] = NIL, π[x] =
NIL, π[y] = NIL, π[z] = NIL
3/9/2024 Dr. D. P. Acharjya 6
Continued …
 For i = 1
 Relax edges (s, t) and (s, y)
 Relax (s, t, w = 6)
 d[t] = ∞, d[s] + 6 = 6
 d[t] > d[s] + 6 = 6
 d[t] = 6
 Relax (s, y, w = 7)
 d[y] = ∞, d[s] + 7 = 7
 d[y] > d[s] + 7 = 7
 d[y] = 7
3/9/2024 Dr. D. P. Acharjya 7
Continued …
 For i = 2
 Relax edges (t, x), (t, z), (t, y),
(y, z), and (y, x)
 Relax (t, x, w = 5)
 d[x] = ∞, d[t] + 5 = 6+5=11
 d[x] > d[t] + 5 = 11
 d[x] = 11
 Relax (t, z, w = – 4)
 d[z] = ∞, d[t] + (– 4) = 6 – 4 = 2
 d[z] > d[t] – 4 = 2
 d[z] = 2
3/9/2024 Dr. D. P. Acharjya 88
Continued …
 Relax (t, y, w = 8)
 d[y] = 7, d[t] + 8 = 6+8=14
 d[y] > d[t] + 8 (False)
 d[y] = 7
 Relax (y, z, w = 9)
 d[z] = 2, d[y] + 9 = 7 + 9 = 16
 d[z] > d[y] + 9 (False)
 d[z] = 2
 Relax (y, x, w = – 3)
 d[x] = 11, d[y] + (–3) = 7–3 = 4
 d[x] > d[y] –3
 d[x] = 4
3/9/2024 Dr. D. P. Acharjya 99
Continued …
 For i = 3
 Relax edges (x, t) and (z, x)
 Relax (x, t, w = – 2)
 d[t] = 6, d[x] + (–2) = 4–2 = 2
 d[t] > d[x] –2
 d[t] = 2
 Relax (z, x, w = 7)
 d[x] = 4, d[z] + 7 = 2+7 = 9
 d[x] > d[z] + 7 (False)
 For i = 4
 Relax edges (t, x), (t, z), (t, y)

3/9/2024 Dr. D. P. Acharjya 10


10
Continued …
 Relax (t, x, w = 5)
 d[x] = 4, d[t] + 5 = 2 + 5 = 7
 d[x] > d[t] + 5 (False)
 d[x] = 4
 Relax (t, y, w = 8)
 d[y] = 7, d[t] + 8 = 2+8 = 10
 d[y] > d[t] + 8 (False)
 d[y] = 7
 Relax (t, z, w = – 4)
 d[z] = 2, d[t] + (– 4) = 2 – 4 = – 2
 d[z] > d[t] – 4
 d[z] = – 2 (Process Terminates)
3/9/2024 Dr. D. P. Acharjya 11
11

You might also like