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.

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

Dynamic Programming (DP)
Overview What is Dynamic Programming? A Sequence of 4 Steps
Problem Solving Dr. Andrew Wallace PhD BEng(hons) EurIng
Dynamic Programming.
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
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Lecture 6: Dynamic Programming 0/1 Knapsack Making Change (any coin set)
1 Dynamic Programming Jose Rolim University of Geneva.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 6 Instructor: Paul Beame TA: Gidon Shavit.
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
Analysis of Algorithms
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
First Ingredient of Dynamic Programming
Dynamic Programming From An Excel Perspective. Dynamic Programming From An Excel Perspective Ranette Halverson, Richard Simpson, Catherine Stringfellow.
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.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized.
1 Summary: Design Methods for Algorithms Andreas Klappenecker.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 9. Intermezzo.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
INTRODUCTION. What is an algorithm? What is a Problem?
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.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
Dynamic Programming David Kauchak cs302 Spring 2013.
1 Chapter 15-1 : Dynamic Programming I. 2 Divide-and-conquer strategy allows us to solve a big problem by handling only smaller sub-problems Some problems.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
12-CRS-0106 REVISED 8 FEB 2013 CSG523/ Desain dan Analisis Algoritma Dynamic Programming Intelligence, Computing, Multimedia (ICM)
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Dynamic Programming David Kauchak cs161 Summer 2009.
Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.
1 Today’s Material Dynamic Programming – Chapter 15 –Introduction to Dynamic Programming –0-1 Knapsack Problem –Longest Common Subsequence –Chain Matrix.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
Lecture 12.
All-pairs Shortest paths Transitive Closure
Dynamic Programming Sequence of decisions. Problem state.
Rod cutting Decide where to cut steel rods:
Fundamental Structures of Computer Science
Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to the Design and Analysis of Algorithms
0/1 Knapsack Making Change (any coin set)
CS200: Algorithm Analysis
CS 3343: Analysis of Algorithms
Dynamic Programming.
Data Structures and Algorithms
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
Dynamic Programming.
Chapter 15-1 : Dynamic Programming I
Analysis of Algorithms CS 477/677
Dynamic Programming.
DYNAMIC PROGRAMMING.
Lecture 4 Dynamic Programming
Presentation transcript:

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 by breaking it into smaller parts) However, some might show special behavior – Optimal sub-structures – Overlapping sub-problems

Dynamic Programming Optimal sub structure – “the best” of sub-solutions constitute “the best” solution E.g., MCS, Closest Pair Overlapping sub-problem – Some instances of sub-problem occur several times

Optimal sub structure The solution to the sub-problems directly constitute the solution of the original problem – Finding the best solutions for sub-problems helps solving the original problem

Overlapping Sub-problem When a sub-problem of some higher level problem is the same instance as a sub- problem of other higher level

Example Fibonacci Problem: compute F(N), the Fibonacci function of N Def: F(N) = F(N-1) + F(N-2) F(1) = 1 F(2) = 1

Recursion Tree F(5)F(4) F(3)F(2) F(1) F(3)F(2) F(1)F(2)

Example F(1) = 1 F(2) = 1 F(3) = 2 F(4) = 3 F(5) = 5 F(6) = 8 F(7) = 13 F(8) = 21 F(9) = 34 F(10) = 55

Example F(1) = 1 F(2) = 1 F(3) = F(2) + F(1) F(4) = F(3) + F(2) F(5) = F(4) + F(3) F(6) = F(5) + F(4) F(7) = F(6) + F(5) F(8) = F(7) + F(6) F(9) = F(8) + F(7) F(10) = F(9) + F(8)

Example F(1) = 1 F(2) = 1 F(3) = F(2) + F(1) F(4) = F(3) + F(2) F(5) = F(4) + F(3) F(6) = F(5) + F(4) F(7) = F(6) + F(5) F(8) = F(7) + F(6) F(9) = F(8) + F(7) F(10) = F(9) + F(8)

Key Idea If there are “overlapping” sub-problem, – Why should we do it more than once? Each sub-problem should be solved only once!!!

Dynamic Programming Method Top-down approach – Memoization – Remember what have been done, if the sub- problem is encountered again, use the processed result Bottom-up approach – Use some kind of “table” to build up the result from the sub-problem

Fibonacci Example: recursive int fibo(int n) { if (n > 2) { return fibo(n-1) + fibo(n-2); } else return 1; }

Fibonacci Example: Memoization int fibo_memo(int n) { if (n > 2) { if (stored[n] == 0) { int value = fibo_memo(n-1) + fibo_memo(n-2); stored[n] = value; } return stored[n]; } else return 1; } Stored is an array of size n, initialized as 0

Memoization Remember the solution for the required sub-problem – it’s caching Need a data structure to store the result – Must know how to identify each sub-problem

Memoization : Defining Subproblem

Code Example : D&C ResultType DandC(Problem p) { if (p is trivial) { solve p directly return the result } else { divide p into p 1,p 2,...,p n for (i = 1 to n) r i = DandC(p i ) combine r 1,r 2,...,r n into r return r }

Code Example : Memoization

Memoization : Data Structure Usually, we use an array or multi-dimension array For example, the Fibonacci

Fibonacci Example: Bottom up From the recurrent, we know that – F(n) needs to know F(n-1) and F(n-2) – i.e., if we know F(n-1) and F(n-2) Then we know F(N) Bottom Up  Consider the recurrent and fill the array from the initial condition to the point we need

Fibonacci Example: Bottom up Initial Condition: – F(1) = 1, F(2) = 2 – i.e., stored[1] = 1; stored[2] = 1; From the recurrent – stored[3] = stored[2] + stored[1] – stored[4] = stored[3] + stored[2] – …

Fibonacci Example: Bottom up Step 1 Step 2 Step 3 Step 4

Fibonacci Example: Bottom up int fibo_buttom_up(int n) { value[1] = 1; value[2] = 1; for (int i = 3;i <= n;++i) { value[i] = value[i-1] + value[i-2]; } return value[n]; }

Approach Preference Bottom up is usually better – But it is harder to figure out Memoization is easy – Directly from the recursive

Binomial Coefficient C n,r =how to choose r things from n things – We have a closed form solution C n,r = n!/ ( r!*(n-r)! ) C n,r = C n-1,r + C n-1,r-1 = 1 ; r = 0 = 1 ; r = n – What is the subproblem? – Do we have overlapping subproblem?

Binomial Coefficient: sub-problem Described by two values (n,r) Data structure should be 2D array

Binomial Coefficient : Code Can you write the recursive version of the binomial coefficient? Can you change it into the memoization version?

Binomial Coefficient : Code int bino_naive(int n,int r) { if (r == n) return 1; if (r == 0) return 1; int result = bino_naive(n-1,r) + bino_naive(n-1,r-1); return result; }

Binomial Coefficient : Memoization int bino_memoize(int n,int r) { if (r == n) return 1; if (r == 0) return 1; if (storage[n][r] != -1) return storage[n][r]; int result = bino_memoize(n-1,r) + bino_memoize(n-1,r-1); storage[n][r] = result; return result; }

Binomial Coefficient: bottom up Pascal Triangle C 4,2 C 4,1 C 5,2