190 likes | 326 Views
CPSC 335. Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada. Dynamic programming. Optimal structure: optimal solution to problem consists of optimal solutions to subproblems Overlapping subproblems : few subproblems in total,
E N D
CPSC 335 Dynamic Programming Dr. Marina Gavrilova Computer Science University of Calgary Canada
Dynamic programming • Optimal structure: optimal solution to problem • consists of optimal solutions to subproblems • Overlapping subproblems: few subproblems in total, • many recurring instances of each • Solve in “near linear” time through recursion, using a table to show state of problem at any given moment of time • Reduces computation time from exponential to linear in some cases
Dynamic programming • Longest Common Subsequence • Problem: Given 2 sequences, A = 〈a1,...,an〉 and B = 〈b1,...,bm〉, find a common subsequence of characters not necessarily adjacent whose length is maximum. • Subsequence must be in order.
Dynamic programming • Straight-forward solution finds all permutations of substrings in string A in exponential time 2^n, then checks for every beginning position in string B – another m times, O(m2^n)
Dynamic programming • Application: comparison of two DNA strings • Ex: X= {A B C B D A B }, Y= {B D C A B A} • Longest Common Subsequence: • X = A B C BD A B • Y = B D C A B A
Dynamic programming • Straight-forward solution finds all permutations of substrings in string A in exponential time 2^n, then checks for every beginning position in string B – another m times, O(m2^n)
Dynamic programming • Similarly to Linear programming, Dynamic programming defines an algorithmic technique to solve the class of problems through recursion on small subproblems and noticing patterns.
Dynamic programming We determine the length of Longest Common Substring and at the same time record the substring as well. Define Ai, Bj to be the prefixes of A and B of length i and j respectively Define L[i,j] to be the length of LCS of Ai and Aj Then the length of LCS of A and B will be L[n,m] We start with i = j = 0 (empty substrings of A and B) LCS of empty string and any other string is empty, so for every i and j: c[0, j] = L[i,0] = 0 First case: A[i]=B[j]: one more symbol in strings A and B matches, so the length of LCS Ai and Aj equals to the length of LCS L [AXi-1, Bi-1]+1 Second case: As symbols don’t match, solution is not improved, and the length of L(Ai , Bj) is the same as before (i.e. maximum of L(Ai, Bj-1) and L(Ai-1,Bj))
LCS algorithm calculates the values of each entry of the array ARRAY[n,m] in O(m*n) since each c[i,j] is calculated in constant time, and there are m*n elements in the array
Knapsack problem Given some items, pack the knapsack to get the maximum total value. Each item has some size and some value. Total size of the knapsack is is no more than some constant C. We must consider sizes of items as well as their value. Item# Size Value 1 2 5 2 3 7 3 5 6
Knapsack problem Given a knapsack with maximum capacity C, and a set Uconsisting of n items {U1,U2,Un} Each item j has some size sjand value vj Problem: How to pack the knapsack to achieve maximum total value of packed items?
Knapsack problem There are two versions of the problem: (1) “0-1 knapsack problem” and (2) “Fractional knapsack problem” (1) Items are single; you either take an item or not. Solved with dynamic programming (2) Items can be taken a number of times. Solved with a greedy algorithm
Knapsack problem • Let’s first solve this problem with a straightforward algorithm • Since there are n items, there are 2n possible combinations of items. We go through all combinations and find the one with the most total value and with total size less or equal to C • Running time will be O(2n)
Knapsack problem If items are labeled 1..n, then a subproblem would be to find an optimal solution for Ui = {items labeled 1, 2, .. i} However, the solution for U4 might not be part of the solution for U5, so this definition of a subproblem is flawed. Adding another parameter: j, which represents the exact size for each subset of items U, is the solution, woth the subproblem to be computed as V[i,j]
Knapsack problem Now, the best subset of Ujthat has total size Jis one of the two: 1) the best subset of Ui-1 that has total size s, or 2) the best subset of Ui-1 that has total size (C-si)plus the item i
Knapsack problem • Intuitively, the best subset of Uithat has the total size seither contains item i or not: First case: si>C. Item i can’t be part of the solution, since if it was, the total size would be more than C, which is unacceptable Second case: si <= C. Then the item ican be in the solution, and we choose the case with greater value benefit (whether it includes item i or not)
Conclusions • Dynamic programming is a useful technique of solving certain kinds of optimization problems • When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and re-use them to save time • Running time (Dynamic Programming) is much better than straight-forward approach