Shortet Path
Myriad of application Finding hortet ditance between location (Google map, etc.) Internet router protocol: OSPF (Open Shortet Path Firt) i ued to find the hortet path to interchange package between erver (IP) Traffic information ytem Routing in VSLI etc...
Shortet ditance between two point not alway follow human intuition It may depend on many more contrain than the pure geometric one.
Shortet path problem in direct weighted graph Given a digraph G = (V, E) with edge weight w : E R, a path p = {v 0,..., v k } i a equence of conecutive edge, where (v i, v i+ ) E define w(p) = k i=0 w(v i, v i+ ). The hortet path between u and v a δ(u, v) = min p {w(p) u p v} v v v v 4 v 5 - -5 δ(v, v 5 ) = If G i undirected, we can conider every edge a doubly directed. Unweighted, every edge of weight =.
Optimal ubtructure of hortet path Given G = (V, E), w : E R, for any hortet path p : u v and any i, j vertice in p, the ub-path p = i j in p ha the hortet ditance δ(i, j). i j u v Negative cycle u 4 5 v δ(u, v) =
Taxonomy of hortet path problem Single ource hortet path (SSSP): Given G = (V, E), w : E R and V, compute δ(, v), v V {v}. In the graph below we want to compute (, a), (, b), (, c), (, d) All path hortet path (APSP): Given = (V, E), w : E R compute δ(u, v) for every pair (u, v) V V. In the graph below we want to compute (, a), (a, ),... (d, b), (b, d), (d, c), (c, d) a b d c
Single ource hortet path Let u conider the particular cae of having a ource and a ink t Aume that w : e R + Brute-force(G, W,, t) for all imple p : t do compute w(p) end for Compare all p return the p with mallet w(p) The number of path could be O( n ) a d 4 n b t t
Shortet Path Tree SSSP algorithm have the property that at termination the reulting path form a hortet path tree. Given G = (V, E) with edge weight w e and a ditinguihed V, a hortet path tree i a directed ub-tree T = (V, E ) of G,.t. T i rooted at, V i the et of vertice in G reachable from, v V the path v in T i the hortet path δ(, v). a d b c f a d b c f
Triangle Inequality Given G = (V, E), W, if u, v, z V, notice the hortet path u v i any other path between u and v. Therefore. Theorem For all u, v, z V δ(u, v) δ(u, z) + δ(z, v). u v u v δ(u, z) Want minimum z δ(z, y) δ(u, v) - - z Notice, in thi cae δ(u, v) =
Baic technique for SSSP: Relaxation Given G = (V, E), W. v V we maintain a SP-etimate d[v], which i an UB on δ(, v). Initially, tart with d[v] = +, v V {} and d[] = 0. Repeatedly improve etimate toward the goal d[v] = δ(, v). For (u, v) E, Relax(u, v, w(u, v)) if d[v] > d[u] + w(u, v) then d[v] = d[u] + w(u, v) end if d[u] d[v] u w(u,v) v
Generic Relaxation algorithm Relaxation(G, W, ) for all v V {} do d[v] = + end for d[] = 0 while (u, v) with d[v] > d[u] + w(u, v) do Relax(u, v, w(u, v)) end while Lemma For all v V, Relaxation(G, W, ) maintain the invariant that d[v] δ(, v). Proof (Induction) I.H. when applying Relax(u, v, w(u, v)) we get d[u] δ(, u) By the triangle ineq. δ(, v) δ(, u) + δ(u, v) d[u] + w(u, v). Therefore, letting δ(u, v) = d[u] + w(u, v) i not a problem.
Generic Relaxation algorithm a b a b d c d c a b d c a d a b b c d c
Relaxation algorithm Note: If a negative-weight cycle i reachable from, then Relaxation(G, W, ) doe not top. If we apply Relaxation(G, W, ) to the graph in the graph below, we get that the number of tep follow T (n) = T (n ) +, T (n) = Θ( n/ ) 4 5 6 n 0 8 8 8 4 4 4 Prove that indeed the complexity of Relaxation on the above graph i T (n) = Θ( n/ )
Recall: Dktra SSSP E.W.Dktra, A note on two problem in connexion with graph. Num. Mathematik, (959) Greedy algorithm. Relax edge in an increaing ball around. Ue a priority queue Q Dtra doe not work with negative weight Complexity: Fatet SSSP algorithm. Dktra(G, W, ) Initialize SP-etimate on V S =, Q = {V } while Q do u =EXT-MIN(Q) S = S {u} for all v Adj[u] do Relax(u, v, w(u, v)) end for end while Q implementation Wort-time complexity Array O(n ) Heap O(m lg n) Fibonacci heap O(m + n lg n)
Bellman-Ford-Moore SSSP R. Bellman (958) L. Ford (956) E. Moore (957) The algorithm BFM i ued for G with negative weight, but without negative cycle. Given G, w, V (G),with n vertice and m edge, the BFM algorithm doe n iteration: Each iteration i doe a relaxation on all edge than can be reached from in at mot i-tep, the remaining one are et to (e, e,..., e n ), (e }{{}, e,..., e n ), (e }{{}, e,..., e n ) }{{} i= i= i=n
BFM Algorithm Recall that given a graph G, V = n, E = n, and a et of edge weight w with a ource vertex v V, BFM (G, w, ) Initialize v, d[v] =, π[v] = u Initialize d[] = 0 for i = to n do for every (u, v) E do Relax(u, v, w(u, v)) end for end for for every (u, v) E do if d[v] > d[u] + w(u, v) then return Negative-weight cycle end if end for Relax(u, v, w(u, v)) if d[v] > d[u] + w(u, v) then d[v] = d[u] + w(u, v) π[v] = u end if
BFM Algorithm: Example 0 0 8 a 4 g b f e c d Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a b c d e f g
BFM Algorithm: Example 0 0 8 0 a 4 g 8 b f e c d Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 0 0 0 0 0 b c d e f g 8 8 8 8 8 8 8
BFM Algorithm: Example 0 0 8 0 a 4 g 8 b f 9 c e d Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 0 0 0 0 0 b c d e f 9 9 9 9 9 9 g 8 8 8 8 8 8 8
BFM Algorithm: Example 0 0 8 5 0 a b 4 g f 8 9 c e 8 d Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 5 5 5 5 5 b 0 0 0 0 0 c d e 8 8 8 8 8 f 9 9 9 9 9 9 g 8 8 8 8 8 8 8
BFM Algorithm: Example 0 0 8 5 6 a b 4 g f 8 9 c e 7 d Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 5 5 5 5 5 b 0 6 0 0 0 c d e 8 7 7 7 7 f 9 9 9 9 9 9 g 8 8 8 8 8 8 8
BFM Algorithm: Example 0 0 8 5 5 7 a b c 4 g f e 8 9 7 d 4 Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 5 5 5 5 5 b 0 6 5 5 5 c 7 7 7 d 4 4 4 e 8 7 7 7 7 f 9 9 9 9 9 9 g 8 8 8 8 8 8 8
BFM Algorithm: Example 0 0 8 5 5 6 a b c 4 g f e 8 9 7 d 0 Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 5 5 5 5 5 b 0 6 5 5 5 c 7 6 6 d 4 0 4 e 8 7 7 7 7 f 9 9 9 9 9 9 g 8 8 8 8 8 8 8
BFM Algorithm: Example 0 0 8 5 5 6 a b c 4 g f e 8 9 7 d 9 Node i 0 4 5 6 7 0 0 0 0 0 0 0 0 a 0 0 5 5 5 5 5 b 0 6 5 5 5 c 7 6 6 d 4 0 9 e 8 7 7 7 7 f 9 9 9 9 9 9 g 8 8 8 8 8 8 8
Complexity BFM O(n) O() O(m) O(nm) O(m) Complexity T(n)=O(nm)
Correctne of BFM Lemma In the BFM-algorithm, after the ith. iteration we have that d[v] the weight of every path v uing at mot i edge, v V. Proof (Induction on i) Before the ith iteration, d[v] min{w(p)} over all path p with at mot i edge. The relaxation only decreae d[v] The ith iteration conider all path with i edge when relaxing the edge to v. at mot i edge v
Correctne of BFM Theorem If G, w ha no negative weight cycle, then at the end of the BFM-algorithm d[v] = δ(, v). Proof Without negative-weight cycle, hortet path are alway imple. Every imple path ha at mot n vertice and n edge. By the previou lemma, the n iteration yield d[v] δ(, v). By the invariance of the relaxation algorithm d[v] δ(, v).
Correctne of BFM Theorem BFM will report negative-weight cycle if there exit in G. Proof Without negative-weight cycle in G, the previou theorem implie d[v] = δ(, v), and by triangle inequality d[v] δ(, u) + w(u, v), o BFM won t report a negative cycle if it doen t exit. If there i a negative-weight cycle, then one of it edge can be relaxed, o BFM will report correctly.
Shortet path in a direct acyclic graph (dag). Min-cot path in DAG INPUT: Edge weighted dag G = (V, E, w), V = n, w : E R together with given, t V. QUESTION: Find a path P : t of minimum total weight. Notice given a dag G = (V, E), W we wih to find a path P from to t.t. min P () P w. a c 6 b e g
Arranging dag into a line Arrange the dag in topological order, o that all edge go from left to right. Thi can be done in O(n) uing DFS. a c 6 b e f c a 6 b f e DFS
We want to find horter ditance from to v. Let d(v) = ditance v d(f ) = min{d(b)+, d(c)+} The chema i baed on the topological linearity of G. c a b f e 6 Shortet ditance in dag G Initialize d() := 0 and v V {}, d(v) := for all v V {} in linearized order do d(v) := min (u,v) E {d(u) + w uv } end for Complexity? T (n) = O(n)
All pair hortet path: APSP Given G = (V, E), V = n, E = m and a weight w : E R we want to determine u, v V, δ(u, v). We aume we can have w < 0 but G doe not contain negative cycle. Naive idea: We apply O(n) time BFM or Dktra (if there are not negative weight) Repetition of BFM: O(n m) Repetition of Dktra: O(nm lg n) (if Q i implemented by a heap)
All pair hortet path: APSP Unlike in the SSSP algorithm that aumed adjacency-lit repreentation of G, for the APSP algorithm we conider the adjacency matrix repreentation of G. For convenience V = {,,... n}. The n n adjacency matrix W = (w(i, j)) of G, w: 0 if i = j w = w if (i, j) E if i j and (i, j) E
All pair hortet path: APSP The input i a n n adjacency matrix W = (w ) 4 0 4 0 W = 0 4 0 0 0 The output i a n n matrix D = (d ), where d = δ(i, j) For the implementation we alo need to compute the et of predeceor matrix Π k
Bernard-Floyd-Warhall Algorithm R. Bernard: Tranitivité et connexité C.R.Aca. Sci. 959 R. Floyd: Algorithm 97: Shortet Path. CACM 96 S. Warhall: A theorem on Boolean matrice. JACM, 96 The BFW Algorithm ued dynamic programming to compare all poible path between each pair of vertice in G. The algorithm work in O(n ) and the number of edge could be O(n ).
Optimal ubtructure of APSP Recall: Triangle inequality δ(u, v) δ(u, z) + δ(z, v). u δ(u, v) δ(u, z) δ(z, y) z Let p = p, p,..., p }{{ r, p } r and intermediate v. Let d (k) be the hortet i j.t. the intermediate vertice are in {,..., k}. So if k = 0, then d (0) = w. v
The recurrence Let p a hortet path i j with value d (k) If k i not an intermediate vertex of p, then d (k) If k i an intermediate vertex of p, then p = (i,..., k) (k,..., j) }{{}}{{} p p = d (k ) By triangle inequality p i a hortet path i k and p i a hortet path k j. { Therefore d (k) w if k = 0 = min{d (k ), d (k ) ik + d (k ) kj } if k
Bottom-up BFW-algorithm Given G = (V, E), w : E Z without negative cycle, the following DP algo. compute d (n), i, j V : BFW W = (w ) for k = to n do for i = to n do for j = to n do d (k) end for end for end for return d (n) = min{d (k ), d (k ) ik + d (k ) kj } Time complexity: T (n) = O(n ), S(n) = O(n ) but S(n) can be lowered to O(n ) How? Correctne follow from the recurrence argument.
Example 4 0 4 0 0 D (0) = 0 4 0 0 D() = 0 0 0 0 0 0 0 0 0 D () = 0 0 0 D() = 0 0 0 D(4) = 0 0 0 0 0 0 0 0 0 0 0 For intance, d, = (uing vertex ) = 4 (uing all vertice) d 4,
Example 0 NIL NIL NIL D (0) = 0 4 0 0 Π (0) = NIL NIL NIL NIL 0 4 NIL NIL NIL 0 NIL NIL NIL D () = 0 0 0 Π () = NIL NIL NIL NIL 0 0 4 NIL NIL 4 0 4 0 NIL NIL NIL D () = 0 0 0 Π () = NIL NIL NIL NIL 0 0 4 NIL NIL 0 D () = 0 0 0 Π () = 0 0 NIL NIL NIL 4 NIL
Contructing the hortet path We want to contruct the matrix Π = (π ), where π = predeceor of j in hortet i j, we define a equence of matrice Π (0),..., Π (n).t. Π (k) = (π (k) ), i.e. the matrix of lat predeceor in the hortet path i j, which ue only vertice in {,..., k}. { If k = 0: π (k) NIL if i = j or w =, = i if i j and w. For k we get the recurrence: π (k) = { π (k ) π (k ) kj if d (k ) d (k ) ik otherwie. + d (k ) kj,
BFW with path BFW W d (0) = W for k = to n do for i = to n do for j = to n do if d (k) d (k) Π (k) d (k ) = d (k ) = Π (k ), d (k ) ik ele d (k) = d (k ) ik + d (k ) kj Π (k) end if end for end for end for return d (n) = Π (k ) kj Complexity: T (n) = O(n ) + d (k ) kj then
Concluion Dktra BFM BFW w 0 O(m lg n) O(nm) O(n ) w R NO O(nm) O(n ) There exit an algorithm by D. Johnon (978) that work in O(n lg n) for pare graph with negative edge. It ue Dktra a a function. For further reading on hortet path, ee chapter 4 and 5 of Cormen, Leieron, Rivet, Stein: Introduction to Algorithm.