Homework #5

Mostly dynamic programming

# problems
1 Your favorite sawmill charges by the length to cut a piece of lumber. (For example, to cut a 7 foot piece at any point costs 7 cents.)
  1. You bring in a 29 foot board and want it cut at points 4, 14, 19, and 27 feet from the left end.  In what order should the mill cut the wood in order to minimize your cost?  Use dynamic programming principles to solve this problem.
    Example: To cut the board in 1, 2, 3, 4 order it will cost 29+25+15+10=79 cents.
  2. Develop and analyze (asymptotic time and space) an algorithm to solve the previous problem for any length of lumber and number of cuts.
2 Consider the function:
f(1) = 1
f(n) = ∑i=1 to n-1 ( i f(i) ),   for n > 1
  1. Give a recursive algorithm which, given n, calculates f(n).
  2. Give and solve a recurrence, as a function of n, for the number of scalar multiplications required by your algorithm from part (a).
  3. Give a dynamic programming algorithm which, given n, calculates f(n).
  4. Find, as a function of n, the number of scalar multiplication operations required by your algorithm from part (c).
  5. Give an algorithm which, given n, calculates f(n) using only a linear number of scalar multiplications (this includes multiplications and divisions).
    Hint: First find a closed form for the value of f(n).
3 Find an optimal binary search tree for the probabilities
p1 = p2 = p3 = p4 = p5 = 0.05
q0=0.0, q1=0.05, q2=0.10, q3=0.15, q4=0.20, q5=0.25
4 Let T be a complete binary tree with n = 2k-1 nodes. The nodes are numbered in level order with the root labelled 1, its two children labelled 2 and 3, etc. Let Ti be the subtree whose root is node i, and so T1 = T. Every node i has a value VAL[i] which can be positive or negative or zero. We define WT(i) to be the weight of subtree i, given by the sum of all values of the nodes in Ti. The problem is to find the maximum weight of all subtrees of T.
  1. Give a recurrence for WT(i).
  2. Give a divide-and-conquer algorithm that finds the maximum WT(i) given the array VAL.
  3. What are the space and time complexities of your D&C algorithm?
  4. Give a dynamic programming algorithm that finds the maximum WT(i) given the array VAL.
  5. What are the space and time complexities of your dynamic programming algorithm?
5 DPV Exercise 6.4 on page 178
You are given a string of n characters s[1...n], which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like "itwasthebestoftimes...").  You wish to reconstruct the document using a dictionary, which is available in the form of a Boolean function dict(*) such that, for any string w, dict(w) has value 1 if w is a valid word, and has value 0 otherwise.
  1. Give a dynamic programming algorithm that determines whether the string s[*] can be reconstituted as a sequence of valid words.  The running time should be at most O(n2), assuming that each call to dict takes unit time.
  2. In the event that the string is valid, make your algorithm output the corresponding sequence of words.
6 Design an algorithm to determine the number of different ways there are to give x cents in change using any numbers of pennies, nickels, dimes, quarters, and half-dollars.  For example, there are six ways to give 17 cents change:  a dime, a nickel, and two pennies;  a dime and seven pennies;  three nickels and two pennies;  two nickels and seven pennies;  one nickel and 12 pennies; and 17 pennies.
7 Design an algorithm to determine the number of different ways there are to give x cents in change from a fixed set of coins consisting of n1 pennies, n2 nickels, n3 dimes, n4 quarters, and n5 half-dollars.
8 Design an efficient algorithm to determine the number of different sequences there are to give x cents in change from a fixed set of coins as in Problem 2.  Here, however, the order of giving coins makes a difference. For example, there are only two ways to give 6 cents change when order is not important:  six pennies; or one penny and one nickel.  In contrast, there are three sequences (where order does matter) of giving 6 cents in change:  PPPPPP, or PN, or NP, where P is a penny and N is a nickel.
9 Design an efficient algorithm to determine the fewest number of coins with which one can make change from a fixed set of coins.  The numbers of coins are as in Problem 2.  Also as in Problem 2, an answer may not exist.  In that case, your algorithm should return -1.
10 Consider the recurrence bn = 3.7bn-1 - 2.8bn-2, with boundary conditions b1 = 1.2, b2 = 2.1.
  • Describe an efficient algorithm that calculates the value of bn, where the measure of efficiency is the number of multiplications performed
  • Determine the number of multiplications used by your algorithm to calculate the value of b50

Last modified: Mar 27, 2020