Download presentation

Presentation is loading. Please wait.

Published byBrady Wattley Modified over 2 years ago

1
CS420 Lecture 9 Dynamic 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 S 1,j and S 2,j, j=1..n. Each station has its own assembly time A i,j, it takes e i time to enter assembly line i, and x i time to leave assembly line i. For rush jobs the product can switch lines to go through faster. Switching from station S i,j costs t i,j time.

6
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Fastest way to get through?

7
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Fastest way to get through: S 1,1, S 2,2 and S 1,3.

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

9
Optimal Sub structure the fastest way to get through S 1,j is based on the fastest ways to get through S 1,j-1 and S 2,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(f 1 (n)+x 1,f 2 (n)+x 2 ) f 1 (j) = e 1 +A 1,1 if j=1 = min(f 1 (j-1)+A 1,j, f 2 (j-1)+t 2,j-1 +A 1,j ) if j>1 f 2 (j) = e 2 +A 2,1 if j=1 = min(f 2 (j-1)+A 2,j, f 1 (j-1)+t 2,j-1 +A 2,j ) if j>1 Draw a call tree, how many times is f i (n-j) called?

11
Recursive definition The fastest way through the assembly lines f* = min(f 1 (n)+x 1,f 2 (n)+x 2 ) f 1 (j) = e 1 +A 1,1 if j=1 = min(f 1 (j-1)+A 1,j, f 2 (j-1)+t 2,j-1 +A 1,j ) if j>1 f 2 (j) = e 2 +A 2,1 if j=1 = min(f 2 (j-1)+A 2,j, f 1 (j-1)+t 2,j-1 +A 2,j ) if j>1 Draw a call tree, how many times is f i (n-j) called: 2 n-j times, so exponential complexity

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

13
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Time at end of S i,1?

14
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Best time at end of S i,2,source? 9 12

15
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Best time at end of S i,3 ? source? 9 12 18 16

16
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Best finish time?, source? 9 12 18 16 20 22

17
S 1,1 S 1,2 S 1,3 S 2,1 S 2,2 S 2,3 793 856 startfinish 4 2 3 3 2 2 2 1 Best Finish time?, source? 9 12 18 16 20 22 23

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 } 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 } 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 } 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: l i [j] is the line number whose station j-1 is used in the fastest way through S i,j. i = lstar print "line" i ", station" n for j = n downto 2{ i = l i [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 v does not need to consist of the longest simple paths from u to w and from w to v.

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

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

29
Q T R 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= is a subsequence of sequence Y= if there is a strictly increasing sequence of indices I= such that: for all j=1,2,..,m we have X[j]=Y[i j ] Eg, X=, Y=, I=. How many sub sequences does Y have?

31
Subsequence A sequence X= is a subsequence of sequence Y= if there is a strictly increasing sequence of indices I= such that: for all j=1,2,..,m we have X[j]=Y[i j ] Eg, X=, Y=, I=. 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 X i is the length i prefix of X. Cormen et.al. prove that for two sequences X= and Y= and LCS Z= of X and Y: 1. if x m =y n then z k =x m and Z k-1 is an LCS of X m-1 and Y n-1 2. if x m y n then z k x m Z is an LCS of X m-1 and Y 3. if x m y n then z k y n Z is an LCS of Y n-1 and X

34
Recursive algorithm for LCS 1. if x m =y n then z k =x m and Z k-1 is an LCS of X m-1 and Y n-1 2. if x m y n then z k x m Z is an LCS of X m-1 and Y 3. if x m y n then z k y n Z is an LCS of Y n-1 and X gives rise to a recursive formulation: LCS(X m,Y n ): if x m =y n find the LCS of X m-1 and Y n-1 and append x m to it, else find the LCSs of X m-1 and Y and of Y n-1 and X and pick the largest. Complexity? (call tree)

35
Length of LCS Length C[i,j] of of X i and Y j : C[i,j] = 0 if i=0 or j=0, C[i-1,j-1]+1 if i,j>0 and x i =y j max(C[i,j-1],C[i-1,j] if i,j>0 and x i y j 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 X i and Y j : C[i,j] = 0 if i=0 or j=0, C[i-1,j-1]+1 if i,j>0 and x i =y j max(C[i,j-1],C[i-1,j] if i,j>0 and x i y j 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=bab y=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.

Similar presentations

OK

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on data collection methods for qualitative research Ppt online form 2015 last date Ppt on different types of computer softwaresoftware Ppt on effect of global warming on weather alaska Ppt on pricing policy in business Ppt on cross-sectional study design Ppt on home security system using laser Ppt on limitation act malaysia Ppt on area related to circles Ppt on shell scripting basics