1 / 11

Lecture 4 Dynamic Programming

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.

irwink
Download Presentation

Lecture 4 Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 Dynamic Programming

  2. 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.

  3. Dynamic Programming • Idea: Break the problem into many closely relatedsub-problems, memorize the result of the sub-problems to avoid repeated computation.

  4. Warmup Example: Fibonacci Numbers f(n) = f(n-1) + f(n-2), f(1) = f(2) = 1

  5. 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) … …

  6. 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)

  7. 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

  8. 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]

  9. 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, …)

  10. 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)

  11. 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.

More Related