70 likes | 283 Views
All-Pairs Shortest Paths (26.0/25). HW: problem 26-1, p. 576/25-1, p. 641 Directed graph G = (V,E), weight E Goal: Create n n matrix of s-p distances (u,v) Running Bellman-Ford once from each vertex O( ) = O( ) on dense graphs
E N D
All-Pairs Shortest Paths (26.0/25) • HW: problem 26-1, p. 576/25-1, p. 641 • Directed graph G = (V,E), weight E • Goal: Create n n matrix of s-p distances (u,v) • Running Bellman-Ford once from each vertex O( ) = O( ) on dense graphs • Adjacency-matrix representation of graph: • n n matrix W = (wij) of edge weights • assume wii = 0 i, s-p to self has no edges in absence of negative cycles
Dynamic Programming (26.1/25.1) • dij(m) = weight of s-p from i to j with m edges dij(0) = 0 if i = j and dij(0) = if i j dij(m) = mink{dik(m-1) + wkj} • Runtime = O( n4) because n-1 passes each computing n2 d’s in O(n) time m-1 j i m-1
Matrix Multiplication (26.1/25.1) • Similar: C = A B, two n n matrices cij = k aik bkj O(n3) operations • replacing: ‘‘ + ’’ ‘‘ min ’’ ‘‘ ’’ ‘‘ + ’’ • gives cij= mink {aik + bkj} • D(m) = D(m-1) ‘‘ ’’ W • identity matrix is D(0) • Cannot use Strassen’s because no subtraction • Time is still O(n n3 ) = O(n4 ) • Repeated squaring: W2n = Wn Wn Compute W, W2 , W4 ,..., W2k , k= log n, O(n3 log n)
Floyd-Warshall Algorithm (26.2/25.2) • Also dynamic programming but faster (by log n) • cij(m) = weight of s-p from i to j with intermediate vertices in the set {1, 2, ..., m} (i, j)= cij(n) • DP: compute cij(n) in terms of smaller cij(n-1) • cij(0) = wij • cij(m) = min {cij(m) , cim(m-1) + cmj(m-1) } intermediate nodes in {1, 2, ..., m} cmj(m-1) m cim(m-1) j i cij(m-1)
Floyd-Warshall Algorithm (26.2/25.2) • Difference from previous: we do not check all possible intermediate vertices. • Code: for m=1..n do for i=1..n do for j = 1..n do cij(m) = min {cij(m-1) , cim(m-1) + cmj(m-1) } • Runtime O(n3 ) • Transitive Closure G* of graph G: • (i,j) G* iff path from i to j in G • Adjacency matrix, elements on {0,1} • Floyd-Warshall with ‘‘ min ’’ ‘‘OR’’ , ‘‘+’’ ‘‘ AND ’’ • Runtime O(n3 ) • Useful in many problems
Johnson’s Algorithm (26.3/25.3) • Johnson’s = Bellman-Ford + Dijkstra’s (or Thorup’s) • Re-weighting of the graph G=(V,E,w): • assign weights to all vertices h: V • change weight of edges w’(u,v) = w(u,v) + h(u) - h(v) • then for any u,v: ’(u, v) = (u,v) + h(u) - h(v) • Addition of auxiliary vertex s: • V V + s • E V + {(s,v), v V} , w(s,v) = 0 • Run Bellman-Ford: find h(v) = (s,v) (or negat. cycle) • w’(u,v) 0 since h(v) h(u) + w(u,v) and w’(u,v) = w(u,v) + h(u) - h(v) 0
Johnson’s Algorithm (26.3/25.3) • Since all weights are nonnegative, apply Dijkstra’s for each source and find all modified distances ’(u, v) • Restore actual distances from modified using (u, v) = ’(u,v) - h(u) + h(v) • Runtime: • O(VE) = V times O(E) - M.Thorup’s algorithm • O(V(E+V log V)) = V times Dijkstra’s with Fibonacci heaps • O(VE log V) = V times Dijkstra’s with binary heaps (faster for sparse graphs) • To find s-p for a single pair = s-p from single source = all pairs shortest paths.