Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.

Slides:



Advertisements
Similar presentations
Algorithm Design approaches Dr. Jey Veerasamy. Petrol cost minimization problem You need to go from S to T by car, spending the minimum for petrol. 2.
Advertisements

§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
Analysis of Algorithms
Overview What is Dynamic Programming? A Sequence of 4 Steps
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),
Introduction to Algorithms
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Dynamic Programming CIS 606 Spring 2010.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
November 7, 2005Copyright © by Erik D. Demaine and Charles E. Leiserson Dynamic programming Design technique, like divide-and-conquer. Example:
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Analysis of Algorithms
Lecture 7 Topics Dynamic Programming
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Dynamic Programming From An Excel Perspective. Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dynamic Programming UNC Chapel Hill Z. Guo.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
DP (not Daniel Park's dance party). Dynamic programming Can speed up many problems. Basically, it's like magic. :D Overlapping subproblems o Number of.
1 Programming for Engineers in Python Autumn Lecture 12: Dynamic Programming.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Dynamic Programming David Kauchak cs302 Spring 2013.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
Introduction to Algorithms Jiafen Liu Sept
CS 3343: Analysis of Algorithms Lecture 18: More Examples on Dynamic Programming.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming David Kauchak cs161 Summer 2009.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Recursion Continued Divide and Conquer Dynamic Programming.
Dynamic Programming … Continued
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Fundamental Structures of Computer Science
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to the Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Dynamic Programming.
Prepared by Chen & Po-Chuan 2016/03/29
Data Structures and Algorithms
Data Structures and Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming.
Module 1-10: Recursion.
Trevor Brown DC 2338, Office hour M3-4pm
Introduction to Algorithms: Dynamic Programming
Dynamic Programming.
DYNAMIC PROGRAMMING.
Lecture 4 Dynamic Programming
Longest Common Subsequence
Advanced Programming Techniques
This is not an advertisement for the profession
Longest Common Subsequence
Presentation transcript:

Dynamic Programming (DP) By Denon

Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum 0-1 Knapsack Problem In-class Exercises Resources

What is DP? Dynamic Programming Is NOT a specified algorithm Is a Design technique (like Divide-and- Conquer)

What is DP? To give you the feeling of DP, we will solve some problems first. Then, we will extract the common methods/approaches used to solve the problems. And come back to the exact definition of DP later.

Fibonacci Numbers Fibonacci Numbers f 0 =0, f 1 =1, f n = f n-1 + f n-2 How to find the n-th Fibonacci Numbers?

Fibonacci Numbers Fibonacci Numbers f 0 =0, f 1 =1, f n = f n-1 + f n-2 Sloooooooooooooooow way: int fib(int n){ if (n == 0){ return 0; } if( n == 1){ return 1; } return fib(n-1) + fib(n-2); }

Fibonacci Numbers Very Sloooooooooooooooow Note how many time F2 is called, imagine you need to find F100

Fibonacci Numbers Fibonacci Numbers f 0 =0, f 1 =1, f n = f n-1 + f n-2 Improve by memorize the intermediate results (Memorization) Sloooow Way: int table[100000]; memset(table, 0xff, sizeof(table)); //negative number indicates undefined table[0] = 0; table[1] = 1; int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; }

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f9 01?

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f9 01??

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f9 01???

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f9 01????????

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f9 011???????

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f9 0112??????

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f ?????

Fibonacci Numbers How is the table filled? int table[100000]; … int fib(int n){ if(table[n] < 0){ table[n] = fib(n-1) + fib(n-2); } return table[n]; } f0f1f2f3f4f5f6f7f8f

Fibonacci Numbers Fibonacci Numbers f 0 =0, f 1 =1, f n = f n-1 + f n-2 Further improve by observing how the table is filled from f0, f1, f2 … to … fn Slow way int table[100000]; table[0] = 0; table[1] = 1; for (int i = 2; i < ; i++){ table[i] = table[i-1] + table[i-2]; } int fib(int n){ return table[n]; }

Fibonacci Numbers What is the difference between filling it recursively (top-down) or iteratively (bottom- up)?

Fibonacci Numbers What is the difference filling it recursively (top-down) or iteratively (bottom-up)? Bottom-upTop-down Order of evaluation ImportantNot important Function callsNopeYup Space requirement LowerHigher (Why??) RedundantPossibleNope

Fibonacci Numbers Why the recursive one consumes more space then iterative one?

Fibonacci Numbers Note that when f2, f3, …, f8 is being called, f9 is not yet returned. Thus, it requires memory space to store the local variable, returning address of the f2, f3, …, f8 (in a place called “Call Stack”) f0f1f2f3f4f5f6f7f8f9 01????????

Fibonacci Numbers In some cases, the stack space may be insufficient to fill the table recursively (i.e. Explode Stack ‘ 爆 Stack’)

Fibonacci Numbers In some cases, the stack space may be insufficient to fill the table recursively (i.e. Explode Stack ‘ 爆 Stack’) But in the contest problems, filling the table recursively is USUALLY okay.

Fibonacci Numbers What have we done to find the Fib. No.? 1.Solve it by recursion 2.Find out that it is tooooooooo slow 3.Do some memorizations 4.Figure out the table 5.Find out the order of filling the table 6.Find the solution by filling the table instead of doing recursion

Longest Common Subsequence (LCS) What is longest common subsequence? Given two strings S1 and S2, find their longest common subsequence (NOT substring) Example – S1: aabcaabc – S2: caadbgaccpoo a b c a is a sub-sequence of S1: aabcaabc a c b c is a sub-sequence of S1: aabcaabc

Longest Common Subsequence (LCS) Example – S1: aabcaabc – S2: caadbgaccpoo Note that, a a a is a sub-sequence of S1: aabcaabc a a a is also a sub-sequence of S2: caadbgaccpoo So, aaa is a common subsequence of S1, S2

Longest Common Subsequence (LCS) So a longest common subsequence is one of the longest common subsequences among all the common subsequences of S1, S2 In our example, – S1: aabcaabc – S2: caadbgaccpoo aabac is one of the lcs of S1, S2

Longest Common Subsequence (LCS) So, how to find the length of the longest common subsequence of S1 and S2? Of course, you can generate all subsequences of S1, S2. Then, compare them one by one. But how long would it takes? (i.e. How many different subsequence exists for a string of length n)

Longest Common Subsequence (LCS) In order to use DP, we first need to able to solve it by recursion. (Divide-and-Conqeur) Here we focus on the last character.

Longest Common Subsequence (LCS) Here we focus on the last character. Case I: Same Character Case II: Different Characters

Longest Common Subsequence (LCS) Case II: Different Characters Let the length of LCS of S1 (of length n1) and S2 (of length n2) be llcs(n1, n2) Claim: llcs(n1, n2) = max( llcs(n1-1, n2), llcs(n1,n2 -1) )

Longest Common Subsequence (LCS) Case 2a) A is in the LCS Case 2b) B is in the LCS Case 2c) A, B are both not in the LCS llcs(n1, n2) = max( llcs(n1-1, n2), llcs(n1,n2 -1) )

