Lecture 4 Dynamic Programming Basic Algorithm Design Techniques
Lecture 4 Dynamic Programming
Basic Algorithm Design Techniques • Divide and conquer • Dynamic Programming • Greedy • Common Theme: To solve a large, complicated problem, break it into many smaller sub-problems.
Dynamic Programming • Idea: Break the problem into many closely related sub-problems, memorize the result of the subproblems to avoid repeated computation.
Warmup Example: Fibonacci Numbers f(n) = f(n-1) + f(n-2), f(1) = f(2) = 1
Naïve solution • Follow the recursion directly f(7) f(6) f(5) f(4) f(3) f(4) ……
Naïve solution • Follow the recursion directly f(7) f(6) f(5) f(4) f(3) f(4) …… f(3)
Memoization f(n) 1. IF n < 3 THEN RETURN 1. 2. IF f(n) has been computed before THEN RETURN stored result. 3. res = f(n-1) + f(n-2) 4. Mark f(n) as computed, Store f(n) = res 5. RETURN res n 1 2 3 4 5 6 7 8 f(n) 1 1 2 3 5 8 13 21 Dynamic Programming Table
Filling in the table iteratively • Observation: f(n) is always computed in the order of n = 1, 2, 3, … • No need to recurse f(n) 1. IF n < 3 THEN RETURN 1. 2. a[1] = a[2] = 1 3. FOR i = 3 TO n 4. a[i] = a[i-1] + a[i-2] 5. RETURN a[n]
Basic Steps in Designing Dynamic Programming Algorithms • Relate the problem recursively to smaller subproblems. (Transition function, f(n) = f(n-1)+f(n-2)) • Organize all sub-problems as a dynamic programming table. (A table for all values of n) • Fill in values in the table in an appropriate order. (n = 1, 2, 3, …)
Example 1: Longest Increasing Subsequence • Input: Array of numbers a[] = {4, 2, 5, 3, 9, 7, 8, 10, 6} • Subsequence: list of numbers appearing in the same order, but may not be consecutive Example: {4, 2, 5}, {4, 3, 8}, {2, 5, 7, 8, 10} • Subsequence b[] is increasing if b[i+1] > b[i] for all i. • Output: Length of the longest increasing subsequence. Example: 5, the subsequence is {2, 5, 7, 8, 10} or {2, 3, 7, 8, 10} (both have length 5)
Example 2 Knapsack Problem • There is a knapsack that can hold items of total weight at most W. There are now n items with weights w 1, w 2, …, wn. Each item also has a value v 1, v 2, …, vn. • Goal: Select some items to put into knapsack 1. Total weight is at most W. 2. Total value is as large as possible.
- Slides: 11