You are on page 1of 63

INDEX

S.NO DATE NAME OF THE PROGRAM PAGE.NO SIGNATURE

1 MERGESORT USING DIVIDE AND


CONQUER METHOD

2 STRASSEN’S MATRIX
MULTIPLICATION USING DIVIDE
AND CONQUER METHOD

3 KNAPSACK PROBLEM USING


DYNAMIC PROGRAMMING

4 MINIMUM SPANNING TREE USING


GREEDY METHOD

5 WARSHALL’SALGORITHM USING
DYNAMIC PROGRAMMING

6 DIJKSRTA’S ALGORITHM USING


GREEDY TECHNIQUE

7 SUBSET SUM PROGRAM USING


BACKTRACKING

8-QUEENS PROBLEM USING


8 BACKTRACKING

9 KNAPSACK PROBLEM USING


BACKTRACKING

10 TRAVELLING SALESPERSON
USING BRANCH AND BOUND
TECHNIQUE
AIM:

To write a C++ program to the divide & conquer technique to arrange a set of numbers using
merge sort method.

ALGORITHM:
1. MERGESORT USING DIVIDE AND CONQUER METHOD

SOURCE CODE:

#include<iostream>

#include<conio.h>

using namespace std;

void mergesort(int *,int,int);

void merge(int *,int,int,int);

int a[20],i,n,b[20];

main()

cout<<"\n enter no of elements";

cin>> n;

cout<<"enter the elements";

for(i=0;i<n;i++)

cin>> a[i];

mergesort(a,0,n-1);

cout<<" numbers after sort";

for(i=0;i<n;i++)

cout<< a[i] << " ";

getch();

void mergesort(int a[],inti,int j)

int mid;

if(i<j)

{
mid=(i+j)/2;

mergesort(a,i,mid);

mergesort(a,mid+1,j);

merge(a,i,mid,j);

void merge(int a[],intlow,int mid ,int high)

inth,i,j,k;

h=low;

i=low;

j=mid+1;

while(h<=mid && j<=high)

if(a[h]<=a[j]

b[i]=a[h++];

else

b[i]=a[j++];

i++;

if( h > mid)

for(k=j;k<=high;k++)

b[i++]=a[k];

else

for(k=h;k<=mid;k++)

b[i++]=a[k];

cout<<"\n";

for(k=low;k<=high;k++)
{ a[k]=b[k];

cout<< a[k] <<" ";

}
OUTPUT:

Enter no of elements

enter the elements

26

15

1 2 5 6 number after sort1 2 5 6


RESULT:

Thus the mergesort program has been executed successfully.


AIM:

To write a C++ program to perform strassen’s matrix multiplication using divide and conquer
method.

ALGORITHM:
2. STRASSEN’S MATRIX MULTIPLICATION USING

DIVIDE AND CONQUER MERHOD

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

void main()

int a[2][2],b[2][2],c[2][2];

int m1,m2,m3,m4,m5,m6,m7,i,j;

cout<<"Matrix Multiplication Strassen's method by-TarunRawat\n";

cout<<"Enter the elements of 2x2 Matrix 1:\n";

for(i=0;i<2;i++)

for(j=0;j<2;j++)

cin>>a[i][j];

cout<<"Enter the elements of 2x2 Matrix 2:\n";

for(i=0;i<2;i++)

for(j=0;j<2;j++)

cin>>b[i][j];

cout<<"\nFirst matrix is:\n";


for(i=0;i<2;i++)

for(j=0;j<2;j++)

cout<<a[i][j];

cout<<"\n";

cout<<"\nSecond matrix is\n";

for(i=0;i<2;i++)

for(j=0;j<2;j++)

cout<<b[i][j];

cout<<"\n";

m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);

m2= (a[1][0]+a[1][1])*b[0][0];

m3= a[0][0]*(b[0][1]-b[1][1]);

m4= a[1][1]*(b[1][0]-b[0][0]);

m5= (a[0][0]+a[0][1])*b[1][1];

m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);

m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7;

c[0][1]=m3+m5;

c[1][0]=m2+m4;

