Beruflich Dokumente
Kultur Dokumente
Recurrences
Dr. ISSAM ALHADID
Modification date 25/2/2019
Algorithm Analysis
???
???
◦ return 1;
else
◦ return x * power(x, n-1);
Recursive Case
Recursive Functions
long power(long x, long n)
if (n == 0)
◦ return 1;
else
◦ return x * power(x, n-1);
Soln. ????
Solving Recurrences
when an algorithm contains a recursive call to
itself, its running time can often be described
by a recurrence.
A recurrence is an equation or inequality
Merge sort
Iteration Method
Iteration method:
◦ Expand the recurrence k times.
◦ Stop the recurrence at smallest value (0 or
1).
◦ Work some algebra to express as a
summation and solve the summation, if
any exist.
Master Theorem
Given: a divide and conquer algorithm:
An algorithm that divides the problem of size
Examples….
Master Theorem (Book Version)
if T(n) = aT(n/b) + f(n) where a ≥ 1 & b > 1
T(n) can be bounded asymptotically as follows:
ε: Epsilon
ε >0
Master Theorem
The function divides the problem of size n into a
subproblems each of size n/b.
◦ There is a gap between cases 2 and 3 when f(n) is larger than n logba but
not polynomially larger and satisfy the “regularity” condition .
◦If f(n) falls into one of these gaps, the master method cannot be
used to solve the recurrence.
Understanding Master
Theorem
regulatory condition : is used to make sure that
the parent does at least as much as the children.
This says that f(n) (the amount of work done in
the root) needs to be at least as big as the
sum of the work done in the lower levels
But what if the function didn't fulfill the
regularity condition?
Ex. cos(n)*cos (n) instead of n*n?
Then the work done on the lower levels might
be bigger than the work done in the root so
you are not certain that the root dominates.
Using Master Method
Examples: Case 1
T(n) = 9T(n/3) + n
T(n) = T(2n/3) + 1
T(n) = 3T(n/4) + n lg n
T(n) = 2T(n/2) + n lg n
Why Master theorem cannot be
applied here !?
Example: T(n) = T(2n/3)+T(n/3)+n