Practice Sheet Divide and Conquer
Practice Sheet Divide and Conquer
Practice Sheet Divide and Conquer
• This practice sheet has all the problems from the Divide and Conquer Paradigm. We
shall devote 5 lectures to cover this paradigm.
• Make sincere attempts to solve the problems. Do not postpone the problems to the
last day before Quiz or mid-semester exam.
• Feel free to contact the instructor if you face any difficulty in these problems.
1
Miscellaneous
1. Counting Double-Inversions
You are given an array A storing n numbers. A pair (i, j) with 0 ≤ i < j ≤ n − 1
is said to be a double-inversion if A[i] > 2A[j]. Design and analyze an O(n log n)
time algorithm based on divide and conquer paradigm to compute the total number
of double-inversions in A.
Geometric Problems
(a) Write a neat and complete pseudocode of the algorithm we discussed in the
lecture (without referring to the slides).
(b) The algorithm we discussed in the lecture assumed that the median line passes
through only one point. What if the median line passes through multiple points
? In particular, how will you ensure that the left half as well as right half has
dn/2e points given that the median line passes through multiple points ?
(c) Is the following statement about the algorithm discussed in the class true ?
2
2. Non-dominated points
In the lectures, we discuss a divide and conquer based algorithm to compute non-
dominated points. However, there is an alternate and equally simple algorithm for
this problem that runs in O(n log n) time. Provide complete description and analysis
of this algorithm.
Hint: The algorithm makes use of sorting as the first step.
4. Convex Hull
The following 2 problems should be attempted after we have discussed the algorithm
for Convex Hull in the class.
(a) In a lecture, we discussed an O(n log2 n) time algorithm to compute the convex
hull of a given set of n points in a plane. If we can improve the time complexity
of the Conquer step of this algorithm to linear, this will result in an O(n log n)
time algorithm for convex hull. You have to modify the current Conquer step so
that it takes at most linear time. You must provide a complete analysis of the
modified Conquer step as well.
(b) An application in computer graphics
There is a set S of n lines: L1 , L2 ,..., Ln . For any 1 ≤ i ≤ n, Line Li is described
by two parameters mi and ci such that the equation of line Li is y = mi x + ci .
Each mi is a positive number. As input, you are given {(mi , ci )|1 ≤ i ≤ n}.
You may assume that no three lines in S pass through the same point. A line
from S is said to be visible from above if there is some real number ai such
that the point through which Li passes at x = ai is the highest (has maximum
y-coordinate) among all the points through which other lines of set S pass at
x = ai . Design an O(n log n) time algorithm to compute all the visible lines from
the set S. Hint: This problem will test your ability to divide the problem. What
seems to be the most natural parameter to be used for dividing the n lines ?
The following problems should be attempted after we have discussed O(n log n) time algo-
rithm to multiply 2 polynomials.
3
2. Toeplitz Matrix
A Toeplitz matrix is an n × n matrix A = (ai,j ) such that ai,j = ai−1,j−1 for i =
2, 3, . . . , n and j = 2, 3, . . . , n.
(a) Is the sum of two Toeplitz matrix necessarily Toeplitz ? What about the product
?
(b) Describe how to represent a Toeplitz matrix so that two n × n matrices can be
added in O(n) time.
(c) Give an O(n log n) time algorithm for multiplying an n × n Toeplitz matrix by a
vector of length n. Use your representation from part (b).
Hint: Using part (b), express this problem as a multiplication of two suitably defined
polynomials.
3. Cartesian sum
Consider two sets A and B, each having n integers in the range from 0 to 10n. We
wish to compute the Cartesian sum of A and B, defined by
C = {x + y : x ∈ A and y ∈ B}.
Note that the integers in C are in the range from 0 to 20n. We want to find the
elements of C and the number of times each element of C is realized as a sum of
elements in A and B. Design an O(n log n) time algorithm to achieve this objective.
They have written the following simple program to compute Fj for all j.
4
For j = 1,2,...,n
Initialize Fj to 0
For i = 1, 2, ..., n
If i < j then
Cqi qj
Add (j−i) 2 to Fj
Endif
Endfor
Output Fj
Endfor
It is not hard to observe that the running time of the above algorithm is Θ(n2 ). The
trouble is, for the large values of n they are working with, the program takes several
minuted to run. On the other hand, their experimental setup is optimized so that
they can throw down n particles, perform the measurements, and be ready to handle
n more particles within a few seconds. So they would really like it if there were a
way to compute all the forces Fj much more quickly, so as to keep up with the rate
of the experiment. Help them out by designing an algorithm which is much faster
than O(n2 ). Hint: Express this problem as a multiplication of a few suitably defined
polynomials.