c[1][1]=m1-m2+m3+m6;
cout<<"\nProduct of both is:\n";

for(i=0;i<2;i++)

for(j=0;j<2;j++)

cout<<c[i][j];

cout<<"\n";

getch();

}
OUTPUT:

Matrix Multiplication Strassen's method by-TarunRawat

Enter the elements of 2x2 Matrix 1:

12

34

Enter the elements of 2x2 Matrix 2:

43

21

First matrix is:

12

34

Second matrix is

43

21

Product of both is:

85

20 13
RESULT:

Thus the strassen’s matrix multiplication program has been executed successfully.
AIM:

To write a C++ program to Solve the knapsack problem using dynamic programming.

ALGORITHM:
3. KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

#include<process.h>

constant max=30;

int MFKS(int,int);

int maximum(int,int);

int value[max];

int weight[max];

int v[max][max];

int x[max];

int N,M;

int MFKS(inti,int j)

int val;

if(v[i][j]==-1)

if(j<weight[i]) val=MFKS(i-1,j);

else val=maximum(MFKS(i-1,j),(MFKS(i-1,j-weight[i])+value[i]));

v[i][j]=val;

return v[i][j];

void svector()
{

int j1;

j1=M;

for(int i=N;i>=0;i--)

if(v[i][j1]!=v[i-1][j1])

x[i]=1;

j1=j1-weight[i];

for(i=1;i=N;i++)

cout<<"{";

for(i=1;i<=N;i++)

cout<<x[i],";";

cout<<"}";

intmaximum(inta,int b)

return (a>b)?a:b;

void main()
{

int optsol;

cout<<"Enter no. of items:";

cin>>N;

cout<<"Enter weights:\n";

for(int i=1;i<=N;i++)

cin>>weight[i];

cout<<"Enter values:\n";

for(i=1;i<=N;i++)

cin>>value[i];

cout<<"Enter Knapsack capacity:";

cin>>M;

for(i=0;i<=M;i++)

v[0][i]=0;

for(i=0;i<=N;i++)

v[i][0]=0;

for(i=1;i<=N;i++)

for(int j=1;j<=M;j++)

v[i][j]=-1;

optsol=MFKS(N,M);

cout<<"Optimal solution = optsol"<<endl;

cout<<"Optimal solution vector is:\n";

for(i=1;i=N;i++)

x[i]=0;

svector();

cout<<endl;

getch();

}
OUTPUT:

Enter no. of items: 4

Enter weights:
2132

Enter values:
12 10 20 15

Enter Knapsack capacity:5


Optimal solution = 37
Optimal solution vector is:
{1; 1; 0; 1 ;}
RESULT:

Thus the strassen’s matrix multiplication program has been executed successfully.
AIM:

To write a C++ program to construct a minimum spanning tree using greedy method.

ALGORITHM:
4. MINIMUM SPANNING TREE USING GREEDY METHOD

SOURCE CODE:

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;

main()

int m,c;

cout<<"enterno of vertices";

cin>> n;

cout<<"enter no of edges";

cin>> m;

cout<<"\nEDGES Cost\n";

for(k=1;k<=m;k++)

cin>>i>>j>>c;

cost[i][j]=c;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]==0)

cost[i][j]=31999;

cout<<"ORDER OF VISITED VERTICES";

k=1;

while(k<n)
{

m=31999;

if(k==1)

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

if(cost[i][j]<m)

m=cost[i][j];

u=i;

else

for(j=n;j>=1;j--)

if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1)

visit[j]=1;

stk[top]=j;

top++;

m=cost[v][j];

u=j;

cost[v][u]=31999;

v=u;

cout<<v << " ";

k++;
visit[v]=0; visited[v]=1;

}
OUTPUT:

enter no of vertices7
enter no of edges9
EDGES Cost
1 6 10
6 5 25
5 4 22
4 3 12
3 2 16
2 7 14
5 7 24
4 7 18
1 2 28
ORDER OF VISITED VERTICES1 6 5 4 3 2
RESULT:

Thus the minimum spanning tree program has been executed successfully.
AIM:

