220 likes | 314 Views
Lecture 8: Dynamic Programming. Shang-Hua Teng. First Example: n choose k. Many combinatorial problems require the calculation of the binomial coefficient. This is equivalent to given n objects how many different ways can you choose k of them. Mathematically we have or,.
E N D
Lecture 8:Dynamic Programming Shang-Hua Teng
First Example: n choose k • Many combinatorial problems require the calculation of the binomial coefficient. • This is equivalent to given n objects how many different ways can you choose k of them. • Mathematically we have • or,
Consider Divide and Conquer int choose(n,k) if (n = = k) return 1; if (n < k) or (k < 0) return 0; return choose(n-1, k-1) + choose(n - k, k); How long does this take? T(n,k) = T(n-1, k-1) + T(n-k, k)
Remember Pascal’s Triangle function choose (n,k) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 By adding two numbers from previous row we can calculate the triangle quickly.
Consider triangle shape 0 1 2 3 4 5 • Looks like a two dimensional array. • Create the triangle in this array and output A(i,j) for 0 1 2 3 4 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Do it algorithmically Create an 2-d array A of size (n+1) by (k +1 ). Set A[i, 0] 1 for i = 0,1,2,.., n Set A[i, i ] 1 for i = 0,1,2,...., n for i = 1 to n for j = 1 to k A[ i, j ] = A[ i-1, j ] + A[ i –1, j-1 ]; Output A[n,k] Runtime is O(nk)
Matrix Chain Multiplication - Review • Recall how to multiply matrices. • Given two matrices • The product is • Time is e = d A B C X d f f e
Matrix Chaining contd. • What about multiplying multiple matrices? • We know matrix multiplication is associative. Can we use this to help minimize calculations? • Sure, consider • If we multiply we do 4000 operations • But instead if we try we do 1575 operations
How do we parenthesize • Brute force - try all ways to parenthesize • Find out which has smallest number of operations by doing multiplication, then pick the best one. • But how many ways can we parenthesize these? • Equivalent to the number of ways to make a binary tree with n nodes. This is see Catalan numbers. • See homework Problem 12-4, ex15.2-3 in the book.
Be greedy • We just learned that a greedy algorithm can sometimes work, let’s try. • Why not try starting with the product with the most operations. • Consider • If we do our greedy method we would compute • This is 2000 operations. • Try instead, which takes 1000 operations.
Another Greedy way • Select the product with the fewest operations. • Consider • Here the new greedy method would compute • This is 109989+9900+108900=228789 operations • Try • Here we only need 9999+89991+89100=189090
Optimality of Subproblems • Consider general question again, we want to parenthesize • Now suppose somehow we new that the last multiplication we need to do was at the ith position. • Then the problem is the same as knowing the best way to parenthesize and
Overlapping Subproblems • We know how to break problem into smaller pieces, why doesn’t divide and conquer work? • We need to optimize each piece why doesn’t greedy work. • Again suppose we know where the final multiply is and we want to generalize the cost with a recurrence, consider • But notice these subproblems overlap.
Dynamic Programming • Since subproblems overlap we can not use divide and conquer method. • Instead work from the “bottom up.” • We know how to parenthesize one matrix, two matrices, we can build from there…
What have we noticed • Optimal solution depends on optimal subproblems • Subproblems overlap. • So a “top down” divide and conquer doesn’t seem to work. • Somehow we need to build up from bottom by remembering solution from subproblem.
Another Example • Biologists need to measure how similar strands of DNA are to determine how closely related an organism is to another. • They do this by considering DNA as strings of letters A,C,G,T and then comparing similarities in the strings. • Formally they look at common subsequences in the strings. • Example X = AGTCAACGTT, Y=GTTCGACTGTG • Both S = AGTG and S’=GTCACGT are subsequences • How to do find these efficiently?
Brute Force • if |X| = m, |Y| = n, then there are 2m subsequences of x; we must compare each with Y (n comparisons) • So the running time of the brute-force algorithm is O(n 2m) • Notice that the LCS problem has optimal substructure: solutions of subproblems are parts of the final solution. • Subproblems: “find LCS of pairs of prefixes of X and Y”
Setup • First we’ll find the length of LCS, along the way we will leave “clues” on finding subsequence. • Define Xi, Yj to be the prefixes of X and Y of length i and j respectively. • Define c[i,j] to be the length of LCS of Xi and Yj • Then the length of LCS of X and Y will be c[m,n]
The recurrence • The subproblems overlap, to find LCS we need to find LCS of c[i, j-1] and of c[i-1, j]
LCS-Length(X, Y) m = length(X), n = length(Y) for i = 1 to m do c[i, 0] = 0 for j = 0 to n do c[0, j] = 0 for i = 1 to m do for j = 1 to n do if ( xi = = yj ) then c[i, j] = c[i - 1, j - 1] + 1 else if c[i - 1, j]>=c[i, j - 1] then c[i, j] = c[i - 1, j] else c[i, j] = c[i, j - 1] return c and b