Dynamic Programming.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J/18.401J/SMA5503
Advertisements

Dynamic Programming.
Overview What is Dynamic Programming? A Sequence of 4 Steps
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Dynamic Programming CIS 606 Spring 2010.
Longest Common Subsequence
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
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.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 17.
CS 8833 Algorithms Algorithms Dynamic Programming.
6/4/ ITCS 6114 Dynamic programming Longest Common Subsequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 14.
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.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 16 Dynamic.
TU/e Algorithms (2IL15) – Lecture 4 1 DYNAMIC PROGRAMMING II
Greedy algorithms: CSC317
Dynamic Programming Typically applied to optimization problems
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Sequence of decisions. Problem state.
Design & Analysis of Algorithm Dynamic Programming
David Meredith Dynamic programming David Meredith
Algorithmics - Lecture 11
Weighted Interval Scheduling
CS 3343: Analysis of Algorithms
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
CS200: Algorithm Analysis
Dynamic Programming Several problems Principle of dynamic programming
CS38 Introduction to Algorithms
Dynamic Programming Comp 122, Fall 2004.
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Prepared by Chen & Po-Chuan 2016/03/29
CS 3343: Analysis of Algorithms
Unit-5 Dynamic Programming
Dynamic Programming General Idea
Chapter 6 Dynamic Programming
CS Algorithms Dynamic programming 0-1 Knapsack problem 12/5/2018.
Chapter 6 Dynamic Programming
Dynamic Programming Dr. Yingwu Zhu Chapter 15.
CS 3343: Analysis of Algorithms
Merge Sort Dynamic Programming
Advanced Algorithms Analysis and Design
Dynamic Programming.
Longest Common Subsequence
Dynamic Programming Comp 122, Fall 2004.
Lecture 8. Paradigm #6 Dynamic Programming
Ch. 15: Dynamic Programming Ming-Te Chi
Trevor Brown DC 2338, Office hour M3-4pm
Dynamic Programming General Idea
Introduction to Algorithms: Dynamic Programming
Dynamic Programming.
DYNAMIC PROGRAMMING.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Longest Common Subsequence
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
Algorithms and Data Structures Lecture X
Analysis of Algorithms CS 477/677
Longest Common Subsequence
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

Dynamic Programming

Introduction Definition: A set of steps to accomplish a task to get the school from your home to find an item in a supermarket In CS, an algorithm is a set of instructions for a computer program to accomplish a task Google map uses a route finding alg to give you a route from your current location to a destination point.

Introduction Basic goals for an algorithm always correct always terminates has good performance performance often draws line between what is possible and what is impossible

Introduction How do we evaluate efficiency? using asymptotic analysis, we can evaluate the efficiency of an algorithm independent of the software and the hardware

Introduction Running time Two kinds of analysis for the running time depends on input (it’s easy to search an element in a sorted sequence) parameterized by the input size It’s desired an upper bound to guarantee the performance Two kinds of analysis for the running time Worst case analysis (usually), maximum time on any input of size n Average case analysis(sometimes), expected time over all inputs of size n

Dynamic Programming a method for solving classical problems (Like Divide & Conquer, it’s not a specific algorithm) ‘programming’ here not refering software. The word itself older than computer. ‘programming’ means any tabular method to accomplish a task. introduced by Richard Bellman in 1949. He developed the method with Lester Ford to find the shortest path in a graph.

Dynamic Programming from “Eye of the Hurricane : an Autobiography” by Richard Bellman

Dynamic Programming DP can be considered as brute force search all posibilities but do it in a smart way to get the optimal solution

Dynamic Programming DP can be considered as brute force search all posibilities but do it in a smart way to get the optimal solution For what type of problems DP is useful? the problems that can be broken into subproblems

Dynamic Programming DP can be considered as brute force search all posibilities but do it in a smart way to get the optimal solution For what type of problems DP is useful? the problems that can be broken into subproblems Divide & Conquer Dynamic Programming you deal with independent subproblems you deal with overlapping subproblems

Stairs Climbing n stairs . . .

Stairs Climbing You take one step or two steps at a time. n stairs . . .

Stairs Climbing You take one step or two steps at a time. How many possible ways to climb n stairs ? n stairs . . .

