1 / 41

CS420 Lecture 9 Dynamic Programming

CS420 Lecture 9 Dynamic Programming. Optimization Problems. In optimization problems a set of choices are to be made to arrive at an optimum, and sub problems are encountered.

leon
Télécharger la présentation

CS420 Lecture 9 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. CS420 Lecture 9Dynamic Programming

  2. Optimization Problems • In optimization problems a set of choices are to be made to arrive at an optimum, and sub problems are encountered. • This often leads to a recursive definition of a solution. However, the recursive algorithm is often inefficient in that it solves the same sub problem many times. • Dynamic programming avoids this repetition by solving the problem bottom up and storing sub solutions.

  3. Dynamic vs Greedy Compared to Greedy, there is no predetermined local best choice of a sub solution but a best solution is chosen by computing a set of alternatives and picking the best.

  4. Dynamic Programming • Dynamic Programming has the following steps - Characterize the structure of the problem - Recursively define the optimum - Compute the optimum bottom up, storing values of sub solutions - Construct the optimum from the stored data

  5. Assembly line scheduling • A factory has two assembly lines with the same stations S1,j and S2,j, j=1..n. • Each station has its own assembly time Ai,j, it takeseitime to enter assembly line i, and xi time to leave assembly line i. • For rush jobs the product can switch lines to go through faster. • Switching from station Si,jcosts ti,jtime.

  6. S1,1 S1,2 S1,3 7 9 3 3 2 2 3 start finish 2 1 4 8 5 6 2 S2,2 S2,1 S2,3 Fastest way to get through?

  7. S1,1 S1,2 S1,3 7 9 3 3 2 2 3 start finish 2 1 4 8 5 6 2 S2,2 S2,1 S2,3 Fastest way to get through: S1,1, S2,2 and S1,3.

  8. Structure of the Optimal Solution •  The fastest way to get through S1,jis: only one way for j=1 two ways for j>1: without extra cost from S1,j-1 or with switch cost from S2,j-1 • A similar argument holds for S2,j.

  9. Optimal Sub structure • the fastest way to get through S1,j is based on the fastest ways to get through S1,j-1 and S2,j-1 • otherwise a faster way could be constructed( contradiction) • This property is called optimal substructure and indicates that a recursive definition of the optimum problem, based on optimal solutions of sub problems can be formulated.

  10. Recursive definition • The fastest way through the assembly lines f* = min(f1(n)+x1,f2(n)+x2) f1(j) = e1+A1,1 if j=1 = min(f1(j-1)+A1,j, f2(j-1)+t2,j-1+A1,j) if j>1 f2(j) = e2+A2,1 if j=1 = min(f2(j-1)+A2,j, f1(j-1)+t2,j-1+A2,j) if j>1 • Draw a call tree, how many times is fi(n-j) called?

  11. Recursive definition • The fastest way through the assembly lines f* = min(f1(n)+x1,f2(n)+x2) f1(j) = e1+A1,1 if j=1 = min(f1(j-1)+A1,j, f2(j-1)+t2,j-1+A1,j) if j>1 f2(j) = e2+A2,1 if j=1 = min(f2(j-1)+A2,j, f1(j-1)+t2,j-1+A2,j) if j>1 • Draw a call tree, how many times is fi(n-j) called: 2n-j times , so exponential complexity

  12. Bottom Up Approach • In the recursive definition we define the f1(n) top down in terms of f1(n-1) and f2(n-1) • When we compute fi(j) bottom up in increasing j order we can reuse the previous fi(j-1) values in the computation of fi(j).

  13. S1,1 S1,2 S1,3 7 9 3 3 2 2 3 start finish 2 1 4 8 5 6 2 S2,2 S2,1 S2,3 Time at end of Si,1?

  14. S1,1 S1,2 S1,3 9 7 9 3 3 2 2 3 start finish 2 1 4 8 5 6 2 12 S2,2 S2,1 S2,3 Best time at end of Si,2 ,source?

  15. S1,1 S1,2 S1,3 9 18 7 9 3 3 2 2 3 start finish 2 1 4 8 5 6 2 12 16 S2,2 S2,1 S2,3 Best time at end of Si,3? source?

  16. S1,1 S1,2 S1,3 9 18 20 7 9 3 3 2 2 3 start finish 2 1 4 8 5 6 2 12 22 16 S2,2 S2,1 S2,3 Best finish time?, source?

  17. S1,1 S1,2 S1,3 9 18 20 7 9 3 3 2 2 3 start finish 23 2 1 4 8 5 6 2 12 22 16 S2,2 S2,1 S2,3 Best Finish time?, source?

  18. FastLine(A,t,e,x,n) { f1[1] = e[1]+A[1,1]; f2[1] = e[2]+A[2,1] for j = 2 to n{ l1[j] = minSource1(j); l2[j] = minSource2(j); } if (f1[n]+x[1] <= f2[n]+x[2]) { fstar = f1[n]+x[1]; lstar=1 } else { fstar= f2[n]+x[2]; lstar=2 } }

  19. minSource1(j) { if (f1[j-1]+A[1,j] <= f2[j-1]+t[2,j-1]+A[1,j]) { f1[j]=f1[j-1]+A[1,j] return 1 } else { f1[j]=f2[j-1]+t[2,j-1]+A[1,j] return 2 } }

  20. minSource2(j) { if (f2[j-1]+A[2,j] <= f1[j-1]+t[1,j-1]+A[2,j]) { f2[j]=f2[j-1]+A[2,j] return 2 } else { f2[j]=f1[j-1]+t[1,j-1]+A[2,j] return 1 } }

  21. The arrays l1 and l2 keep track of which source station is used: li[j] is the line number whose station j-1 is used in the fastest way through Si,j. i = lstar print "line" i ", station" n for j = ndownto 2{ i = li[j] print "line" i ", station" j-1 }

  22. Optimal substructure • Dynamic programming works when a problem has optimal substructure. • Not all problems have optimal substructure.

  23. Shortest Paths • Find a shortest path in terms of number of edges from u to v (uv) in an un-weighted graph. • In lecture 7 we saw that breadth first search finds such shortest paths, and that these paths have no cycles. • Any path from u to v has an intermediate vertex w (which can be u or v). The shortest path from u to v consists of the shortest path from u to w and the shortest path from w to v. Why?

  24. Shortest Paths • Find a shortest path in terms of number of edges from u to v (uv) in an un-weighted graph. • In lecture 7 we saw that breadth first search finds such shortest paths, and that these paths have no cycles. • Any path from u to v has an intermediate vertex w (which can be u or v). The shortest path from u to v consists of the shortest path from u to w and the shortest path from w to v. If not there would be a shorter shortest path (contradiction). So?

  25. Shortest Paths • ... has optimal subsructure

  26. Longest simple paths • Simple: no cycles • Again in an unweighted graph • Suppose again w is on the path from u to v. • Then the longest simple path from u to vdoes not need to consist of the longest simple paths from u to w and from w to v.

  27. Q R T Longest simple path from Q to T?

  28. Q R T Longest simple path from Q to T: QRT Longest simple path from R to T?

  29. Q R T Longest simple path from Q to T: QRT Longest simple path from R to T: RQT Longest path from Q to T is Q ->R-> T, but it does not consist of the longest paths from Q to R and from R to T. The longest simple path problem has no optimal substructure!

  30. Subsequence • A sequence X=<x1,x2,..,xm> is a subsequence of sequence Y=<y1,y2,..,yn> if there is a strictly increasing sequence of indices I=<i1,i2,..,im> such that: for all j=1,2,..,m we have X[j]=Y[ij] • Eg, X=<b,c,d,b>, Y=<a,b,c,b,d,a,b>, I=<2,3,5,7>. • How many sub sequences does Y have?

  31. Subsequence • A sequence X=<x1,x2,..,xm> is a subsequence of sequence Y=<y1,y2,..,yn> if there is a strictly increasing sequence of indices I=<i1,i2,..,im> such that: for all j=1,2,..,m we have X[j]=Y[ij] • Eg, X=<b,c,d,b>, Y=<a,b,c,b,d,a,b>, I=<2,3,5,7>. • How many sub sequences does Y have: 2|Y|

  32. Longest Common Subsequence (LCS) • A sequence Z is a common subsequence of X and Y if it is a subsequence of X and of Y. • The longest common subsequence problem (LCS) is to find, well yes, the longest common subsequence of two sequences X and Y. • Trial and error takes exponential because there are an exponential #subsequences

  33. LCS has optimal substructure Xi is the length i prefix of X. Cormenet.al. prove that for two sequences X=<x1,x2,..,xm> and Y=<y1,y2,..,yn> and LCS Z=<z1,z2,..,zk> of X and Y: 1. if xm=yn then zk=xm and Zk-1 is an LCS of Xm-1 and Yn-1 2. if xmyn then zkxm Z is an LCS of Xm-1 and Y 3. if xmyn then zkyn Z is an LCS of Yn-1 and X

  34. Recursive algorithm for LCS 1. if xm=yn then zk=xm and Zk-1 is an LCS of Xm-1 and Yn-1 2. if xmyn then zkxm Z is an LCS of Xm-1 and Y 3. if xmyn then zkyn Z is an LCS of Yn-1 and X gives rise to a recursive formulation: LCS(Xm,Yn ): if xm=yn find the LCS of Xm-1 and Yn-1 and append xm to it, else find the LCSs of Xm-1 and Y and of Yn-1 and X and pick the largest. Complexity? (call tree)

  35. Length of LCS Length C[i,j] of of Xi and Yj: C[i,j] = 0 if i=0 or j=0, C[i-1,j-1]+1 if i,j>0 and xi=yj max(C[i,j-1],C[i-1,j] if i,j>0 and xiyj This can be computed in dynamic fashion We build C bottom up and we build a table B B[i,j] points at the entry chosen in the recurrence of C: for C[i-1,j-1]  for C[i,j-1} for C[i-1,j] In which order?

  36. Length of LCS Length C[i,j] of of Xi and Yj: C[i,j] = 0 if i=0 or j=0, C[i-1,j-1]+1 if i,j>0 and xi=yj max(C[i,j-1],C[i-1,j] if i,j>0 and xiyj This can be computed in dynamic fashion We build C bottom up and we build a table B B[i,j] points at the entry chosen in the recurrence of C: for C[i-1,j-1]  for C[i,j-1} for C[i-1,j] In which order? Def before use  row major is OK

  37. LCS algorithm for i in 1 to m C[i,0]=0 for j in 0 to n C[0,j] = 0 for i = 1 to m for j = 1 to n if (X[i]==Y[j]) { C[i,j]=C[i-1,j-1]+1; B[i,j]= } else if (C[i-1,j]>=C[i,j-1]) { C[i,j]=C[i-1,j]; B[i,j]=  } else { C[i,j]=C[i,j-1]; B[i,j]=  }

  38. try it on x=baby=ababa C: B: 0 0 0 0 0 0 0 0 1 1 1 1  0 1 1 2 2 2  0 0 2 2 3 3 

  39. Finding LCS • By backtracking through B, starting at B[n,m] we can print the LCS. • A  at B[i,j] indicates that X[i]=Y[j] is the end of the prefix of the LCS still to be printed.

  40. Print LCS if (i==0 or j==0) return if (B[i,j]==) { Print-LCS(i-1,j-1); print(X[i]) else if (B[i,j]== ) Print-LCS(i-1,j) else Print-LCS(i,j-1)

  41. LCS • The B table is not necessary; • C can be used in Print-LCS. • The creation of C and B in LCS takes O(mn) time and space • Print-LCS takes O(m+n) time.

More Related