1 / 14

Dynamic Programming

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.

zora
Download Presentation

Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Programming Chapter 7

  2. 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.

  3. All-Paris Shortest Paths Section 7.5

  4. 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.

  5. 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

  6. Recursive Formula The k vertex j i are vertices chosen from {1, 2, ..,k-1}

  7. 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

  8. 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]}

  9. 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

  10. 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.

  11. The idea k j Dk[k,j] k Dk[i,k] i Dk[i,j] = min{ , + }

  12. 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

  13. The idea k j Dk[k,j] k Dk[i,k] i Dk[i,j] Add + & compare with if smaller update

  14. Performance Analysis • Time = (n3) • Space = (n2)

More Related