Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

1.

STACK USING ARRAY

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define size 5
static int top=-1;
class stack
{
private:
int arr[size];
public:
void push(int item);
void pop();
void display();
};
void stack::push(int item)
{
if(top==size-1)
cout<<"stack overflow";
else
{
arr[++top]=item;
cout<<"the element"<<item<<"is inserted sucessfully";
}
}
void stack::pop()
{
if(top<0)
cout<<"stack underflow";
else
{
cout<<" /n element"<<arr[top]<<"is removed sucessfully";
top--;
}
}
void stack::display()
{
if(top<0)
cout<<"the stack is empty";
else
for(int i=top;i>=0;i--)
cout<<arr[i]<<"\n";
}
void main()
{
char choice;
int ch,num;
stack ob;
cout<<"\n stack";
cout<<"\n ......";
cout<<"\n 1.push";
cout<<"\n 2.pop";
cout<<"\n 3.display";
cout<<"\n 4.Exit";
do
{
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<" \nEnter the element you want to push";
cin>>num;
ob.push(num);
break;
case 2:
ob.pop();
break;
case 3:
ob.display();
break;
case 4:
exit(0);
break;
default:cout<<"Enter a valid choice";
}
}
while(ch<5);
getch();
}
2.QUEUE USING ARRAY

#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{
public:
int q[5],front,rear,x,result;
void enqueue();
void dequeue();
void display();
queue()
{
front=-1;
rear=-1;
}
};
void queue::enqueue()
{
if(rear>=4)
{
cout<<"Queue is full";
getch();
return;
}
rear++;
cout<<"\nEnter the element to be inserted:";
cin>>x;
q[rear]=x;
cout<<"\nThe inserted element is "<<q[rear];
if(front==-1)
front++;
}
void queue::dequeue()
{
if(front==-1)
{
cout<<"\nQueue underflow";
getch();
return;
}
cout<<"\nThe deleted element is";
result=q[front];
cout<<result;
if(front==rear)
front=rear=-1;
else
front++;
}
void queue::display()
{
int i;
if(front==-1)
{
cout<<"\nQueue is empty";
getch();
return;
}
cout<<"\nThe elements are:\n";
for(i=front;i<=rear;i++)
cout<<q[i]<<"\n";
}
void main()
{
int c;
queue qu;
clrscr();
cout<<"Queue";
cout<<"\n.....";
cout<<"\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit";
do{
cout<<"\nEnter your choice:";
cin>>c;
switch(c) {
case 1:
qu.enqueue();
break;
case 2:
qu.dequeue();
break;
case 3:
qu.display();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter a valid number";} }
while(c<5);
getch()
}
3.PRIORITY QUEUE

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int priority;
int info;
struct node *link;
};
class PQueue
{
private:
node *front;
public:
PQueue()
{
front=NULL;
}
void insert(int item,int priority)
{
node *tmp,*q;
tmp=new node;
tmp->info=item;
tmp->priority=priority;
if(front==NULL||priority<front->priority)
{
tmp->link=front;
front=tmp;
}
else
{
q=front;
while(q->link!=NULL&&q->link->priority<=priority)
q=q->link;
tmp->link=q->link;
q->link=tmp;
}
}
void del()
{
node *tmp;
if(front==NULL)
cout<<"Queue underflow";
else
{
tmp=front;
cout<<"deleted item is"<<tmp->info<<endl;
front=front->link;
free(tmp);
}
}
void display()
{
node *ptr;
ptr=front;
if(front==NULL)
cout<<"\n Queue is Empty...\n";
else
{
cout<<"QUEUE is:\n";
cout<<"priority item\n";
while(ptr!=NULL)
{
cout<<ptr->priority<<" "<<ptr->info<<endl;
ptr=ptr->link;
}
}
}
};
void main() {
int choice,item,priority;
clrscr();
PQueue pq;
cout<<"\n Priority queue";
cout<<"\n...............";
do
{
cout<<"\n insert";
cout<<"\n delete";
cout<<"\n Display";
cout<<"Quit";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n input the vale to be added to the queue";
cin>>item;
cout<<"\n Enter its prioriy:";
cin>>priority;
pq.insert(item,priority);
break;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
case 4:
break;
default:
cout<<"wrong choice...\n";
}
}
while(choice!=4);
getch()
}
4.BINARY SEARCH