To write a C++ program to perform warshall’s algorithm using dynamic programming.

ALGORITHM:

5. WARSHALL’S ALGORITHM USING DYNAMIC PROGRAMMING


SOURCE CODE:

#include<iostream.h>

#include<conio.h>

void floyds(int b[][7])

inti,j,k;

for(k=0;k<7;k++)

for(i=0;i<7;i++)

for(j=0;j<7;j++)

if((b[i][k]*b[k][j]!=0)&&(i!=j))

if((b[i][k]+b[k][j]<b[i][j])||(b[i][j]==0))

b[i][j]=b[i][k]+b[k][j];

for(i=0;i<7;i++)

cout<<"\n Minimum Cost With Respect to Node:"<<i<<endl;

for(j=0;j<7;j++)

cout<<b[i][j]<<"\t";
}

intmain()

intb[7][7];

cout<<"ENTER VALUES OF ADJACENCY MATRIX \n\n";

for(int i=0;i<7;i++)

cout<<"enter values for"<<(i+1)<<"row"<<endl;

for(int j=0;j<7;j++)

cin>>b[i][j];

floyds(b);

getch();

return 0;

OUTPUT:
RESULT:

Thus the warshall’s algorithm program has been executed successfully.


AIM:

To write a C++ program to solve Dijkstra’s algorithm using greedy technique.

ALGORITHM:

6. DIJKSTRA’S ALGORITHM USING GREEDY TECHNIQUE


SOURCE CODE:

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

intshortest(int,int);

int cost[10][10],dist[20],i,j,n,k,m,s[20],v,totcost,path[20],p;

void main()

int c;

cout<<"enter number of vertices";

cin>>n;

cout<<"enter number 0f edges";

cin>>m;

cout<<"\n enter \n EDGE Cost \n";

for(k=1;k<=m;k++)

cin>>i>>j>>c;

cost[i][j]=c;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]==0)

cost[i][j]=31999;

cout<<"enter initial vertex";

cin>>v;

cout<<v<<"\n";

shortest(v,n);

}
intshortest(intv,int n)

int min;

min;

for(i=1;i<=n;i++)

s[i]=0;

dist[i]=cost[v][i];

path[++p]=v;

s[v]=1;

dist[v]=0;

for(i=2;i<=n-1;i++)

k=-1;

min=31999;

for(j=1;j<=n;j++)

if(dist[j]<min&&s[j]!=1)

min=dist[j];

k=j;

if(cost[v][k]<=dist[k])

p=1;

path[++p]=k;

for(j=1;j<=p;j++)
cout<<path[j];

cout<<"\n";

//cout<<k;

s[k]=1;

for(j=1;j<=n;j++)

if(cost[k][j]!=31999&&dist[j]>=dist[k]+cost[k][j]&&s[j]!=1)

dist[j]=dist[k]+cost[k][j];

return 0;

OUTPUT:
enter number of vertices6

enter number of edges11

enter

EDGE Cost

1 2 50

1 3 45

1 4 10

2 3 10

2 4 15

3 5 30

2 1 10

4 5 15

5 2 20

5 3 35

253

enter initial vertex1

14

145

1452

13
RESULT:

Thus the Dijkstra’s algorithm program has been executed successfully.


AIM:

To write a C++ program to solve subset sum problem using backtracking.

ALGORITHM:

7. SUBSET
SOURCE CODE:

#include<iostream.h>

#include<stdlib.h>

#include<conio.h>

int *S,*A,C,n;

void main()

inti,j,k,sum;

cout<<"Enter C and n\n";

cin>>C;

cin>>n;

S=(int*)malloc((n+1)*sizeof(int));

A=(int*)malloc((C+1)*sizeof(int));

if(!S||!A)

cout<<"malloc failed\n",__LINE__;

exit(0);

cout<<"Enter set of positive numbers are\n";

S[0]=0;

for(i=1;i<=n;i++)

cin>>S[i];

for(i=0;i<=C;i++)

A[i]=(-1);

A[0]=0;

for(i=0;i<=n;i++)

for(j=A[i]+1;j<=n;j++)

