HW3 Sol
HW3 Sol
Homework 3
Instructor: Shi Li Deadline: 3/28/2021
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 ).
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).
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.