Module 5: Dynamic Programming

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Memoization general approach

1) Calculate and store solutions to subproblems. 2) Before solving it (again), look to see if you've remembered it Runs in Big theta n time

Memoization storage types

1) Use a table abstract data type. Lookup key: whatever identifies a subproblem. Value stored: the solution. 2) Could be an array/vector or 2D table(s). Eg for fibonacci, store fib(n) using index n. Need to initialize the array. 3) Could use a map/hash-table

Sequences bottom up implementation time and space complexity

Big theta of n * m

Solution to determine the scheduling intervals:

Computing a sub-array each step would make it quadratic running time To determine the intervals: if vj + M[p(j)] >= M[j - 1]: then j is part of the solution, and consider p(j) else: then j is NOT part of the solution, and consider j - 1

The objective function

The score for each feasible solution on some criteria. Example: the sum of the edge weights in path

How to solve the i, j problem (I is which coins, and j is the amount)

- If denom[I] > j, then not possible to include this coin. The solution is the same as the (I + 1), j problem. In the table, that's the cell right below the current cell. - Maybe the best answer does use a coin denomination i. Then the solution is 1 more than the i, (j - denom[I]) problem. j changes to j - denom[I] because we subtract off the value of the coin used. i doesn't change because there could be multiple coins of denomination i used in the solution. - Maybe the best answer does NOT use a coin of denomination I. Then the solution is the same as the (i + 1), j problem. In the table, that's the cell right below the current cell.

Knapsack recursive structure. What's a recursive definition of how a solution of size n is built from optimal results for smaller sub-problems? S = {s_1, s_2, s_3, ..., s_(n-1), s_n}

- Let's say s_n not in O_n (last item is not in optimal solution for S_n). Last item didn't add anything to best solution for smaller subproblem. We need optimal solution O_n-1 for the following smaller subproblem S_n-1: n-1 items using same knapsack capacity C - Let's say s_n in O (last item is in optimal solution for S_n). Last item contributed w_i to total weight we're carrying. We need optimal solution O_n-1 for the following smaller subproblem S_n-1: n-1 items using reduced capacity C - w_n. Note: "getting smaller" decreases number of items and also maybe capacity.

What is dynamic programming?

- Similar to greedy algorithms. - solves problems that have optimal substructure, but do not have a known greedy choice for optimal solutions. - Instead try every option for the first "greedy" choice and see which one leads to optimal solution. (with optimizations)

Process for DP

1) Recognize what the sub-problems are 2) Identify the recursive structure of the problem in terms of its sub-problems. At the top level, what is the last thing done? This helps you see a recursive solution for any generic sub-problem in terms of smaller sub-problems 3) Formulate a data-structure (array, table) that can look up solution to any sub-problem in constant time (usually an array). 4) Develop an algorithm that loops through data structure solving each sub-problem one at a time. Bottom-up: from smallest sub-problems, to next largest, ..., to complete problem. Top-down: implement recursively and store solutions as you go.

What are the three cases to calculate Knap(k, w)?

1) There is sufficient capacity to add item s_k to the knapsack, and that creates an optimal solution for k items. 2) There is sufficient capacity to add item s_k to the knapsack, and that does not create an optimal solution for the k items. 3) There is insufficient capacity to add item s_k to the knapsack

We can make a coin change solution using which two approaches?

1) top down solution (recursive calls) 2) bottom-up approach (build a table)

Coin change how to get the coins chosen

1) trace back through values 2) keep a used boolean array. If used[I][j] is true, then the solution for I,j does use a coin of denom[I] for amount j. If false, it does not.

Making Change recurrence: Var/Func: A, Denom[]

C(I, a) = Min(C(i + 1, a), 1 + C(i, a - Denom[i]))

Weighted Interval Scheduling recurrence: Var/Func: V[], P()

C(i) = Max(C(i - 1), V[I] + C(P(i)))

Discrete Knapsack recurrence: Var/Func: C, W[], V[]

C(i, w) = Max(C(i - 1, w), V[i] + C(i - 1,w - W[i]))

Rod Cutting recurrence: Var/Func: P[]

C(n) = Max_i=1...n{C(n - i) + P[i]}

When to use DP instead of greedy.

DP is good when sub-problems overlap, when they're not independent. No need to repeat the calculation to solve them. DP has stored them, so doesn't repeat the calculation.

The optimal solution

One (or more) feasible solutions that scores highest (by the objective function) is the optimal solution.

Sequences subproblem

Opt(I, j) = min cost aligning prefix strings Xi = x1 ... xi and Yj = y1 ... yj. Our recurrence for Opt(I, j) will have three cases: 1) (xi, yi) in M. The cost will be: mismatch cost for (xm, yn), plus min-cost to align x1 ... x_i-1 and y1 ... y_j-1 2) xi is not matched. The cost will be: gap cost for xi, plus min-cost to align x1 ... x_i-1 and y1 ... yj. 3) yj is not matched. The cost will be: gap cost for yj, plus min-cost to align x1 ... xi and y1 ... y_j-1 We can prove optimal substructure property using an exchange argument.

Knapsack how to determine items

To find which items were chosen, we can trace backward through the table, starting at V[n, C]. - If V[k, w] = V[k - 1, w], then s_k is not an item in the knapsack (this was from cases 2 and 3). Look at V[k - 1, w] next. - Otherwise, s_k is an item in the knapsack, and we look at V[k - 1, w - w_k] next (this was from case 1).

Scheduling subproblem

base case: Opt(0) = 0 Opt(j) = Max(Opt(j - 1), Vj + Opt(p(j)))

dynamic programming scheduling time

without memoization: exponential with memoization: linear


Set pelajaran terkait

Nutrition and Addiction EAQ questions

View Set

Business Law Ch.6: Tort Law & Cybertorts

View Set

SEC+ 501 - CHAPTER ONE REVIEW QUESTION

View Set

CHAPTER 1: MENTAL HEALTH AND MENTAL ILLNESSES

View Set

PNU 120 Taylor PrepU Chapter 40: Fluid, Electrolyte. and Acid-Base Balance

View Set

MKT 300 Concept Check CH.4 (Hapke)

View Set

Pharmacology practice assessment b

View Set

Foundations and Practice of Mental Health Nursing HESI EXIT 5

View Set