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

04 - Analyzing Running Times of Recursive Programs

The document discusses techniques for analyzing the running time of recursive program code, including the back substitution method, recursive tree method, and Master's theorem. It provides examples of recursive functions with running times of O(n^2), O(n log n), O(2^n), O(log n), O(n), and O(n log n). Results show that recursive functions with running times of T(n) = T(n-1), T(n) = n + T(n-1), T(n) = 2T(n/2) have running times of O(n), O(n^2), and O(n log n) respectively. Video links provide lectures on
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)
22 views

04 - Analyzing Running Times of Recursive Programs

The document discusses techniques for analyzing the running time of recursive program code, including the back substitution method, recursive tree method, and Master's theorem. It provides examples of recursive functions with running times of O(n^2), O(n log n), O(2^n), O(log n), O(n), and O(n log n). Results show that recursive functions with running times of T(n) = T(n-1), T(n) = n + T(n-1), T(n) = 2T(n/2) have running times of O(n), O(n^2), and O(n log n) respectively. Video links provide lectures on
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/ 12

Analyzing Running Times

Of
Recursive Program Code

1
Techniques of Analyzing
Recursive Code
• Back Substitution Method
• Recursive Tree Method
• Master’s Theorem

2
0(n )
2

demo(int n)
{
if(n>0)
{
for(i=1; i<=n; i++)
print (“message”);
demo(n-1);
}
else
return 1;
}
3
0(n logn)
demo(int n)
{
if(n>0)
{
for(i=1; i<=n; i=i*2)
print (“message”);
demo(n-1);
}
else
return 1;
}
4
0(2 )
n

demo(int n)
{
if(n>0)
{
print (“message”);
demo(n-1);
demo(n-1);
}
else
return 1;
}
5
0(logn)
demo(int n)
{
if(n>1)
{
print (“message”);
demo(n/2);
}
else
return 1;
}
6
0(n)
demo(int n)
{
if(n>1)
{
for(i=1; i<=n; i=i+1)
print (“message”);
demo(n/2);
}
else
return 1;
}
7
0(n logn)
demo(int n)
{
if(n>1)
{
for(i=1; i<=n; i=i+1)
print (“message”);
demo(n/2);
demo(n/2);
}
else
return 1;
}
8
0(n)
T(n) = c + 2T(n/2) n>1
=c n=1

9
Results

T(n) = c + T(n-1) => 0(n)


= 1 + T(n-1) => 0(n)
= n + T(n-1) => 0(n2)
= n2 + T(n-1) => 0(n3)
= logn + T(n-1) => 0(nlogn)

T(n) = 2T(n-1) + c => 0(2n)


= 3T(n-1) + 1 => 0(3n)
= 2T(n-1) + n => 0(n2n)
= 2T(n-1) + log n => 0(log n.2n)
10
• T(n) = T(n/2) +1 = 0 (log n)
= T(n/2) + n = 0 ( n)
= 2T(n/2) +n = 0 (n log n)

11
Video Lecture of the topic can be found at

Analyzing Running Time of Iterative Program Code


https://youtu.be/zcPF4ZXt5FI

Analyzing Running Time of Recursive Code using Back Substitution method


https://youtu.be/9OXy5V7jMYc

Analyzing Running Time of Recursive Code using Recursive tree Method


https://youtu.be/KPtjTsAmDn4

12

You might also like