Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming.

Similar presentations


Presentation on theme: "Dynamic Programming."— Presentation transcript:

1 Dynamic Programming

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

3 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

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

5 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

6 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 He developed the method with Lester Ford to find the shortest path in a graph.

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

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

9 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

10 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

11 Stairs Climbing n stairs . . .

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

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

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

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

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

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

18 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

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

20 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)

21 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)

22 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)

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

24 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

25 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

26 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

27 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

28 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

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

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

31 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

32 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

33 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

34 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

35 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

36 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

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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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)) }

46 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))

47 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

48 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

49 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] }

50 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

51 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

52 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

53 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

54 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)

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

56 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]

57 Longest Common Subsequence
construct recurrence relation

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

59 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

60 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

61 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] if x[i] = y[j] max { c[i,j-1], c[i-1,j] } otherwise c[i,j] =

62 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]

63 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,

64 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)

65 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)

66 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?

67 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 )

68 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

69 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]

70 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)

71 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

72 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]

73 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

74 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

75 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

76 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

77 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

78 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

79 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

80 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

81 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

82 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

83 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

84 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

85 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

86 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

87 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!

88 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!

89 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!

90 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!

91 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!

92 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!

93 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

94 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

95 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

96 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

97 Rod Cutting define subproblems

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

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

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

101 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) =

102 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 +

103 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) }

104 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]

105 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

106 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

107 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

108 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

109 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

110 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

111 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

112 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

113 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

114 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

115 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

116 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

117 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

118 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

119 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

120 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

121 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

122 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

123 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


Download ppt "Dynamic Programming."

Similar presentations


Ads by Google