Understanding The Basic Concepts of Recursion and Backtracking
Understanding The Basic Concepts of Recursion and Backtracking
Submitted by:
Name: Tofayel Ahmmed Babu
ID: 1904005
Level: 2
Term: II
Section: A
Experiment Name: Understanding the basic concepts of Recursion and Backtracking.
Objectives: The main objectives of this experiment are-
int Factorial(int n) {
if(n==0 || n==1) { //base case
return 1;
}
return n * Factorial(n-1); //recursive case
}
#include <bits/stdc++.h>
using namespace std;
#include<bits/stdc++.h>
int soln(int n) {
if(n == 0) return 0;
return soln(n-1) + n;
}
int32_t main() {
n
Code Explanation: ∑ i =n + (n – 1) + . . . . . . . . + 2 + 1 + 0
i=0
Statement: Given three integers n, m and k. We are asked to print an n x n matrix. Here a[i][j]
defined by the following conditions –
a[i][j] = 0 if i = 0 and j = 0
a[i][j] = a[i-1][j-1] + k if i = j
a[i][j] = a[i-1][j] if i > j
a[i][j] = a[i][j-1] - 1 if i < j
Source code:
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[102][102];
bool chk(int i, int j) {
if(i < 0 or j < 0 or i > n - 1 or j > n - 1) return true;
return false;
}
int calc(int i, int j) {
if(chk(i, j)) return 0;
if(i==0 and j==0) {
return a[i][j] = m;
}
if(a[i][j]) return a[i][j];
calc(i-1, j-1);
calc(i, j-1);
calc(i-1, j);
if(i == j)return a[i][j] = a[i-1][j-1] + k;
if(i > j) return a[i][j] = a[i-1][j] - 1;
if(i < j) return a[i][j] = a[i][j-1] - 1;
return 0;
}
int main() {
cin >> n >> m >> k;
calc(n-1, n-1);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << a[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
Code Explanation: Using three recursive call, we can solve this problem. But if we don’t use
dynamic programming, the judge will gives TLE (Time Limit Error).
Discussion: In this experiment, we have learnt about the basic concepts of recursion and
backtracking. We also have implemented some recursive problem and backtracking problem
in this experiment. In the classroom we have generated all possible permutation of a string
using backtracking and Fibonacci number using recursion. We didn’t solve N-queen problem.
Later I tried to understand the N-queen problem in home. I understand the task what have asked
to do and code it myself. In the report I add three home works. Actually these problem are
given from different online judges. First two problems were very easy and I solved them in my
first attempt. But in the third problem I done it in my second attempt. In first submission I got
TLE. Then I used the concept of dynamic programming (DP) and got accepted which was not
taught us in the respective experiment yet. Thus, I learned some basic stuffs of algorithm in
this experiment and improved my recursion concepts a little bit. Hopefully, I can use them
properly in later arising any real life problem.
THE END
References:
https://www.geeksforgeeks.org/backtracking-algorithms/
https://www.simplilearn.com/tutorials/cpp-tutorial/what-is-recursion-in-
cpp#:~:text=Recursion%20is%20a%20method%20in,case%20and%20a%20recursive
%20condition.
https://www.youtube.com/watch?v=xFv_Hl4B83A
https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/