Data_Structures_and_Algorithms-1 (1)
Data_Structures_and_Algorithms-1 (1)
We also analysed the worst case running time (clock cycles) needed
to solve the problem. Every operation is free except the basic
operation required to compare to elements which is assumed to take
1 clock cycle irrespective of the size/nature of the elements.
using some guess work and induction, but guessing cannot be used
at all times.
All recurrences require a base case to fall back on. These base cases
are usually assumed to be of the form T (k) = k 0 where k and k 0
are constants. By constants we refer to finite numbers. In most
cases if we are dealing with a constant size input, the problem is
considered to be solvable in constant time.
So most of the times, the base cases are usually not given explicitly.
In other words, for most problems that we will be dealing with it is
assumed that
T (O(1)) = O(1)
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
This method does not require us to guess the answer, but it may
require a little algebra.
T (n) = 2 ∗ (T (n/2)) + 7n + 34
This could be interpreted as
Solving the problem for an instance of size n, involves solving two
instances of the same problem each of size n/2 and an additional
work of 7n + 34 is required just to put together the two smaller
solutions to get the solution for the original problem.
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
n
T (n) = 2T + [7n + 34] (1)
2
n n
= 2[2T 2 + 7 + 34] + [7n + 34]
2 2
n
= 4T ( 2 ) + [7n ∗ (1 + 1) + 34(1 + 2)] (2)
2
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
!
n n
T (n) = 4 2T ( 3 ) + 7( ) + 34 + [7n(1 + 1) + 34(1 + 2)]
2 4
!
n
= 8T + [7n(1 + 1 + 1) + 34(1 + 2 + 4)]
23
!
n
= 23 T + [7n(3) + 34(1 + 2 + 22 )]
23
!
n
= 23 T + [7n(3) + 34(23 − 1)] (3)
23
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
Substitution method
In such a scenario, we may guess the solution and try to confirm its
correctness by using induction.
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
We seek to prove that T (n) ≤ cn2 for some constant c, and for all
values of n > n0 where n0 is some threshold which we shall try to
find.
Lets assume that for inputs of size less than n − 1, this argument is
true.
Thus, there exists a constant c, such that T (n) ≤ cn, for all input
sizes beyond a threshold.
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
I Case 1:
If f (n) = O(nlogb a− ) for some > 0, then T (n) = Θ(nlogb a )
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
I Case 1:
If f (n) = O(nlogb a− ) for some > 0, then T (n) = Θ(nlogb a )
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
I Case 1:
If f (n) = O(nlogb a− ) for some > 0, then T (n) = Θ(nlogb a )
I Case 2:
If f (n) = Θ(nlogb a ) then T (n) = Θ(nlogb a lg n)
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
I Case 1:
If f (n) = O(nlogb a− ) for some > 0, then T (n) = Θ(nlogb a )
I Case 2:
If f (n) = Θ(nlogb a ) then T (n) = Θ(nlogb a lg n)
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
I Case 1:
If f (n) = O(nlogb a− ) for some > 0, then T (n) = Θ(nlogb a )
I Case 2:
If f (n) = Θ(nlogb a ) then T (n) = Θ(nlogb a lg n)
I Case 3:
If f (n) = Ω(nlogb a+ ), for some > 0 and if a ∗ f ( bn ) ≤ c ∗ f (n)
for some constant c < 1 and all sufficiently large n, then
T (n) = Θ(f (n)).
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method
First we verify that the given recurrence is in the desired format i.e
First we verify that the given recurrence is in the desired format i.e
T (n) = 2T (n/2) + n2
T (n) = 2n T (n/2) + nn
This recurrence cannot be solved using master theorem, because the
value of a (i.e 2n ) happens to be dependent on n and hence its not
a constant.
Few Groundrules
Recap
Recursion Tree/Iteration Method
Functions and Recurrences
Substitution method
Methods to solve Recurrences
Master Method