1 / 39

Analysis of Algorithms CS 477/677

Analysis of Algorithms CS 477/677. Instructor: Monica Nicolescu Lecture 22. Shortest Path Problems. How can we find the shortest route between two points on a map? Model the problem as a graph problem: Road map is a weighted graph: vertices = cities edges = road segments between cities

alvaro
Download Presentation

Analysis of Algorithms CS 477/677

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Analysis of AlgorithmsCS 477/677 Instructor: Monica Nicolescu Lecture 22

  2. Shortest Path Problems • How can we find the shortest route between two points on a map? • Model the problem as a graph problem: • Road map is a weighted graph: vertices = cities edges = road segments between cities edge weights = road distances • Goal: find a shortest path between two vertices (cities) CS 477/677 - Lecture 22

  3. t x 6 3 9 3 4 1 2 0 s 2 7 3 5 5 11 6 y z Shortest-Path Representation For each vertex v  V: • d[v] = δ(s, v): a shortest-path estimate • Initially, d[v]=∞ • Reduces as algorithms progress • [v] = predecessor of v on a shortest path from s • If no predecessor, [v] = NIL •  induces a tree—shortest-path tree • Shortest paths & shortest path trees are not unique CS 477/677 - Lecture 22

  4. Initialization Alg.: INITIALIZE-SINGLE-SOURCE(V, s) • for each v  V • do d[v] ←  • [v] ← NIL • d[s] ← 0 • All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE CS 477/677 - Lecture 22

  5. u u u u v v v v s s 2 2 2 2 5 5 5 5 9 6 6 7 Relaxation • Relaxing an edge (u, v) = testing whether we can improve the shortest path to v found so far by going through u If d[v] > d[u] + w(u, v) we can improve the shortest path to v  update d[v] and [v] • After relaxation: • d[v]  d[u] + w(u, v) RELAX(u, v, w) RELAX(u, v, w) CS 477/677 - Lecture 22

  6. RELAX(u, v, w) • if d[v] > d[u] + w(u, v) • then d[v] ← d[u] + w(u, v) • [v] ← u • All the single-source shortest-paths algorithms • start by calling INIT-SINGLE-SOURCE • then relax edges • The algorithms differ in the order and how many times they relax each edge CS 477/677 - Lecture 22

  7. Bellman-Ford Algorithm • Single-source shortest paths problem • Computes d[v] and [v] for all v  V • Allows negative edge weights and cycles • Returns: • TRUE if no negative-weight cycles are reachable from the source s • FALSE otherwise  no solution exists • Idea: • Traverse all the edges |V| – 1 times, every time performing a relaxation step of each edge CS 477/677 - Lecture 22

  8. u v 2 6  11 5 y 4 4 x Single-Source Shortest Paths in DAGs • Given a weighted DAG: G = (V, E) – solve the shortest path problem • Idea: • Topologically sort the vertices of the graph • Relax the edges according to the order given by the topological sort • for each vertex, we relax each edge that starts from that vertex • Are shortest-paths well defined in a DAG? • Yes, (negative-weight) cycles cannot exist CS 477/677 - Lecture 22

  9. DAG-SHORTEST-PATHS(G, w, s) • topologically sort the vertices of G • INITIALIZE-SINGLE-SOURCE(V, s) • for each vertex u, taken in topologically sorted order • do for each vertex v Adj[u] • do RELAX(u, v, w) Running time: (V+E) (V+E) (V) (V) (E) CS 477/677 - Lecture 22

  10. 6 6 1 1 x x s s t t x x y y z z 5 5 2 2 7 7 -1 -1 -2 -2   0 0         3 3 4 4 2 2 6 2 Example 6 1 x s t x y z 5 2 7 -1 -2  0     3 4 2 CS 477/677 - Lecture 22

  11. 6 1 x s t x y z 5 2 7 -1 -2  0 2 6   6 4 3 4 2 6 1 5 x s t x y z 5 2 7 -1 -2  0 2 6 6  4 3 6 4 1 2 x s t x y z 5 2 7 -1 -2  0 2 6 5  3 4 3 4 2 Example (cont.) CS 477/677 - Lecture 22

  12. 6 1 x s t x y z 5 2 7 -1 -2  0 2 6 5 3 3 4 2 Example (cont.) CS 477/677 - Lecture 22

  13. u u v v s s 2 2 5 5 7 6 Shortest Path Properties • Triangle inequality For all (u, v)  E, we have: δ(s, v) ≤ δ(s, u) + w(u, v) • If u is on the shortest path to v we have the equality sign CS 477/677 - Lecture 22

  14. v v x x 5 5 6 6   -2 -2 6 6 -3 -3 8 8 7 7 0 0 s s -4 -4 2 2 7 7 2 7 7   4 11 9 9 y y z z 4 11 2 2 Shortest Path Properties • Upper-bound property We always have d[v] ≥ δ(s, v) for all v. Once d[v] = δ(s, v), it never changes. • The estimate never goes up – relaxation only lowers the estimate Relax (x, v) CS 477/677 - Lecture 22

  15. a b -4 3 -1 4 3 c g d 6 8 5 0 - 5 11 s -3 y 2 7 3 - - -6 e f h i 2   3 -8  j Shortest Path Properties • No-path property If there is no path from s to v then d[v] = ∞ always. • δ(s, h) = ∞ and d[h] ≥ δ(s, h)  d[h] = ∞ h, i, j not reachable from s (s, h) = (s, i) =(s, j) = CS 477/677 - Lecture 22

  16. Shortest Path Properties • Convergence property If s u → v is a shortest path, and if d[u] = δ(s, u) at any time prior to relaxing edge (u, v), then d[v] = δ(s, v) at all times afterward • If d[v] > δ(s, v) after relaxation: • d[v] = d[u] + w(u, v) • d[v] = 5 + 2 = 7 • Otherwise, the value remains unchanged, because it must have been the shortest path value u v 2 6  5 8 11 5 0 s 4 4 7 CS 477/677 - Lecture 22

  17. Shortest Path Properties • Path relaxation property Let p = v0, v1, . . . , vk be a shortest path from s = v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . . , (vk-1, vk), even intermixed with other relaxations, then d[vk] = δ(s, vk). v1 v2 2 d[v2] = δ(s, v2) 6  v4   11 7 5  14 5 3 v3 d[v1] = δ(s, v1) 4 d[v4] = δ(s, v4)  0 s 11 d[v3] = δ(s, v3) CS 477/677 - Lecture 22

  18. Dijkstra’s Algorithm • Single-source shortest path problem: • No negative-weight edges: w(u, v) > 0  (u, v)  E • Maintains two sets of vertices: • S = vertices whose final shortest-path weights have already been determined • Q = vertices in V – S: min-priority queue • Keys in Q are estimates of shortest-path weights (d[v]) • Repeatedly select a vertex u  V – S, with the minimum shortest-path estimate d[v] CS 477/677 - Lecture 22

  19. t t x x 1 1     9 9 10 10 2 2 4 4 3 3 6 6 0 0 s s 7 7 5 5 10     2 2 y y z z 5 Dijkstra (G, w, s) • INITIALIZE-SINGLE-SOURCE(V, s) • S ←  • Q ← V[G] • while Q  • dou ← EXTRACT-MIN(Q) • S ← S  {u} • for each vertex v  Adj[u] • do RELAX(u, v, w) CS 477/677 - Lecture 22

  20. t t t t x x x x 1 1 1 1 8 8 8 10  13 14 9 8 14 13 9 9 9 9 10 10 10 10 2 2 2 2 4 4 4 4 3 3 3 3 6 6 6 6 0 0 0 0 s s s s 7 7 7 7 5 5 5 5 5 5 5 5  7 7 7 7 2 2 2 2 y y y y z z z z 9 Example CS 477/677 - Lecture 22

  21. Dijkstra (G, w, s) (V) • INITIALIZE-SINGLE-SOURCE(V, s) • S ←  • Q ← V[G] • while Q  • dou ← EXTRACT-MIN(Q) • S ← S  {u} • for each vertex v  Adj[u] • do RELAX(u, v, w) Running time: O(VlgV + ElgV) = O(ElgV) O(V) build min-heap Executed O(V) times O(lgV) O(E) times; O(lgV) CS 477/677 - Lecture 22

  22. y x Correctness of Dijskstra’s Algorithm • For each vertex u  V, we have d[u] = δ(s, u) at the time when u is added to S. u V - S • Proof: • Let u be the first vertex for which d[u]  δ(s, u) when added to S S s • Let’s look at a true shortest path from s to u (p): • Before adding u to S, path p connects a vertex in S (s) with one in V - S (u) • Let (x, y) be the first edge crossing (S, V – S) CS 477/677 - Lecture 22

  23. y x Correctness of Dijskstra’s Algorithm • Claim: d[y] = δ(s, y) at the time when u is added to S • u is the first vertex with d[u]  δ(s, u) • x  S and we must have d[x] = δ(s, x) • Relax (x, y) (convergence property) •  d[y] = δ(s, y) • y is before u on the shortest • path from s to u: •  d[y] = δ(s, y)  δ(s, u)  d[u] • Both u and y V- S when u was chosen and since we chose u: d[u]  d[y] • d[y] = δ(s, y) = δ(s, u) = d[u] contradiction with choice of u! u S s  relation above is an equality: CS 477/677 - Lecture 22

  24. 2 4 3 8 1 3 2 1 -4 -5 7 5 4 6 All-Pairs Shortest Paths • Given: • Directed graph G = (V, E) • Weight function w : E → R • Compute: • The shortest paths between all pairs of vertices in a graph • Representation of the result: an n × n matrix of shortest-path distances δ(u, v) CS 477/677 - Lecture 22 24

  25. 2 4 3 8 1 3 2 1 -4 -5 7 5 4 6 All-Pairs Shortest Paths • Assume the graph (G) is given as adjacency matrix of weights • W = (wij), n x n matrix, |V| = n • Vertices numbered 1 to n if i = j wij = if i  j , (i, j)  E if i  j , (i, j)  E • Output the result in an n x n matrix D = (dij), where dij = δ(i, j) • Solve the problem using dynamic programming 0 weight of (i, j) ∞ CS 477/677 - Lecture 22

  26. at most m edges  11 j i p’ Optimal Substructure of a Shortest Path • All subpaths of a shortest path are shortest paths • Let p: a shortest path from vertex i to j that contains at most m edges • If i = j • w(p) = 0 and p has no edges k at most m - 1 edges • If i  j:p = i k  j • p’ has at most m-1 edges • p’ is a shortest path • δ(i, j) = δ(i, k) + wkj CS 477/677 - Lecture 22 26

  27. at most m edges  11 j i Recursive Solution • lij(m) = weight of shortest path i j that contains at most m edges • m = 0: lij(0) = if i = j if i  j • m  1: lij(m) = • Shortest path from i to j with at most m – 1 edges • Shortest path from i to j containing at most m edges, considering all possible predecessors (k) of j 0  k lij(m-1) min { , } min {lik(m-1) + wkj} 1  k  n = min {lik(m-1) + wkj} 1  k  n CS 477/677 - Lecture 22 27

  28. Computing the Shortest Paths • m = 1: lij(1) = • The path between i and j is restricted to 1 edge • Given W = (wij), compute: L(1), L(2), …, L(n-1), where L(m) = (lij(m)) • L(n-1) contains the actual shortest-path weights Given L(m-1) and W  compute L(m) • Extend the shortest paths computed so far by one more edge • If the graph has no negative cycles: all simple shortest paths contain at most n - 1 edges δ(i, j) = lij(n-1) and lij(n), lij(n+1). . . wij L(1) = W = lij(n-1) CS 477/677 - Lecture 22

  29. j i k k Extending the Shortest Path lij(m)= min {lik(m-1) + wkj} 1  k  n j i “*” = L(m-1) W L(m) n x n Replace: min  + +   Computing L(m) looks like matrix multiplication CS 477/677 - Lecture 22 29

  30. EXTEND(L, W, n) • allocate L’, an n × n matrix • for i ← 1to n • do for j ← 1to n • do lij’ ←∞ • for k ← 1to n • do lij’ ← min(lij’, lik + wkj) • return L’ Running time: (n3) lij(m)= min {lik(m-1) + wkj} 1  k  n CS 477/677 - Lecture 22

  31. SLOW-ALL-PAIRS-SHORTEST-PATHS(W, n) • L(1) ← W • for m ← 2 to n - 1 • do L(m) ←EXTEND (L(m - 1), W, n) • return L(n - 1) Running time: (n4) CS 477/677 - Lecture 22

  32. 2 4 3 8 1 3 2 1 -4 -5 7 5 4 6 Example lij(m)= min {lik(m-1) + wkj} 1  k  n W L(m-1) = L(1) 0 3 8 2 -4 3 0 -4 1 7 … and so on until L(4) L(m) = L(2) 4 0 5 11  2 -1 -5 0 -2 8 1 6 0  CS 477/677 - Lecture 22

  33. Improving Running Time • No need to compute all L(m) matrices • If no negative-weight cycles exist: L(m) = L(n - 1) for all m  n – 1 • We can compute L(n-1) by computing the sequence: L(1) = W L(2) = W2 = W “” W L(4) = W4 = W2 “” W2 L(8) = W8 = W4 “” W4 … CS 477/677 - Lecture 22

  34. FASTER-APSP(W, n) • L(1) ← W • m ← 1 • while m < n - 1 • do L(2m) ← EXTEND(L(m), L(m), n) • m ← 2m • return L(m) • OK to overshoot: “products” don’t change after L(n - 1) • Running Time: (n3lg n) CS 477/677 - Lecture 22

  35. 2 4 3 8 1 3 2 1 -4 -5 7 5 4 6 The Floyd-Warshall Algorithm • Given: • Directed, weighted graph G = (V, E) • Negative-weight edges may be present • No negative-weight cycles could be present in the graph • Compute: • The shortest paths between all pairs of vertices in a graph CS 477/677 - Lecture 22 35

  36. 3 6 0.5 1 4 3 2 1 2 5 The Structure of a Shortest Path • Vertices in G are given by V = {1, 2, …, n} • Consider a path p = v1, v2, …, vl • An intermediate vertex of p is any vertex in the set {v2, v3, …, vl-1} • E.g.: p = 1, 2, 4, 5: {2, 4} p = 2, 4, 5: {4} 2 CS 477/677 - Lecture 22 36

  37. The Structure of a Shortest Path • For any pair of vertices i, j  V, consider all paths from i to j whose intermediate vertices are all drawn from a subset {1, 2, …, k} • Find p, a minimum-weight path from these paths p1 pu j i pt No vertex on these paths has index > k CS 477/677 - Lecture 22 37

  38. 3 6 0.5 1 2 4 3 2 1 Example dij(k) = the weight of a shortest path from vertex i to vertex j with all intermediary vertices drawn from {1, 2, …, k} • d13(0) = • d13(1) = • d13(2) = • d13(3) = • d13(4) = 6 6 5 5 4.5 CS 477/677 - Lecture 22 38

  39. Readings • Chapter 22 CS 477/677 - Lecture 22

More Related