110 likes | 118 Views
This lecture introduces the basic algorithm design techniques including divide and conquer, dynamic programming, and greedy. It explains the common theme of solving large, complicated problems by breaking them into smaller sub-problems. It also provides examples of dynamic programming algorithms for Fibonacci numbers, longest increasing subsequence, and knapsack problem.
E N D
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 relatedsub-problems, memorize the result of the sub-problems 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(3) f(4) f(5) f(4) f(3) f(4) … …
Naïve solution • Follow the recursion directly f(7) f(6) f(5) f(4) f(5) f(3) f(4) … … f(3) f(4)
Memoization f(n) IF n < 3 THENRETURN 1. IF f(n) has been computed before THEN RETURN stored result. res = f(n-1) + f(n-2) Mark f(n) as computed, Store f(n) = res RETURN res 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) IF n < 3 THENRETURN 1. a[1] = a[2] = 1 FORi = 3 TO n a[i] = a[i-1] + a[i-2] RETURN a[n]
Basic Steps in Designing Dynamic Programming Algorithms • Relate the problem recursively to smaller sub-problems. (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 numbersa[] = {4, 2, 5, 3, 9, 7, 8, 10, 6} • Subsequence: list of numbers appearing in the same order, but may not be consecutiveExample: {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 w1,w2,…, wn. Each item also has a value v1,v2,…,vn. • Goal: Select some items to put into knapsack1. Total weight is at most W.2. Total value is as large as possible.