CSPP 55001 Algorithms — Autumn 2011

Homework 5 (assigned October 27, due November 2)

Reading: CLRS chapters 9 and 15. Reading for next week's lecture: CLRS chapter 22.

Written assignment: Solve the following "Do" exercises and assigned problems. Only solutions to the assigned problems should be turned in.
Note: You are responsible for the material covered in both "Do" exercises and assigned problems.
Note: If you work with others, indicate their names at the top of your homework paper. Everyone must submit their own independently written solutions.

"Do" Exercises (not to be handed in):

  1. The following problem asks you to develop a dynamic programming algorithm to find the longest monotonically increasing subsequence within a sequence of n numbers.

  2. You are given an array A[1..n] of n integers, not necessarily positive. Your goal is to find two indices i and j such that the sum of the array entries from A[i] to A[j] is maximum.

  3. You are going on a long trip. You start on the road at mile post 0. Along the way there are n hotels, at mile posts m1 < m2 < … < mn, where each mi is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel, at distance mn, which is your destination. You would like to travel 200 miles a day, but this may not be possible, depending on the spacing of the hotels. If you travel x miles during a day, the penalty for that day is (200 − x)2. You want to plan your trip so as to minimize the total penalty, i.e., the sum, over all travel days, of the daily penalties. Give an dynamic programming algorithm that determines the optimal sequence of hotels at which to stop.

Assigned Problems (to be handed in):

Important: When describing an algorithm in pseudocode, explain the meaning of your variables in English. Comment the lines of your pseudocode. Your pseudocode should be short, clear, and complete to receive full credit. If your code is long, difficult to read, or incomplete, you will not receive full credit.

  1. The longest common substring (not subsequence) of two strings X and Y is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of PHOTOGRAPH and TOMOGRAPHY is OGRAPH.
    Given two strings X = x1x2xn and Y = y1y2ym , find the length of their longest common substring, i.e., the largest k for which there are indices i and j with xixi + k − 1 = yjyj + k − 1. Give a O(nm) dynamic programming algorithm to solve this problem. The strings X and Y are given as arrays of characters.

  2. Solve the knapsack problem for the following input values using the dynamic programming knapsack algorithm from class: w = [6, 3, 4, 2], v = [30, 14, 16, 9], W = 10.

  3. The edit distance d(x, y) of two strings of text, x[1..m] and y[1..n], is defined to be the minimum possible cost of a sequence of "edit" operations (defined below) that transforms string x[1..m] into string y[1..n]. To define the effect of the edit operations, we use an auxiliary string z[1..s] that holds the intermediate results. At the beginning of the transformation sequence, s = m and z[1..s] = x[1..m], i.e., we start with string x[1..m]. At the end of the transformation sequence, we should have s = n and z[1..s] = y[1..n]. Throughout the transformation, the current length s of string z is updated as part of each operation.
    Each "edit" operation may alter the string z and its size s. Each "edit" operation also has an associated cost. The cost of a sequence of "edit" operations is the sum of the costs of the individual operations in the sequence. The goal of the edit-distance problem is to find a sequence of "edit" operations of miminum cost that transforms x[1..m] into y[1..n].

    We define three edit operations:

    Operation Cost Effect
    replace(i, c) 6 replace the character at position i with the character c by setting z[i] = c
    delete(i) 5 delete the character at position i by setting z[i..s] = z[i + 1..s] and decrementing s
    insert(i, c) 5 insert the character c into string z at position i by setting z[i + 1..s + 1] = z[i..s], z[i] = c, and incrementing s.

  4. You are a consultant. Your basic decision, each week, is whether to take a low-stress job or a high-stress job. If you select a low-stress job in week i, then you get a revenue of l[i] > 0 dollars; if you select a high-stress job in week i, you get a revenue of h[i] > 0 dollars. However, in order to take a high-stress job in week i, it is necessary that you do no job of either type in week i − 1. On the other hand, you can take a low-stress job in week i even if you have done a job of either type in week i − 1.
    Given a sequence of n weeks, a plan is specified by a choice of "low-stress", "high-stress", or "none" for each of the n weeks, with the property that if "high-stress" is chosen for week i > 1, then "none" has to be chosen for week i − 1. (You can choose a high-stress job in week 1.) The value of the plan is determined as follows: for each i, you add l[i] to the value if you choose "low-stress" in week i, and you add h[i] to the value if you choose "high-stress" in week i. You add 0 if you choose "none" in week i.

    Problem. Given sets of values l[1], l[2], …, l[n] and h[1], h[2], …, h[n], give a dynamic programming algorithm that finds the value of an optimal plan, i.e., a plan of maximum value. Your algorithm should run in O(n) time.


Gerry Brady
Thursday October 27 18:19:02 CDT 2011