Longest Common Subsequence (LCS) Case I: Same Character Let the length of LCS of S1 (of length n1) and S2 (of length n2) be llcs(n1, n2) Claim: llcs(n1, n2) = llcs(n1-1, n2-1) + 1

Longest Common Subsequence (LCS) Case 1a) Both A is in the LCS Case 1b) One of the A is in the LCS Case 1c) Both A is not in the LCS llcs(n1, n2) = llcs(n1-1, n2-1) + 1

Longest Common Subsequence (LCS) Case 1a) Both A is in the LCS  Make sense Case 1b) One of the A is in the LCS  The same Case 1c) Both A is not in the LCS  Contradiction llcs(n1, n2) = llcs(n1-1, n2-1) + 1

Longest Common Subsequence (LCS) string s1, s2; int llcs(n1, n2){ //base case if(n1 == 0 || n2 == 0) return 0; if(s1[n1-1] == s2[n2-1]) { return llcs(n1-1, n2-1) + 1; }else{ return max(llcs(n1-1, n2), llcs(n1, n2-1)); }

Longest Common Subsequence (LCS) Of course, this is too sloooooooooooow. Improve by memorization. string s1, s2; int table[N][N]; memset(table, 0xff, sizeof(table)); int llcs(n1, n2){ //base case if(n1 == 0 || n2 == 0) return 0; if(table[n1][n2] < 0){ if(s1[n1-1] == s2[n2-1]) { table[n1][n2] = llcs(n1-1, n2-1) + 1; }else{ table[n1][n2] = max(llcs(n1-1, n2), llcs(n1, n2-1)); } return table[n1][n2]; }

Longest Common Subsequence (LCS) How the table is filled? if(s1[n1-1] == s2[n2-1]) { return llcs(n1-1, n2-1) + 1; }else{ return max(llcs(n1-1, n2), llcs(n1, n2-1)); }

Longest Common Subsequence (LCS) If s1[2] == s2[3] if(s1[n1-1] == s2[n2-1]) { return llcs(n1-1, n2-1) + 1; }else{ return max(llcs(n1-1, n2), llcs(n1, n2-1)); } s2 \ s k 30K

Longest Common Subsequence (LCS) If s1[2] != s2[3] if(s1[n1-1] == s2[n2-1]) { return llcs(n1-1, n2-1) + 1; }else{ return max(llcs(n1-1, n2), llcs(n1, n2-1)); } s2 \ s h 30kmax(h, k) 40

Longest Common Subsequence (LCS) If s1[2] == s2[3] s2 \ s k 30K If s1[2] != s2[3] s2 \ s h 30kmax(h, k) 40 //s1 of length n1, s2 of length n2 for(int c1 = 0; c1 < n1; c1 ++){ for(int c2 = 0; c2 < n2; c2 ++){ if(c1 == 0 || c2 == ){ table[c1][c2] = 0; }else if(s1[c1-1] == s2[c2-1]){ table[c1][c2] = table[c1-1][c2-1] + 1; }else{ table[c1][c2] = max(table[c1-1][c2], table[c1][c2-1]); }

Longest Common Subsequence (LCS) What have we done to find the LCS? 1.Solve it by recursion 2.Find out that it is tooooooooo slow 3.Do some memorizations 4.Figure out the table 5.Find out the order of filling the table 6.Find the solution by filling the table instead of doing recursion

What is DP? (2) It is all about filling the “right” table. In fact, the word “programming” here is an old word that means any tabular method for accomplishing something – Dynamic Programming, Linear Programming …

More formal view on DP Recall that, when we find fib(10), we make use the result of fib(9) and fib(8). And when we find the lcs(12, 11), we make use the result of lcs(11, 10), lcs(12, 10) and lcs(11, 11).

More formal view on DP To be more specific, S1 = “a b a f s” S2 = “a a b s t” The LCS of S1, S2 contains the LCS of the sub- string of S1, “a b a” and the sub-string S2, “a a b”.

More formal view on DP In other words, an optimal solution to a problem instance contains (one of) the optimal solutions to its sub-problems. This is called Optimal substructure

More formal view on DP Also recall that, without memorization, when we find fib(10), we evaluate the value of fib(1), fib(2), fib(3) … fib(9) over and over again. And when we find the lcs(12, 11), we evaluate the value of lcs(11, 10), lcs(12, 10), lcs(11, 11)… over and over again.

More formal view on DP In other words, The recursive solution contains a “small” number of distinct sub-problems repeated many times. This is called Overlapping subproblems.

When to use DP? So, when your problem exhibits these 2 properties, i.e. Optimal substructure Overlapping subproblems You may consider solving it using DP.

Break Let’s have 10 mins break Question: What if I want to print the LCS instead of just its length?

Subset Sum Given a set S of n numbers ai sum up to M, and any K ≤ M, what are the possible sums of its subset? i.e. S = { 1, 3, 1, 4} Some possible Subset Sums are 0, (1), (3), (4), (1 + 1 = 2), (1 + 4 = 5), (3 + 4 = 7), ( = 8), ( = 9).

Subset Sum First, how to solve it recursively?

Subset Sum First, how to solve it recursively? Hints: Let S = { 1, 3, 1, 4} If I know that {1, 3, 1} can form {0, 1, 3, 4, 5}, i.e. bool[ length = 3 ][] = {T, T, F, T, T, T, F,…} What can {1, 3, 1, 4} form? i.e. bool[ length = 4][] = {????}

Subset Sum First, how to solve it recursively? Let S = { 1, 3, 1, 4} {}TFFFFFFFF {1} {1,3} {1,3,1 } {1,3,1,4}

Subset Sum First, how to solve it recursively? Let S = { 1, 3, 1, 4} {}TFFFFFFFF {1}TTFFFFFFF {1,3} {1,3,1 } {1,3,1,4}

Subset Sum First, how to solve it recursively? Let S = { 1, 3, 1, 4} {}TFFFFFFFF {1}TTFFFFFFF {1,3}TTFTTFFFF {1,3,1 } {1,3,1,4}

Subset Sum First, how to solve it recursively? Let S = { 1, 3, 1, 4} {}TFFFFFFFF {1}TTFFFFFFF {1,3}TTFTTFFFF {1,3,1 } TTTTTTFFF {1,3,1,4}

Subset Sum First, how to solve it recursively? Let S = { 1, 3, 1, 4} {}TFFFFFFFF {1}TTFFFFFFF {1,3}TTFTTFFFF {1,3,1 } TTTTTTFFF {1,3,1,4} TTTTTTTTT

Subset Sum bool table[M+10]; Memset(table, 0, sizeof(table); m[0]=true; for(i=0; i<n; i++) for(j=M; j>=s[i]; j--)// Must be decreasing here m[j] |= m[j-a[i]];

0-1 Knapsack There are n items in a shop. The i-th item has weight w i and value v i. A thief has a knapsack which can carry at most a weight of W. What should she steal to maximize the total value of the stolen items? Assumption: all numbers in this problem are positive integers

0-1 Knapsack There are n items in a shop. The i-th item has weight w i and value v i. A thief has a knapsack which can carry at most a weight of TW. What should she steal to maximize the total value of the stolen items? W = { 10, 5, 2, 7} V = { 4, 2, 1 8} TW = 17

0-1 Knapsack Hints: – Take it or leave it.

0-1 Knapsack 0 if i = 0 or w = 0 c[i,w] = c[i-1, w] if w i ≥ 0 max [v i + c[i-1, w-w i ], c[i-1, w]} if i>0 and w ≥ w i

Practices In-class Exercises – 900 Brick Wall Patterns – The Twin Towers – 562 Dividing Coins – 357 Let Me Count The Ways (Hints: Top-Down filling is good enough)

Resources

Suggested Problems (from Louis Siu) Easy: 116, 147, 231, 847, 926, 988, 10036, 10081, 10192, 10198, 10304, 10337, 10359, 10400, 10404, 10453, 10532, 10617, 10651, 10702, 10759, 10891, 10912, 10970, Medium: 562, 607, 711, 714, 882, 10003, 10130, 10944, 10890, 10981, 11002, 11003, Hard: 757, 10020, 10564, 10663, 10859, 10911, 10934, 10940, 11045, 11081