Professional Documents
Culture Documents
Lab 2
Lab 2
Course: Algorithms
Lab #2
Algorithm Analysis
Divide and Conquer
This lab contains two parts. The first part continues examples on the analysis of algorithms while the
second introduces divide and conquer programming paradigm. Finally, Student will try to solve a certain
problem as lab work using java or c++.
Problem 1:
Consider the recurrence relation T(n) = 3T(n/4) + cn2 for some constant c. We assume that n is an exact
power of 4. In the recursion-tree method we expand T(n) into a tree:
Applying T(n) = 3T(n/4) + cn2 to T(n/4) leads to T(n/4) = 3T(n/16) + c(n/4) 2, expanding the leaves:
1
Applying T(n) = 3T(n/4) + cn2 to T(n/16) leads to T(n/16) = 3T(n/64) + c(n/16) 2, expanding the leaves:
T(n) = cn2 + 3/16cn2 + (3/16)2 cn2 + ··· = cn2 ( 1 + 3/16+ (3/16)2 + ··· )
For n = 4k , k = log4(n), we have:
2
So the explicit sum is
Applying
To get rid of log4(n) term for simplification, we consider the sum to infinity, not only to log 4n. Summation of
a geometric sum from zero to infinity = a / (1 - r). recall that
Problem 2:
Problem 3:
1. Breaking it into subproblems that are themselves smaller instances of the same type of problem
2. Recursively solving these subproblems
3. Appropriately combining their answers
4
Example Problem : Large Numbers Multiplication (Gauss Method):
With divide-and-conquer multiplication, we split each of the numbers into two halves, each with n/2 digits.
I'll call the two numbers we're trying to multiply a and b, with the two halves of a being aL (the left or upper
half) and aR (the right or lower half) and the two halves of b being bL and bR.
Basically, we can multiply these two numbers as follows.
That image is just a picture of the idea, but more formally, the derivation works as follows:
Thus, in order to multiply a pair of n-digit numbers, we can recursively multiply four pairs of n/2-digit
numbers. The rest of the operations involved are all O(n) operations. (Multiplying by 10nmay look like a
multiplication (and hence not O(n)), but really it's just a matter of appending n zeroes onto the number,
which takes O(n) time.)
That's fine as far as it goes, but it turns out that it's not far enough: If you write down the recurrence and
solve it, it turns out to solve to O(n2), which is what we had from grade school. And this algorithm is much
more complicated. Not a very encouraging result.
But there turns out to be a very clever approach, we permits us to reduce the number of n/2-digit
multiplications from four to three! This clever idea yields a better result. The idea works as follows: We're
trying to compute
What we'll do is compute the following three products using recursive calls.
x1 = aL bL
x2 = aR bR
x3 = (aL + aR) (bL + bR)
These have all the information that we want, since the following is true.
And we already reason that this last is equal to the product of a and b.
5
Writing the recurrence relation of this algorithm, T(n) = 3T(n/2) + O(n) compared to the first one which takes
T(n) = 4T(n/2) + O(n).
Using master method we can easily infer that T(n) = O(n log2 3 ) which approximates to O(n1.59).
Hint: you can obtain left and right parts easily using shift operators, or division and mode operations.
These practice problems are intended for student practice at home and are not to be submitted as
homework nor discussed in labs.
6
2) You are given an array of n elements, and you notice that some of the elements are duplicates; that is,
they appear more than once in the array. Show how to remove all duplicates from the array in time O(n
log n).
3) You are given an n by n grid of distinct numbers. A number is a local minimum if it is smaller than all of
its neighbors. (A neighbor of a number is one immediately above, below, to the left, or the right. Most
numbers have four neighbors; numbers on the side have three; the four corners have two.) Use the
divide-and-conquer algorithm design paradigm to compute a local minimum with only O(n)
comparisons between pairs of numbers. (Note: since there are n2 numbers in the input, you cannot
afford to look at all of them. Hint: Think about what types of recurrences would give you the desired
upper bound.).
Good Luck