You are on page 1of 19

Single-source Shortest Path

Bellman-Ford Algorithm – contd.

Instructor: Ashok Singh Sairam


Bellman-Ford Algorithm

MA512: Data Structures and Algorithms


2
Exercise

E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

MA512: Data Structures and Algorithms


3
Bellman-Ford Algorithm: Complexity

(V)
O(V)
O(VE)
O(E)

O(E)

MA512: Data Structures and Algorithms


4
Shortest path properties (1)
• Triangle inequality
s
• For all (u, v)  E, we have:
δ (s, v) ≤ δ (s, u) + δ (u, v) u v

- If u is on the shortest path to v we have the


equality sign s
u
v

MA512: Data Structures and Algorithms


5
Shortest path properties (2)
• Upper-bound property
▪ We always have v.d ≥ δ (s, v) for all v.
▪ The estimate never goes up – relaxation only lowers the
estimate
v 5 x v 5 x
6 
4
11 2
6 
4
11
6 -2 6 -2
-3 Relax (x, v) -3
8 7 8 7
s 0 -4 s 0 -4
7 2 7 2
7 9 
2 7 9 
2
y z y z

MA512: Data Structures and Algorithms


6
Shortest path properties (3)
• No path property
If there is no path from s to v, then we always have
𝑣. 𝑑 = 𝛿 𝑠, 𝑣 = ∞
a -4
b
3 -1 h i
3 4
g  2

c 6 d 8
5
s 0 5 11 - -8 3 h, i, j not
y
-3  reachable
2 3 7
- - j from s
-6
e f
δ(s, h) = δ(s, i) = δ(s, j) = 
MA512: Data Structures and Algorithms
7
Shortest path properties (4)
• Convergence property
If s u → v is a shortest path, and if u.d = δ(s, u) at
any time prior to relaxing edge (u, v), then v.d = δ(s, v)
at all times after relaxing (u, v).

u 2 v • If v.d > δ(s, v)  after relaxation:


5
6 
8
11 v.d = u.d + w(u, v)
5
v.d = 5 + 2 = 7
s 0 • Otherwise, the value remains
4
4
unchanged, because it must have
7 been the shortest path value

MA512: Data Structures and Algorithms


8
Shortest path properties (5)
• Path relaxation property
Let p = v0, v1, . . . , vk be a shortest path from
s = v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . . ,
(vk-1, vk), even intermixed with other relaxations, then
vk.d = δ (s, vk).

v1 2 v2 v .d = δ (s, v )

6
5 7 2
11 2
v4
5 3 
14
4 v3
s 0 v1.d = δ (s, v1) 11  v4.d = δ (s, v4)
v3.d = δ (s, v3)
MA512: Data Structures and Algorithms
9
Shortest path properties (6)
• Precedessor-subgraph property
Once 𝑣. 𝑑 = 𝛿 𝑠, 𝑣 for all 𝑣 ∈ 𝑉, the predecessor
subgraph is a shortest-paths tree rooted at s

MA512: Data Structures and Algorithms


10
Correctness of Belman-Ford Algorithm
• Theorem: Show that v.d = δ (s, v), for every v, after
|V-1| passes.
Case 1: G does not contain negative cycles which
are reachable from s
▪ Assume that the shortest path from s to v is
p = v0, v1, . . . , vk, where s=v0 and v=vk, k≤|V-1|

▪Use mathematical induction on the number of


passes i to show that:
vi.d= δ (s, vi) , i=0,1,…,k

MA512: Data Structures and Algorithms


11
Base Case: i=0 v0.d= δ(s, v0)= δ (s, s)= 0
Inductive Hypothesis: vi-1.d= δ (s, vi-1)
Inductive Step: vi.d = δ (s, vi)
s
vi-1 vi After relaxing (vi-1, vi):
w
vi.d ≤ vi-1.d+w = δ(s, vi-1)+w = δ (s, vi)
vi-1.d= δ(s, vi-1)

MA512: Data Structures and Algorithms


12
• Case 2: G contains a negative cycle which is reachable from s

Proof by After relaxing (vi-1,vi): 𝑣𝑖 . 𝑑 ≤ 𝑣𝑖−1 . 𝑑 + 𝑤(𝑣𝑖−1 , 𝑣𝑖 )


Contradiction: or σ𝑘𝑖=1 𝑣𝑖 . 𝑑 ≤ σ𝑘𝑖=1 𝑣𝑖−1. 𝑑 + σ𝑘𝑖=1 𝑤(𝑣𝑖−1, 𝑣𝑖 )
suppose the Each vertex in c appears exactly once, so
algorithm σ𝑘𝑖=1 𝑣𝑖 . 𝑑 = σ𝑘𝑖=1 𝑣𝑖−1 . 𝑑
returns a but σ𝑘𝑖=1 𝑤(𝑣𝑖−1, 𝑣𝑖 ) ≥ 0
solution Therefore, inequality cannot be true,
Contradiction!
MA512: Data Structures and Algorithms
13
Directed Acyclic Graph
• Suppose that an edge-weighted digraph has no
directed cycles.
• Is it easier to find shortest paths than in a general
digraph?
4 6

6 7

MA512: Data Structures and Algorithms


14
Shortest path in DAG
• Goal: Compute shortest paths from a single source
• Consider vertices in topological order
• Make one pass over the vertices in the
topologically sorted order
▪ Relax each edge

• Topological sort algorithm computes SPT in any


edge weighted DAG in time proportional to E + V.

MA512: Data Structures and Algorithms


15
MA512: Data Structures and Algorithms
16
MA512: Data Structures and Algorithms
17
Find the shortest Path
• Find shortest path b/w 0 and 6.

MA512: Data Structures and Algorithms


18
Acknowledgement
• Dr George Bebis, Foundation Professor, Dept of
Computer Science and Engineering, University of
Nevada Reno

MA512: Data Structures and Algorithms


19

You might also like