0% found this document useful (0 votes)
28 views

Assignment5 CSE429

The document contains a homework assignment with 3 problems related to algorithms. Problem 1 involves finding the interval of a sequence of integers that maximizes the sum and provides a linear time algorithm. Problem 2 asks to find the longest palindrome subsequence in a given string and provides a dynamic programming solution in O(n^2) time. Problem 3 involves determining the best way to cut a cloth to maximize profits from making products, providing an algorithm that tries all cut and item options in polynomial time.

Uploaded by

Divya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Assignment5 CSE429

The document contains a homework assignment with 3 problems related to algorithms. Problem 1 involves finding the interval of a sequence of integers that maximizes the sum and provides a linear time algorithm. Problem 2 asks to find the longest palindrome subsequence in a given string and provides a dynamic programming solution in O(n^2) time. Problem 3 involves determining the best way to cut a cloth to maximize profits from making products, providing an algorithm that tries all cut and item options in polynomial time.

Uploaded by

Divya Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE421: Design and Analysis of Algorithms November 8, 2020

Homework 5
Anup Rao Due: November 14, 2020

Read the fine print1 . Each problem is worth 10 points:

1. Given a sequence of integers x1 , . . . , xn (possibly including


P negative integers) and an interval
of coordinates I = [i, j], write xI to denote the sum i≤k≤j xk . Give a linear time algorithm
to find the interval that maximizes xI , given the numbers x1 , . . . , xn as input.

Solution. Idea: Iterating through the sequence once to find the max element and set
interval max interval[i, j] where i = j = index of that element. Then iterate through the
sequence again. Set the current sum to the sum of consecutive subsequence where sum is not
negative. Update the maxSum and the corresponding interval if needed.
1
In solving the problem sets, you are allowed to collaborate with fellow students taking the class, but each
submission can have at most one author. If you do collaborate in any way, you must acknowledge, for each
problem, the people you worked with on that problem. The problems have been carefully chosen for their pedagogical
value, and hence might be similar to those given in past offerings of this course at UW, or similar to other courses
at other schools. Using any pre-existing solutions from these sources, for from the web, constitutes a violation of the
academic integrity you are expected to exemplify, and is strictly prohibited. Most of the problems only require one
or two key ideas for their solution. It will help you a lot to spell out these main ideas so that you can get most of the
credit for a problem even if you err on the finer details. Please justify all answers. Some other guidelines for writing
good solutions are here: http://www.cs.washington.edu/education/courses/cse421/08wi/guidelines.pdf.

