We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21
Dynamic Programming
Dynamic Programming
Assembly Line Scheduling
Dynamic Programming
1. Steps in Dynamic Programming
1. Characterize structure of an optimal solution. 2. Define value of optimal solution recursively. 3. Compute optimal solution values either top-down with caching or bottom-up in a table. 4. Construct an optimal solution from computed values. Dynamic Programming
Show that a solution to a problem consists of making a
choice, which leaves one or more subproblems to solve. Suppose that you are given this last choice that leads to an optimal solution. Given this choice, determine which subproblems arise and how to characterize the resulting space of subproblems. Show that the solutions to the subproblems used within the optimal solution must themselves be optimal. Usually use cut-and-paste. Need to ensure that a wide enough range of choices and subproblems are considered. Optimal substructure varies across problem domains: 1. How many subproblems are used in an optimal solution. 2. How many choices in determining which subproblem(s) to use.
Informally, running time depends on (# of subproblems overall) (# of
choices). How many subproblems and choices do the examples considered contain? Dynamic programming uses optimal substructure bottom up. First find optimal solutions to subproblems.
Then choose which to use in optimal solution to the problem.
Does optimal substructure apply to all optimization problems? No. Applies to determining the shortest path but NOT the longest simple path of an unweighted directed graph. Why? Shortest path has independent subproblems. Solution to one subproblem does not affect solution to another subproblem of the same problem. Subproblems are not independent in longest simple path. Solution to one subproblem affects the solutions to other subproblems. Example: Overlapping Subproblem
The space of subproblems must be “small”.
The total number of distinct subproblems is a polynomial in the input size. A recursive algorithm is exponential because it solves the same problems repeatedly. If divide-and-conquer is applicable, then each problem solved will be brand new. Dynamic Programming
Longest Common Subsequence
Document diffing is one of the most prominent uses
of LCS. It forms basis of data comparison programs such as the diff utility, and has applications in computational linguistics and bioinformatics. S1 = {B, C, D, A, A, C, D} S2 = {A, C, D, B, A, C} Then, common subsequences are {B, C}, {C, D, A, C}, {D, A, C}, {A, A, C}, {A, C}, {C, D}, ... Among these subsequences, {C, D, A, C} is the longest common subsequence. A brute force approach :-enumerate all subsequence of X and check each subsequence to see it is also subsequence of Y There are 2m subsequence of X This approach requires exponential time example
Let us take two sequences:
1.first sequence =“ACABD”, Second Sequence=“CBDA” “CD” of length 2 2.LCS for input Sequences “ABCDGH” and “AEDFHR” “ADH” of length 3. 3.LCS for input Sequences “AGGTAB” and “GXTXAYB” “GTAB” of length 4. Consider the strings "PQRSTPQRS" and "PRATPBRQRPS". What is the length of the longest common subsequence? A 9 B 8 C 7 D 6 Consider two strings A = “qpqrr” and B = “pqprqrp”. Let x be the length of the longest common subsequence (not necessarily contiguous) between A and B and let y be the number of such longest common subsequences between A and B. Then x + 10y = ___. (A) 33 (B) 23 (C) 43 (D) 34 The LCS is of length 4. There are 3 LCS of length 4 “qprr”, “pqrr” and qpqr A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m] and Y[n] of lengths m and n respectively, with indexes of X and Y starting from 0. We wish to find the length of the longest common sub-sequence(LCS) of X[m] and Y[n] as l(m,n), where an incomplete recursive definition for the function l(i,j) to compute the length of The LCS of X[m] and Y[n] is given below: l(i,j) = 0, if either i=0 or j=0 = expr1, if i,j > 0 and X[i-1] = Y[j-1] = expr2, if i,j > 0 and X[i-1] != Y[j-1] (A) expr1 ≡ l(i-1, j) + 1 (B) expr1 ≡ l(i, j-1) (C) expr2 ≡ max(l(i-1, j), l(i, j-1)) (D) expr2 ≡ max(l(i-1,j-1),l(i,j)) A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m] and Y[n] of lengths m and n, respectively with indexes of X and Y starting from 0. We wish to find the length of the longest common sub-sequence (LCS) of X[m] and Y[n] as l(m,n), where an incomplete recursive definition for the function I(i,j) to compute the length of the LCS of X[m] and Y[n] is given below: l(i,j) = 0, if either i = 0 or j = 0 = expr1, if i,j > 0 and X[i-1] = Y[j-1] = expr2, if i,j > 0 and X[i-1] ≠ Y[j-1] The value of l(i, j) could be obtained by dynamic programming based on the correct recursive definition of l(i, j) of the form given above, using an array L[M, N], where M = m+1 and N = n + 1, such that L[i, j] = l(i, j).Which one of the following statements would be TRUE regarding the dynamic programming solution for the recursive definition of l(i, j)? A) All elements of L should be initialized to 0 for the values of l(i, j) to be properly computed. B)The values of l(i,j) may be computed in a row major order or column major order of L[M, N]. C) The values of l(i, j) cannot be computed in either row major order or column major order of L[M, N]. D ) L[p, q] needs to be computed before L[r, s] if either p < r or q < s.