Stairs Climbing You take one step or two steps at a time. How many possible ways to climb n stairs ? 1 1 1 . . . 1 1 2 2 2 . . . 2 2 1 1 1 . . . 1 2 create all possibilities and count them . . . 2 1 2 . . . 1 2 1 2 1 . . . 2 1 n stairs . . .

Stairs Climbing T(n) = # of ways to climb n stairs T(n) . . .

Stairs Climbing T(n) = # of ways to climb n stairs T(n) = T(n-1) + T(n-2) T(n) T(n-1) T(n-2) . . .

Stairs Climbing T(n) = # of ways to climb n stairs T(n) = T(n-1) + T(n-2) T(n) T(n-1) finding # of different ways solving this recurrence relation = T(n-2) . . .

Stairs Climbing T(n) = # of ways to climb n stairs T(n) = T(n-1) + T(n-2) T(n) T(n-1) finding # of different ways solving this recurrence relation = T(n-2) . . . Fibonacci number T(n) = F(n) = ϕn

Stairs Climbing SC (n) if n ≤ 1 return 1 else return SC (n-1) + SC (n-2)

Stairs Climbing SC (n) if n ≤ 1 return 1 else return SC (n-1) + SC (n-2) SC (4) SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0)

Stairs Climbing SC (n) if n ≤ 1 return 1 else return SC (n-1) + SC (n-2) SC (4) SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0) In order to calculate SC(4), the program makes 9 recursive calls; 5 for SC(3) + 3 for SC(2)

Stairs Climbing SC (n) if n ≤ 1 return 1 else return SC (n-1) + SC (n-2) SC (4) SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0) In order to calculate SC(4), the program makes 9 recursive calls; 5 for SC(3) + 3 for SC(2) # of calls for SC(n) = # of calls for SC(n-1) + # of calls for SC(n-2) # of calls for SC(n) = F(n) ≈ ϕn ; nth Fibonacci number So the running time will be exponential O(ϕn)

Stairs Climbing SC (4) SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) We deal with the overlapping subproblems

Stairs Climbing SC (4) SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) We deal with the overlapping subproblems Whenever computing subproblems, keep them in a table to avoid recomputation, and refer them whenever they are needed

Stairs Climbing SC (4) SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) We deal with the overlapping subproblems Whenever computing subproblems, keep them in a table to avoid recomputation, and refer them whenever they are needed MEMOIZATION

Stairs Climbing SC (4) SC (n) initialize a memory M if n ≤ 1 return 1 if M contains n return M[n] else A = SC(n-1) + SC(n-2) M[n] = A return A SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0) We deal with the overlapping subproblems Whenever computing subproblems, keep them in a table to avoid recomputation, and refer them whenever they are needed MEMOIZATION

Stairs Climbing SC (4) SC (n) initialize a memory M if n ≤ 1 return 1 if M contains n return M[n] else A = SC(n-1) + SC(n-2) M[n] = A return A SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0) We deal with the overlapping subproblems Whenever computing subproblems, keep them in a table to avoid recomputation, and refer them whenever they are needed MEMOIZATION

Stairs Climbing Top-Down SC (4) SC (n) initialize a memory M if n ≤ 1 return 1 if M contains n return M[n] else A = SC(n-1) + SC(n-2) M[n] = A return A SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0) Top-Down We deal with the overlapping subproblems Whenever computing subproblems, keep them in a table to avoid recomputation, and refer them whenever they are needed MEMOIZATION

Stairs Climbing Can we come up with simpler program ? SC (4) SC (3)

Stairs Climbing Can we come up with simpler program ? get rid of recursion use a simple for loop

Stairs Climbing Can we come up with simpler program ? SC (n) initialize a memory M M[0] = 1 M[1] = 1 for ( i=2 to n) M[i] = M[i-1] + M[i-2] return M[n] SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) SC (1) SC (0) Can we come up with simpler program ? get rid of recursion use a simple for loop

Stairs Climbing Bottom-Up Can we come up with simpler program ? SC (n) initialize a memory M M[0] = 1 M[1] = 1 for ( i=2 to n) M[i] = M[i-1] + M[i-2] return M[n] SC (3) SC (2) SC (2) SC (1) SC (1) SC (0) Bottom-Up SC (1) SC (0) Can we come up with simpler program ? get rid of recursion use a simple for loop

Dynamic Programming analyze structure of the optimal solution and define subproblems that need to be solved in order to get the optimal solution establish the relationship between the optimal solution and those subproblems (construct the recurrence relation) compute the optimal values of subproblems, save them in a table (memoization), then compute the optimal values of larger subproblems, and eventually compute the optimal value of the original problem

