DAA Journal
DAA Journal
Year: 2020-21
Page | 1
DAA LAB EXPERIMENTS
INDEX
Page | 2
Experiment No.1: Implementing The Binary Search
Aim:
To study the Binary Search and implementing it.
Theory:
Page | 3
Page | 4
Page | 5
Algorithm:
Page | 6
Time Complexity:
Page | 7
Conclusion:
We have completed understanding the Binary Search.
Codes:
Page | 8
Output:
Page | 9
Page | 10
Experiment No.2: Implementing The Merge Sort
Aim:
To study the Merge Sort and implementing it.
Theory:
Page | 11
Page | 12
Algorithm:
Time Complexity:
Page | 13
Page | 14
Page | 15
Conclusion:
We have completed studying the merge sort.
Codes:
Page | 16
Output:
Page | 17
Experiment No.3: Implementing The Selection Sort
Aim:
To study the Selection Sort and implementing it.
Theory:
Page | 18
Page | 19
Algorithm:
Page | 20
Time Complexity:
Page | 21
Conclusion:
We have completed studying the selection sort.
Code:
Page | 22
Output:
Page | 23
Experiment No.4: Implementing The Insertion Sort
Aim:
To study the Insertion Sort and implementing it.
Theory:
Page | 24
Page | 25
Page | 26
Algorithm:
Time Complexity:
Page | 27
Page | 28
Conclusion:
We have completed studying the insertion sort.
Code:
Page | 29
Output:
Page | 30
Experiment No.5: Implementing The BFS
Aim:
To study the BFS and implementing it.
Theory:
Algorithm:
Page | 31
Time Complexity:
Conclusion:
We have completed studying the BFS.
Code:
Page | 32
Page | 33
Page | 34
Page | 35
Output:
Page | 36
Experiment No.6: Implementing The DFS
Aim:
To study the DFS and implementing it.
Theory:
Page | 37
Algorithm:
Time Complexity:
Conclusion:
We have completed studying the DFS.
Page | 38
Code:
Output:
Page | 39
Experiment No.7: Solving Knapsack Problem Using Backtracking
Aim:
To solve Knapsack Problem using Backtracking.
Theory:
The knapsack problem is a problem in combinatorial optimization. It derives its name
from the problem faced by someone who is constrained by a fixed-size knapsack and must
fill it with the most valuable items. The problem often arises in resource allocation where the
decision makers have to choose from a set of non-divisible projects or tasks under a fixed
budget or time constraint, respectively.
Given a set of items, each with a weight and a value, determine the number of each
item to include in a collection so that the total weight is less than or equal to a given limit
and the total value is as large as possible i.e weights and values of n items, put these items in
a knapsack of capacity W to get the maximum total value in the knapsack. In other words,
given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights
associated with n items respectively. Also given an integer W which represents knapsack
capacity, find out the maximum value subset of val[] such that sum of the weights of this
subset is smaller than or equal to W. You cannot break an item, either pick the complete item
or don’t pick it (0-1 property).
Algorithm:
// cp is the current profit total, cw is the current
// weight total; k is the index of the last removed
// item; and m is the knapsack size.
{
b :=cp; c :=cw;
for i :=k + 1to n do
{
c :=c+ w[i];
else return b+(1- (c-m)/w[i])*p[i];
}
return b;
}
Time Complexity:
Page | 40
Time complexity of 0-1 knapsack problem is O (nw) where n is number of elements in
knapsack and w is a capacity of knapsack.
Each entry of the table requires constant time θ(1) for its computation. It takes θ(nw)
time to fill (n+1)(w+1) table entries. It takes θ(n) time for tracing the solution since tracing
process traces the n rows.
Thus, overall θ(nw) time is taken to solve 0/1 knapsack problem.
Conclusion:
We have completed studying the Knapsack problem using backtracking.
Code:
program:
#include <stdio.h>
#include <conio.h>
#define max 10
int w[max],i,j,p[max];
int n,m;
float unit[max];
int y[max],x[max],fp=-1,fw;
void get()
{
printf("\n Enter total number of items:" );
scanf("%d",&n);
printf("\n Enter the Maximum capacity of the Sack: ");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("\n Enter the weight of the item # %d : ",i+1);
scanf("%d",&w[i]);
printf("\n Enter the profit of the item # %d : ", i+1);
scanf("%d", &p[i]);
}
}
void show()
Page | 41
{
float s=0.0;
printf("\n\tItem\tWeight\tCost\tUnit Profit\tSelected ");
for(i=0;i<n;i++)
printf("\n\t%d\t%d\t%d\t%f\t%d",i+1,w[i],p[i],unit[i],x[i]);
printf("\n\n The Sack now holds following items : ");
for(i=0;i<n;i++)
if(x[i]==1)
{
printf("%d\t",i+1);
s += (float) p[i] * (float) x[i];
}
printf("\n Maximum Profit: %f ",s);
}
/*Arrange the item based on high profit per Unit*/
void sort()
{
int t,t1;
float t2;
for(i=0;i<n;i++)
unit[i] = (float) p[i] / (float) w[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(unit[i] < unit[j])
{
t2 = unit[i];
unit[i] = unit[j];
unit[j] = t2;
t = p[i];
p[i] = p[j];
p[j] = t;
Page | 42
t1 = w[i];
w[i] = w[j];
w[j] =t1;
}
}
}
}
float bound(float cp,float cw,int k)
{
float b = cp;
float c = cw;
for(i=k;i<=n;i++)
{
c = c+w[i];
if( c < m)
b = b +p[i];
else
return (b+(1-(c-m)/ (float)w[i])*p[i]);
}
return b;
}
void knapsack(int k,float cp,float cw)
{
if(cw+w[k] <= m)
{
y[k] = 1;
if(k <= n)
knapsack(k+1,cp+p[k],cw+w[k]);
if(((cp+p[k]) > fp) && ( k == n))
{
fp = cp+p[k];
fw = cw+w[k];
for(j=0;j<=k;j++)
Page | 43
x[j] = y[j];
}
}
if(bound(cp,cw,k) >= fp)
{
y[k] = 0;
if( k <= n)
knapsack(k+1,cp,cw);
if((cp > fp) && (k == n))
{
fp = cp;
fw = cw;
for(j=0;j<=k;j++)
x[j] = y[j];
}
}
}
void main()
{
printf("\n\n\n\t\t ******** KNAPSACK PROBLEM ********");
get();
printf("\n The Sack is arranged in the order is \n");
sort();
knapsack(0,0.0,0.0);
show();
getch();
}
Page | 44
Page | 45
Page | 46
Output:
Page | 47
Experiment 8: Solving N-Queen’s Problem Using Backtracking
Aim:
To solve N-Queen’s Problem using Backtracking.
Theory:
N-Queens Problem is a famous puzzle in which n-queens are to be placed on a (n
x n) chess board such that no two queens are in the same row, column or diagonal.
Backtracking is used to solve the problem.
The concept behind backtracking algorithm which is used to solve this problem is to
successively place the queens in columns/row. If we see that the queen is under attack at its
chosen position, we try the next position.
If a queen is under attack at all the positions in a row, we backtrack and change the
position of the queen placed prior to the current position. Repeat this process of placing a
queen and backtracking until all the N queens are placed successfully.
Algorithm:
1 Algorithm Place(k,i)
2 // Returns true if a queen can be placed in kth row and
3 // ith column. Otherwise it returns false. x[] is a
4 // global array whose first (k -1) values have been set.
5 // Abs(r) returns the absolute value of r.
6{
7 for j :=1to k-1do
8 if ((x[j]= i) // Two in the same column
9 or {Abs(x[j}-i) = Abs(j- k)))
10 // or in the same diagonal
11 then return false;
12 return true;
13 }
Page | 48
1 Algorithm NQueens(k, n)
2 // Using backtracking, this procedure prints all
3 // possible placements of n queens on an n x n
4 // chessboard so that they are nonattacking.
5{
6 for i :=1to n do
7 {
8 if Place(k, i)then
9 {
10 x[k]:=i;
11 if (k = n) then write (x[l : n]);
12 else NQueens(k+l, n);
13 }
14 }
15 }
Time Complexity:
Now number of possible arrangements of N Queens on n×n chessboard is n!, given you
are skipping row or column, already having a queen placed. For each invocation of the place
Queen method, there is a loop which runs for O(n) time.
In each iteration of this loop, there is Safe invocation which is O(n) and a recursive
call with a smaller argument.
If we add all this up and define the run time as T(n). Then T(n) = O(n 2) + n*T(n-1).
solving the above recurrence by iteration or recursion tree, the time complexity of the n-
Queen problem is = O(n).
So the worst, average and best case complexity remains O(n!).
Conclusion:
We have completed studying the N-Queen’s problem using backtracking.
Code:
Page | 49
program:
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
printf(" - N Queens Problem Using Backtracking -");
printf("\n\nEnter number of Queens:");
scanf("%d",&n);
queen(1,n);
return 0;
}
//function for printing the solution
void print(int n)
{
int i,j;
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
/*funtion to check conflicts
If no conflict for desired postion returns 1 otherwise returns 0*/
int place(int row,int column)
{
int i;
Page | 50
for(i=1;i<=row-1;++i)
{
//checking column and digonal conflicts
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1; //no conflicts
}
//function to check for proper positioning of queen
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}
Page | 51
Output:
Page | 52
Page | 53
Experiment 9: Solving Graph Coloring Problem Using Backtracking
Aim:
To solve Graph coloring Problem using Backtracking.
Theory:
Graph coloring is the procedure of assignment of colors to each vertex of a graph G such
that no adjacent vertices get same color. The objective is to minimize the number of colors while
coloring a graph. The smallest number of colors required to color a graph G is called its chromatic
number of that graph.
Using the Backtracking approach the idea is to assign colors one by one to different
vertices, starting from the vertex 0. Before assigning a color, check for safety by considering
already assigned colors to the adjacent vertices i.e check if the adjacent vertices have the same
color or not. If there is any color assignment that does not violate the conditions, mark the color
assignment as part of the solution. If no assignment of color is possible then backtrack and return
false.
Algorithm:
1. Create a recursive function that takes the graph, current index, number of vertices, and
output color array.
2. If the current index is equal to the number of vertices. Print the color configuration in
output array.
3. Assign a color to a vertex (1 to m).
4. For every assigned color, check if the configuration is safe, (i.e. check if the adjacent
vertices do not have the same color) recursively call the function with next index and
number of vertices
5. If any recursive function returns true break the loop and return true.
If no recursive function returns true then return false.
Time Complexity:
Since backtracking is also a kind of brute force approach, there would be total O(m V)
possible color combinations. It is to be noted that the upperbound time complexity remains the
same but the average time taken will be less due to the refined approach.
Conclusion:
We have completed studying the graph coloring problem using backtracking.
Page | 54
Code:
program:
#include<stdio.h>
int G[50][50],x[50]; //G:adjacency matrix,x:colors
void next_color(int k)
{
int i,j;
x[k]=1; //coloring vertex with color1
for(i=0;i<k;i++)
{ //checking all k-1 vertices-backtracking
if(G[i][k]!=0 && x[k]==x[i]) //if connected and has same color
x[k]=x[i]+1; //assign higher color than x[i]
}
}
int main()
{
int n,e,i,j,k,l;
printf("Enter no. of vertices : ");
scanf("%d",&n); //total vertices
printf("Enter no. of edges : ");
scanf("%d",&e); //total edges
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
G[i][j]=0; //assign 0 to all index of adjacency matrix
printf("Enter indexes where value is 1-->\n");
for(i=0;i<e;i++){
scanf("%d %d",&k,&l);
G[k][l]=1;
G[l][k]=1;
}
for(i=0;i<n;i++)
next_color(i); //coloring each vertex
printf("Colors of vertices -->\n");
for(i=0;i<n;i++) //displaying color of each vertex
printf("Vertex[%d] : %d\n",i+1,x[i]);
return 0;
}
Page | 55
Output:
Page | 56
Page | 57
Experiment No.10: Solving Travelling Salesman Problem Using
Backtracking and Dynamic Programming
Aim:
To solve Travelling Salesman Problem using backtracking and dynamic programming.
Theory:
Dynamic Programming:
The TSP problem is stated as ‘given a set of n-cities and distance between each pair of cities
in the minimum length path such that it covers The city exactly once and terminates the tour at
the starting point’
This is a complete NP problem. There are ‘n!’ parts and the search of the optimal path
becomes slow when n is considerably large.
Each edge(u,v) in the TSP graph is assigned some non-negative weight, which represents
the distance between city u and v. This problem can be solved by finding the hamiltonian cycle of
the graph.
The distance between cities is best described by a weighted graph where edge (u,v) indicates
the path from u to v) and w(u,v) represents dist btw u and v.
Formulating the solution TSP using dynamic programming:
1. Let d[i,j] indicate the dist. btw the cities i and j. Function ([x, V-{x}]) is the cost of the path
starting from city x. V is the set of the cities in the given graph. The aim of TSP is to minimise
the cost function.
2. Assume that the graph contains ‘n’ vertices V1, V2, … Vn. TSP finds a path covering all the
vertices exactly once and at the same time it tries to minimise the overall travelling distance.
3. Mathematical formula to find minimum distance is:
C(i,V)= min{d[i,j] + C(j, V - {j},)}, j ∈ V and i ∈ V.
4. TSP possesses the principle of optimality, ie. for d[V1,Vn) to be minimum, any intermediate path
(Vi,Vj) must be minimum.
Page | 58
Algorithm:
Dynamic Programming:
Dynamic Programming Approach for TSP:
Data: s: starting point; N: a subset of input cities;
dist(): distance among the cities
Result: Cost : TSP result
Visited/NJ = O;
Cost= O;
Procedure TSP(N, s)
Visited[s] = 1;
if N = 2 and k ≠ s then
Cost(N,k) = dist(s,k)
Return cost;
else
for j ∈ N do
for I ∈ N and visited [i] = 0 do
if j ≠ s then
cost(N,j) = min(TSP(N-{i}, j) + dist(j,i))
Visited[j] = 1;
end
end
end
end
return cost;
end
Time Complexity:
Dynamic Programming:
Dynamic programming approach contains sub-problems. Here after reaching xth node,
finding the remaining minimum distance to that xth node is a sub-problem.
Solving by recursive equations, we will get total (n-1)2(n-2) sub-problems, which is O(n2).
Each sub-problem will take O(n) time.
Therefore total time complexity is O(n2n)*O(n) = O(n22n).
The space time complexity is O(n2n).
Backtracking Approach:
Time complexity for backtracking is same as dynamic programming that is
O(n2n)*O(n) = O(n22n)
The worst case (n -1)! permutation, each has to do n -1 addition to find cost so
(n-1) * (n-1) → O(n!)
Conclusion:
We have completed studying the traveling salesman problem using backtracking and
dynamic programming.
Code:
Page | 60
Dynamic Programming:
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
printf("Enter the number of cities: ");
scanf("%d",&n);
printf("\nEnter the Cost Matrix\n");
for(i=0;i < n;i++)
{
printf("\nEnter Elements of Row: %d\n",i+1);
for( j=0;j < n;j++)
scanf("%d",&ary[i][j]);
completed[i]=0;
}
printf("\n\nThe cost list is:");
for( i=0;i < n;i++)
{
printf("\n");
for(j=0;j < n;j++)
printf("\t%d",ary[i][j]);
}
}
void mincost(int city)
{
int i,ncity;
completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
Page | 61
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i < n;i++)
{
if((ary[c][i]!=0)&&(completed[i]==0))
if(ary[c][i]+ary[i][c] < min)
{
min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
int main()
{
takeInput();
printf("\n\nThe Path is:\n");
mincost(0); //passing 0 because starting vertex
printf("\n\nMinimum cost is %d\n ",cost);
return 0;
}
Page | 62
Output:
Enter the number of cities: 4
Minimum cost is 7
Page | 63
Page | 64
Backtracking:
#include<stdio.h>
#include<stdlib.h>
int DistanceMatrix[10][10],VisitedCities[10],n,cost=0;
void getData()
{
int i,j;
printf("\n\nEnter number of cites-->");
scanf("%d",&n);
printf("\nEnter(%d x %d) DistanceMatrix: \n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&DistanceMatrix[i][j]);
Page | 65
}
VisitedCities[i]=0;
}
printf("\n\nThe DistanceMatrix is: \n");
for(i=0;i<n;i++)
{
printf("\n\n");
for(j=0;j<n;j++)
{
printf("\t%d",DistanceMatrix[i][j]);
}
}
}
void mincost(int city)
{
int i,ncity;
VisitedCities[city]=1;
printf("%d-->",city+1);
ncity = least(city);
if(ncity==999)
{
ncity = 0;
printf("%d",ncity+1);
cost+=DistanceMatrix[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i<n;i++)
Page | 66
{
if((DistanceMatrix[c][i]!=0)&&(VisitedCities[i]==0))
if(DistanceMatrix[c][i]<min)
{
min = DistanceMatrix[i][0]+DistanceMatrix[c][i];
kmin = DistanceMatrix[c][i];
nc=i;
}
}
if(min!=999)
{
cost+=kmin;
}
return nc;
}
void DisplayPath()
{
printf("\n\nMinimum cost:");
printf("%d",cost);
}
int main()
{
getData();
printf("\n\nThe Path is:\n\n");
mincost(0);
DisplayPath();
}
Output:
Enter number of cities -- > 4
Enter (4 x 4) DistanceMatrix:
0467
Page | 67
4063
5906
2150
Minimum cost:19
Page | 68
Output:
Dynamic Programming:
Page | 69
Backtracking:
Page | 70
Page | 71