AA-WK-6-Lec-11-12

advertisement
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
(Week 6) Lectures 11 & 12
Objectives:
Learning objectives of these lectures are
ο‚·
Recursion
o Recursive Definition
o Recursive Algorithm
o Recurrence Relation
ο‚·
Different Methods to Compute Time Complexity
o Backward Substitution Method
o Forward Substitution Method
o Recursion Tree Method
o Telescoping Sum Method
o Master Theorem
o Induction Method
Text Book & Resources:
1. Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
and Clifford Stein, The MIT Press; 3rdEdition (2009). ISBN-10: 0262033844
2. Introduction to the Design and Analysis of Algorithms by Anany Levitin, Addison Wesley;
2ndEdition (2006). ISBN-10: 0321358287
3. Algorithms in C++ by Robert Sedgewick (1999). ASIN: B006UR4BJS
4. Algorithms in Java by Robert Sedgewick, Addison-Wesley Professional; 3rd Edition(2002).
ISBN-10: 0201361205
Lectures-11-12
Page 1 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
What is Recursion?
Recursion in computer science is a method where the solution to a problem depends on solutions
to smaller instances of the same problem.
Example:
We want to compute sum of first 100 numbers like
1+2+3+4+5+6+……..+99+100
Here total sum can be computed if sum for first 99 numbers are computed and then 100 is added
to the result.
Then to compute 99 we have to sum of 98 numbers, for 98 we have to compute 97 and so on.
Here solution of problem depends on solution of sub problems.
Solving problem in this fashion is referred as recursion.
Example
We want to compute sum of squares of first 100 numbers like
12+22+32+42+52+62+……..+992+1002
Similar solution can be computed as explained above.
What is Recursive Algorithm?
A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values,
and which obtains the result for the current input by applying simple operations to the returned
value for the smaller (or simpler) input.
More generally if a problem can be solved utilizing solutions to smaller versions of the same
problem, and the smaller versions reduce to easily solvable cases, then one can use a recursive
algorithm to solve that problem. For example, the elements of a recursively defined set, or the
value of a recursively defined function can be obtained by a recursive algorithm.
If a set or a function is defined recursively, then a recursive algorithm to compute its members or
values mirrors the definition. Initial steps of the recursive algorithm correspond to the basis
clause of the recursive definition and they identify the basic elements. They are then followed by
steps corresponding to the inductive clause, which reduce the computation for an element of one
generation to that of elements of the immediately preceding generation.
In general, recursive computer programs require more memory and computation compared with
iterative algorithms, but they are simpler and for many cases natural way of thinking about the
problem.
Example 1: Algorithm for finding the k-th even natural number
Note here that this can be solved very easily by simply outputting 2*(k - 1) for a given k . The
purpose here, however, is to illustrate the basic idea of recursion rather than solving the
problem.
Lectures-11-12
Page 2 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Algorithm 1: Even(positive integer k)
Input: k , a positive integer
Output: k-th even natural number (the first even being 0)
Algorithm:
if k = 1, then return 0;
else return Even(k-1) + 2 .
Here the computation of Even(k) is reduced to that of Even for a smaller input value, that
is Even(k-1). Even(k) eventually becomes Even(1) which is 0 by the first line. For example, to
compute Even(3), Algorithm Even(k) is
called
with k = 2.
In
the
computation
of Even(2), Algorithm Even(k) is called with k = 1. Since Even(1) = 0, 0 is returned for the
computation of Even(2), and Even(2) = Even(1) + 2 = 2 is obtained. This value 2 for Even(2) is
now returned to the computation of Even(3), and Even(3) = Even(2) + 2 = 4 is obtained.
As can be seen by comparing this algorithm with the recursive definition of the set of
nonnegative even numbers, the first line of the algorithm corresponds to the basis clause of the
definition, and the second line corresponds to the inductive clause. By way of comparison, let us
see how the same problem can be solved by an iterative algorithm.
Algorithm 1-a: Even(positive integer k)
Input: k, a positive integer
Output: k-th even natural number (the first even being 0)
Algorithm:
int i, even;
i := 1; even := 0;
while( i < k ) {
even := even + 2;
i := i + 1;
}
return even .
Example 2: Recursive Algorithm for computing the k-th power of 2
Algorithm 2: Power_of_2(natural number k)
Input: k , a natural number
Output: k-th power of 2
Algorithm: Power_of_2 (k)
if k = 0, then return 1;
else return 2*Power_of_2(k - 1) .
Lectures-11-12
Page 3 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
By way of comparison, let us see how the same problem can be solved by an iterative algorithm.
Algorithm 2-a Power_of_2(natural number k)
Input: k , a natural number
Output: k-th power of 2
Algorithm:
int i, power;
i := 0;
power := 1;
while( i < k ) {
power := power * 2;
i := i + 1;
}
return power
The next example does not have any corresponding recursive definition. It shows a recursive
way of solving a problem.
Example 3: Recursive Algorithm for Sequential Search
Algorithm 3 SeqSearch(L, i, j, x)
Input: L is an array, i and j are positive integers, i
j, and x is the key to be searched for in L.
Output: If x is in L between indexes i and j, then output its index, else output 0.
Algorithm:
if i
j , then
{
if L(i) = x, then return i ;
else return SeqSearch(L, i+1, j, x)
}
else return 0.
Recursive algorithms can also be used to test objects for membership in a set.
Lectures-11-12
Page 4 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Example 4: Algorithm for testing whether or not a number x is a natural number
Algorithm 4 Natural(a number x)
Input: A number x
Output: "Yes" if x is a natural number, else "No"
Algorithm:
if x < 0, then return "No"
else
if x = 0, then return "Yes"
else return Natural( x - 1 )
Recursive Functions
Recursive function is a function that makes a call to itself. To prevent infinite recursion,
you need an if-else statement (of some sort) where one branch makes a recursive call, and the
other branch does not. The branch without a recursive call is usually the base case (base cases do
not make recursive calls to the function).
Example:
Recursive function to compute sum of first N numbers can be written as
Algorithm: Sum(N)
Input: N
Output: sum from 1 to N
Procedure:
SumοƒŸ0
If(N=1)
Return 1
Else
Sum(N-1) +N
Here we know the solution of 1, so it will be the base case.
Recursive Definition
A mathematical or formal representation of recursive function is referred as recursive
definition.
A recursive definition of a function defines values of the functions for some inputs in terms of
the values of the same function for other inputs. A definition is written as,
π΅π‘Žπ‘ π‘’ πΆπ‘Žπ‘ π‘’,
π΅π‘Žπ‘ π‘’ πΆπ‘œπ‘›π‘‘π‘–π‘‘π‘–π‘œπ‘›
𝑓 𝑖𝑛𝑝𝑒𝑑 =
π‘…π‘’π‘π‘’π‘Ÿπ‘ π‘–π‘£π‘’ πΆπ‘Žπ‘™π‘™,
πΆπ‘œπ‘›π‘‘π‘–π‘‘π‘–π‘œπ‘› π‘“π‘œπ‘Ÿ π‘Ÿπ‘’π‘π‘’π‘Ÿπ‘ π‘–π‘£π‘’ π‘π‘Žπ‘™π‘™
Lectures-11-12
Page 5 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Example:
Recursive definition for algorithm to compute sum of N integers can be written as
𝑓 𝑁 =
1,
𝑓 𝑁 − 1 + 𝑁,
𝑁=1
𝑁≥1
Recurrence Relation
A recurrence relation is an equation that recursively defines a sequence, once one or more
initial terms are given: each further term of the sequence is defined as a function of the preceding
terms.
Recurrence relation represents no of operations that are required to solve recursive function. On
the left hand of the equation is T(input) means total time and on right hand side the number of
operations will be given.
Example:
Recurrence relation for above given definition will be as
T(N) = T(N-1) +1 Where N>=1 and T(1) =1
Here T(N-1) will be time to compute f(N-1) and 1 is added because, after computing f(N-1) we
have to add N into it which needs one operation to total number of operations will be T(N-1) +1.
Now we have to solve this relation/equation to compute the actual time complexity
Solution:
T(N) = T(N-1) + 1
= (T(N-1-1)+1) +1 (Here we have solved T(N-1), we for N it is N-1, so for N-1,
there will be 1 subtracted and 1 will be added)
= T(N-2) + 2
= (T(N-2-1)+1)+2
= T(N-3) + 3
--------------------------------------= T(N-i) + i for ith term
= T(N-N-1) + N-1
Lectures-11-12
for (N-1)th term, we put N-1 here because we know the
solution of T(1)
Page 6 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
= T(1) + N-1
=1+N–1
= N
Hence
T(N) = O(N)
Let us consider another example
Example
Recursive definition for factorial is given as
𝑓 𝑁 =
1,
𝑓 𝑁 − 1 ∗ 𝑁,
𝑁≤1
𝑁≥2
Recursive relation for above definition will be as
T(N)
=
T(N-1) + 1, T(0)=1
Here T(N-1) is for f(N-1) and 1 is added to multiply the result of f(N-1) with N.
Above recurrence relation can be similarly as solved above
Example:
Fibonacci Series
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence
relation.
Fn = Fn-1 + Fn-2
with seed values
F0 = 0 and F1 = 1
Recursive Definition for Fibonacci Series
0
Fib (n) =
1
Fib (n-1) + Fib (n-2)
Lectures-11-12
if (n = 0)
if (n = 1)
if (n > 1)
Page 7 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Recursive Algorithm
Fib(n) {
IF (N=0) Return 0
Else IF (N=1) Return 1
Else Return Fib(n-1) + Fib(n-2)
}
Recurrence Relation for above definition will be as
T(N)
= T(N-1) + T(N-2) + 1 and T(1)=T(2)=1
Here T(N-1) is for Fib(N-1) and T(N-2) is for Fib(N-2) and 1 is added to sum the result of
Fib(N-1) with Fib(N-2).
Above recurrence relation can similarly be solved as above
Recursion Examples
Tower of Hanoi puzzle
As our next example, we consider another educational workhorse of recursive algorithms: the
Tower of Hanoi puzzle. In this puzzle, we (or mythical monks, if you do not like to move disks)
have n disks of different sizes that can slide onto any of three pegs. Initially, all the disks are on
the first peg in order of size, the largest on the bottom and the smallest on top. The goal is to
move all the disks to the third peg, using the second one as an auxiliary, if necessary. We can
move only one disk at a time, and it is forbidden to place a larger disk on top of a smaller one.
The problem has an elegant recursive solution, which is illustrated in Figure. To move n>1 disks
from peg 1 to peg 3 (with peg 2 as auxiliary), we first move recursively n − 1 disks from peg 1 to
peg 2 (with peg 3 as auxiliary), then move the largest disk directly from peg 1 to peg 3, and,
finally, move recursively n − 1 disks from peg 2 to peg 3 (using peg 1 as auxiliary). Of course, if
n = 1, we simply move the single disk directly from the source peg to the destination peg.
Lectures-11-12
Page 8 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Recursive definition for tower of Hanoi is given below
π‘šπ‘œπ‘£π‘’ 1 π‘‘π‘œ 3,
𝑛=1
𝑓 𝑛 =
𝑓 1,2, 𝑛 − 1 π‘Žπ‘›π‘‘ 𝑓(2,3, 𝑛 − 1),
π‘₯≥0
Recursive definition depicts that if there is only one disk then you have to move disc directly 1 to
3 otherwise move n-1 disks to 2, and 1 disk to 3 then n-1 from 2 to 3.
Recurrence relation for above definition will be as follow
T(N) =
T(N-1) + T(N-1) + 1
Time for 1 to 2
Time for moving disks 2 to 3
(here 1 is time to one disk from 1 to 3)
And T(1) =1, means if there is only one disk we need only one operation
Solution of above recurrence relation
T(N) =
T(N-1) + T(N-1) + 1
=
2T(N-1) +1
=
2(2T(N-1-1)+1) +1 (Here we have solve T(N-1)
=
22T(N-2) +2 +1
=
22(2T(N-2-1)+1) +2 +1
=
23T(N-3) +22 +2 +1
=
23(T(N-3-1) +1) +22 +2 +1
=
24T(N-4) +23 +22 +2 +1
=
25T(N-5) + 24 +23 +22 +2 +1
For ith term
T(N) =
2iT(N-i) + 2i-1 + 2i-2 ………..+ 24 +23 +22 +2 +1
For (N-1) the term
T(N)
=
2N-1T(N-(N-1)) + 2N-2 + 2N-3 ………..+ 24 +23 +22 +2 +1
Lectures-11-12
Page 9 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
=
2N-1T(1) + 2N-2 + 2N-3 ………..+ 24 +23 +22 +2 +1
We know that T(1)=1
T(N) =
2N-1(1)+ 2N-2 + 2N-3 ………..+ 24 +23 +22 +2 +1
It is sum of two power and we can write it as
𝑁−1 𝑖
T(N) =
𝑖=0 2
Here is a formula
So above relation becomes
T(N)
T(N)
T(N)
=
=
=
2N-1+1 -1
2N -1
O(2N)
Recursive Version of Binary Search Algorithm
Algorithm: BSearch(A[N], s,e,n)
Input: Sorted Array, start point, end point and number to find
Output: location of number
Procedure:
mid=(s+e)/2
if(e<s)
Base Case
return -1
if(A[mid] = n)
return mid
else
if(A[mid]>n)
e=mid-1
else
s=mid+1
Recursive Call
return BSearch(A[N],s,e,n)
Recurrence Relation for above algorithm will be as follow
T(N)
=
T(N/2) +1 and T(1)=1
Lectures-11-12
Page 10 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Because every time problem size will remain half, and we have to solve only half of the
remaining array.
T(N)
=
=
=
=
=
=
For kth therm
T(N) =
T(N/2) +1
T(N/4)+1+1
T(N/22) +1+1
(T(N/22.2)+1) +1 +1
T(N/23) +1 +1 +1
T(N/24) +1 +1 +1 +1
T(N/2k) +1+………+1
1 will be added k times for kth term
Hence we can write
T(N) =
T(N/2k) +k
By using k=lg2N we get
T(N) =
T(N/2 lg2N) + lg2N
We know that
Se we get
T(N) =
T(N/Nlg22) + lg2N
We also know that
T(N)
T(N)
=
=
T(N/N(1)) + lg2N
T(1) + lg2N = 1 + lg2N = O(lgN)
Calculation Power of Positive Number
Recursive Definition
Here ‘a’ is the positive number whose nth power is being computed.
1
if n = 0
Power (a , n) =
a ∗ Power (a , n β€’1) if n > 0
Lectures-11-12
Page 11 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Recursive Algorithm
Power (a , n)
{
IF (n = 0)
RETURN 1
ELSE
RETURN a ∗ Power (a , n β€’1)
}
Recurrence Relation
T(n) = T(n-1) + 1
T(1) = 1
Students should solve this recurrence relation as this recurrence relation has been already solved
in the previous examples.
Different Methods to Compute Time Complexity
1.
2.
3.
4.
5.
6.
Backward Substitution Method
Forward Substitution Method
Recursion Tree Method
Telescoping Sum Method
Master Theorem
Induction Method
Backward substitution Method Examples
Example 1: Recurrence relation for computing sum of N numbers will be as
T(N) = T(N-1) +1 Where N>=1 and T(1) is 1.
Here T(N-1) will be time to compute f(N-1) and 1 is added because, after computing f(N-1) we
have to add N into it which needs one operation to total number of operations will be T(N-1) +1.
Now we have to solve this relation/equation to compute the actual time complexity
Lectures-11-12
Page 12 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
Solution:
T(N)
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
= T(N-1) + 1
= (T(N-1-1)+1) +1 (Here we have solved T(N-1), we for N it is N-1, so for N-1, there will be 1
subtracted and 1 will be added)
= T(N-2) + 2
= (T(N-2-1)+1)+2
= T(N-3) + 3
--------------------------------------= T(N-i) + i for ith term
= T(N-N-1) + N-1
= T(1) + N-1
=1+N–1
= N
Hence
for (N-1)th term, we put N-1 here because we know the solution of T(1)
T(N) = O(N)
Example 2: Tower of Hanoi
Recurrence relation for above definition will be as follow
T(N) =
T(N-1)
+ T(N-1) + 1
Time for moving disks 1 to 2
Time for moving disks 2 to 3
(here 1 is time to move one disk from 1 to 3)
And T(1) =1, means if there is only one disk we need only one operation
Solution of above recurrence relation
T(N) =
T(N-1) + T(N-1) + 1
=
2T(N-1) +1
=
2(2T(N-1-1)+1) +1 (Here we have solve T(N-1)
=
22T(N-2) +2 +1
=
22(2T(N-2-1)+1) +2 +1
=
23T(N-3) +22 +2 +1
=
23(T(N-3-1) +1) +22 +2 +1
=
24T(N-4) +23 +22 +2 +1
=
25T(N-5) + 24 +23 +22 +2 +1
Lectures-11-12
Page 13 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
For ith term
T(N) =
2iT(N-i) + 2i-1 + 2i-2 ………..+ 24 +23 +22 +2 +1
For (N-1) the term
T(N) =
2N-1T(N-(N-1)) + 2N-2 + 2N-3 ………..+ 24 +23 +22 +2 +1
=
2N-1T(1) + 2N-2 + 2N-3 ………..+ 24 +23 +22 +2 +1
We know that T(1)=1
T(N) =
2N-1(1)+ 2N-2 + 2N-3 ………..+ 24 +23 +22 +2 +1
It is sum of two power and we can write it as
𝑁−1 𝑖
T(N) =
𝑖=0 2
Here is a formula
So above relation becomes
T(N)
T(N)
T(N)
=
=
=
2N-1+1 -1
2N -1
O(2N)
Example 3: Recursive Version of Binary Search Algorithm
Algorithm: BSearch(A[N], s,e,n)
Input: Array, start point, end point and number to find
Output: location of number
Procedure:
mid=(s+e)/2
if(e<s)
return -1
if(A[mid] = n)
return mid
else
if(A[mid]>n)
e=mid-1
else
Base Case
Recursive Call
Lectures-11-12
Page 14 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
s=mid+1
return BSearch(A[N],s,e,n)
Recurrence Relation for above algorithm will be as follow
T(N) =
T(N/2) +1 and T(1)=1
Because every time problem size will remain half, and we have to solve only half of the
remaining array.
T(N) =
T(N/2) +1
=
T(N/4)+1+1
=
T(N/22) +1+1
=
(T(N/22.2)+1) +1 +1
=
T(N/23) +1 +1 +1
=
T(N/24) +1 +1 +1 +1
For kth term
T(N) =
T(N/2k) +1+………+1
1 will be added k times for kth term
Hence we can write
T(N) =
T(N/2k) +k
By using k=lg2N we get
T(N) =
T(N/2 lg2N) + lg2N
We know that
Hence we can write
N=2k
lg2N= lg2 2k
lg2N= k lg2 2
k = lg2N
as lg2 2 =1
By using k=lg2N we get
Se we get
T(N) =
T(N/Nlg22) + lg2N
We also know that
T(N)
T(N)
=
=
T(N/N(1)) + lg2N
T(1) + lg2N = 1 + lg2N = O(lgN)
Lectures-11-12
Page 15 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
Example 4:
T(N) =
2T(N/2) +1
T(1) =
1
T(N) =
2T(N/2) +1
=
2( 2T(N/4) + 1 )+1
=
22T(N/22) +2+1
=
22T(N/22) +3
=
22 ( 2T(N/8) + 1 ) + 3
=
23T(N/23) +4+3
=
23T(N/23) +7
----------------------------=
2kT(N/2k) + (2k β€’ 1)
For kth term
T(N) =
2k T(N/2k) + (2k β€’ 1)
T(N)
Since
T(N)
T(N)
T(N)
T(N)
T(N)
T(N)
=
2 lg2N T(N/2 lg2N) + 2 lg2N β€’1
,
=
=
=
=
=
=
lg 2
2
lg 2
2 )
N
T(N/N
+ N lg22 β€’1
N T(N/N) + N β€’1
N T(1) + N β€’1
N + N β€’1
2N β€’1
O(N)
Example 5:
T(N) =
2T(N/2) +N
T(1) =
1
T(N) =
2T(N/2) +N
=
2( 2T(N/4) + N/2 ) + N
=
22T(N/22) + N + N
=
22T(N/22) +2N
=
22 ( 2T(N/8) + N/4 ) + 2N
=
23T(N/23) + N + 2N
=
23T(N/23) +3N
----------------------------=
2kT(N/2k) + kN
Lectures-11-12
Page 16 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
For kth term
T(N) =
2k T(N/2k) + kN
Hence we can write
N=2k
lg2N= lg2 2k
lg2N= k lg2 2
k = lg2N
as lg2 2 =1
By using k=lg2N we get
T(N) =
2 lg2N T(N/2 lg2N) + N( lg2N)
Since
T(N)
T(N)
T(N)
T(N)
T(N)
,
=
=
=
=
=
2 lg2N T(N/2 lg2N) + N( lg2N )
N lg22 T(N/N lg22) + N( lg2N)
N T(1) + N( lg2N )
N + N( lg2N)
O(N lg2N)
Example 6:
T(N) =
4T(N/4) + C
T(1) =
1
T(N) =
4T(N/4) +C = 4T(N/4) + 40C
=
4( 4T(N/16) + C )+C
=
42T(N/42) + 4C + C
=
42T(N/42) +5C = 42T(N/42) + (40+41)C
=
42 ( 4T(N/64) + C ) + 5C
=
43T(N/43) + 16C + 5C
=
43T(N/43) + 21C = 43T(N/43) + (40+41 +42)C
----------------------------=
4kT(N/4k) + (40+41 +42 +…+4k-1 )C
For kth term
T(N) =
4k T(N/4k) + (40+41 +42 +…+4k-1 )C
Hence we can write
N=4k
Lg4N= lg4 4k
lg4N= k lg4 4
k = lg4N
as lg4 4 =1
Lectures-11-12
Page 17 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
By using k=lg4 N we get
k ο€­1
T(N)
=
4 lg4N T(N/4 lg4N) + C οƒ₯ 4i
i ο€½0
Since
T(N)
,
=
T(N)
=
T(N)
=
T(N)
=
N lg44 T(N/N lg44) + C (
4k ο€­11 ο€­ 1
)
4 ο€­1
4lg 4 N ο€­ 1
N T(N/N) + C (
)
3
N ο€­1
)
N T(1) + C (
3
O(N)
Example 7:
T(N) =
4T(N/4) + N
T(1) =
1
T(N) =
4T(N/4) + N
=
4( 4T(N/16) + N/4 )+ N
=
42T(N/42) + N + N
=
42T(N/42) +2N
=
42 ( 4T(N/64) + N/42 )+ 2N
=
43 T(N/43) + N + 2N
=
43T(N/43) +3N
----------------------------=
4kT(N/4k) + kN
For kth term
T(N) =
4k T(N/4k) + kN
Hence we can write
N=4k
Lg4N= lg4 4k
lg4N= k lg4 4
k = lg4N
as lg4 4 =1
By using k=lg4 N we get
T(N) =
4 lg4N T(N/4 lg4N) + N( lg4N)
Since
Lectures-11-12
,
Page 18 of 19
Analysis of Algorithm (CS-542)
Dr. Naseer Ahmed Sajid
Mrs. Maryam Amin
T(N)
T(N)
T(N)
T(N)
=
=
=
=
email id: naseer@biit.edu.pk
email id: maryam@biit.edu.pk
Whatsapp# 0346-5100010
Whatsapp# 0346-5410878
N lg44 T(N/N lg44) + N( lg4N)
N T(N/N) + N( lg4N)
N T(1) + N( lg4N)
O(N( lg4N) )
Lectures-11-12
Page 19 of 19
Download