{
k=i+S[j];

if(k>C)

continue;

if(A[k]==(-1)||A[k]>j)

cout<<"changed to\t"<<j;

cout<<"\n";

A[k]=j;

if(C<=50)

cout<<"i\t\tS[i]\n";

cout<<".........................\n";

for(i=1;i<=n;i++)

cout<<"\n"<<i<<"\t\t"<<S[i];

//cout<<"\n\n A[i] \t\t i \n";

//cout<<".........................\n";

for(i=1;i<=C;i++)

//cout<<"\t"<<i;

if(A[i]==(-1))

cout<<"\nIMPOSSIBLE\n";

else{

//cout<<"\n"<<(A[i])<<S[A[i]];

if(A[C]==(-1))
cout<<"\n No solution\n";

else

cout<<"\n\nSolution is:\n";

sum=C;

while(sum>0)

cout<<"\n"<<"\t\t"<<S[A[sum]];

sum=S[A[sum]];

free(S);

free(A);

getch();

OUTPUT:
RESULT:

Thus the subset sum program has been executed successfully.


AIM:

To write a C++ program to implement 8-queens problem using backtracking.

ALGORITHM:

8. 8-QUEENS PROBLEMS USING BACKTRACKING


SOURCE CODE:

#include<iostream.h>

intx,y,i,n,count,queen=0,a[20][20];

void equation(int);

void disp();

void change(int);

intmain()

cout<<"\n N-QUEEN PROBLEM USING BACKTRACKING METHOD:\N";

cout<<"\n.......................";

cout<<"\n Enter the number of queens:";

cin>>n;

cout<<"\n Queen position is:\n",n;

equation(0);

return 0;

void change(int k)

for(i=0;i<n;i++)

a[i][k]=0;

void disp()

inti,j;

count++;

if(queen==0)

for(i=0;i<n;i++)
{

for(j=0;j<n;j++)

if(a[i][j])

queen=1;

if(x&&y)

x=i+1;

y=j+1;

cout<<"Q";

else

cout<<"*";

cout<<"\n\n";

cout<<"\n";

//cout<<"\n The queen place is",x+1,y+1;

void equation(int k)

int i, j, r,count1;

if(queen==0)

if(k==n)

disp();
return;

r=0;

while(r<n)

count1=1;

for(j=0;j<n;j++)

if(a[r][j]==1)

count1=0;

i=r;

j=k;

while(count1&&(i<n)&&(j<n))

if(a[i][j])

count1=0;

i++;

j++;

i=r;

j=k;

while(count1&&(i>-1)&&(j>-1))

if(a[i][j])

count1=0;

i--;

j--;

i=r;
j=k;

while(count1&&(i>-1)&&(j>-1))

if(a[i][j])

count1=0;

i--;

j++;

i=r;

j=k;

while(count1&&(i<n)&&(j>-1))

if(a[i][j])

count1=0;

i++;

j--;

if(count1)

a[r][k]=1;

equation(k+1);

change(k);

r++;

}
OUTPUT:

N-QUEEN PROBLEM USING BACKTRACKING METHOD:N

……………………

Enter the number of queens:8

Q*******

******Q*

****Q***

*******Q

*Q******

***Q****

****Q***

**Q*****
RESULT:

Thus the 8-queens program has been executed successfully.


AIM:

To write a C++ program toimplement knapsack problem using backtracking.

ALGORITHM:

9. KNAPSACK PROBLEM USING BACKTRACKING


SOURCE CODE:

#include<iostream.h>

#include<conio.h>

#include<math.h>

float p[10]={0},w[10]={0},y[10]={0},x[10]={0};

inti,n,max,k,cp=0,cw=0,fp,fw;

class back

public:

void get();

void knapsack(int,int,int);

intbound(int,int,int);

};

void back::get()

int i;

cout<<"\nEnter the capacity:";

cin>>max;

cout<<"\n\nEnter the number of object:";

cin>>n;

for(i=1;i<=n;i++)

cout<<"Enter the weight of the object{w}"<<i,i;

cout<<":";

cin>>w[i];

cout<<"Enter the profit of the object{p}"<<i,i;

cout<<":";

cin>>p[i];
}

