140 likes | 248 Views
Dynamic Programming. Chapter 7. The Design Technique. Divide the problem into sub-problems which may overlap. Find optimal solutions for the sub-problems Extend solutions to other subdivision which may overlap with previous ones.
E N D
Dynamic Programming Chapter 7
The Design Technique • Divide the problem into sub-problems which may overlap. • Find optimal solutions for the sub-problems • Extend solutions to other subdivision which may overlap with previous ones. • Combine the sub-solutions to obtain a solution for the original problem.
All-Paris Shortest Paths Section 7.5
Problem • Let G=(V, E) be a directed graph with vertices V={1,2, .., n} and edges E. • Let L(i, j) be the non-negative length of the edge (i, j), • where L(i, j) = if there is no edge between i and j. • Distance between i and j = the length of the shortest path from i to j • Find the distances between all vertices.
Solution • Let be the length of the shortest path between i & j that may passes possibly through only the vertices {1, 2, ..., k} • Notice that = L(i, j) and = distance between i and j
Recursive Formula The k vertex j i are vertices chosen from {1, 2, ..,k-1}
Finding the distance • We do it recursively • One can do this by using the recursive formula (but some elements will be repeated!) • But Floyd did it in bottom-up fashion. • Use n+1 matrices D0, D1, ...., Dn, where Dk is used in the k-th iteration
Recursive Formula • If i=j, then D0[i,j]= 0; and otherwise D0[i,j]= L(i,j) • Then Dk[i,j]=min{ Dk-1[i,j],Dk-1[i,k]+ Dk-1[k,j]}
Recursive Formula Notice that • If i=k or j=k then Dk[i,j]= Dk-1[i,j] Dk[i,j]=min{ Dk-1[i,j],Dk-1[i,k]+ Dk-1[k,j]} • This means that the k-th row & column doen’t change during the k-th iteration. • Thus one can use only one matrix D
Floyd’s Algorithm • Doesn’t use n+1 arrays • It uses the same array D because the k-th row & the k-th column don’t change from the previous iteration • All other values D[i,j], where i and j are different from k, can be computed by using only the values in the k-th row & k-th column.
The idea k j Dk[k,j] k Dk[i,k] i Dk[i,j] = min{ , + }
Floyd’s Algorithm Input:L[1..n,, 1..n] length matrix Output: D[1..n, 1..n] distance matrix D L For k 1 to n for i 1 to n for j 1 to n D[i,j] = min{D[i,j], D[i,k] + D[k,j] } end for end for End for
The idea k j Dk[k,j] k Dk[i,k] i Dk[i,j] Add + & compare with if smaller update
Performance Analysis • Time = (n3) • Space = (n2)