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

L

Divide and Conquer

By

Prasanta K. Jana, IEEE Senior Member

Department of Computer Science and Engineering


Indian Institute of Technology (ISM), Dhanbad
E-mail: prasantajana@iitism.ac.in
Working Principle:
Given a problem of size n (inputs),
1) Split the inputs into k distinct subsets, 1< k <= n. yielding k sub-problems.
2) Solve these k sub-problems.
3) Combine the sub-solutions to obtain the final solution of the whole problem

Control Abstraction:

Time Complexity:

Time Complexity of many D&C algorithms can also be given by recurrence of the form:

Where,
 a and b are two constants
 T(1) is assumed to be known and n is a power of b, i.e., nb.

Exercise: Solve the above equation for a =2, b =2, T(1) = 2 and f(n) = n
So we are supposed to solve:
T(n) = 2T(n/2) + n, given T(1) = 2.

Hints: Use method of substitution


2. Solve by changing variables:
3. Solve by Recursion Tree
FINDING MAXIMUM AND MINIMUM OF N ELEMENTS
Recursive Algorithm
The procedure is initially invoked by MaxMin(1, n, x, y)
Hand Simulation:
Time complexity:

Solution: Assume n is a power of 2.

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

= 2i T(n/2i) + 2i + 2i-1 + … 4 + 2

= 2i T(n/2i) + 2( 1+ 2+ 22 + … + 2i-1)

i i
2i  1
= 2 T(n/2 ) + 2.
2 1

n
Let  2 , then
2i

T(n) = n/2 T(2) + 2. (n/2-1) = n/2 + n-2

3n
T(n) = 2
2
Order Statistics:

 The ith order statistic of a set of n elements is the ith smallest element

 For example, the minimum of a set of elements is the first order statistic (i = l).

 and the maximum is the nth order statistic (i = n).

The problem of selecting the ith order statistic from a set of n distinct numbers is as
follows:

Input: A set A of n (distinct) numbers and an integer i, with 1 ≤ i ≤ n.

Output: The element x ⋲ A that is larger than exactly i - 1 other elements of A.


Exercise Problems:

1. Solve by method of substitution:


i. T(n) = T(n-1) + 1 and T(1) = c
4
n
ii. T ( n)  T    n 3
3

2. Solve by changing variables:

T ( n)  2T  n  1
3. Solve by recursion tree:

n
T (n)  4T      n
2
4. Find asymptotic time complexity in θ-notation for the given exponential function

f(n)=3*2" + 4n2 +5n+2


The master method for solving recurrences
 The master method provides a “cookbook" method for solving recurrences of the form

T(n) = aT (n/b) +f(n)

where a land b > T are constants and f(n) is an asymptotically poSu function.

 To use the master method, you will need to memorize three cases.

 But then you will be able to solve many recurrences quite easily, often without pencil and
paper.

Master theorem:
Let a > l and b > 1 be constants, let f(n) be a function, and let T (n) be defined
on the nonnegative integers by the recurrence:

T(n) = aTn/b) + f(n),


Examples:
Strassen’s Algorithm
Let A and B be two n×n matrices

The product matrix C= AB is also an n×n matrix

for 1≤ i ≤ j ≤ n

Conventional algorithm: takes O(n3) time.

Straight Forward Divide-and-Conquer Strategy:

Assumption n = 2k. If not fill with many zeroes and make it a power of 2.

n n
Divide A and B with four  matrices and then form the product as follows:
2 2

If n = 2, then formulas (3.11) and (3.12) are computed using a multiplication operation for the
elements of A and B directly.

Time Complexity:
n n n n
Eqn. 3.12 requires eight  matrix multiplications and four  matrix additions.
2 2 2 2
n n
Since two  matrices can be added in O(n2) time, then we have
2 2
Solving T(n) = O(n3) (Task 1)

So, no improvement.

Strassen’s matrix multiplications:

n n
Strategy: Decrease no. of  matrix multiplications and compensate it by increasing
2 2
n n
 matrix additions.
2 2

So, we reorganize Eqn. (3.12)

as follows:

Time Complexity:

Eqns. 3.13 and 1.14 require al together:

n n n n
Seven  matrix multiplications and eighteen  matrix additions. Thus we have
2 2 2 2

Solving we obtain:
T (n)  O(nlog2 7 )  O(n2.81 ) (Task 2)
Ex 1. Verify by hand that Equations 3.13 and 3.14 yield the correct values for C11, C12, C21,
and C22.

Ex 2.

Ex 3.

You might also like