Download presentation

Presentation is loading. Please wait.

Published byLuis Baird Modified over 3 years ago

1
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine

2
2 Shortest paths Single-source shortest paths Nonnegative edge weights Dijkstras algorithm: O(E + V lg V) General Bellman-Ford: O(VE) DAG One pass of Bellman-Ford: O(V + E) All-pairs shortest paths Nonnegative edge weights Dijkstras algorithm |V| times: O(VE + V 2 lg V) General Three algorithms today. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

3
3 All-pairs shortest paths Input: Digraph G = (V, E), where |V| = n, with edge-weight function w : E. Output: n × n matrix of shortest-path lengths (i, j) for all i, j V. IDEA #1: Run Bellman-Ford once from each vertex. Time = O(V 2 E). Dense graph O(V 4 ) time. Good first try! © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

4
4 Dynamic programming Consider the n × n adjacency matrix A = (a ij ) of the digraph, and define d ij (m) = weight of a shortest path from i to j that uses at most m edges. Claim: We have d ij (0) = 0 if i = j, if i j; and for m = 1, 2, …, n – 1, d ij (m) = min k {d ik (m–1) + a kj }. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

5
5 Proof of claim d ij (m) = min k {d ik (m–1) + a kj }. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Relaxation! for k 1 to n do if d ij > d ik + a kj then d ij d ik + a kj Note: No negative-weight cycles implies (i, j) = d ij (n–1) = d ij (n) = d ij (n+1) = … m – 1 edges ksks

6
6 Matrix multiplication Compute C = A · B, where C, A, and B are n × n matrices Time = (n 3 ) using the standard algorithm. What if we map + min and · +? c ij = min k {a ik + b kj }. Thus, D (m) = D (m–1) × A. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Identity matrix = I == D 0 = (d ij (0) ).

7
7 Matrix multiplication (continued) © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. The (min, +) multiplication is associative, and with the real numbers, it forms an algebraic structure called a closed semiring. Consequently, we can compute D (1) = D (0) · A = A 1 D (2) = D (1) · A = A 2 D (n–1) = D (n–2) · A = A n–1, yielding D (n–1) = ( (i, j)). Time = (n·n 3 ) = (n 4 ). No better than n × B-F.

8
8 Improved matrix multiplication algorithm © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Repeated squaring: A 2k = A k × A k. Compute A 2, A 4, …, A 2. O(lg n) squarings Note: A n–1 = A n = A n+1 =. Time = (n 3 lg n). To detect negative-weight cycles, check the diagonal for negative values in O(n) additional time.

9
9 Floyd-Warshall algorithm © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Also dynamic programming, but faster! Define c ij (k) = weight of a shortest path from i to j with intermediate vertices belonging to the set {1, 2, …, k}. Thus, (i, j) = c ij (n). Also, c ij (0) = a ij.

10
10 Floyd-Warshall recurrence © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. c ij (k) = min k {c ij (k–1), c ik (k–1) + c kj (k–1) } intermediate vertices in {1, 2, …, k}

11
11 Pseudocode for Floyd- Warshall for k 1 to n do for i 1 to n do for j 1 to n do if c ij > c ik + c kj then c ij c ik + c kj relaxation Notes: Okay to omit superscripts, since extra relaxations cant hurt. Runs in (n 3 ) time. Simple to code. Efficient in practice. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19.

12
12 Transitive closure of a directed graph © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Compute t ij = 1 if there exists a path from i to j, 0 otherwise. IDEA: Use Floyd-Warshall, but with (, ) instead of (min, +): t ij (k) = t ij (k–1) (t ik (k–1) t kj (k–1) ). Time = (n 3 ).

13
13 Graph reweighting © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. Theorem. Given a label h(v) for each v V, reweight each edge (u, v) E by ŵ(u, v) = w(u, v) + h(u) – h(v). Then, all paths between the same two vertices are reweighted by the same amount. Proof. Let p = v 1 v 2 v k be a path in the grap Then, we have

14
14 Johnsons algorithm © 2001 by Charles E. Leiserson Introduction to Algorithms Day 32 L19. 1.Find a vertex labeling h such that ŵ(u, v) 0 for all (u, v) E by using Bellman-Ford to solve the difference constraints h(v) – h(u) w(u, v), or determine that a negative-weight cycle exists. Time = O(VE). 2.Run Dijkstras algorithm from each vertex using ŵ. Time = O(VE + V 2 lg V). 3.Reweight each shortest-path length ŵ(p) to produce the shortest-path lengths w(p) of the original graph. Time = O(V 2 ). Total time = O(VE + V 2 lg V).

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google