Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Cairo University

Faculty of Computers &Information


Department of Computer Science

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++.

Part #1: Analysis.

Problem 1:

We will consider here more problems on recurrence trees analysis method.

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:

We sum the cost at each level of the tree:

T(n) = cn2 + 3/16cn2 + (3/16)2 cn2 + ··· = cn2 ( 1 + 3/16+ (3/16)2 + ··· )
For n = 4k , k = log4(n), we have:

Consider a finite sum first:

To find an explicit form of the solution we do

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

Which follows that the relation is O(n2)

Problem 2:

T(n) = T(n/3) + T(2n/3) + cn.

Problem 3:

T(n) = T(n/4) + T(n/2) + n2


3
Problem 4:

T(n) = 2T(n/2) + n2.

Similar to above, this will yield a performance of O(n2)

Part#2: Divide and conquer:

The divide-and-conquer strategy solves a problem by :

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:

ab = (aL 10n/2 + aR) (bL 10n/2 + bR)


= aL bL 10n + aL bR 10n/2 + aR bL 10n/2 + aR bR
= aL bL 10n + (aL bR + aR bL) 10n/2 + aR bR

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

aL bL 10n + (aL bR + aR bL) 10n/2 + aR bR

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.

x1 10n + (x3 - x1 - x2) 10n/2 + x2


= aL bL 10n + ((aL bL + aL bR + aR bL + aR bR) - aL bL - aR bR) 10n/2 + aR bR
= aL bL 10n + (aL bR + aR bL) 10n/2 + aR bR

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).

Part#4: Lab work

Implement Gauss algorithm for multiplication of two real numbers.

Hint: you can obtain left and right parts easily using shift operators, or division and mode operations.

Part #5: Exercises

These practice problems are intended for student practice at home and are not to be submitted as
homework nor discussed in labs.

1) Suppose you are choosing between the following three algorithms:


1. Algorithm A solves problems by dividing them into five subproblems of half the size, recursively
solving each subproblem, and then combining the solutions in linear time.
2. Algorithm B solves problems of size n by recursively solving two subproblems of size n − 1 and then
combining the solutions in constant time.
3. Algorithm C solves problems of size n by dividing them into nine subproblems of size n/3,
recursively solving each subproblem, and then combining the solutions in O(n 2 ) time.
What are the running times of each of these algorithms (in big-O notation), and which would you
choose?

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

You might also like