All-Pars Shortest Paths Gven an n-vertex drected weghted graph, fnd a shortest path from vertex to vertex for each of the n vertex pars (,). Dstra s Sngle Source Algorthm Use Dstra s algorthm n tmes, once wth each of the n vertces as the source vertex. Performance Tme complexty s O(n ) tme. Wors only when no edge has a cost < 0. Dynamc Programmng Soluton Tme complexty s Theta(n ) tme. Wors so long as there s no cycle whose length s < 0. When there s a cycle whose length s < 0, some shortest paths aren t fnte. ƒ If vertex s on a cycle whose length s -, each tme you go around ths cycle once you get a to path that s unts shorter than the prevous one. Smpler to code, smaller overheads. Known as Floyd s shortest paths algorthm. Decson Sequence Frst decde the hghest ntermedate vertex (.e., largest vertex number) on the shortest path from to. If the shortest path s,,,,,,, the frst decson s that vertex s an ntermedate vertex on the shortest path and no ntermedate vertex s larger than. Then decde the hghest ntermedate vertex on the path from to, and so on. Problem State (,,) denotes the problem of fndng the shortest path from vertex to vertex that has no ntermedate vertex larger than. (,,n) denotes the problem of fndng the shortest path from vertex to vertex (wth no restrctons on ntermedate vertces).
Cost Functon Let c(,,) be the length of a shortest path from vertex to vertex that has no ntermedate vertex larger than. c(,,n) c(,,n) s the length of a shortest path from vertex to vertex that has no ntermedate vertex larger than n. No vertex s larger than n. Therefore, c(,,n) s the length of a shortest path from vertex to vertex. c(,,0) c(,,0) s the length of a shortest path from vertex to vertex that has no ntermedate vertex larger than 0. ƒ Every vertex s larger than 0. ƒ Therefore, c(,,0) s the length of a sngle-edge path from vertex to vertex. Recurrence For c(,,), > 0 The shortest path from vertex to vertex that has no ntermedate vertex larger than may or may not go through vertex. If ths shortest path does not go through vertex, the largest permssble ntermedate vertex s -. So the path length s c(,,-). < Recurrence For c(,,) ), > 0 Shortest path goes through vertex. We may assume that vertex s not repeated because no cycle has negatve length. Largest permssble ntermedate vertex on to and to paths s -. Recurrence For c(,,) ), > 0 to path must be a shortest to path that goes through no vertex larger than -. If not, replace current to path wth a shorter to path to get an even shorter to path.
Recurrence For c(,,) ), > 0 Recurrence For c(,,) ), > 0 Smlarly, to path must be a shortest to path that goes through no vertex larger than -. Therefore, length of to path s c(,,-), and length of to path s c(,,-). So, c(,,) = c(,,-) + c(,,-). Combnng the two equatons for c(,,), we get c(,,) = mn{c(,,-), c(,,-) + c(,,-)}. We may compute the c(,,)s n the order =,,,, n. Floyd s Shortest Paths Algorthm for (nt = ; <= n; ++) for (nt = ; <= n; ++) for (nt = ; <= n; ++) c(,,) = mn{c(,,-), c(,,-) + c(,,-)}; Tme complexty s O(n ). More precsely Theta(n ). Theta(n ) space s needed for c(*,*,*). Space Reducton c(,,) = mn{c(,,-), c(,,-) + c(,,-)} When nether nor equals, c(,,-) s used only n the computaton of c(,,). column (,) So c(,,) can overwrte c(,,-). row Space Reducton c(,,) = mn{c(,,-), c(,,-) + c(,,-)} When equals, c(,,-) equals c(,,). ƒ c(,,) = mn{c(,,-), c(,,-) + c(,,-)} = mn{c(,,-), 0 + c(,,-)} = c(,,-) So, when equals, c(,,) can overwrte c(,,-). Smlarly when equals, c(,,) can overwrte c(,,-). So, n all cases c(,,) can overwrte c(,,-). Floyd s Shortest Paths Algorthm for (nt = ; <= n; ++) for (nt = ; <= n; ++) for (nt = ; <= n; ++) c(,) = mn{c(,), c(,) + c(,)}; Intally, c(,) = c(,,0). Upon termnaton, c(,) = c(,,n). Tme complexty s Theta(n ). Theta(n ) space s needed for c(*,*).
Buldng The Shortest Paths Let ay(,) be the largest vertex on the shortest path from to. Intally, ay(,) = 0 (shortest path has no ntermedate vertex). for (nt = ; <= n; ++) for (nt = ; <= n; ++) for (nt = ; <= n; ++) f (c(,) > c(,) + c(,)) {ay(,) = ; c(,) = c(,) + c(,);} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Example Intal Cost Matrx c(*,*) = c(*,*,0) Fnal Cost Matrx c(*,*) = c(*,*,n) ay Matrx 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Shortest Path Buld A Shortest Path Shortest path from to. Path length s. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The path s. ay(,) = ay(,) = ay(,) =
Buld A Shortest Path Buld A Shortest Path 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The path s. ay(,) = 0 ay(,) = ay(,) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The path s. ay(,) = 0 ay(,) = 0 ay(,) = Buld A Shortest Path Output A Shortest Path 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The path s. ay(,) = 0 ay(,) = 0 publc statc vod outputpath(nt, nt ) {// does not output frst vertex () on path f ( == ) return; f (ay[][] == 0) // no ntermedate vertces on path System.out.prnt( + " "); else {// ay[][] s an ntermedate vertex on the path outputpath(, ay[][]); outputpath(ay[][], ); } } Tme Complexty Of outputpath O(number of vertces on shortest path)