SlideShare a Scribd company logo
Lecture 2
Time complexity Analysis of
Recursive Algorithms
CSE 373: Design & Analysis of Algorithms
Recursive Factorial Function
// Computes the factorial of a nonnegative integer.
// Precondition: n must be greater than or equal to 0.
// Postcondition: Returns the factorial of n; n is unchanged.
int Factorial(int n)
{
if (n == 0)
return (1);
else
return (n * Factorial(n-1));
}
void main()
{
int n; cin>>n;
Factorial(n);
}
T(n) = O(1) , if n = 0
T(n) = T(n-1)+O(1) , otherwise
O(1) time
Let factorial(n) call takes total T(n) time
So Factorial(n-1) call will take T(n-1) time
O(1) time
O(1) time
template<class ItemType>
bool BinarySearch(ItemType info[], ItemType item, int fromLocation,
int toLocation)
{
if (fromLocation > toLocation) // Base case 1
return false;
else
{
int midPoint;
midPoint = (fromLocation + toLocation) / 2;
if (item < info[midPoint])
return BinarySearch(info, item, fromLocation, midPoint - 1);
else if (item == info[midPoint]) // Base case 2
return true;
else
return BinarySearch(info, item, midPoint + 1, toLocation);
}
}
Binary Search: The Recursive Way
What’s its time complexity?
Tower of Hanoi Problem
• There is a tower of n discs stacked in increasing order of size in the first peg
• Goal: transfer the entire tower to the 3rd
peg in the fewest possible moves.
• Constraints:
• You can only move one disk at a time and
• You can never stack a larger disk onto a smaller disk.
Recursive Solution
• Problem: Move N discs from source peg to destination
peg using auxiliary peg as via
• Base case:
• Move disc from source peg to destination peg (N = 1)
Recursive Solution
• Problem: Move N discs from source peg to destination
peg using auxiliary peg as via
• General case:
• Move N-1 discs from source peg to auxiliary peg via destination
peg
• Move disc from source peg to destination peg
• Move N-1 discs from auxiliary peg to destination peg via source
peg
#include <stdio.h>
void hanoi(int n, char src, char dst, char aux)
{
if(n == 1)
printf("Move disc from peg %c to peg %cn", src, dst);
else
{
hanoi(n-1, src, aux, dst);
hanoi(1, src, dst, aux);
hanoi(n-1, aux, dst, src);
}
}
int main()
{
hanoi(4, 'A', 'C', 'B');
return 0;
}
Recursive Function for Solving Hanoi
What’s its time complexity?

More Related Content

PPT
Tower of Hanoi.ppt
SACHINVERMA255386
 
PDF
searching.pdf
ISHAN194169
 
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
 
PDF
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
PDF
Dsoop (co 221) 1
Puja Koch
 
PPTX
designs and analysis of algorithmss.pptx
rachelevelynr9007sse
 
PPT
Recursion
Malainine Zaid
 
PPTX
Control System Homework Help
Matlab Assignment Experts
 
Tower of Hanoi.ppt
SACHINVERMA255386
 
searching.pdf
ISHAN194169
 
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
 
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
Dsoop (co 221) 1
Puja Koch
 
designs and analysis of algorithmss.pptx
rachelevelynr9007sse
 
Recursion
Malainine Zaid
 
Control System Homework Help
Matlab Assignment Experts
 

Similar to L2_Time_complexity_recursive_function.pptx (20)

PPTX
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
PDF
6-Python-Recursion.pdf
AshishPalandurkar2
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PDF
Recursive algorithms
subhashchandra197
 
PPTX
Recursion in Data Structure
khudabux1998
 
PPTX
Data structure
Mahnoor Hashmi
 
PPTX
Complexity Analysis of Recursive Function
Meghaj Mallick
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PPTX
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
PPTX
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
PPTX
Divided and conqurddddddddddddddfffffe.pptx
belalAbdullah5
 
