Chapter 25: All-Pairs Shortest Paths - PowerPoint PPT Presentation

Loading...

PPT – Chapter 25: All-Pairs Shortest Paths PowerPoint presentation | free to download - id: 80ded1-Yjg1O



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Chapter 25: All-Pairs Shortest Paths

Description:

Chapter 25: All-Pairs Shortest Paths Application: Computing distance table for a road atlas. Atlanta Chicago Detroit Atlanta - 650 ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 26
Provided by: Jame3656
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Chapter 25: All-Pairs Shortest Paths


1
Chapter 25 All-Pairs Shortest Paths
Application Computing distance table for a road
atlas.
One Approach Run single-source SP algorithm V
times. Nonnegative Edges Use Dijkstra. Time
complexity O(V3) with linear array O(VElg V)
with binary heap O(V2lg V VE) with Fibonacci
heap Three algorithms in this chapter Repeated
Squaring O(V3lg V) Floyd-Warshall
O(V3) Johnsons O(V2lg V VE)
Negative Edges Use Bellman-Ford. Time
Complexity O(V2E) O(V4) for dense graphs
negative edges allowed, but no negative cycles
2
Repeated Squaring Algorithm
A dynamic-programming algorithm. Assume input
graph is given by an adjacency matrix. W
(wij) Let dij(m) minimum weight of any path
from vertex i to vertex j,
containing at most m edges. dij(0) dij(m)
min(dij(m-1), mindik(m-1) wkj) min1
? k ? ndik(m-1) wkj, since wjj 0. Assuming
no negative-weight cycles d(i,j) dij(n-1)
dij(n) dij(n1)
3
Repeated Squaring (Continued)
So, given W, we can simply compute a series of
matrices D(1), D(2), , D(n-1) where D(1)
W D(m) (dij(m)) Well improve on this shortly.
Extend-SP(D, W) n rowsD for i 1 to n
do for j 1 to n do dij ? for k
1 to n do dij min(dij, dik
wkj) od od od return D
n rowsW D(1) W for m 2 to n 1 do
D(m) Extend-SP(D(m-1), W) od return D(n-1)
4
Repeated Squaring and Matrix Mult.
Running time is O(V4). Note the similarity to
matrix multiplication
Matrix-Multiply(A, B) n rowsA for i 1
to n do for j 1 to n do cij 0 for k
1 to n do cij cij aik?bkj od od
od return C
SP algorithm computes the following matrix
products D(1) D(0)?W W D(2) D(1)?W
W2 D(3) D(2)?W W3 ? D(n-1) D(n-2)?W Wn-1
5
Improving the Running Time
Can improve time to O(V3 lg V) by computing
products as follows D(1) W D(2) W2
W?W D(4) W4 W2?W2 D(8) W8
W4?W4 ? D(2?lg(n-1)?) W(2?lg(n-1)? ) W
2?lg(n-1)? -1?W 2?lg(n-1)? -1 D(n-1)
D(2?lg(n-1)?) Called repeated squaring.
n rowsW D(1) W m 1 while n 1 gt m
do D(2m) Extend-SP(D(m), D(m)) m
2m od return D(m)
Can modify algorithm to use only two
matrices. Can also modify to compute predecessor
matrix p.
Exercise Run on example graph.
6
Floyd-Warshall Algorithm
Also dynamic programming, but with different
recurrence. Runs in O(V3) time. Let dij(k)
weight of SP from vertex i to vertex j with all
intermediate vertices in the set
1, 2, , k. dij(k)
two possibilities
k
j
j
i
i
all in 1, , k1
all in 1, , k1
7
FW (Continued)
d(i,j) dij(n). So, want to compute D(n)
(dij(n))
n rowsD D(0) W for k 1 to n do for
i 1 to n do for j 1 to n do dij(k)
min(dij(k-1), dik(k-1) dkj(k-1))
od od od return D(n)
Exercise Run on example graph.
Can reduce space from O(V3) to O(V2) see
Exercise 25.2-4. Can also modify to compute
predecessor matrix.
8
Predecessor Matrix
Let pij(k) predecessor of vertex j on SP from
vertex i with all intermediate
vertices in 1, 2, , k.
pij(0)
pij(k)
Exercise Add computation of ? matrix to the
algorithm.
9
Transitive Closure
T.C. Alg
G (V, E)
G (V, E)
E (i,j) ? path from i to j in G. Can
compute T.C. using Floyd-Warshall in O(V3) time
using edge weights of 1. (i,j) ? E iff dij lt
n.
10
Example
1
2
1 2 3 4 1 0 ? ? ? 2 2
0 1 1 3 1 ? 0 1 4 3 1 2
0
D(3)
3
4
1 2 3 4 1 0 ? ? ? 2 ? 0
1 1 3 1 ? 0 1 4 ? 1 ? 0
1 2 3 4 1 0 ? ? ? 2 2
0 1 1 3 1 2 0 1 4 3 1 2
0
D(0) W
D(4)
D(1) D(0)
1 2 3 4 1 0 ? ? ? 2 ? 0
1 1 3 1 ? 0 1 4 ? 1 2 0
1
2
D(2)
3
4
11
Another O(V3) T.C. Algorithm
Uses only bits, so is better in practice.
Let tij(k) 1 iff there exists a path in G from
i to j going through
intermediate vertices in 1, 2, , k.
tij(0)
tij(k) tij(k-1) ? (tik(k-1) ? tkj(k-1))
12
Code
TC(G) n VG for i 1 to n do for j
1 to n do if i j or (i,j) ? E then tij(0)
1 else tij(0) 0 fi od od for k 1 to
n do for i 1 to n do for j 1 to n
do tij(k) tij(k-1) ? (tik(k-1) ?
tkj(k-1)) od od od return T(n)
See book for how this algorithm runs on previous
example.
13
Johnsons Algorithm
  • An O(V2 lg V VE) algorithm.
  • Good for sparse graphs.
  • Uses Dijkstra and Bellman-Ford as subroutines.
  • Basic Idea Reweight edges to be nonnegative.
    Then, run Dijkstras algorithm once per vertex.
  • Use Bellman-Ford to compute new edge weights w.
  • Must have
  • For all u and v, a SP from u to v using w is also
    a SP form u to v using w.
  • For all u and v, w(u,v) is nonnegative.




