CS 561, Lecture 24 Outline All-Pairs Shortest Paths Example

CS 561, Lecture 24 Outline All-Pairs Shortest Paths Example

Outline CS 561, Lecture 24 • All Pairs Shortest Paths Jared Saia • TSP Approximation Algorithm University of New Mexico 1 All-Pairs Shortest Paths Example • For the single-source shortest paths problem, we wanted to find the shortest path from a source vertex s to all the other vertices in the graph • We will now generalize this problem further to that of finding • For any vertex v, we have dist(v, v) = 0 and pred(v, v) = the shortest path from every possible source to every possible NULL destination • If the shortest path from u to v is only one edge long, then • In particular, for every pair of vertices u and v, we need to dist(u, v) = w(u → v) and pred(u, v) = u compute the following information: • If there’s no shortest path from u to v, then dist(u, v) = ∞ – dist(u, v) is the length of the shortest path (if any) from and pred(u, v) = NULL u to v – pred(u, v) is the second-to-last vertex (if any) on the short- est path (if any) from u to v 2 3 APSP Lots of Single Sources • The output of our shortest path algorithm will be a pair of |V | × |V | arrays encoding all |V |2 distances and predecessors. • Many maps contain such a distance matric - to find the • Most obvious solution to APSP is to just run SSSP algorithm distance from (say) Albuquerque to (say) Ruidoso, you look |V | times, once for every possible source vertex in the row labeled “Albuquerque” and the column labeled • Specifically, to fill in the subarray dist(s, ∗), we invoke either “Ruidoso” Dijkstra’s or Bellman-Ford starting at the source vertex s • In this class, we’ll focus only on computing the distance array • We’ll call this algorithm ObviousAPSP • The predecessor array, from which you would compute the actual shortest paths, can be computed with only minor ad- ditions to the algorithms presented here 4 5 ObviousAPSP Analysis • The running time of this algorithm depends on which SSSP ObviousAPSP(V,E,w){ algorithm we use for every vertex s{ • If we use Bellman-Ford, the overall running time is O(|V |2|E|) = dist(s,*) = SSSP(V,E,w,s); O(|V |4) } • If all the edge weights are positive, we can use Dijkstra’s in- } stead, which decreases the run time to Θ(|V ||E|+|V |2 log |V |) = O(|V |3) 6 7 Problem Dynamic Programming • We’d like to have an algorithm which takes O(|V |3) but which • Recall: Dynamic Programming = Recursion + Memorization can also handle negative edge weights • Thus we first need to come up with a recursive formulation • We’ll see that a dynamic programming algorithm, the Floyd of the problem Warshall algorithm, will achieve this • We might recursively define dist(u, v) as follows: • Note: the book discusses another algorithm, Johnson’s al- gorithm, which is asymptotically better than Floyd Warshall 0 if u = v dist(u, v) = on sparse graphs. However we will not be discussing this minx dist(u, x) + w(x → v) otherwise algorithm in class. 8 9 The problem The solution • To avoid this circular dependency, we need some additional • In other words, to find the shortest path from u to v, try all parameter that decreases at each recursion and eventually possible predecessors x, compute the shortest path from u reaches zero at the base case to x and then add the last edge u → v • One possibility is to include the number of edges in the short- • Unfortunately, this recurrence doesn’t work est path as this third magic parameter • To compute dist(u, v), we first must compute dist(u, x) for • So define dist(u, v, k) to be the length of the shortest path every other vertex x, but to compute any dist(u, x), we first from u to v that uses at most k edges need to compute dist(u, v) • Since we know that the shortest path between any two ver- • We’re stuck in an infinite loop! tices uses at most |V | − 1 edges, what we want to compute is dist(u, v, |V | − 1) 10 11 The Recurrence The Algorithm • It’s not hard to turn this recurrence into a dynamic program- ming algorithm • Even before we write down the algorithm, though, we can 4 0 if u = v tell that its running time will be Θ(|V | ) dist(u, v, k) = ∞ if k = 0 and u 6= v • This is just because the recurrence has four variables — u, minx dist(u, x, k − 1) + w(x → v) otherwise v, k and x — each of which can take on |V | different values • Except for the base cases, the algorithm will just be four nested “for” loops 12 13 DP-APSP The Problem DP-APSP(V,E,w){ for all vertices u in V{ for all vertices v in V{ if(u=v) dist(u,v,0) = 0; • This algorithm still takes O(|V |4) which is no better than the else ObviousAPSP algorithm dist(u,v,0) = infinity; • If we use a certain divide and conquer technique, there is a }} way to get this down to O(|V |3 log |V |) (think about how you for k=1 to |V|-1{ might do this) for all vertices u in V{ • However, to get down to O(|V |3) run time, we need to use for all vertices u in V{ a different third parameter in the recurrence dist(u,v,k) = infinity; for all vertices x in V{ if (dist(u,v,k)>dist(u,x,k-1)+w(x,v)) dist(u,v,k) = dist(u,x,k-1)+w(x,v); }}}}} 14 15 Floyd-Warshall The recurrence • Number the vertices arbitrarily from 1 to |V | • Define dist(u, v, r) to be the shortest path from u to v where all intermediate vertices (if any) are numbered r or less We get the following recurrence: • If r = 0, we can’t use any intermediate vertices so shortest path from u to v is just the weight of the edge (if any) w(u → v) if r = 0 between u and v dist(u, v, r) = min{dist(u, v, r − 1), • If r > 0, then either the shortest legal path from u to v goes dist(u, r, r − 1) + dist(r, v, r − 1)} otherwise through vertex r or it doesn’t • We need to compute the shortest path distance from u to v with no restrictions, which is just dist(u, v, |V |) 16 17 The Algorithm Analysis FloydWarshall(V,E,w){ for u=1 to |V|{ for v=1 to |V|{ dist(u,v,0) = w(u,v); }} • There are three variables here, each of which takes on |V | for r=1 to |V|{ possible values for u=1 to |V|{ • Thus the run time is Θ(|V |3) for v=1 to |V|{ • Space required is also Θ(|V |3) if (dist(u,v,r-1) < dist(u,r,r-1) + dist(r,v,r-1)) dist(u,v,r) = dist(u,v,r-1); else dist(u,v,r) = dist(u,r,r-1) + dist(r,v,r-1); }}}} 18 19 Take Away TSP • Floyd-Warshall solves the APSP problem in Θ(|V |3) time • A version of the TSP problem is: “Given a weighted graph even with negative edge weights G, what is the shortest Hamiltonian Cycle of G?” • Floyd-Warshall uses dynamic programming to compute APSP • Where a Hamiltonian Cycle is a path that visits each node • We’ve seen that sometimes for a dynamic program, we need in G exactly once and returns to the starting node to introduce an extra variable to break dependencies in the • This TSP problem is NP-Hard by a reduction from Hamilto- recurrence. nian Cycle • We’ve also seen that the choice of this extra variable can • However, there is a 2-approximation algorithm for this prob- have a big impact on the run time of the dynamic program lem if the edge weights obey the triangle inequality 20 21 Triangle Inequality Approximation Algorithm • In many practical problems, it’s reasonable to make the as- sumption that the weights, c, of the edges obey the triangle inequality • Given a weighted graph G, the algorithm first computes a • The triangle inequality says that for all vertices u, v, w ∈ V : MST for G, T , and then arbitrarily selects a root node r of c(u, w) ≤ c(u, v) + c(v, w) T . • It then lets L be the list of the vertices visited in a depth • In other words, the cheapest way to get from u to w is always first traversal of T starting at r. to just take the edge (u, w) • Finally, it returns the Hamiltonian Cycle, H, that visits the • In the real world, this is often a pretty natural assumption. vertices in the order L. For example it holds if the vertices are points in a plane and the cost of traveling between two vertices is just the euclidean distance between them. 22 23 Triangle Inequality Approximation Algorithm • In many practical problems, it’s reasonable to make the as- sumption that the weights, c, of the edges obey the triangle inequality Approx-TSP(G){ • The triangle inequality says that for all vertices u, v, w ∈ V : T = MST(G); L = the list of vertices visited in a depth first traversal c(u, w) ≤ c(u, v) + c(v, w) of T, starting at some arbitrary node in T; • In other words, the cheapest way to get from u to w is always H = the Hamiltonian Cycle that visits the vertices in the to just take the edge (u, w) order L; • In the real world, this is usually a pretty natural assumption.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us