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

L2 Time Complexity Recursive Function

The document discusses the time complexity analysis of recursive algorithms, focusing on examples such as the factorial function and binary search. It also introduces the Tower of Hanoi problem, explaining its recursive solution and providing a sample implementation in C. The document raises questions about the time complexity of the discussed algorithms.

Uploaded by

nazibul.nabil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

L2 Time Complexity Recursive Function

The document discusses the time complexity analysis of recursive algorithms, focusing on examples such as the factorial function and binary search. It also introduces the Tower of Hanoi problem, explaining its recursive solution and providing a sample implementation in C. The document raises questions about the time complexity of the discussed algorithms.

Uploaded by

nazibul.nabil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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)
{ T(n) = O(1) , if n = 0
O(1) time T(n) = T(n-1)+O(1) , otherwise
if (n == 0)
O(1) time
return (1);
else
return (n * Factorial(n-1));
} O(1) time So Factorial(n-1) call will take T(n-1) time

void main()
{
int n; cin>>n; Let factorial(n) call takes total T(n) time
Factorial(n);
Binary Search: The Recursive Way
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);
}
}

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
Recursive Function for Solving Hanoi

#include <stdio.h>
void hanoi(int n, char src, char dst, char aux)
{
if(n == 1)
printf("Move disc from peg %c to peg %c\n", 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;
}
What’s its time complexity?

You might also like