Weighted Interval Scheduling given a set of intervals (I1, I2, ... , In) each interval Ii has a starting time si, a finishing time fi, and a weight wi your task is to find a subset of intervals (pairwise nonoverlapping) such that the total weight of intervals is maximized

Weighted Interval Scheduling given a set of intervals (I1, I2, ... , In) each interval Ii has a starting time si, a finishing time fi, and a weight wi your task is to find a subset of intervals (pairwise nonoverlapping) such that the total weight of intervals is maximized 7 5 6 3 3 4 1

Weighted Interval Scheduling given a set of intervals (I1, I2, ... , In) each interval Ii has a starting time si, a finishing time fi, and a weight wi your task is to find a subset of intervals (pairwise nonoverlapping) such that the total weight of intervals is maximized 7 5 6 3 3 4 1

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation j Two cases : sj fj J-1

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation j Two cases : either optimal solution does not include interval j, then continue with OPT (j-1) sj fj J-1

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation j Two cases : either optimal solution does not include interval j, then continue with OPT (j-1) (2) or optimal solution includes interval j, then continue with wj + sj fj J-1

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation j Two cases : either optimal solution does not include interval j, then continue with OPT (j-1) (2) or optimal solution includes interval j, then continue with wj + sj fj J-1 p(j) : largest index i such that i < j and fi < sj

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation j Two cases : either optimal solution does not include interval j, then continue with OPT (j-1) (2) or optimal solution includes interval j, then continue with wj + OPT (p(j)) sj fj J-1 p(j) : largest index i such that i < j and fi < sj

Weighted Interval Scheduling first sort all the intervals according to their finishing time : i1, i2, … , in such that f1 ≤ f2 ≤ ... ≤ fn define subproblems OPT (j) : value of the optimal solution for the first j intervals 1, … , j construct the recurrence relation j Two cases : either optimal solution does not include interval j, then continue with OPT (j-1) (2) or optimal solution includes interval j, then continue with wj + OPT (p(j)) sj fj J-1 OPT (j) = max { OPT (j-1), wj + OPT (p(j)) }

Weighted Interval Scheduling OPT (n) sort intervals according to finishing time if n = 0 return 0 else find p(n) if OPT (n-1) ≥ wn + OPT (p(n)) return OPT (n-1) return wn + OPT (p(n))

Weighted Interval Scheduling OPT (n) sort intervals according to finishing time if n = 0 return 0 else find p(n) if OPT (n-1) ≥ wn + OPT (p(n)) return OPT (n-1) return wn + OPT (p(n)) Similar to stairs climbing, # of calls here also F(n) ≈ ϕn

Weighted Interval Scheduling OPT (n) sort intervals according to finishing time if n = 0 return 0 else find p(n) if OPT (n-1) ≥ wn + OPT (p(n)) return OPT (n-1) return wn + OPT (p(n)) Similar to stairs climbing, # of calls here also F(n) ≈ ϕn Do Memoization

Weighted Interval Scheduling OPT (n) sort intervals according to finishing time if n = 0 return 0 else find p(n) if OPT (n-1) ≥ wn + OPT (p(n)) return OPT (n-1) return wn + OPT (p(n)) Similar to stairs climbing, # of calls here also F(n) ≈ ϕn Do Memoization OPT (n) sort intervals according to finishing time initialize a memory M compute p(1), ... , p(n) M[0] = 0 for (i=1 to n) M[i] = max { wi + M[p(i)], M[i-1] }

Weighted Interval Scheduling OPT (n) sort intervals according to finishing time if n = 0 return 0 else find p(n) if OPT (n-1) ≥ wn + OPT (p(n)) return OPT (n-1) return wn + OPT (p(n)) Similar to stairs climbing, # of calls here also F(n) ≈ ϕn Do Memoization OPT (n) sort intervals according to finishing time initialize a memory M compute p(1), ... , p(n) M[0] = 0 for (i=1 to n) M[i] = max { wi + M[p(i)], M[i-1] } Top-Down Bottom-Up

Longest Common Subsequence given two sequence x[1…m] and y[1…n], find a longest subsequence common to both of them (doesn’t need to be unique) x : A B C B D A B y : B D C A B A

