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

Understanding The Basic Concepts of Recursion and Backtracking

you can find out a brief description about recursion & Backtracking. I also added some problems and their solution with explanation. Hopefully, it will help you to clear the concept of recursion and backtracking.

Uploaded by

TOFAYEL AHMMED
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Understanding The Basic Concepts of Recursion and Backtracking

you can find out a brief description about recursion & Backtracking. I also added some problems and their solution with explanation. Hopefully, it will help you to clear the concept of recursion and backtracking.

Uploaded by

TOFAYEL AHMMED
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Chittagong University of Engineering and Technology

Department of Computer Science and Engineering

Assignment: Understanding the basic concepts of Recursion and Backtracking.

Course Code: CSE - 244


Course Title: Algorithm Design and Analysis (sessional)
Assignment No. : 02
Date of allotment: Nov 29, 2022
Date of submission: Dec 04, 2022

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-

 To understand the basic concepts of recursion


 To understand the basics of backtracking
 To calculate Factorial and Fibonacci using recursion
 To find all the permutation of a string
 To implement the N-queen problem using backtracking
Theory: Recursion: Recursion in a method in any algorithm which calls itself it directly or
indirectly until a suitable condition is met. In this method, we repeatedly call the function
within the same function. Basically recursive function consists of two parts –
i. Base case or terminated case
ii. Recursive condition
If there is no base case, then the recursive function makes an infinite loop. But as we want to
solve a problem using recursion, base is the most important case to terminate the execution of
recursive call. A trivial example of a recursion calculating the factorial of a number. Following
is the recursive function for calculating the factorial –

int Factorial(int n) {
if(n==0 || n==1) { //base case
return 1;
}
return n * Factorial(n-1); //recursive case
}

Backtracking: Backtracking is an algorithmic technique for solving problems recursively by


trying to build a solution incrementally, one piece at a time, removing those solution that fails
to satisfy the constraints of the problem at any point of time. Basically there are three types of
problems in backtracking –
i. Decision Problem (search for the feasible solution)
ii. Optimization Problem (search for the best solution)
iii. Enumeration Problem (Find all possible solutions)
N queen problem, knight’s tour problem, generate all permutation etc. are some classical
problems that can be solved using the concept of backtracking. Following is the code for all
possible permutation of a string using backtracking –
void permute(string& a, int l, int r)
{
if (l == r) // Base case
cout << a << endl;
else {
// Permutations made
for (int i = l; i <= r; i++) {
swap(a[l], a[i]); // Swapping done
permute(a, l + 1, r); // Recursion called
swap(a[l], a[i]); // backtrack
}
}
}

Homework: Problem – a: source: https://www.hackerrank.com/challenges/30-


recursion/problem
Problem statement: Given an integer n. Find the factorial of n.
Constraints: 2 <= n <= 12
Source code:

#include <bits/stdc++.h>
using namespace std;

#define int long long


int factorial(int n) {
if(n <= 1) return 1;
return n * factorial(n - 1);
}
int32_t main() {
int n; cin >> n;
cout << factorial(n) << endl;
return 0;
}

Code explanation: As we know n! = n*(n-1)! = n*(n-1)*(n-2)! = n*(n-1)*(n-2) . . . . . . 3*2*1

Problem – b: Source: https://www.eolymp.com/en/problems/8609


Problem statement: given an integer n. Asked to print the sum of first n natural number using
recursion.
Constraints: 0 <= n <= 1000
Source code:

#include<bits/stdc++.h>

using namespace std;


#define int long long

int soln(int n) {
if(n == 0) return 0;
return soln(n-1) + n;
}
int32_t main() {

int n; cin >> n;


cout << soln(n) << '\n';
return 0;
}

n
Code Explanation: ∑ i =n + (n – 1) + . . . . . . . . + 2 + 1 + 0
i=0

Problem – c: Source: https://www.hackerrank.com/contests/w35/challenges/triple-


recursion/problem

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/

You might also like