0% found this document useful (0 votes)
23 views25 pages

Bellman Ford2

The document discusses the Bellman-Ford algorithm for finding single-source shortest paths in directed graphs, including its correctness, running time, and handling of negative-weight cycles. It outlines the algorithm's relaxation process and provides examples to illustrate its application. Additionally, it touches on the algorithm's use in distance-vector routing protocols and introduces the concept of shortest paths in directed acyclic graphs (DAGs).

Uploaded by

Soumi Ghosh
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)
23 views25 pages

Bellman Ford2

The document discusses the Bellman-Ford algorithm for finding single-source shortest paths in directed graphs, including its correctness, running time, and handling of negative-weight cycles. It outlines the algorithm's relaxation process and provides examples to illustrate its application. Additionally, it touches on the algorithm's use in distance-vector routing protocols and introduces the concept of shortest paths in directed acyclic graphs (DAGs).

Uploaded by

Soumi Ghosh
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/ 25

6.

006
Introduction to Algorithms

Lecture 15: Shortest Paths II


Prof. Erik Demaine
Today
• Bellman‐Ford algorithm
for single‐source shortest paths
• Running time
• Correctness
• Handling negative‐weight cycles
• Directed acyclic graphs
Recall: Shortest Paths

• if there’s no path from to
• if there’s a path from to
that visits a negative‐weight cycle
Example:
1
5 A B 3
2 −7
s 2 t
−2 C D 10
3
Recall: Single‐Source
Shortest Paths
• Problem: Given a directed graph
with edge‐weight function , and a
source vertex , compute for all
– Also want shortest‐path tree represented by
Example: 0 1
1
5 A B 3
2 7
0 s 2 t 4

−2 C D 10
3
−2 8
Recall: Relaxation Algorithm
for in :

while some edge has :


pick such an edge
relax : u
if : s
v
Relaxation Algorithm Issues
• Never stop relaxing in a graph with
negative‐weight cycles: infinite loop!
• A poor choice of relaxation order can
lead to exponentially many relaxations:
8 4 2 1

v1 v2 v3 v4 v5 v6 v7 …
8 8 4 4 2 2 1
0 ∞ ∞ ∞ ∞ ∞ ∞
Bellman & Ford

Richard E. Bellman Lester R. Ford, Jr.


(1920–1984) (1927–)
IEEE Medal of Honor, 1979 president of MAA, 1947–48
http://www.amazon.com/Bellman‐Continuum‐
http://www.maa.org/aboutmaa/maaapresidents.html
Collection‐Works‐Richard/dp/9971500906
Bellman‐Ford Algorithm
• Relaxation algorithm
• “Smart” order of edge relaxations
• Label edges
• Relax in this order:

repetitions
Bellman‐Ford Algorithm
for in :

for from to :
for in :
relax : u
if : s ,

v
Bellman‐Ford Example
8 4 2 1

v1 v2 v3 v4 v5 v6 v7 …
8 8 4 4 2 2 1
0 ∞ ∞ ∞ ∞ ∞ ∞

edges ordered right to left


Bellman‐Ford Example
8 4 2 1

v1 v2 v3 v4 v5 v6 v7 …
8 8 4 4 2 2 1
0 ∞ ∞ ∞ ∞ ∞ ∞

edges ordered left to right


Bellman‐Ford Example

∞ ∞
1
5 A B 3
4 −1
0 s 2 t ∞

−2 C D 1
4
∞ ∞

edges ordered top down, left to right


Bellman‐Ford in Practice
• Distance‐vector
routing protocol
– Repeatedly relax edges
until convergence
– Relaxation is local!
• On the Internet:
– Routing Information
Protocol (RIP)
– Interior Gateway Routing
Protocol (IGRP) photo by Ross Imlach, 2011
http://www.flickr.com/photos/rossimlach/5446205998/
Bellman‐Ford Algorithm with
Negative‐Weight Cycle Detection
for in :

for from to : u
for in :
s ,
relax
for in : v
if :
report that a negative‐weight cycle exists
Bellman‐Ford Analysis
for in :

for from to :
for in :
relax
for in :
if :
report that a negative‐weight cycle exists
Recall: Relaxing Is Safe
• Lemma: The relaxation algorithm maintains
the invariant that for all .
• Proof: By induction on the number of steps.
– Consider relax
u
– By induction,
– By triangle inequality, s
v
– So setting is “safe”
Bellman‐Ford Correctness
• Claim: After iteration of Bellman‐Ford,
is at most the weight of every path from
to using at most edges, for all .
• Proof: By induction on .
– Before iteration ,
– Relaxation only decreases ’s remains true
– Iteration considers all
paths with edges
when relaxing ’s s v
incoming edges
edges
Bellman‐Ford Correctness
• Theorem: If has no negative‐
weight cycles, then at the end of Bellman‐
Ford, for all .
• Proof:
– Without negative‐weight cycles,
shortest paths are always simple
– Every simple path has vertices,
so edges
– Claim iterations make
– Safety
Bellman‐Ford Correctness
• Theorem: Bellman‐Ford correctly reports
negative‐weight cycles reachable from .
• Proof:
– If no negative‐weight cycle, then previous theorem
implies , and by triangle inequality,
, so Bellman‐Ford won’t
incorrectly report a negative‐weight cycle.
– If there’s a negative‐weight cycle, then one of its
edges can always be relaxed (once one of its
values becomes finite), so Bellman‐Ford reports.
Computing
for in :

for from to :
for in : u
relax s ,
for from to :
for in : v
if :
Correctness of
• Theorem: After the algorithm,
for all .
• Proof:
– As argued before, after loop, every negative‐
weight cycle has a relaxable edge
– Setting takes limit of relaxation
– All reachable nodes also have
– Path from original to any vertex (including )
with has at most edges
– (So relaxation is impossible after loop.)
Why Did This Work So Well?
8 4 2 1

v1 v2 v3 v4 v5 v6 v7 …
8 8 4 4 2 2 1
0 ∞ ∞ ∞ ∞ ∞ ∞

• It’s a DAG (directed acyclic graph)


• We followed a topological sorted order

edges ordered left to right


Shortest Paths in a DAG
• Simplified Bellman‐Ford: no iteration, no cycles
for in :

topologically sort the vertices


# now in
for in : (in order)
for in :
relax
Correctness in DAG
• Theorem: In a DAG, this algorithm sets
for all .
• Proof: By induction on rank
– Claim by induction that
when we hit in outer loop
– Base case: correct (no cycles)
– When we hit , we’ve already hit all previous
vertices, including all vertices with edges into
– By induction, these vertices had correct
values when we relaxed the edges into
Next Up
s
• Dijkstra’s algorithm
– Relax edges in a growing ball around
– Fast: nearly linear time
– Only one pass through edges, but need
logarithmic time to pick next edge to relax
– Doesn’t work with negative edge weights

You might also like