/
Lecture 4 Dynamic Programming Lecture 4 Dynamic Programming

Lecture 4 Dynamic Programming - PowerPoint Presentation

jones
jones . @jones
Follow
66 views
Uploaded On 2023-06-21

Lecture 4 Dynamic Programming - PPT Presentation

Basic Algorithm Design Techniques Divide and conquer Dynamic Programming Greedy Common Theme To solve a large complicated problem break it into many smaller subproblems Dynamic Programming ID: 1001363

problems table subsequence programming table problems programming subsequence dynamic return items increasing problem computed order total large directlyf recursion

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Lecture 4 Dynamic Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. Lecture 4 Dynamic Programming

2. Basic Algorithm Design TechniquesDivide and conquerDynamic ProgrammingGreedyCommon Theme: To solve a large, complicated problem, break it into many smaller sub-problems.

3. Dynamic ProgrammingIdea: Break the problem into many closely related sub-problems, memorize the result of the sub-problems to avoid repeated computation.

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

5. Naïve solutionFollow the recursion directlyf(7)f(6)f(5)f(5)f(4)f(4)f(3)f(4)f(3)… …

6. Naïve solutionFollow the recursion directlyf(7)f(6)f(5)f(5)f(4)f(4)f(3)f(4)f(3)… …

7. Memoizationf(n)IF n < 3 THEN RETURN 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) = resRETURN resn12345678f(n)1123581321Dynamic Programming Table

8. Filling in the table iterativelyObservation: f(n) is always computed in the order of n = 1, 2, 3, …No need to recursef(n)IF n < 3 THEN RETURN 1. a[1] = a[2] = 1FOR i = 3 TO n a[i] = a[i-1] + a[i-2]RETURN a[n]

9. Basic Steps in Designing Dynamic Programming AlgorithmsRelate 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 SubsequenceInput: 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 ProblemThere 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.