Homework #6 -- due Monday Wk 7

# required problems topic
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.
dynamic pgm
2 CLR Problem 15-2 on page 365 printing neatly
3 Consider the function:
f(1) = 1
f(n) = ∑i=1 to n-1 ( i f(i) ),   for n > 1
  1. Give a divide-and-conquer 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).
algorithm based on recurrence

# suggested problems topic
4 CLR Exercise 15.4-6 on page 356 longest increasing subsequence
5 CLR Problem 15-1 on page 364 bitonic euclidean tsp
6 Baase Exercise 10.9 on page 477 optimal bst
7 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
optimal bst
8 Find the optimal binary search tree consisting of n elements having probabilities
p1 = p2 = ... = pn = 0.0
qi < 0.5 qi +1, for 0 < i < n
Give an explanation of why it is optimal.
optimal bst
9 Baase Exercise 10.16 on page 478 dynamic pgm - binomial coefficients
10 Baase Exercise 10.21 on page 479 dynamic pgm - partition problem
11 The Strahler number of a binary tree is defined as follows:
An empty tree has Strahler number 0.  If the binary tree T has subtrees TL and TR, the Strahler number S(T) of T is defined by

max{ S(TL),S(TR) },         if S(TL) ≠ S(TR)

S(TL) + 1,                 otherwise

Give an algorithm (possibly recursive) to find the Strahler number of a binary tree.
dynamic pgm
12 One version of the game of Nim is played with counters that are arranged in three rows.  A position (the pattern of counters) can be conveniently represented by an ordered triple of integers describing the number of counters in each row.

The game is played by two persons who move in turns.  Each move consists of removing one or more counters from one of the rows.  A player wins if he/she removes all remaining counters, i.e., the position <0,0,0> results from his/her move.

Any position in which the next player to move, if he/she moves correctly, is guaranteed to eventually win (regardless of which moves his/her opponent may make) is called a winning position.  Let WIN(i, j, k) be true iff <i, j, k> is a winning position.

  1. Give a recurrence for the values of WIN, including boundary conditions.
  2. Using your recurrence from part a, produce a polynomial-time algorithm that determines whether any given position <a, b, c> is a winning position.
  3. Analyze the time and space complexity of your algorithm as a function of a, b, c. What is the complexity as a function of n when a=b=c=n?
    Hint: A good solution would be θ(n4). A better solution would be θ(n3).
Nim
13 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?
DC+dyn pgm
14 DPV exercise 6.4 on p.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.
dynamic pgm
15 DPV exercise 6.5 on p.178
We are given a checkerboard which has 4 rows and n columns, and has an integer written in each square.  We are also given a set of 2n pebbles, and we wish to place some or all of these on the checkerboard (each pebble can be placed on exacyly one square) so as to maximize the sum of integers in the squares that are covered by pebbles.  There is one constraint: for a placement of pebbles to be legal, no two of them can be on horizontally or vertically adjacent squares (diagonal adjacency is fine).
  1. Determine the number of legal patterns that can occur in any column (in isolation, ignoring the pebbles in adjacent columns) and describe these patterns.

Call two patterns compatible if they can be placed on adjacent columns to form a legal placement.  Let us consider subproblems consisting of the first k columns 1≤kn.  Each subproblem can be assigned a type, which is the pattern occurring in the last column.

  1. Using the notions of compatibility and type, give an O(n)-time dynamic programming algorithm for computing an optimal placement.
dynamic pgm


Dan Hirschberg
Computer Science Department
University of California, Irvine, CA 92697-3435
dan at ics.uci.edu
Last modified: Jun 18, 2008