Longest Common Subsequence given two sequence x[1…m] and y[1…n], find a longest subsequence common to both of them (doesn’t need to be unique) x : A B C B D A B LCS(x,y) = BCAB y : B D C A B A

Longest Common Subsequence given two sequence x[1…m] and y[1…n], find a longest subsequence common to both of them (doesn’t need to be unique) x : A B C B D A B LCS(x,y) = BCAB y : B D C A B A these subsets don’t need to be continuous

Longest Common Subsequence Brute-Force check every subsequence of x[1…m] whether it is a subsequence of y[1…n] each check takes O(n) time 2m subsequence of x (each bit-vector defines a subsequence). total running time will be O(2m.n)

Longest Common Subsequence Simplified Version rather than directly calculating LCS(x,y), calculate the length of LCS(x,y) ( = LCS(x,y) ) define subproblems

Longest Common Subsequence Simplified Version rather than directly calculating LCS(x,y), calculate the length of LCS(x,y) ( = LCS(x,y) ) define subproblems consider the prefix x[1…i] of x and the prefix y[1…j] of y c[i,j] = LCS(x[1…i], y[1…j]) : length of the longest common subsequence of the prefixes x[1…i] and y[1…j]

Longest Common Subsequence construct recurrence relation

Longest Common Subsequence construct recurrence relation i n x Two cases : y j m

Longest Common Subsequence construct recurrence relation i n x Two cases : if x[i] = y[j], then continue with c[i-1,j-1] + 1 y j m

Longest Common Subsequence construct recurrence relation i n x Two cases : if x[i] = y[j], then continue with c[i-1,j-1] + 1 (2) otherwise continue with max { c[i,j-1], c[i-1,j] } y j m

Longest Common Subsequence construct recurrence relation i n x Two cases : if x[i] = y[j], then continue with c[i-1,j-1] + 1 (2) otherwise continue with max { c[i,j-1], c[i-1,j] } y j m c[i-1,j-1] + 1 if x[i] = y[j] max { c[i,j-1], c[i-1,j] } otherwise c[i,j] =

Longest Common Subsequence LCS (x,y,n,m) if i = 0 and j = 0 return 0 if x[n] = y[m] c[n,m] = LCS(x,y,n-1,m-1) + 1 else c[n,m] = max { LCS(x,y,n-1,m), LCS(x,y,n,m-1) } return c[n,m]

Longest Common Subsequence Let’s check it for m = 6, n = 7 7,6 6,6 7,5 5,6 6,5 6,5 7,4 4,6 5,5 5,5 6,4 5,5 6,4 6,4 7,3 The height of the tree m + n,

Longest Common Subsequence Let’s check it for m = 6, n = 7 7,6 6,6 7,5 5,6 6,5 6,5 7,4 4,6 5,5 5,5 6,4 5,5 6,4 6,4 7,3 The height of the tree m + n, So the running time will be O(2m+n)

Longest Common Subsequence Let’s check it for m = 6, n = 7 7,6 6,6 7,5 5,6 6,5 6,5 7,4 4,6 5,5 5,5 6,4 5,5 6,4 6,4 7,3 The height of the tree m + n, So the running time will be O(2m+n)

Longest Common Subsequence Let’s check it for m = 6, n = 7 7,6 6,6 7,5 5,6 6,5 6,5 7,4 4,6 5,5 5,5 6,4 5,5 6,4 6,4 7,3 The height of the tree m + n, So the running time will be O(2m+n) How many different subproblems are there?

Longest Common Subsequence Let’s check it for m = 6, n = 7 7,6 6,6 7,5 5,6 6,5 6,5 7,4 4,6 5,5 5,5 6,4 5,5 6,4 6,4 7,3 The height of the tree m + n, So the running time will be O(2m+n) How many different subproblems are there? ( m.n )

Longest Common Subsequence Let’s check it for m = 6, n = 7 7,6 6,6 7,5 5,6 6,5 6,5 7,4 4,6 5,5 5,5 6,4 5,5 6,4 6,4 7,3 The height of the tree m + n, So the running time will be O(2m+n) How many different subproblems are there? ( m.n ) Do Memoization

Longest Common Subsequence LCS (x,y,n,m) (with Memoization) initialize a memory M M[0,0] = 0 if M[n,m] = null if x[n] = y[m] M[n,m] = LCS(x,y,n-1,m-1) + 1 else M[n,m] = max { LCS(x,y,n-1,m), LCS(x,y,n,m-1) } return M[n,m]

