0% found this document useful (0 votes)
44 views

Algo Mod5 Recurrences

The document discusses recurrence relations and the master method for solving recurrences. It defines recurrence relations as equations that describe a function in terms of its values on smaller inputs. The master method provides a framework for solving recurrences of the form T(n) = aT(n/b) + f(n) where a >= 1 and b > 1. It categorizes solutions into three cases depending on whether f(n) grows slower, faster, or at the same rate as nlogba. Regularity conditions are also discussed.

Uploaded by

ISSAM HAMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Algo Mod5 Recurrences

The document discusses recurrence relations and the master method for solving recurrences. It defines recurrence relations as equations that describe a function in terms of its values on smaller inputs. The master method provides a framework for solving recurrences of the form T(n) = aT(n/b) + f(n) where a >= 1 and b > 1. It categorizes solutions into three cases depending on whether f(n) grows slower, faster, or at the same rate as nlogba. Regularity conditions are also discussed.

Uploaded by

ISSAM HAMAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Dr.

ISSAM ALHADID
Modification date 25/2/2019
 Running Time: O(n4)
???

???

 Running Time: ???


 Running time is the Exact # of times sum++ is executed ..
 Inner loop : 1 + 2 + 3+ …. + n2
 (Paper soln.)
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*(n+1)*c5+n*n*c6+n*n*c7+n*c8
The time required for this algorithm is proportional to n2
Recall – nested loops time complexity = N * M
Inner loop : 1 + 2 + 3+ …. + n2
 long power(long x, long n)
 if (n == 0)
◦ return 1;
 else
◦ return x * power(x, n-1);

How many times is this executed?


 T(n) = Time required to solve a problem of
size n
 Recurrence relations are used to determine
the running time of recursive programs –
recurrence relations themselves are recursive.

 T(0) = time to solve problem of size 0 – Base


Case
 T(n) = time to solve problem of size n –
Recursive Case
 long power(long x, long n)
 if (n == 0)
◦ return 1;
 else
◦ return x * power(x, n-1);

 T(0) = c1 for some constant c1


 T(n) = c2 + T(n − 1) for some constant c2

 Soln. ????
 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
(variance ) that describes a function in terms
of its value on smaller inputs.
 For example, we saw that the worst-case
running time T (n) of the MERGE-SORT
procedure could be described by the
recurrence

Where the solution was claimed to be T (n) = (n lg n).


 The following methods are used for solving
recurrences:
◦ substitution method (only the idea)
◦ Iteration method (recursion-tree method)
◦ master method
 In the substitution method, we guess a bound
and then use mathematical induction to prove
our guess correct.
 The substitution method for solving
recurrences entails two steps:
1. Guess the form of the solution.
2. Use mathematical induction to find the constants
and show that the solution works.
Power example

Merge sort
 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.
 Given: a divide and conquer algorithm:
 An algorithm that divides the problem of size
n into a sub-problems, each of size n/b.
 Let the cost of each stage (i.e., the work to
divide the problem + combine solved sub-
problems) be described by the function f(n).

 Then, the Master Theorem gives us a


cookbook for the algorithm’s running time:
if T(n) = aT(n/b) + f(n) where a ≥ 1 & b > 1
(n > n0), when n/ n0 is a power of b
Then:

 Examples….
 if T(n) = aT(n/b) + f(n) where a ≥ 1 & b > 1
T(n) can be bounded asymptotically as follows:

ε: Epsilon
ε >0
 The function divides the problem of size n into a
subproblems each of size n/b.

 The subproblems are solved recursively each in


time T(n/b).

 The cost of dividing the problem and combining


the results of the subproblems is described by
the f(n).
 We interpret n/b to mean either ⎡n/b⎤ or ⎣n/b⎦,
which does not affect the asymptotic behaviour
of the recurrence.
 In each of the three cases, we are comparing
the largest term in f(n) with nlogba , the solution
to the recurrence is determined by the larger of
the two functions.
◦ In case 1, if the function nlogba is the larger, then the
solution T(n) = Θ(nlogba ).
◦ In case 3, if the function f(n) is the larger, then the
solution is T(n) = Θ(f(n)).
◦ In case 2, if the two functions are the same size, then
the solution is T(n) = Θ(nlogba lg n) = Θ(f(n) lg n).
◦  two functions: T(n) = aT(n/b) + f(n)
 In case 1, not only must f(n) be smaller than
nlogba, it must be polynomially smaller. That is
f(n) must be asymptotically smaller than nlogba
by a factor of nε for some constant ε > 0.

 In case 3, not only must f(n) be larger than


nlogba, it must be polynomially larger ; That is
f(n) must be asymptotically Larger than nlogba
by a factor of nε for some constant ε > 0, in
addition satisfy the “regularity” condition that:
a f(n/b) ≤ c f(n). – (you will understand
using an Example)
 It is important to realize that the three cases do
not cover all the possibilities for f(n).
◦ There is a gap between cases 1 and 2 when f(n) is smaller
than nlogba but not polynomially smaller.

◦ polynomially smaller : The ratio f(n)/nlogba is asymptotically


less than nε for any positive constant ε >0.

◦ There is a gap between cases 2 and 3 when f(n) is larger


than nlogba but not polynomially larger and satisfy the
“regularity” condition .

 polynomially larger: The ratio f(n)/nlogba is asymptotically


greater than nε for any positive constant ε >0

◦If f(n) falls into one of these gaps, the master method
cannot be used to solve the recurrence.
 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.
 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
 Example: T(n) = T(2n/3)+T(n/3)+n

The master method works only for following type of


recurrences:
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1

You might also like