#include<iostream.h>
#include<conio.h>
class bin_search {
int num[50],n,key;
public:
void getdata(void);
int searchfun(void);
void display(void);
};
void bin_search::getdata(void) {
cout<<endl;
cout<<"Enter the number of elements in the array:";
cin>>n;
cout<<"Enter"<<n<<"elements in sorted order:\n";
for(int i=0;i<n;i++)
cin>>num[i];
cout<<"Enter the elements to be searched:";
cin>>key;
}
int bin_search::searchfun(void) {
int found=0;
int high=n-1,low=0,mid;
while(high>=low) {
mid=(high+low)/2;
if(num[mid]==key{
found=1;
break;
}
else {
if(num[mid]>key)
high=mid-1;
else
low=mid+1;
}}
if(found==1)
return mid;
else
return -1;
}
void bin_search::display(void){
int result;
result=searchfun();
if(result==-1)
cout<<"\n\n Enterd search is invalid";
else
cout<<"\n\n search is located at"<<result<<"position\n";}
void main() {
clrscr();
cout<<"\nbinary search\n";
cout<<"\n.............";
bin_search bs;
bs.getdata();
bs.display();
getch(); }
5.QUICK SORT

#include<iostream.h>
#include<conio.h>
class Qsort
{
public:
void quicksort(int*,int,int);
};
void Qsort::quicksort(int a[],int low,int high)
{
int i=low,j=high,h;
int x=a[(low+high)/2];
do
{
while(a[i]<x)
i++;
while(a[j]>x)
j--;
if(i<=j)
{
h=a[j];
a[i]=a[j];
a[j]=h;
i++;
j--;
}
}
while(i<=j);
if(low<j)
quicksort(a,low,j);
if(i<high)
quicksort(a,i,high);
}
void main() {
int a[20],i,n;
clrscr();
cout<<"\nQuick sort";
cout<<"\n...........";
cout<<"\nEnter the no of elements";
cin>>n;
cout<<"\nEnter the elments";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nInitial array\n";
for(i=0;i<n;i++) {
cout<<a[i]<<"\t";
}
Qsort qs;
qs.quicksort(a,0,n-1);
cout<<"\n\n sorted elements\n";
for(i=0;i<n;i++) {
cout<<a[i]<<"\t";
}
getch();
}
6.STRASSEN’S MATRIX MULTIPLICATION

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main() {
int a[2][2],b[2][2],c[2][2];
int m1,m2,m3,m4,m5,m6,m7,i,j;
clrscr();
cout<<"\nSTRASSEN'S MATRIX";
cout<<"\n*******************";
cout<<"\nEnter the 4 elements in the first matrix";
for(i=0;i<2;i++){
for(j=0;j<2;j++){
cin>>a[i][j];
}
}
cout<<"\nEnter the 4 elements in the second matrix";
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]<<"\t";
}
cout<<"\n";
}
cout<<"\nsecond matrix is:\n";
for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
cout<<b[i][j]<<"\t";
}
cout<<"\n";
}
m1=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
m2=(a[1][0]+a[0][0])*(b[1][1]+b[0][0]);
m3=a[0][0]*(b[1][0]+b[1][1]);
m4=a[1][1]-(b[0][1]+b[0][0]);
m5=a[0][0]-(b[1][0]+b[1][1]);
m6=(a[1][1]+a[1][0])*(b[0][1]+b[0][0]);
m7=(a[0][1]+a[0][0])*(b[0][0]+b[1][0]);
c[0][0]=m1+m4-m5+m6;
c[1][0]=m3+m5;
c[0][1]=m2+m4;
c[1][1]=m1-m2+m3+m4+m5;
cout<<"\n product of matrix is:\n";
for(i=0;i<2;i++) {
for(j=0;j<2;j++)
cout<<c[i][j]<<"\t";{
cout<<"\n";
}}
getch();
}
7.PRIMS ALGORITHM