Longest Common Subsequence LCS (x,y,n,m) (with Memoization) initialize a memory M M[0,0] = 0 if M[n,m] = null if x[n] = y[m] M[n,m] = LCS(x,y,n-1,m-1) + 1 else M[n,m] = max { LCS(x,y,n-1,m), LCS(x,y,n,m-1) } return M[n,m] running time : O(m.n) space : O(m.n)

Longest Common Subsequence LCS (x,y,n,m) (with Memoization) initialize a memory M M[0,0] = 0 if M[n,m] = null if x[n] = y[m] M[n,m] = LCS(x,y,n-1,m-1) + 1 else M[n,m] = max { LCS(x,y,n-1,m), LCS(x,y,n,m-1) } return M[n,m] running time : O(m.n) space : O(m.n) Top-Down

Longest Common Subsequence Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m]

Longest Common Subsequence Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C D

Longest Common Subsequence j = 1 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C D i = 1

Longest Common Subsequence j = 1 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C D i = 1

Longest Common Subsequence j = 2 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C D i = 1

Longest Common Subsequence j = 2 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 3 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 3 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 4 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 4 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 1

Longest Common Subsequence j = 1 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 2

Longest Common Subsequence j = 1 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D i = 2

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4 We can reconstruct LCS by tracing backwards Whenever we have a diagonal, we hav a match!

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4 A We can reconstruct LCS by tracing backwards Whenever we have a diagonal, we hav a match!

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4 A We can reconstruct LCS by tracing backwards Whenever we have a diagonal, we hav a match!

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4 C A We can reconstruct LCS by tracing backwards Whenever we have a diagonal, we hav a match!

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4 C A We can reconstruct LCS by tracing backwards Whenever we have a diagonal, we hav a match!

Longest Common Subsequence j = 5 Bottom-up initialize a memory M for i=0 to n M[i,0] = 0 for i=1 to m M[0,i] = 0 for i=1 to n for j=1 to m if x[i] = y[j] M[i,j] = M[i-1,j-1] + 1 else M[i,j] = max { M[i-1,j], M[i,j-1] } return M[n,m] A B C 1 D 2 3 i = 4 B C A We can reconstruct LCS by tracing backwards Whenever we have a diagonal, we hav a match!

Rod Cutting given a rod of length n with the prices p1,…,pn where pi is the price of a rod of length i, find an optimal way of cutting the given rod that minimizes the profit

Rod Cutting given a rod of length n with the prices p1,…,pn where pi is the price of a rod of length i, find an optimal way of cutting the given rod that minimizes the profit length 1 2 3 4 price 5 8 9 4

Rod Cutting given a rod of length n with the prices p1,…,pn where pi is the price of a rod of length i, find an optimal way of cutting the given rod that minimizes the profit 1 1 1 length 1 2 3 4 price 5 8 9 2 1 1 4 4 3 1 2 2 1

Rod Cutting given a rod of length n with the prices p1,…,pn where pi is the price of a rod of length i, find an optimal way of cutting the given rod that minimizes the profit 1 1 1 length 1 2 3 4 price 5 8 9 2 1 1 4 4 3 1 2 2 1 4 10 7 9 9

Rod Cutting define subproblems

Rod Cutting define subproblems c(i) : max profit for the first length i part

Rod Cutting define subproblems c(i) : max profit for the first length i part construct recurrence relation

Rod Cutting define subproblems c(i) : max profit for the first length i part construct recurrence relation c(i) =

Rod Cutting define subproblems c(i) : max profit for the first length i part construct recurrence relation focus on last cutting <-------- i – k --------> <-- k --> c(i) =

Rod Cutting define subproblems c(i) : max profit for the first length i part construct recurrence relation focus on last cutting find best k maximizing the profit <-------- i – k --------> <-- k --> c(i) = max { pk +

Rod Cutting define subproblems c(i) : max profit for the first length i part construct recurrence relation focus on last cutting find best k maximizing the profit recursively continue on the remaining part <-------- i – k --------> <-- k --> c(i) = max { pk + c(i-k) }

Rod Cutting input : ( n ; p1 , p2 , ... , pn ) initialize a memory M for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n]

Rod Cutting input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5

Rod Cutting j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 i = 1