5-1
Input: A sequence of integers
Result: Interval[i, j] where 1 ≤ i, j ≤ n such that the sum of all the integers in this
interval is maximum
Set i, j, curStart = 1;
Set curSum, maxSum = x1 ;
for integer ` in 1 through n do
if x` > maxSum then
maxSum = x` ;
i = `;
j = `;
end
end
if maxSum ≤ 0 then
output [i, j];
end of algorithm;
end
curSum = 0; maxSum = 0;
for integer s in 1 through n do
curSum += xs ;
if curSum < 0 then
curSum = 0;
curStart = s + 1;
end
else
if curSum > maxSum then
maxSum = curSum;
j = s; i = curStart;
end
end
end
output [i, j];

Runtime: The algorithm goes through the sequence twice. Inside each for loop, all the
operation can be done in constant time. So the algorithm has runtime O(n).

Correctness: CurSum keeps track of the largest interval ending at xs . We look at all positive
intervals, and for each one of them we compare it with maxSum. Therefore the interval we
output is the max interval among all the possible positive intervals. The first iteration sets up
maxSum. It ensure if all elements in the sequence are negative integers, then we will output
the single largest elements since the sum only decline when adding another negative integer
to the sum.

5-2
2. Given a sequence of characters c1 , . . . , cn , we say that a subsequence is a palindrome if it
reads the same forwards and backwards. For example, “a,b,a,c,a,b,a” is a palindrome. Give
an O(n2 ) time algorithm to find the longest palindrome subsequence in the input sequence
c1 , . . . , cn . For example, in the sequence c, l, m, a, l, f, d, c, a, f, m, the longest palindrome
subsequence is m, a, d, a, m. HINT: For i < j, let p(i, j) denote the length of the longest
palindrome in xi , . . . , xj . Express p(i, j) in terms of p(i + 1, j), p(i, j − 1), p(i + 1, j − 1).
Evaluate the values p(i, j) in order of increasing |i − j|.

Solution. The pseudocode is given below.

Input: A list c[1, . . . , n] of characters.


Result: The longest palindrome subsequence of c.
Let P (i, j) be the n × n matrix where the i, jth entry represents the best palindrome found
thus far between ci and cj , inclusive.;
Let P (i, j) be the empty string whenever i > j;
Let P (i, i) be ci for all i;
for length = 2, . . . , n do
for start = 1, . . . , n − length + 1 do
Let end = start + length − 1;
if c[start] == c[end]) then
Let P (start, end) = cstart + P (start + 1, end − 1) + cend ;
end
else
if P (start, end − 1).length > P (start + 1, end) then
Let P (start, end) = P (start, end − 1);
end
else
Let P (start, end) = P (start + 1, end);
end
end
end
end
return P (1, n);

Runtime: The first three assignments, since they are to an array of size n × n and each
assignment occurs only once to a location, each run in O(n2 ). Then, inside the double for
loop, each statement runs in constant time(it is essentially a lookup). Each loop has up to n
values it can take, so the overall double for loop runs in O(n2 ). The return is merely a table
lookup, so it runs in constant time. Thus, the entire algorithm runs in O(n2 ) time.

Proof of correctness: We now are left with the task of proving |OP T (i, j)| = |P (i, j)|,
where |OP T (i, j)| refers to the longest palindrome subsequence from i to j. It is sufficient to

5-3
prove

|OP T (i, j)| ≥ |P (i, j)| (1)


|OP T (i, j)| ≤ |P (i, j)| (2)

To prove equation (1), we use the fact that the palindrome subsequence |P (i, j)| is feasible
solution. Given that it is clear that |OP T (i, j)| ≥ |P (i, j)|.
To prove equation (2), we perform an induction on the length of the sequence, that is an
induction on j − i + 1.
Base Case: j = i+1. The longest palindrome is of length 1 or 2 depending on whether ci = cj .
This implies that |OP T (i, j)| ≤ max{P (i, j − 1), P (i + 1, j), 2 + P (i + 1, j − 1)} = |P (i, j)|.
Induction Hypothesis: |OP T (i, j)| ≤ |P (i, j)| ∀i, j s.t j − i + 1 = k
Consider the solution OP T (i, j) (∀i, j s.t j − i + 1 = k + 1). We have the following cases:

(a) ci ∈ OP T (i, j) ∧ cj ∈
/ OP T (i, j): We can conclude that OP T (i, j) = OP T (i, j − 1).
(b) ci ∈
/ OP T (i, j) ∧ cj ∈ OP T (i, j): We can conclude that OP T (i, j) = OP T (i + 1, j).
(c) ci ∈ OP T (i, j) ∧ cj ∈ OP T (i, j) : We can conclude that OP T (i, j) = ci .OP T (i + 1, j −
1).cj

Now we have, |OP T (i, j)| ≤ max{|OP T (i, j − 1)|, |OP T (i + 1, j)|, 2 + |OP T (i + 1, j − 1)|}.
Now by induction hypothesis we have |OP T (i, j)| ≤ |P (i, j)|.

Common errors. Some solutions returned the length of the longest palindromic subse-
quence instead of the subsequence itself. Some solutions filled the matrix in an incorrect
order, trying to call entries the algorithm hadn’t filled out yet.

5-4
3. You are given a rectangular piece of cloth with dimensions X ×Y , where X and Y are positive
integers, and a list of n products that can be made using the cloth. For each product i you
know that a rectangle of cloth of dimensions ai × bi is needed and that the selling price of the
product is ci Assume the ai , bi and ci are all positive integers. You have a machine that can
cut any rectangular piece of cloth into two pieces either horizontally or vertically. Design an
algorithm that runs in time that is polynomial in X, Y, n and determines the best return on
the X × Y piece of cloth, that is, a strategy for cutting the cloth so that the products made
from the resulting pieces give the maximum sum of selling prices. You are free to make as
many copies of a given product as you wish, or none, if desired.

Solution. The crux of this problem is to identify precisely which actions are available to
the machine:

• Make a vertical cut


• Make a horizontal cut
• Do nothing (and sell the current item)

Input: Dimensions of cloth X,Y, and a list of item values and dimensions.
Result: Best possible value of the cloth
Let cut be an X by Y dimensional array with every entry initialized to 0.
for x ∈ [0, X − 1] do
for y ∈ [0, Y − 1] do
for xcut ∈ [1, x − 1] do
cut[x, y] = max(cut[x, y], cut[xcut , y] + cut[x − xcut , y])
end
for ycut ∈ [1, y − 1] do
cut[x, y] = max(cut[x, y], cut[x, ycut ] + cut[x, y − ycut ])
end
for item ∈ Items do
if itemdimensions == (x, y) then
cut[x, y] = max(cut[x, y], itemvalue )
end
end
end
end
return cut[X − 1, Y − 1]
// Note: This does not actually retrieve the necessary cuts. The cuts could be retrieved by
storing which actions are taken along the way, and storing those actions along side their
corresponding values in cut.

Run time: The outer two loops lead to O(XY ) iterations over the inner most piece, which
does tries every possible vertical cut, horizontal cut, and item. The overall runtime is
O(XY ) · O(X + Y + n) = O(XY (X + Y + n)).

5-5
Proof of correctness: We have to prove that OP T (x, y) = cut(x, y). Here, OP T refers to
the optimum solution to the problem and cut refers to the solution returned by the above
algorithm. It is sufficient to prove

OP T (x, y) ≥ cut(x, y) (3)


OP T (x, y) ≤ cut(x, y) (4)

To prove equation (1), we use the fact that the solution returned by cut(x, y) is a feasible
solution and hence OP T (x, y) can only do better, impying OP T (x, y) ≥ cut(x, y).
We prove equation 2 by induction on the size of xy.
Base Case: (x, y) = (1, 1). It is clear here that OP T (1, 1) could be 0 or the maximum price
given by a product of dimension 1 × 1. In both cases, OP T (1, 1) = cut(1, 1).
Induction Hypothesis: OP T (x0 , y 0 ) ≤ cut(x0 , y 0 ) ∀x0 ≤ x, y 0 ≤ y.
To prove: OP T (x + 1, y) ≤ cut(x + 1, y). Let us consider the optimum solution. It is true
that there exist an i such that the piece given by dimensions (x + 1) × y is cut horizontally
or vertically. This says that OP T (x + 1, y) = OP T (i, y) + OP T (x + 1 − i, y)(when cut
horizontally) or OP T (x + 1, y) = OP T (x + 1, i) + OP T (x + 1, y − i)(when cut vertically).
By induction hypothesis OP T (x0 , y 0 ) ≤ cut(x0 , y 0 ) for all x0 ≤ x and y 0 ≤ y. This implies
OP T (x + 1, y) ≤ cut(x + 1, y). A similar argument would give OP T (x, y + 1) ≤ cut(x, y + 1).
This completes the proof.

Common errors. Some solutions used recursion. Some solutions used a mix of recursion
and storing values; these solutions got full credit, although a fully dp solution is preferred.
Many people did not rotate products. Some people tried cutting various products without
trying every possible cut.

5-6

You might also like