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

Shortest Paths

The document describes Dijkstra's algorithm and Bellman-Ford-Moore algorithm for solving the single-source shortest path problem in graphs. Dijkstra's algorithm works for graphs with non-negative edge weights, while Bellman-Ford-Moore can handle graphs with negative edge weights. Johnson's algorithm is presented as a way to solve the all-pairs shortest path problem for graphs with negative edge weights by transforming the graph into an equivalent graph with non-negative edge weights.

Uploaded by

sj
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)
22 views

Shortest Paths

The document describes Dijkstra's algorithm and Bellman-Ford-Moore algorithm for solving the single-source shortest path problem in graphs. Dijkstra's algorithm works for graphs with non-negative edge weights, while Bellman-Ford-Moore can handle graphs with negative edge weights. Johnson's algorithm is presented as a way to solve the all-pairs shortest path problem for graphs with negative edge weights by transforming the graph into an equivalent graph with non-negative edge weights.

Uploaded by

sj
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/ 17

Dijkstra’s Algorithm

SSSP, non-neg

Edge weights = w(x,y)

Final distances = d(x,y) = dw(x,y)


2 b 2

s 2 e
6 2
c
6 1

d
Dijkstra’s Algorithm
SSSP, non-neg

s b c d e
L(x) 0 ∞ ∞ ∞ ∞

2 b 2

s 2 e
6 2
c x <- extractmin

6 1 // L(x) is the distance of s to x


// mark x as final
d
"relax” all the edges out of x
L(y) <- min ( L(y), L(x) + w(x,y) )
Dijkstra’s Algorithm
SSSP, non-neg

s b c d e
L(x) 0 2 6 6 ∞

2 b 2

s 2 e
6 2
c x <- extractmin

6 1 // L(x) is the distance of s to x


// mark x as final
d
"relax” all the edges out of x
L(y) <- min ( L(y), L(x) + w(x,y) )
Dijkstra’s Algorithm
SSSP, non-neg

s b c d e
L(x) 0 2 4 6 4

2 b 2

s 2 e
6 2
c x <- extractmin

6 1 // L(x) is the distance of s to x


// mark x as final
d
"relax” all the edges out of x
L(y) <- min ( L(y), L(x) + w(x,y) )
Dijkstra’s Algorithm
SSSP, non-neg

s b c d e
L(x) 0 2 4 6 4

2 b 2

s 2 e
6 2
c x <- extractmin

6 1 // L(x) is the distance of s to x


// mark x as final
d
"relax” all the edges out of x
L(y) <- min ( L(y), L(x) + w(x,y) )
Dijkstra’s Algorithm
SSSP, non-neg

s b c d e
L(x) 0 2 4 5 4

2 b 2

s 2 e
6 2
c x <- extractmin

6 1 // L(x) is the distance of s to x


// mark x as final
d
"relax” all the edges out of x
L(y) <- min ( L(y), L(x) + w(x,y) )
Dijkstra’s Algorithm m decreasekeys
SSSP, non-neg n extractmins

Fib heap: O(m + n log n)

s b c d e
L(x) 0 2 4 5 4

2 b 2

s 2 e
6 2
c x <- extractmin

6 1 // L(x) is the distance of s to x


// mark x as final
d
"relax” all the edges out of x
L(y) <- min ( L(y), L(x) + w(x,y) )
Bellman-Ford-Moore Algorithm
SSSP, neg wts OK
Bellman-Ford-Moore Algorithm n rounds
SSSP, neg wts OK m time per round
O(mn) time

s b c d e
L(x) 0 ∞ ∞ ∞ ∞

// L(x) is an upper bound on d(s,x)


2 b 2

2 e for t = 1 to n-1
s
6 -2 for all vertices x
c // "relax” all the edges out of x
6 1 L(y) <- min ( L(y), L(x) + w(x,y) )

d Claim: if graph has non negative cycles, B-F-M is OK.


Proof: induction. At end of round t, L(y) is shortest
path using at most t edges.
Bellman-Ford-Moore Algorithm n rounds
SSSP, neg wts OK m time per round
O(mn) time

s b c d e
L(x) 0 2 2 5 4

// L(x) is an upper bound on d(s,x)


2 b 2

2 e for t = 1 to n-1
s
6 -2 for all vertices x
c // "relax” all the edges out of x
6 1 L(y) <- min ( L(y), L(x) + w(x,y) )

d Claim: if graph has non negative cycles, B-F-M is OK.


Proof: induction. At end of round t, L(y) is shortest
path using at most t edges.
Bellman-Ford-Moore Algorithm n rounds
SSSP, neg wts OK m time per round
O(mn) time

s b c d e
L(x) 0 2 2 5 4

// L(x) is an upper bound on w(s,x)


2 b 2

2 e for t = 1 to n-1
s
6 -2 for all vertices x
c // "relax” all the edges out of x
6 1 L(y) <- min ( L(y), L(x) + w(x,y) )

d Claim: If at end, some edge xy is “over-tight”


( it has L(y) > L(x) + w(x,y) )
then graph has negative cycle.
Lots more work!
E.g., extensions and implementations of (just) Dijkstra’s algorithm:

slide from Mikkel Thorup


All Pairs SP

Non-neg weights:
n Dijkstras O(mn + n2 log n)

Neg weights:
n B-F-M O(mn2)
Johnson’s Algorithm
APSP, neg wts OK

Find “feasible potentials” ©(x) such that


the “reduced weights”
ŵ(x,y) := ©(x) + w(x,y) - ©(y) ≥ 0

Fact: reduced weights ŵ non-neg


Claim: dw (x,y) = dŵ(x,y) + ©(y) - ©(x)
2 b 2
e So shortest paths don’t change, though
s 2
6 -2 their weights might.
c  suffices to find APSP in this
non-neg weights graph!
6 1
How to find these “feasible potentials”?
d
Johnson’s Algorithm
APSP, neg wts OK

Find “feasible potentials” ©(x) such that


the “reduced weights”
ŵ(x,y) := ©(x) + w(x,y) - ©(y) ≥ 0

Fact: reduced weights ŵ non-neg


Claim: dw (x,y) = dŵ(x,y) + ©(y) - ©(x)
2 b 2
e So shortest paths don’t change, though
s 2
6 -2 their weights might.
c  suffices to find APSP in this
non-neg weights graph!
6 1
0
0 How to find these “feasible potentials”?
d 0
0 Shortest paths from Z.
0 Z No negative cycles created, so B-F-M works.
All Pairs SP

Non-neg weights:
n Dijkstras O(mn + n2 log n)

Neg weights:
n B-F-M O(mn2)

Neg weights:
B-F-M + n Dijkstras O(mn + n2 log n)

Neg weights:
Floyd-Warshall O(n3)

for all vertices z


for all vertices x,y
d(x,y) <- min{ d(x,y), d(x,z) + d(z,y)
All Pairs SP

Non-neg weights:
n Dijkstras O(mn + n2 log n)

Neg weights:
n B-F-M O(mn2)

Neg weights:
B-F-M + n Dijkstras O(mn + n2 log n)

Neg weights:
Floyd-Warshall O(n3)

Neg weights:
Naïve Min-Sum-Product O(n3 log n)

You might also like