0% found this document useful (0 votes)
9 views5 pages

Lab5 9

The document outlines various recursive algorithms implemented in C++ and their corresponding recurrence relations. It includes analyses of these relations using recursion trees and substitution methods, as well as a summary of time complexities for different recurrence relations. Each algorithm is accompanied by explanations of its behavior and complexity, providing a comprehensive overview of recursive function analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Lab5 9

The document outlines various recursive algorithms implemented in C++ and their corresponding recurrence relations. It includes analyses of these relations using recursion trees and substitution methods, as well as a summary of time complexities for different recurrence relations. Each algorithm is accompanied by explanations of its behavior and complexity, providing a comprehensive overview of recursive function analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise 01: Formulate Recurrence Relations

We will implement the recursive algorithms in C++ and derive their recurrence
relations.

1. Algorithm 1: Simple Recursion

#include <iostream>
using namespace std;

void test(int n) {
if (n <= 0) return; // Base case
cout << n << endl; // Constant work
test(n - 1); // Recursive call
}

int main() {
test(5); // Example call
return 0;
}
 Recurrence Relation: T(n)=T(n−1)+O(1)T(n)=T(n−1)+O(1)
 Explanation: The function makes a single recursive call with n−1n−1 and
performs constant work (printing).

2. Algorithm 2: Recursion with a Loop

#include <iostream>
using namespace std;

void test(int n) {
if (n <= 0) return; // Base case
for (int i = 0; i < n; i++) { // O(n) work
cout << n << endl;
}
test(n - 1); // Recursive call
}

int main() {
test(5); // Example call
return 0;
}
 Recurrence Relation: T(n)=T(n−1)+O(n)T(n)=T(n−1)+O(n)
 Explanation: The function makes a single recursive call with n−1n−1 and
performs O(n)O(n) work in the loop.
3. Algorithm 3: Double Recursion

#include <iostream>
using namespace std;

void test(int n) {
if (n <= 0) return; // Base case
cout << n << endl; // Constant work
test(n - 1); // First recursive call
test(n - 1); // Second recursive call
}

int main() {
test(3); // Example call
return 0;
}
 Recurrence Relation: T(n)=2T(n−1)+O(1)T(n)=2T(n−1)+O(1)
 Explanation: The function makes two recursive calls with n−1n−1 and
performs constant work (printing).

4. Algorithm 4: Factorial

#include <iostream>
using namespace std;

int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}

int main() {
cout << factorial(5) << endl; // Example call
return 0;
}
 Recurrence Relation: T(n)=T(n−1)+O(1)T(n)=T(n−1)+O(1)
 Explanation: The function makes a single recursive call with n−1n−1 and
performs constant work (multiplication).

5. Algorithm 5: Fibonacci

#include <iostream>
using namespace std;

int fibonacci(int n) {
if (n <= 1) return n; // Base case
return fibonacci(n - 1) + fibonacci(n - 2); // Two recursive calls
}

int main() {
cout << fibonacci(5) << endl; // Example call
return 0;
}
 Recurrence
Relation: T(n)=T(n−1)+T(n−2)+O(1)T(n)=T(n−1)+T(n−2)+O(1)
 Explanation: The function makes two recursive calls, one
with n−1n−1 and another with n−2n−2, and performs constant work
(addition).

Exercise 02: Analyze Recurrence Relations Using Recursion


Tree and Substitution Method

We will analyze the given recurrence relations in C++.

1. Recurrence Relation 1: T(n)=T(n−1)+O(1)T(n)=T(n−1)+O(1)


 Recursion Tree Method:
o Each level contributes O(1)O(1).
o The tree has nn levels.
o Total time complexity: O(n)O(n).
 Substitution Method:
o Assume T(n)=O(n)T(n)=O(n).
o Verify by
induction: T(n)=T(n−1)+O(1)≤c(n−1)+O(1)≤cnT(n)=T(n−1)+O(1)≤c(
n−1)+O(1)≤cn.

2. Recurrence Relation 2: T(n)=T(n−1)+O(n)T(n)=T(n−1)+O(n)


 Recursion Tree Method:
o Each level contributes O(n)O(n).
o The tree has nn levels.
o Total time complexity: O(n2)O(n2).
 Substitution Method:
o Assume T(n)=O(n2)T(n)=O(n2).
o Verify by
induction: T(n)=T(n−1)+O(n)≤c(n−1)2+O(n)≤cn2T(n)=T(n−1)+O(n)
≤c(n−1)2+O(n)≤cn2.

3. Recurrence Relation 3: T(n)=2T(n−1)+O(1)T(n)=2T(n−1)+O(1)


 Recursion Tree Method:
o Each level contributes O(1)O(1).
o The tree has 2n2n nodes.
o Total time complexity: O(2n)O(2n).
 Substitution Method:
o Assume T(n)=O(2n)T(n)=O(2n).
o Verify by
induction: T(n)=2T(n−1)+O(1)≤2⋅c2n−1+O(1)≤c2nT(n)=2T(n−1)+O(
1)≤2⋅c2n−1+O(1)≤c2n.

4. Recurrence Relation 4: T(n)=T(n−1)+O(log⁡n)T(n)=T(n−1)+O(logn)


 Recursion Tree Method:
o Each level contributes O(log⁡n)O(logn).
o The tree has nn levels.
o Total time complexity: O(nlog⁡n)O(nlogn).
 Substitution Method:
o Assume T(n)=O(nlog⁡n)T(n)=O(nlogn).
o Verify by
induction: T(n)=T(n−1)+O(log⁡n)≤c(n−1)log⁡(n−1)+O(log⁡n)≤cnlog⁡nT(
n)=T(n−1)+O(logn)≤c(n−1)log(n−1)+O(logn)≤cnlogn.

Exercise 03: Master Theorem

Recurrence Relation Time Complexity (Big-


O Notation)
T(n)=T(n−1)+1T(n)=T(n−1)+1 O(n)O(n)
T(n)=T(n−1)+log⁡nT(n)=T(n−1)+logn O(nlog⁡n)O(nlogn)
T(n)=T(n−1)+nT(n)=T(n−1)+n O(n2)O(n2)
T(n)=T(n−1)+n2T(n)=T(n−1)+n2 O(n3)O(n3)
T(n)=2T(n−1)+1T(n)=2T(n−1)+1 O(2n)O(2n)
T(n)=3T(n−2)+1T(n)=3T(n−2)+1 O(3n/2)O(3n/2)
T(n)=3T(n−log⁡n)+nlog⁡nT(n)=3T(n−logn)+nlogn O(nlog⁡n)O(nlogn)

Explanation of the Table:

1. T(n)=T(n−1)+1T(n)=T(n−1)+1:
oThe function makes a single recursive call with n−1n−1 and
performs constant work.
o Time Complexity: O(n)O(n).
2. T(n)=T(n−1)+log⁡nT(n)=T(n−1)+logn:
o The function makes a single recursive call with n−1n−1 and
performs O(log⁡n)O(logn) work.
o Time Complexity: O(nlog⁡n)O(nlogn).
3. T(n)=T(n−1)+nT(n)=T(n−1)+n:
oThe function makes a single recursive call with n−1n−1 and
performs O(n)O(n) work.
o Time Complexity: O(n2)O(n2).
4. T(n)=T(n−1)+n2T(n)=T(n−1)+n2:
oThe function makes a single recursive call with n−1n−1 and
performs O(n2)O(n2) work.
o Time Complexity: O(n3)O(n3).
5. T(n)=2T(n−1)+1T(n)=2T(n−1)+1:
oThe function makes two recursive calls with n−1n−1 and performs
constant work.
o Time Complexity: O(2n)O(2n).
6. T(n)=3T(n−2)+1T(n)=3T(n−2)+1:
oThe function makes three recursive calls with n−2n−2 and performs
constant work.
o Time Complexity: O(3n/2)O(3n/2).
7. T(n)=3T(n−log⁡n)+nlog⁡nT(n)=3T(n−logn)+nlogn:
o The function makes three recursive calls with n−log⁡nn−logn and
performs O(nlog⁡n)O(nlogn) work.
o Time Complexity: O(nlog⁡n)O(nlogn).

You might also like