Rod Cutting c(1) = M[0] + p1 c(1) = 1 1 2 3 4 5 pi 8 9 10 M[i] 5 j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 i = 1 c(1) = M[0] + p1 c(1) = 1

Rod Cutting j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 i = 2

Rod Cutting k=1 c(2) = M[1] + p1 c(2) = 2 1 2 3 4 5 pi 8 9 10 M[i] 5 j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 i = 2 k=1 c(2) = M[1] + p1 c(2) = 2

Rod Cutting k=1 c(2) = M[1] + p1 c(2) = 2 k=2 c(2) = M[0] + p2 j = 2 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 i = 2 k=1 c(2) = M[1] + p1 c(2) = 2 k=2 c(2) = M[0] + p2 c(2) = 5

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(3) = M[2] + p1 c(3) = 6 j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 i = 3 k=1 c(3) = M[2] + p1 c(3) = 6

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(3) = M[2] + p1 c(3) = 6 j = 2 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 i = 3 k=1 c(3) = M[2] + p1 c(3) = 6 k=2 c(3) = M[1] + p2

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(3) = M[2] + p1 c(3) = 6 j = 3 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 i = 3 k=1 c(3) = M[2] + p1 c(3) = 6 k=2 c(3) = M[1] + p2 k=3 c(3) = M[0] + p3 c(3) = 8

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(4) = M[3] + p1 c(4) = 9 j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 i = 4 k=1 c(4) = M[3] + p1 c(4) = 9

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(4) = M[3] + p1 c(4) = 9 j = 2 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 i = 4 k=1 c(4) = M[3] + p1 c(4) = 9 k=2 c(4) = M[2] + p2 c(4) = 10

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(4) = M[3] + p1 c(4) = 9 j = 3 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 i = 4 k=1 c(4) = M[3] + p1 c(4) = 9 k=2 c(4) = M[2] + p2 c(4) = 10 k=3 c(4) = M[1] + p3 c(4) = 9

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(4) = M[3] + p1 c(4) = 9 j = 4 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 2 i = 4 k=1 c(4) = M[3] + p1 c(4) = 9 k=2 c(4) = M[2] + p2 c(4) = 10 k=3 c(4) = M[1] + p3 c(4) = 9 k=4 c(4) = M[0] + p4

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(5) = M[4] + p1 c(5) = 11 j = 1 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 2 i = 5 k=1 c(5) = M[4] + p1 c(5) = 11

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(5) = M[4] + p1 c(5) = 11 j = 2 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 2 i = 5 k=1 c(5) = M[4] + p1 c(5) = 11 k=2 c(5) = M[3] + p2 c(5) = 13

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(5) = M[4] + p1 c(5) = 11 j = 3 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 2 i = 5 k=1 c(5) = M[4] + p1 c(5) = 11 k=2 c(5) = M[3] + p2 c(5) = 13 k=3 c(5) = M[2] + p3

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 5 k=1 c(5) = M[4] + p1 c(5) = 11 j = 4 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 5 2 3 2 i = 5 k=1 c(5) = M[4] + p1 c(5) = 11 k=2 c(5) = M[3] + p2 c(5) = 13 k=3 c(5) = M[2] + p3 k=4 c(5) = M[1] + p4 c(5) = 10

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 13 5 k=1 c(5) = M[4] + p1 j = 5 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 13 5 2 3 2 2 i = 5 k=1 c(5) = M[4] + p1 c(5) = 11 k=2 c(5) = M[3] + p2 c(5) = 13 k=3 c(5) = M[2] + p3 k=4 c(5) = M[1] + p4 c(5) = 10 k=5 c(5) = M[0] + p5

Rod Cutting 1 2 3 4 5 pi 8 9 10 M[i] 13 5 k=1 c(5) = M[4] + p1 j = 5 input : ( n ; p1 , p2 , ... , pn ) initialize a memory M M[0] = 0 for i=1 to n M[i] = - ∞ for j=1 to i q = M[i-j] + pj if q > M[i] M[i] = q return M[n] 1 2 3 4 5 pi 8 9 10 M[i] 13 5 2 3 2 2 i = 5 k=1 c(5) = M[4] + p1 c(5) = 11 k=2 c(5) = M[3] + p2 c(5) = 13 k=3 c(5) = M[2] + p3 k=4 c(5) = M[1] + p4 c(5) = 10 k=5 c(5) = M[0] + p5 2 3