#include<iostream.h>
#include<conio.h>
#define SIZE 50
#define INFINITY 999
void prim(int G[][SIZE],int nodes) {
int select[SIZE],i,j,k;
int mindist,v1,v2,total=0;
for(i=0;i<nodes;i++)
select[i]=0;
cout<<"\n\n the minimum spanning tree is..\n";
select[0]=1;
for(k=1;k<nodes;k++)
{
mindist=INFINITY;
for(i=0;i<nodes;i++) {
for(j=0;j<nodes;j++) {
if(G[i][j]&&((select[i]&&!select[j])||(!select[i]&&select[j])))
{
if(G[i][j]<mindist)
{
mindist=G[i][j];
v1=i;v2=j;
}}}}}
cout<<"\n Edge("<<v1<<" "<<v2<<")and weight="<<mindist;
select[v1]=select[v2]=1;
total=total+mindist;
}
cout<<"\n\n totalpath length is="<<total;
}
void main() {
int G[SIZE][SIZE],nodes;
int v1,v2,wt,i,j,n;
clrscr();
cout<<"\n\t prim's algorithm";
cout<<"\n\t ****************";
cout<<"\n Enter the number of nodes in the graph:";
cin>>nodes;
cout<<"\n Enter the number of edges in the graph:";
cin>>n;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
G[i][j]=0;
cout<<"\n Enter edges and weight....\n";
for(i=0;i<n;i++) {
cout<<"\n enter edge by v1 and v2";
cin>>v1>>v2;
cout<<"\n Enter corresponding weight";
cin>>wt;
G[v1][v2]=G[v2][v1]=wt;}
getch();
cout<<"\n\t";
prim(G,nodes);
getch();
}
8.KRUSKALS ALGORITHM

#include<iostream.h>
#include<conio.h>
#define MAX 50
#define INFINITY 4000
int wt[MAX][MAX];
int edge[MAX][MAX];
int edge1,edge2;
int c=INFINITY;
int mstree[2*MAX];
int n;
int set[MAX];
int flag;
class kruskal
{
public:
void makeset(int);
void findmin(int);
void unionset(int,int);
int findset(int);
};
void kruskal::makeset(int n)
{
int i;
for(i=1;i<=n;i++)
{
set[i]=i;
}}
void kruskal::findmin(int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(edge[i][j]==1)
{
if(wt[i][j]<=c)
{
c=wt[i][j];
edge1=i;
edge2=j;
}
}
}
}
edge[edge1][edge2]=0;
c=INFINITY;
}
int kruskal::findset(int a)
{
return set[a];
}
void kruskal::unionset(int a,int b)
{
int z;
int temp;
temp=set[b];
set[b]=set[a];
for(z=0;z<n;z++)
{
if(set[z]==temp)
{
set[z]=set[a];
}}}
void main()
{
kruskal ks;
int i,j;
int k=0,sum=0,tedge=0;
clrscr();
cout<<"\n kruskal's algorithm";
cout<<"\n********************";
cout<<"\n Enter the number of vertices";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cout<<"Enter the weight of edge("<<i<<" ,"<<j<<");";
cin>>wt[i][j];
if(wt[i][j]==0) {
edge[i][j]=0;
edge[j][i]=0;
}
if(wt[i][j]!=0)
{
edge[i][j]=1;
edge[j][i]=1;
tedge=tedge+2;
}
}
ks.makeset(n);
for(i=1;i<=tedge;i++)
{
ks.findmin(n);
if(ks.findset(edge1)!=ks.findset(edge2)) {
mstree[k++]=edge1;
mstree[k++]=edge2;
edge[edge1][edge2]=0;
ks.unionset(edge1,edge2);
}}
k=0;
cout<<"Edge of spanning tree is \n";
for(i=0;i<n;i++) {
cout<<mstree[k+1]<<"-->"<<mstree[k]<<"and weight is="<<wt[mstree[k+1]][mstree[k]]<<"\n";
sum=sum+wt[mstree][k+1]][mstree[k]];
k=k+2;
}
cout<<"minimum cost is"<<sum;
getch();
}
9.ALL PAIR SHORTEST PATH

