0% found this document useful (0 votes)
601 views4 pages

HW3 Sol

This document contains the instructions for homework 3 in an algorithm analysis and design course. It includes 3 problems to solve involving analyzing recurrences using the master theorem, designing a divide-and-conquer algorithm to count strong inversions in an array, and designing a divide-and-conquer algorithm to find the largest rectangle in a histogram. Pseudocode is provided for the algorithms.

Uploaded by

harsh
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)
601 views4 pages

HW3 Sol

This document contains the instructions for homework 3 in an algorithm analysis and design course. It includes 3 problems to solve involving analyzing recurrences using the master theorem, designing a divide-and-conquer algorithm to count strong inversions in an array, and designing a divide-and-conquer algorithm to find the largest rectangle in a histogram. Pseudocode is provided for the algorithms.

Uploaded by

harsh
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/ 4

CSE 431/531: Algorithm Analysis and Design Spring 2021

Homework 3
Instructor: Shi Li Deadline: 3/28/2021

Your Name: Your Student ID:

Problems 1 2 3 Total
Max. Score 12 14 14 40
Your Score

Problem 1 For each of the following recurrences, use the master theorem to give the
tight asymptotic upper bound.
1. T (n) = 4T (n/2) + O(n2 ). T (n) = O(n2 log n).
2. T (n) = 5T (n/2) + O(n2 ). T (n) = O(nlog2 5 ).
3. T (n) = T (n/3) + O(n). T (n) = O(n).
4. T (n) = 2T (n/2) + O(n2 ). T (n) = O(n2 ).

Problem 2 We consider the following problem of counting stronger inversions. Given


an array A of n positive integers, a pair i, j ∈ {1, 2, 3, · · · , n} of indices is called a strong
inversion if i < j and A[i] > 2A[j]. The goal of the problem is to count the number of
strong inversions for a given array A.
Give an O(n log n)-time divide-and-conquer algorithm to solve the problem.

We shall modify the divide-and-conquer algorithm for counting inversions slightly.


The only thing that needs to be changed is the procedure merge-and-count(B, C, n1 , n2 ).
As in the algorithm for counting inversions, we are given two sorted arrays: B of length
n1 , and C of length n2 . But now we need to count the number of strong inversions
between B and C, and merge B and C. The two tasks are performed using two different
while loops: in the first while loop, we count the number of strong inversions between B
and C, that is, the number of pairs (i, j) such that B[i] > 2 × C[j]. In the second while
loop, we merge B and C into a sorted array. The procedure is given by the following
pseudo-code:

1
Algorithm 1 merge-and-count(B, C, n1 , n2 )
1: count ← 0, A ← []
2: i ← 1; j ← 1
3: while i ≤ n1 or j ≤ n2 do
4: if j > n2 or (i ≤ n1 and B[i] ≤ 2 × C[j]) then
5: i←i+1
6: count ← count + (j − 1)
7: else
8: j ←j+1
9: i ← 1; j ← 1
10: while i ≤ n1 or j ≤ n2 do
11: if j > n2 or (i ≤ n1 and B[i] ≤ C[j]) then
12: append B[i] to A; i ← i + 1
13: else
14: append C[j] to A; j ← j + 1
15: return (A, count)

The recurrence for the running time is still T (n) = 2T (n/2) + O(n). So, the running
time of the algorithm is O(n log n).

Problem 3 This problem asks for the


largest rectangle in a histogram for an ar- 20
ray of n non-negative integers. For example,
if the input is (3, 5, 10, 11, 20, 4, 8, 10), then
15
the largest rectangle has size 30, with height
10 and width 3, covering column 3 to column
5. (See Figure 1). 10
Design an O(n log n)-time divide-and-
conquer algorithm to solve the problem.
5

Figure 1: The largest rectangle of in the


histogram is given by the shaded area.

Algorithm 2 Main Algorithm . A and n are global variables.


1: (s, a, b, c) ← largest-rectangle(1, n)
2: print “maximum area is s, rectangle covers columns a to b, and its height is c.”

2
Algorithm 3 largest-rectangle(`, r)
1: if ` = r then return (A[`], `, r, A[`])
2: m ← b(` + r)/2c
3: (sL , aL , bL , cL ) ← largest-rectangle(`, m)
4: (sR , aR , bR , cR ) ← largest-rectangle(m + 1, r)
5: (sM , aM , bM , cM ) ← largest-rectangle-between(`, m, r)
6: p∗ ← arg maxp∈{L,R,M} sp , return (sp∗ , ap∗ , bp∗ , cp∗ )

Algorithm 4 largest-rectangle-between(`, m, r)
1: A0 [m] ← min {A[m], A[m + 1]}, A0 [m + 1] ← A0 [m]
2: for i ← m − 1 down to ` do: A0 [i] ← min {A0 [i + 1], A[i]}
3: for i ← m + 2 to r do: A0 [i] ← min {A0 [i − 1], A[i]}
4: i ← m, j ← m + 1, h ← A0 [m], s ← 0
5: while true do
6: if (j − i + 1) × h > s then
7: s ← (j − i + 1) × h, a ← i, b ← j, c ← h
8: if i = ` and j = r then break
9: if j = r or A0 [i − 1] > A0 [j + 1] then
10: i ← i − 1, h ← A0 [i]
11: else
12: j ← j + 1, h ← A0 [j]
13: return (s, a, b, c)

The combine step is given in Algorithm 4, which runs in O(n0 ) time, where n0 = r−`+1
is the length of the sub-array considered. So, the recurrence for the running time of the
algorithm is T (n) = 2T (n/2) + O(n), which gives a running time of O(n log n).
If you have the pseudo-code, and say that running time is O(n log n), you will get a
full score. The explanations in this solution are not necessary. It is only to make sure
that you understand the algorithm. As designing the combine step is a bit hard, having
the pseudo-code itself already shows that you know how to solve the problem.
The combine step of the algorithm is given by the largest-rectangle-between procedure
(Algorithm 4). We have 1 ≤ ` ≤ m < r ≤ n, and the procedure will find the largest
rectangle in the histogram between column ` to r, that covers columns m and m + 1. We
define an array A0 [`..r] as follows:
(
min {A[i], A[i + 1], A[i + 2], · · · , A[m + 1]} if i ≤ m
A0 [i] = .
min {A[m], A[m + 1], A[m + 2], · · · , A[i]} if i ≥ m + 1

It is not hard to prove that the largest rectangle containing column m and m + 1
in the histogram for A is the same as that for A0 . A0 has a “mountain” shape: A0 [`] ≤
A0 [` + 1] ≤ A0 [` + 2] ≤ · · · ≤ A0 [m] = A0 [m + 1] ≥ A0 [m + 1] ≥ A0 [m + 3] ≥ · · · ≥ A0 [r]. For
every height, we want to compute the maximum width of a rectangle with that height.
To do so, we maintain the left-side i, right-side j, and the height h of the rectangle. Each

3
iteration, we need to decrease h to make the rectangle wider. If A0 [i − 1] > A0 [j + 1], we
decrease i by 1 and change h to A0 [i]; otherwise, we increase j by 1 and change h to A0 [j].
s will be the maximum area of all the rectangles we enumerated.

You might also like