14
Counterexample
  • Subtracting the minimum weight from every weight
    doesnt work.
  • Consider
  • Paths with more edges are unfairly penalized.

15
Johnsons Insight
  • Add a vertex s to the original graph G, with
    edges of weight 0 to each vertex in G
  • Assign new weights w to each edge as follows
  • w(u, v) w(u, v) d(s, u) - d(s, v)

0
0
s
0
16
Question 1
  • Are all the ws non-negative? Yes
  • Otherwise, s ? u ? v would be shorter than the
    shortest path from s to v.

d(s, u)
u
w(u, v)
s
d(s, v)
v
17
Question 2
  • Does the reweighting preserve shortest paths?
    Yes Consider any path

the sum telescopes
A value that depends only on the endpoints, not
on the path.
In other words, we have adjusted the lengths of
all paths by the same amount. So this will not
affect the relative ordering of the
paths shortest paths will be preserved.
18
Johnsons Running Time
  • Computing G Q(V)
  • Bellman-Ford Q(VE)
  • Reweighting Q(E)
  • Running (Modified) Dijkstra ?(V2lgV VElgV)
  • Adjusting distances Q(V2)
  • Total is dominated by Dijkstra ?(V2lgVVElgV)

19
A General Result about Reweighting

Define w(u,v) w(u,v) h(u) h(v), where h
V ? ?.
Lemma 25.1 Let p v0, v1, , vk. Then, (i)
w(p) d(v0, vk) iff w(p) d(v0, vk). (ii) G
has a negative-weight cycle using w iff G has a
negative-weight cycle using w.



Proof of (i)
20
Reweighting in Johnsons Algorithm
2
Want to define h s.t. w(u,v) ? 0. Do it like
this
1
0
0

3
4
1
3
8
0
0
-5
0
2
7
1
-4
-5
0
4
0
2
6
4
5
1
0
1
5
4
0
1
3
13
0
Define h(v) d(s, v) ?v ? V?. By Lemma 24.10, ?
(u,v) ? E? h(v) ? h(u) w(u,v). Thus, w(u,v)
w(u,v) h(u) h(v) ? 0.
0
5
0
2
10
0
0
0
4
4
0
2
4
5

0
21
LP, shortest paths BF
-1
0
1
-3
-3
5
4
-1
It is possible to find a feasible solution to a
system of difference constraints by finding
shortest-path weights (from v0) in the
corresponding constraint graph,
22
  • Find a vector x ltx1,x2,,xngt
  • such that max. an objective function c1x1
    c2x2 cnxn
  • subject to m constraints Ax ? b
  • For some special cases, a linear program can be
    solved more efficiently than with a general
    purpose LP algorithm
  • Example the maximal flow problem has an LP
    representation, starting with any feasible
    augmenting solution
  • BF.

23
equivalent to solving the difference constraints
Find a vector x ltx1,x2 x3,x4,x5gt that
One solution is x (-5, -3, 0, -1, -4)
Another solution is x (0, 2, 5, 4, 1)
In fact, for any d, (d-5, d-3, d, d-1, d-4) is a
solution!
24
Code for Johnsons Algorithm
Compute G, where VG VG ? s, EG
EG ? (s,v) v ? VG if Bellman-Ford(G, w,
s) false then negative-weight cycle else for
each v ? VG do set h(v) to ?(s, v) computed
by Bellman-Ford od for each (u,v) ? EG
do w(u,v) w(u,v) h(u) h(v) od for
each u ? VG do run Dijkstra(G, w, u) to
compute ?(u, v) for all v ? VG for each v ?
VG do duv ?(u, v) h(v)
h(u) od od fi
Running time is O(V2 lg V VE). See Book.




25
Example
2
2/1
4
0
1
3
13
0/0
2/-3
2
10
0
0
0
0/-4
2/2
2
4
5
About PowerShow.com