#include<iostream.h>
#include<conio.h>
#define INFINITY 2000
void main()
{
int a[20][20],b[20][20];
int i,j,k,n;
clrscr();
cout<<"\n All pair shortest path";
cout<<"\n......................";
cout<<"\n Enter the number of vertices";
cin>>n;
cout<<"\n Enter the weight of matrices";
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
{
cout<<"Node"<<i<<"to node"<<j<<":";
cin>>a[i][j];
}
cout<<endl;
}
for(i=1;i<=n;i++)
a[i][j]=0;
cout<<"The weighted matrix is...";
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
b[i][j]=a[i][j];
if(!a[i][j]&&(i!=j))
b[i][j]=INFINITY;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(b[i][k]&&b[k][j])
if(b[i][k]+b[k][j]<b[i][j]);
b[i][j]=b[i][k]+b[k][j];
cout<<"\n Resultant set shortest path matrix is...\n";
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) {
cout<<b[i][j]<<" ";
}
cout<<"\n";
}
getch();
}
10. N-QUEEN PROBLEM

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define MAX=10
class queen
{
public:
int fnplace(int k,int l,int x[10]);
void fnqueen(int k,int n);
};
int queen::fnplace(int k,int l,int x[10])
{
int j;
for(j=1;j<=k-1;j++)
{
if(x[j]==l||abs(j-k)==abs(x[j]-l))
return 0;
}
return 1;
}
void queen::fnqueen(int k,int n)
{
int l,j,p;
static int count,x[10];

for(l=1;l<=n;l++)
{
if(fnplace(k,l,x))
{
x[k]=l;
if(k==n)
{
cout<<"\nFeasible solution:"<<++count;
cout<<"\n\nSolution are:";
for(j=1;j<=n;j++)
cout<<x[j]<<" ";
for(j=1;j<=n;j++)
{
cout<<"\n\n";
for(p=1;p<=n;p++)
{
if(p==x[j])
cout<<"X"<<" ";
else
cout<<"0"<<" ";
}
}
}
else
fnqueen(k+1,n);
}
}
}
void main()
{
int num;
clrscr();
cout<<"\nN-Queen problem";
cout<<"\n***************";
cout<<"\nEnter the number of queen:";
cin>>num;
queen q;
q.fnqueen(1,num);
getch();
}
11.BREADTH FIRST SEARCH

#include<iostream.h>
#include<conio.h>
#define MAX 10
class bfsearch {
public:
void buildadjmatrix(int adj[MAX][MAX],int n);
void bfs(int x,int visited[],int adj[MAX][MAX],int n);
};
void bfsearch::buildadjmatrix(int adj[MAX][MAX],int n) {
int i,j;
cout<<"\n Enter 1 there is an edge,otherwise enter 0\n";
cout<<"\n\n Adjacency Matrix\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"adj["<<i+1<<"]["<<j+1<<"]=";
cin>>adj[i][j];
}
cout<<"\n";
}
}
void bfsearch::bfs(int x,int visited[],int adj[MAX][MAX],int n)
{
int i,q[MAX],rear=-1,front=-1;
q[++rear]=x;
visited[x]=1;
while(rear!=front)
{
x=q[++front];
cout<<"\n The visited node is :"<<x+1<<"\n";
for(i=0;i<n;i++)
{
if(adj[x][1]&&!visited[i]) {
q[++rear]=i;
visited[i]=1;
}}}}
void main()
{
int adj[MAX][MAX],node,n;
int i,visited[MAX]={0};
clrscr();
cout<<"\nBreadth first search";
cout<<"\n....................";
cout<<"\n Maximum node is: "<<MAX;
cout<<"\n Enter the number of niodes in the graph";
cin>>n;
bfsearch b;
b.buildadjmatrix(adj,n);
cout<<"\n Breadth first search";
cout<<"\n......................\n";
b.bfs(0,visited,adj,n);
getch();
}
12.DEPTH FIRST SEARCH

