0% found this document useful (0 votes)
57 views6 pages

Lecture 17: Shortest Paths III: Bellman-Ford

This document provides an overview and analysis of the Bellman-Ford algorithm for finding shortest paths in a graph. It begins with background on shortest path problems and notation. It then introduces the generic shortest path algorithm and analyzes its complexity. The remainder of the document focuses on the Bellman-Ford algorithm, providing pseudocode, an explanation of its operation, and a proof of correctness. It notes that Bellman-Ford can detect negative cycles unlike Dijkstra's algorithm. The document concludes by discussing the related problems of finding longest and shortest simple paths.

Uploaded by

Ioannis Frangis
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)
57 views6 pages

Lecture 17: Shortest Paths III: Bellman-Ford

This document provides an overview and analysis of the Bellman-Ford algorithm for finding shortest paths in a graph. It begins with background on shortest path problems and notation. It then introduces the generic shortest path algorithm and analyzes its complexity. The remainder of the document focuses on the Bellman-Ford algorithm, providing pseudocode, an explanation of its operation, and a proof of correctness. It notes that Bellman-Ford can detect negative cycles unlike Dijkstra's algorithm. The document concludes by discussing the related problems of finding longest and shortest simple paths.

Uploaded by

Ioannis Frangis
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/ 6

Lecture 17 Shortest Paths III: Bellman-Ford 6.

006 Fall 2011

Lecture 17: Shortest Paths III:


Bellman-Ford
Lecture Overview
• Review: Notation

• Generic S.P. Algorithm

• Bellman-Ford Algorithm

– Analysis
– Correctness

Recall:

path p = < v0 , v1 , . . . , vk >


(v1 , vi+1 ) ∈ E 0 ≤ i < k
k −1
X
w(p) = w(vi , vi+1 )
i−0

Shortest path weight from u to v is δ(u, v). δ(u, v) is ∞ if v is unreachable from u,


undefined if there is a negative cycle on some path from u to v.

-ve

u v

Figure 1: Negative Cycle.

1
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011

Generic S.P. Algorithm

d [v] ← ∞
Initialize: for v ∈ V :
Π [v] ← NIL
d[S] ← 0
Main: repeat
select edge (u, v) [somehow]

if d[v] > d[u] + w(u, v) :
“Relax” edge (u, v)  d[v] ← d[u] + w(u, v)
π[v] ← u
until you can’t relax any more edges or you’re tired or . . .

Complexity:
Termination: Algorithm will continually relax edges when there are negative cycles
present.

-1
1 -4
u v
0 1 3 4
1 2 1
0 2
d[u] -1 1
-2 0
etc

Figure 2: Algorithm may not terminate due to negative cycles.

Complexity could be exponential time with poor choice of edges.

2
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011

v0 v1 v2 v3 v4 v5 v6
4 8 10 12 13 14
13
ORDER
10 11 12
(v0, v1)
11
T(n) = 3 + 2T(n-2) (v1, v2)
4 6 8 9 10
all of v2, vn
T(n) = θ(2n/2) (v0, v2)
all of v2, vn

Figure 3: Algorithm could take exponential time. The outgoing edges from v0 and v1
have weight 4, the outgoing edges from v2 and v3 have weight 2, the outgoing edges
from v4 and v5 have weight 1.

5-Minute 6.006
Figure 4 is what I want you to remember from 6.006 five years after you graduate!

Bellman-Ford(G,W,s)

Initialize ()
for i = 1 to |V | − 1
for each edge (u, v) ∈ E:
Relax(u, v)
for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
then report a negative-weight cycle exists

At the end, d[v] = δ(s, v), if no negative-weight cycles.

Theorem:
If G = (V, E) contains no negative weight cycles, then after Bellman-Ford executes
d[v] = δ(s, v) for all v ∈ V .

3
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011

Exponential Bad Polynomial Good

T(n) = C1 + C2T(n - C3) T(n) = C1 + C2T(n / C3)

C2 > 1 okay provided C3 > 1


if C2 > 1, trouble! if C3 > 1
Divide & Explode Divide & Conquer

Figure 4: Exponential vs. Polynomial.

Proof:
Let v ∈ V be any vertex. Consider path p = hv0 , v1 , . . . , vk i from v0 = s to vk = v
that is a shortest path with minimum number of edges. No negative weight cycles
=⇒ p is simple =⇒ k ≤ |V | − 1.

Consider Figure 6. Initially d[v0 ] = 0 = δ(s, v0 ) and is unchanged since no negative


cycles.
After 1 pass through E, we have d[v1 ] = δ(s, v1 ), because we will relax the edge
(v0 , v1 ) in the pass, and we can’t find a shorter path than this shortest path. (Note
that we are invoking optimal substructure and the safeness lemma from Lecture 16
here.)
After 2 passes through E, we have d[v2 ] = δ(s, v2 ), because in the second pass we will
relax the edge (v1 , v2 ).
After i passes through E, we have d[vi ] = δ(s, vi ).
After k ≤ |V | − 1 passes through E, we have d[vk ] = d[v] = δ(s, v). 

Corollary
If a value d[v] fails to converge after |V | − 1 passes, there exists a negative-weight
cycle reachable from s.
Proof:
After |V | − 1 passes, if we find an edge that can be relaxed, it means that the current
shortest path from s to some vertex is not simple and vertices are repeated. Since this
cyclic path has less weight than any simple path the cycle has to be a negative-weight
cycle. 

4
Lecture 17 Shortest Paths III: Bellman-Ford 6.006 Fall 2011

1
∞ -1 -1
B 1 B 1
-1 2 -1 2
3 3
0 4 7 0 4 7
A 2 E ∞ A 2 E ∞ 1
3 1 3 1 1

5 8 5 8
4 2 -3 4 2 -3
6 6
C D C D
5 5 1 -2
4 2 ∞ ∞ 2 ∞ 2 3
2 3

End of pass 1 End of pass 2 (and 3 and 4)

Figure 5: The numbers in circles indicate the order in which the δ values are computed.

v
v1 vk
δ (s, vi) =
S δ (s, vi-1) + w (vi-1,vi)
p: v0 v2

Figure 6: Illustration for proof.

Longest Simple Path and Shortest Simple Path


Finding the longest simple path in a graph with non-negative edge weights is an NP-
hard problem, for which no known polynomial-time algorithm exists. Suppose one
simply negates each of the edge weights and runs Bellman-Ford to compute shortest
paths. Bellman-Ford will not necessarily compute the longest paths in the original
graph, since there might be a negative-weight cycle reachable from the source, and
the algorithm will abort.
Similarly, if we have a graph with negative cycles, and we wish to find the longest
simple path from the source s to a vertex v, we cannot use Bellman-Ford. The shortest
simple path problem is also NP-hard.

5
MIT OpenCourseWare
http://ocw.mit.edu

6.006 Introduction to Algorithms


Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy