Lab5 9
Lab5 9
We will implement the recursive algorithms in C++ and derive their recurrence
relations.
#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).
#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).
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)+lognT(n)=T(n−1)+logn:
o The function makes a single recursive call with n−1n−1 and
performs O(logn)O(logn) work.
o Time Complexity: O(nlogn)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−logn)+nlognT(n)=3T(n−logn)+nlogn:
o The function makes three recursive calls with n−lognn−logn and
performs O(nlogn)O(nlogn) work.
o Time Complexity: O(nlogn)O(nlogn).