void back::knapsack(intk,intcp,intcw)

int j;

if(cw+w[k],+max)

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=1;j<=n;++j)

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=1;j<=n;j++)
x[j]=y[j];

int back::bound(intcp,intcw,int k)

inti,b,c;

b=cp;

c=cw;

for(i=k+1;i<=n;i++)

c=c+w[i];

if(c<max)

b=b+p[i];

else

return(b+(1-(c-max)/w[i])*p[i]);

return b;

void main()

cout<<"\n\nKnapsack Using Backtracking\n";

back obj;

obj.get();
k=1;

cp=0;

cw=0;

obj.knapsack(k,cp,cw);

cout<<"\nSelected object are:\n";

for(i=1;i<=n;i++)

if(x[i]==1)

cout<<"\nobject:"<<""<<i;

cout<<"\nMax Profit of the knapsack is:"<<fp;

cout<<"\nTotal weight of the knapsack is:"<<fw;

getch();

OUTPUT:
Knapsack Using Backtracking

Enter the capacity:20

Enter the number of object:3

Enter the weight of the object{w}1:20

Enter the weight of the object{p}1:34

Enter the weight of the object{w}2:25

Enter the weight of the object{p}2:39

Enter the weight of the object{w}3:13

Enter the weight of the object{p}3:44

Selected object are:

object:1

object:2

object:3

Max Profit of the knapsack is:117

Total weight of the knapsack is:58


RESULT:

Thus the knapsack program has been executed successfully.


AIM:

To write a C++ program to find the solution of travelling salesperson problem using branch
and bound.

ALGORITHM:

10. TRAVELLING SALESPERSON PROBLEM USING BRANCH AND BOUND


SOURCE CODE:

#include<iostream.h>

#include<conio.h>

intmain()

inti,j,k,n,min,g[20][20],c[20][20],s,s1[20][1],s2,lb;

cout<<"\n TRAVELLING SALESMAN PROBLEM";

cout<<"\n Input number of cities:";

cin>>n;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

c[i][j]=0;

}}

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(i==j)

continue;

else

cout<<"input"<<i<<"to"<<j<<"cost:";

cin>>c[i][j];

}
}

for(i=2;i<=n;i++)

g[i][0]=c[i][1];

for(i=2;i<=n;i++)

for(j=2;j<=n;j++)

if(i!=j)

g[i][j]=c[i][j]+g[j][0];

for(i=2;i<=n;i++)

for(j=2;j<=n;j++)

if(i!=j)

break;

for(k=2;k<=n;k++){

if(i!=k&&j!=k){

if((c[i][j]+g[i][k])<(c[i][k]+g[k][j]))

g[i][j]=c[i][j]+g[j][k];

s1[i][j]=j;

}
else

g[i][1]=c[i][k]+g[k][j];

s1[i][1]=k;

min=c[1][2]+g[2][1];

s=2;

for(i=3;i<n;i++)

if((c[i][i]+g[i][i])<min)

min=c[1][i]+g[i][1];

s=i;

int y=g[i][1]+g[i][j]+g[i][i];

lb=(y/2);

cout<<"Edge Matrix";

for(i=1;i<=n;i++)

cout<<"\n";

for(j=1;j<=n;j++)

cout<<"\t"<<c[i][j];

}
cout<<"\n min"<<min;

cout<<"\n\b"<<lb;

for(i=2;i<=n;i++)

if(s!=i&&s1[s][1]!=i)

s2=i;

cout<<"\n"<<1<<"-->"<<s<<"-->"<<s1[s][1]<<"-->"<<s2<<"-->"<<1<<"\n";

getch();

return (0);

OUTPUT:
TRAVELLING SALESMAN PROBLEM

Input number of cities:3

input1to2cost:20

input1to3cost:12

input2to1cost:33

input2to3cost:23

input3to1cost:31

input3to2cost:12

Edge Matrix

0 20 12

33 0 23

31 12 0

min20

22

12031
RESULT:

Thus the travelling salesperson program has been executed successfully.

You might also like