#include<iostream.h>
#include<conio.h>
# define MAX 10
class dfsearch
{
public:
void buildadjmatrix(int adj[MAX][MAX],int n);
void dfs(int x,int visited[],int adj[MAX][MAX],int n);
};
void dfsearch::buildadjmatrix(int adj[MAX][MAX],int n)
{
int i,j;
cout<<"\nEnter 1 if these is an edge.otherwise enter 0\n";
cout<<"\nAdjacency Matrix ..\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"adj["<<i+1<<"]["<<j+1<<"]=";
cin>>adj[i][j];
}
cout<<"\n";
}
}
void dfsearch::dfs(int x,int visited[],int adj[MAX][MAX],int n)
{
int j;
visited[x]=1;
cout<<"\n The visited node is :"<<x+1<<"\n";
for(j=0;j<n;j++)
if(adj[x][j]==1&&visited[j]==0)
dfs(j,visited,adj,n);
}
void main()
{
int adj[MAX][MAX],node,n;
int i,visited[MAX]={0};
clrscr();
cout<<"\nDepth First Search";
cout<<"\n___________________";
cout<<"\nMaximum node is:"<<MAX;
cout<<"\nEnter the number of nodes in the graph:";
cin>>n;
dfsearch d;
d.buildadjmatrix(adj,n);
cout<<"\nDepth First Search";
cout<<"\n*******************";
d.dfs(0,visited,adj,n);
getch();
}
13.TRAVELLING SALESPERSON PROBLEM

#include<iostream.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;
class travel
{
public:
void get();
void mincost(int city);
int least(int c);
void put();
};
void travel::get()
{
int i,j;
cout<<"\n Enter the number of vertices";
cin>>n;
cout<<"\n Enter the elemnts of cost matrix:\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
visited[i]=0;
}
}
cout<<"\n The cost matrix is...\n";
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<a[i][j];
}
}
void travel::mincost(int city)
{
int i,ncity;
visited[city]=1;
cout<<city+1<<"->";
ncity=least(city);
if(ncity==999)
{
ncity=0;
cout<<ncity+1;
cost+=a[city][ncity];
return;
}
mincost(ncity);
}
int travel::least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i<n;i++)
{
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i]<min)
{
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
}}
if(min!=999)
cost+=kmin;
return nc;
}
void travel::put()
{
cout<<"\n\n Minimum cost:";
cout<<cost;
}
void main()
{
clrscr();
cout<<"Travelling sales person problem\n";
cout<<"\n..............................";
travel ts;
ts.get();
cout<<"\n The path is:";
ts.mincost(0);
ts.put();
getch();
}
14.KNAPSACK USING GREEDY METHOD

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float n,m,loc,max,ps,temp,i,k;
float x[100],v[100],w[100],p[100];
cout<<endl<<"---knapsack problem using greedy method---"<<endl<<endl;
cout<<"Enter total number of object=";
cin>>n;
cout<<"Enter capacity of knapsack=";
cin>>m;
cout<<endl<<endl;
for(i=1;i<=n;i++)
{
cout<<"Enter profit of object"<<i<<"=";
cin>>p[i];
cout<<"Enter the weight of object"<<i<<"=";
cin>>w[i];
v[i]=p[i]/w[i];
cout<<"profit per unit of object"<<i<<"is="<<v[i]<<endl<<endl<<endl;
x[i]=0.0;
}
for(ps=1;ps<=n-1;ps++)
{
max=v[ps];
loc=ps;
for(k=ps+1;k<=n;k++)
{
if(max<v[k])
{
max=v[k];
loc=k;
}
}
temp=v[ps];
v[ps]=v[loc];
v[loc]=temp;
temp=p[ps];
p[ps]=p[loc];
p[loc]=temp;
temp=w[ps];
w[ps]=w[loc];
w[loc]=temp;
}
cout<<endl<<endl<<endl;
cout<<"After arranging the objects:"<<endl<<endl;
for(i=1;i<=n;i++)
{
cout<<"profit of object"<<i<<"is="<<p[i]<<endl;
cout<<"weight of object"<<i<<"is="<<w[i]<<endl;
cout<<"profit per unit of object"<<i<<"is="<<v[i]<<endl<<endl<<endl;
}
for(i=1;i<=n;i++)
{
if(w[i]>m)
break;
x[i]=1.0;
m=m-w[i];
}
if(i<=n)
x[i]=m/w[i];
float TP=0,TW=0;
for(i=1;i<=n;i++)
{
cout<<"Fraction of object"<<i<<"is="<<x[i]<<endl<<endl;
TP=TP+(p[i]*x[i]);
TW=TW+(w[i]*x[i]);
}
cout<<"Optimal solution:"<<endl<<endl;
cout<<"Total profit="<<TP<<endl;
cout<<"Total weight="<<TW;
getch();
}

You might also like