Professional Documents
Culture Documents
Divide and Conquer
Divide and Conquer
By
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.
= 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
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).
The problem of selecting the ith order statistic from a set of n distinct numbers is as
follows:
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
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:
for 1≤ i ≤ j ≤ n
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.
n n
Strategy: Decrease no. of matrix multiplications and compensate it by increasing
2 2
n n
matrix additions.
2 2
as follows:
Time Complexity:
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.