PPT
Analysis of Algorithm Recursive Algorithm
Devikashanker1
 
PPTX
06 Recursion in C.pptx
MouDhara1
 
PPTX
M2-Recursion.pptx
smithashetty24
 
PPTX
Recursion Merge Sort Quick Sort.pptx
JeoJoyA
 
PPTX
Recursion
Nalin Adhikari
 
PPT
Recursion.ppt
ssuser53af97
 
PPT
Dc8c4f010f40.hanoi.towers
Sumedha
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
6-Python-Recursion.pdf
AshishPalandurkar2
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Recursive algorithms
subhashchandra197
 
Recursion in Data Structure
khudabux1998
 
Data structure
Mahnoor Hashmi
 
Complexity Analysis of Recursive Function
Meghaj Mallick
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
Divided and conqurddddddddddddddfffffe.pptx
belalAbdullah5
 
Analysis of Algorithm Recursive Algorithm
Devikashanker1
 
06 Recursion in C.pptx
MouDhara1
 
M2-Recursion.pptx
smithashetty24
 
Recursion Merge Sort Quick Sort.pptx
JeoJoyA
 
Recursion
Nalin Adhikari
 
Recursion.ppt
ssuser53af97
 
Dc8c4f010f40.hanoi.towers
Sumedha
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Ad

Recently uploaded (20)

PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PDF
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
CDH. pptx
AneetaSharma15
 
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
Ad

L2_Time_complexity_recursive_function.pptx

  • 1. Lecture 2 Time complexity Analysis of Recursive Algorithms CSE 373: Design & Analysis of Algorithms
  • 2. Recursive Factorial Function // Computes the factorial of a nonnegative integer. // Precondition: n must be greater than or equal to 0. // Postcondition: Returns the factorial of n; n is unchanged. int Factorial(int n) { if (n == 0) return (1); else return (n * Factorial(n-1)); } void main() { int n; cin>>n; Factorial(n); } T(n) = O(1) , if n = 0 T(n) = T(n-1)+O(1) , otherwise O(1) time Let factorial(n) call takes total T(n) time So Factorial(n-1) call will take T(n-1) time O(1) time O(1) time
  • 3. template<class ItemType> bool BinarySearch(ItemType info[], ItemType item, int fromLocation, int toLocation) { if (fromLocation > toLocation) // Base case 1 return false; else { int midPoint; midPoint = (fromLocation + toLocation) / 2; if (item < info[midPoint]) return BinarySearch(info, item, fromLocation, midPoint - 1); else if (item == info[midPoint]) // Base case 2 return true; else return BinarySearch(info, item, midPoint + 1, toLocation); } } Binary Search: The Recursive Way What’s its time complexity?
  • 4. Tower of Hanoi Problem • There is a tower of n discs stacked in increasing order of size in the first peg • Goal: transfer the entire tower to the 3rd peg in the fewest possible moves. • Constraints: • You can only move one disk at a time and • You can never stack a larger disk onto a smaller disk.
  • 5. Recursive Solution • Problem: Move N discs from source peg to destination peg using auxiliary peg as via • Base case: • Move disc from source peg to destination peg (N = 1)
  • 6. Recursive Solution • Problem: Move N discs from source peg to destination peg using auxiliary peg as via • General case: • Move N-1 discs from source peg to auxiliary peg via destination peg • Move disc from source peg to destination peg • Move N-1 discs from auxiliary peg to destination peg via source peg
  • 7. #include <stdio.h> void hanoi(int n, char src, char dst, char aux) { if(n == 1) printf("Move disc from peg %c to peg %cn", src, dst); else { hanoi(n-1, src, aux, dst); hanoi(1, src, dst, aux); hanoi(n-1, aux, dst, src); } } int main() { hanoi(4, 'A', 'C', 'B'); return 0; } Recursive Function for Solving Hanoi What’s its time complexity?