homework4w24
homework4w24
Winter, 2024
Divide and conquer
Due Friday, March 1 , 11:59 PM
Ungraded problems
2. Consider the following problem: You are given a pointer to the root r of
a complete, balanced binary tree, (meaning the sizes of left and right sub-
trees are equal) and where each vertex v has pointers v.lc and v.rc to the
left and right child, and a value V al(v) If v.lc or v.rc have the value NIL
it indicates that v has no child of that type. You wish to find a connected
sub-tree containing the root, that maximizes the total sum of values of
vertices in the sub-tree. (Since some vertices might have negative values,
it might not be optimal to include all of them) Your algorithm can return
just the maximum possible sum, not the sub-tree.
1
5 points Give a recurrence for the time your algorithm takes.
5 points Use the Master theorem to give a time analysis for your algorithm.
5 points Extend your time analysis to the case when the tree is an arbitrary
binary tree, not necessarily balanced.
5. The multi-select problem generalizes the Select problem by asking to, given
an unsorted array A[1..n] of distinct integers, and k ranks 1 ≤ r1 < r2 <
.. < rk ≤ n , ouput the r1 ’st largest, r2 ’nd largest,...and rk ’th largest
elements in the array. Show how, using linear-time select as a subroutine,
we can solve multi-select in O(n log k) time.
6. Give matching (up to constant factors) upper and lower bounds in terms
of the array size n and the number of processors P on the parallel time
complexity in the Valiant model of deciding whether an element x is in a
sorted array y1 ≤ y2 ≤ y3 ≤ ... ≤ yn .
Graded Problems
2
(a) Show that M (T ri(n−2), T ri(n−1), T ri(n))+ = (T ri(n−1), T ri(n), T ri(n+
1))+ where v + is the transpose (to make the dimensions work) and
n ≥ 2. (2 points)
(b) From this, prove that M n (0, 1, 2)+ = (T rin , T rin+1 , T rin+2 )+ (3
points).
(c) Use the above formula to design a divide-and-conquer based algo-
rithm that given n , computes T rin . (10 points)
(d) Give a time analysis of your algorithm if we view arithmetic opera-
tions as constant time. (5 points)
(e) Give a time analysis of your algorithm in terms of bit-operations if
we use grade-school arithmetic. (5 points)
(f) Give a time analysis of your algorithm in terms of bit operations if
we use an O(nlog 2 n) F F T based method to perform integer multi-
plication. (5 points)
2. One vector (x1 , ..xd ) dominates another vector (y1 ., , , yd ) if xi ≥ yi for
all 1 ≤ i ≤ d. In the dominating vector problem, you are given two sets
of d dimensional vectors, X and Y and want to determine whether there
is any x ∈ X and any y ∈ Y so that x dominates y. Give as efficient
an algorithm as you can for the dominating vector problem for any fixed
constant dimension d. (Hint: First try d = 2. Use the result for dimension
d − 1 as a sub-routine in your algorithm for dimension d.)
15 points correctness, 5 points short proof of correctness (only requires a
paragraph or so), 20 points for time analysis and efficiency. There will be
no points for efficiency for O(n2 ) algorithms, where n = |X| + |Y | is the
total number of vectors.
3. Implement two algorithms for multiplying polynomials of degree n − 1,
given as the arrays a[0..n − 1] and b[0..n − 1] of co-efficients. The first
should be a naive FOIL based O(n2 ) algorithm. The second should be a
O(nlog 3 ) divide-and-conquer algorithm based on Karatsuba’s algorithm,
e.g. from the first problem above. Then consider mixes that use the
divide-and-conquer recursion when the input size is at least a threshold
n0 , and uses the brute force algorithm if the input is smaller than n0 .
(Note that this will also apply to recursive calls, so it could change the
time even for sizes n > n0 .)
Try this with a variety of thresholds n0 , and plot the performance of each
version (on a log-log scale). From this data, say whether it is a good idea
to use such a threshold in an implementation and give a good value for
the threshold if so.
(10 points, clear description of experiment performed; 10 points, clear
presentation of data obtained; 10 points, conclusions drawn from the ex-
periment.)