Report On Internet Programming
Report On Internet Programming
Objectives:
➢ To understand the basics of recursion.
➢ To understand the basics of backtracking
➢ To calculate the Factorial/Fibonacci number using recursion
➢ To find all permutations of a strig
➢ To implement the N-queue problem using backtracking.
Theory:
Recursion:The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called a recursive function. Using a
recursive algorithm, certain problems can be solved quite easily. Examples of such
problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS
of Graph, etc. A recursive function solves a particular problem by calling a copy of itself
and solving smaller subproblems of the original problems. Many more recursive calls
can be generated as and when required. It is essential to know that we should provide a
certain case in order to terminate this recursion process. So we can say that every time
the function calls itself with a simpler version of the original problem.
For example, we compute factorial n if we know factorial of (n-1). The base case for
factorial would be n = 0. We return 1 when n = 0.
int fact(int n)
{
if(n<=1)
return 1; // Base-case
Else
return n*fact(n-1);
}
Backtracking:
Backtracking is an algorithmic technique for solving problems recursively by trying to
build a solution incrementally, one piece at a time, removing those solutions that fail to
satisfy the constraints of the problem at any point in time (by time, here, is referred to
the time elapsed till reaching any level of the search tree). Backtracking can also be
said as an improvement to the brute force approach. So basically, the idea behind the
backtracking technique is that it searches for a solution to a problem among all the
available options. Initially, we start the backtracking from one possible option and if the
problem is solved with that selected option then we return the solution else we
backtrack and select another option from the remaining available options. There also
might be a case where none of the options will give you the solution and hence we
understand that backtracking won’t give any solution to that particular problem. We can
also say that backtracking is a form of recursion. This is because the process of finding
the solution from the various option available is repeated recursively until we don’t find
the solution or we reach the final state. So we can conclude that backtracking at every
step eliminates those choices that cannot give us the solution and proceeds to those
choices that have the potential of taking us to the solution.
void findPath(parameters)
{
// Explore the path
If(found a solution)
{
// Print Solution
Return;
}
Else
{
//Choose another path
findPath(parameters);
}
}
Homework
#1:
Problem Statement: Calculate the factorial of a number using the concept of recursion.
Solution:
#include<bits/stdc++.h>
using namespace std;
int factorial(int n)
{
if(n==0 || n==1)
return 1;
else
return n*factorial(n-1);
}
int main()
{
int n;
cin>>n;
cout<<factorial(n)<<endl;
}
#2:
Problem statement:
Implement the following recursive function:
Solution:
#include<bits/stdc++.h>
using namespace std;
int f(int n)
{
if(n==0)
return 0;
else
return n+f(n-1);
}
int main()
{
int n;
cin>>n;
cout<<f(n)<<endl;
}
#3
Solution:
#include<bits/stdc++.h>
using namespace std;
long long arr[200][200];
long long n,m,k;
void rf(long long i,long long j,long long x)
{
if(j==n)
return;
arr[i][j]=x;
rf(i,j+1,x-1);
}
void cf(long long i,long long j,long long x)
{
if(i==n)
return;
arr[i][j]=x;
cf(i+1,j,x-1);
}
void f(long long i,long long j,long long x)
{
if(i==n || j==n)
return ;
arr[i][j]=x;
rf(i,j+1,x-1);
cf(i+1,j,x-1);
f(i+1,j+1,x+k);
}
int main()
{
long long i,j;
cin>>n>>m>>k;
f(0,0,m);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
Discussion: In this lab I have learned about backtracking & recursive process which
was very useful for us and solve solve some problem using these methode. I have
solved 1 problem in the lab.I have solved all the problem that are given as homework
and also solve all the problem that was given in the lab. But I faced some problem while
I was solving these .With the help of my team leader I could find the problem and fixed
it. It is very difficult for me to dry the code. My team mates helped me a lot while solving
those problem. I wish with the knowledge I gather in the lab, I would solve many
problem as possible in future.