Lecture Notes on 27 Nov 2023 * Method of induction: How do you prove that 1 + 2 + ... + n = n * (n + 1) / 2 Assume that this relation holds good for n = k. Prove that this relation holds good for n = k + 1 Then show that this relation holds good for n = 1 Argue that if it holds good for n = 1 then it must hold good for n = 2 and if it holds good for n = 2 then it must hold good for n = 3 and so on. * We will apply a similar type of reasoning to the rod cutting problem * We will explore the concepts of overlapping sub-problems and optimal substructure in the context of the rod cutting problem. Here is a worksheet for the classic rod cutting problem: https://www.cs.utexas.edu/users/mitra/csFall2023/cs313/notes/Rod_Cutting_Problem.pdf https://www.cs.utexas.edu/users/mitra/csFall2023/cs313/notes/Rod_Cutting_Problem.docx * In this problem you are asked to find the optimal price for cutting a rod of size 22. * In the table the first column giving the lengths is i and the second column is the optimal cost of cutting a rod of that length C[i]. The third column gives the actual cuts. * We are going to use an approach similar to the proof by induction. * Assume that the table is correctly filled up to a rod of size 21. * Then to get the entries for the next line for a rod of size 22, this is the reasoning that we go through: Make the cuts 1 through 8 (since these are the sizes that the rods are sold), find the costs for each cut. Then find the maximum of those costs. C[22] = max {(C[21] + C[1]), (C[20] + C[2]), (C[19] + C[3]), (C[18] + C[4]), (C[17] + C[5]), (C[16] + C[6]), (C[15] + C[7]), (C[14] + C[8])} This assumes that all the costs in the second column are the optimal costs for those cuts. This is what we mean by optimal substructure. Each of the terms (C[21] + C[1]), (C[20] + C[2]), etc are sub-problems and it is quite evident that they overlap. Hence this problem is a prime candidate for solving using dynamic programming and memoization. Once you have found the maximum cost for cutting the rod of size 22, you write in the third column what that cut was. * How do you fill the table (or memo)? Start from line 1. Code for the Rod Cutting Problem def cut_rod (p, n): r = [] s = [] for i in range (n + 1): r.append (0) s.append (0) for j in range (1, n + 1): max_price = -1 for k in range (1, j + 1): new_price = 0 if (k < len(p)): new_price = p[k] + r[j - k] else: new_price = r[j - k] if (new_price > max_price): max_price = new_price # remember the best value of the cut s[j] = k r[j] = max_price print (r) print (s) return r, s def main(): # define the price per length of rod p = [0, 1, 5, 8, 9, 10, 17, 17, 20] # prompt the user to enter the size of the rod to be cut n = int (input ("Enter size of rod: ")) # get the optimal price for cutting a rod of length n r, s = cut_rod (p, n) # print the optimal price print ("Optimal price = ", r[n]) # print the cuts of the rod while (n > 0): print (s[n]) n = n - s[n] main() Code for Number of Cuts for a Given Rod Size: def main(): dim = int (input ("Enter dimension of grid: ")) cuts = [] for i in range (dim + 1): row = [] for j in range (dim + 1): row.append (0) cuts.append (row) for i in range (dim + 1): for j in range (dim + 1): if (i == 0) and (j == 0): cuts[i][j] = 1 else: if (i > j): cuts[i][j] = cuts[i - 1][j] else: cuts[i][j] = cuts[i - 1][j] + cuts[i][j - i] # now print the grid for i in range (dim + 1): for j in range (dim + 1): print (cuts[i][j], end = " ") print () print () main() * Coin Change problem: You have a large number of coins of the following denominations [1, 7, 13, 23]. What is the minimum number of coins that you can use to hand out change for 37? Number of coins: C[37] = min {(C[37 - 1], C[37 - 7], C[37 -13], C[37 -23]} + 1 = min {C[36], C[30], C[24], C[14]} + 1 * Here are the worksheets for the Coin Change Problem: https://www.cs.utexas.edu/users/mitra/csFall2023/cs313/notes/Coin_Change_Problem.pdf https://www.cs.utexas.edu/users/mitra/csFall2023/cs313/notes/Coin_Change_Problem.docx * Maximum Weighted Independent Set: You have a set of bottle with different volumes filled with your favorite beverage. You may drink as much as you want with the constraint that you cannot drink from two adjacent bottles. How can you maximize your intake? The volumes are {6, 2, 9, 5, 1, 4, 7, 8, 3} Let us say you have n bottles. Then you chose the last bottle or you do not chose the last bottle. If you do not chose the last bottle then you want to find the best solution from among the first n - 1 bottles. If V[i] is the volume of the ith bottle and S[i] is the largest sum so far, then the solution is of the form: S[i] = max {(V[i] + S[i - 2]), S[i - 1]} * Here are the worksheets for the Maximum Weighted Independent Set: https://www.cs.utexas.edu/users/mitra/csFall2023/cs313/notes/Maximum_Weighted_Independent_Set.pdf https://www.cs.utexas.edu/users/mitra/csFall2023/cs313/notes/Maximum_Weighted_Independent_Set.docx