Computer Science C73 October 8, 2014 Scarborough Campus University of Toronto

Computer Science C73
Scarborough Campus
October 8, 2014
University of Toronto
Homework Assignment #3
Due: November 5, 2014, by 9:30 am
(in the drop box for your CSCC73 tutorial section)
Appended to this document is a cover page for your assignment. Fill it out, staple your answers to it,
and deposit the resulting document into the drop box for your tutorial section (without putting it in an
envelope). An assignment submitted on behalf of two students will be returned to the tutorial in whose
box it was deposited.
Question 1. (10 marks) (Exercise 6.1 in DPV, slightly reworded.) A contiguous subsequence of a list
S is a subsequence consisting of consecutive elements of S. For instance, if S is
5, 15, −30, 10, −5, 40, 10
then 15, −30, 10 is a contiguous subsequence of S, but 5, 15, 40 is not. Consider the following problem:
Input: A list of numbers S = a1 , a2 , . . . , an
Output: The maximum number m such that m is the sum of all elements of a contiguous subsequence
of S. (We define the sum of an empty subsequence to be 0.)
It is trivial to design an O(n2 ) algorithm to solve this problem, since there are only O(n2 ) contiguous
subsequences of a sequence of length n. Use dynamic programming to give an O(n) algorithm to solve this
problem. Explain why your algorithm is correct.
Hint: For each integer i, 1 ≤ i ≤ n, consider the maximum number that is the sum of all elements of a
contiguous subsequence of S that ends with ai .
Question 2. (10 marks) (Exercise 6.7 in DPV, reworded.) A palindrome is a sequence that reads the
same backwards as forwards.
a. (9 marks) Give an O(n2 ) algorithm which, given a sequence S of length n, returns the maximum
number m such that m is the length of a subsequence (not necessarily contiguous) of S that is a palindrome.
Explain why your algorithm is correct.
Hint: For all integers i, j, 1 ≤ i ≤ j ≤ n, consider the quantity P [i, j] defined as the maximum length of
a subsequence of S[i..j] that is a palindrome.
b. (1 marks) Modify your algorithm in part (a) so that it returns a maximum-length subsequence of S
that is a palindrome.
Question 3. (10 marks) In the Subset Sum problem we are given a sequence of positive integers and we
want to select a subsequence of these integers whose sum is as large as possible without exceeding a given
“capacity”. Consider a variation of this problem, where we are given an additional input ∆, and we add
the constraint that any two elements in the chosen subsequence differ by at least ∆.
More precisely, the input
P is a sequence of positive integers w1 , w2 , . . . , wn , W, ∆. We
Pwish to find
S ⊆ {1, 2, . . . , n} such that j∈S wj is as large as possible, subject to the constraints that (a) j∈S wj ≤ W ,
and (b) for all i, j ∈ S, if i 6= j then |wi − wj | ≥ ∆.
Give a dynamic programming algorithm for this problem. Explain why your algorithm is correct, and
analyse its time complexity. Your algorithm should run in time polynomial in n and W .
1
Question 4. (10 marks) We are given an n × n array A[1..n, 1..n] of bits (0s or 1s). We want to find a
largest square of 1s within this array. (For example, the array could represent a map, where a 0 denotes a
square parcel of private land and a 1 denotes a square parcel of public land. We want to find the largest
possible square piece of public land, say to build a new school.)
More precisely, a square of 1s is a triple of positive integers (i, j, `) such that i, j ≥ 1, i + ` < n,
j + ` < n, and A[i0 , j 0 ] = 1 for all i0 , j 0 such that i ≤ i0 < i + ` and j ≤ j 0 < j + `. We want to find a square
of 1s such that ` is as large as possible.
It is easy to get an O(n5 ) time algorithm for this problem. You should do better. Use dynamic
programming, explain why your algorithm is correct, and state its running time. For full credit, you must
give an O(n2 ) algorithm.
Hint: Consider, for each i, j, the size of a largest clot whose lower right corner is [i, j].
Question 5. (10 marks) In Assignment 1 we saw a greedy algorithm for the “optimal layout” problem:
Given a sequence of words, and their lengths, arrange the words, in the given sequence, on lines so that
each word appears in its entirety on one line, and the sum of the lengths of the words on each line does
not exceed the maximum width L of a line. Recall that we defined the penalty of a line to be the amount
of white space at the end of the line (i.e., the difference between L and the sum of the lengths of the words
on the line). Then we can define the “cost” (or “badness”) of a layout in different ways: For example, we
can define it as the maximum of all line penalties, or as the sum of all line penalties. Accordingly, we want
to find a layout that minimizes that cost. As we saw, the greedy algorithm found a layout that minimizes
the sum of the line penalties, but does not necessarily minimize the maximum line penalty.
Now suppose that we define the cost of a layout as the sum of the squares of the line penalties. The
idea here is that lines with a lot of white space are ugly, so we want to penalize heavily a line with much
white space. In particular, we prefer a layout that produces two lines each with penalty 5 to a layout that
produces two lines with penalties 0 and 10, even though both layouts have the same sum of line penalties,
namely 10. Spiffy text-processing systems like TeX use a cost function similar to this one to define the
quality of layouts.
a. (1 marks) Give a counterexample to show that the greedy algorithm we saw in Homework Assignment 1
does not always produce a layout that minimizes this cost.
b. (8 marks) Give a polynomial-time dynamic programming algorithm that computes the minimum
cost of a layout (under this definition of cost).
c. (1 marks) Explain how to modify your algorithm in part (b) so that it also prints the lines of a
minimum cost layout.
2
Cover page for
CSCC73 Assignment #3
Submitted by
(2) Family Name:
(1) Family Name:
Given Name:
Given Name:
Student Number:
Student Number:
By virtue of submitting this homework I/we acknowledge that I am/we are aware of the policy on
homework collaboration for this course.
3