You are on page 1of 43

QUICKSORT

#inlude<iostream.h> #include<conio.h> class quick { public: int partition(int*,int,int); void quicksort(int*,int,int); }q; int quick::partition(int a[20],int lb,int ub) { int down=lb; int up=ub; int s=a[lb]; while(down<up) { while(a[down]<=s) down++; while(a[up]>s) up--; if(down<up) { int temp; temp=a[down]; a[down]=a[up]; a[up]=temp; } }

a[lb]=a[up]; a[up]=s; return up; } void quick::quicksort(int a[20],int low,int high) { int j; if(low<high) { j=q.partition(a,low,high); q.quicksort(a,low,j-1); q.quicksort(a,j+1,high); } } void main() { int a[20],n,i; clrscr(); cout<<"\n\t QUICK SORT"; cout<<"\n\n Enter the number of elements :"; cin>>n; cout<<"\nEnter the elements: "; for(i=0;i<n;i++) cin>>a[i]; q.quicksort(a,0,n-1); cout<<"\nSorted list is... "; for(i=0;i<n;i++) cout<<a[i]<<"\t"; getch(); }

OUTPUT

QUICK SORT

Enter the number of elements : 5

Enter the elements: 34 54 12 10 65

Sorted list is... 10

12

34

54

65

RESULT The program is done successfully and output is displayed

BINARY SEARCH

#include<iostream.h> #include<conio.h> int binary(int a[20],int n,int i) { int low,high,mid; low=0; high=n; mid=(low+high)/2; while((low<high)&&(a[mid]!=i)) { if(a[mid]<i) low=mid+1; if(a[mid-1]) mid=(low+high)/2; } return(a[mid]); } void main() { int a[20],i,j,elt,n; clrscr(); cout<<"\n\t BINARY SEARCH"; cout<<"\n\t --------------"; cout<<"\n\nEnter the no.of elements : "; cin>>n; cout<<"\nEnter the elements: "; for(i=0;i<n;i++)

cin>>a[i]; cout<<"\nEnter the element to be searched : "; cin>>elt; for(i=0;i<n;i++) for(j=j+1;j<n;j++) if(a[i]>a[j]) { a[i]=a[i]+a[j]; a[j]=a[i]-a[j]; a[i]=a[i]-a[j]; } if(binary(a,n,elt)==elt) cout<<"\nElement "<<elt<<" is presented"; else cout<<"\nElement is not present"; getch(); }

OUTPUT

BINARY SEARCH --------------------------

Enter the no.of elements : 5

Enter the elements: 6 3 1 4 9

Enter the element to be searched : 1

Element 1 is presented

RESULT The program is done successfully and output is displayed

BINARY TREE TRAVERSAL

#include<iostream.h> #include<conio.h> #include<process.h> #include<alloc.h> struct tree { int item; struct tree *lchild,*rchild; }; struct tree *root; struct tree *insert(struct tree *,int); void display(struct tree *,int,int,int); void preorder(struct tree *); void postorder(struct tree *); void inorder(struct tree *); void main() { int op,item,cl=30,rw=15,wid=8,x,y; char ch; clrscr(); cout<<"\n\t BINARY TREE TRAVERSAL"; cout<<"\n\t ----------------------"; cout<<"\n\n MENU \n\t1.insert\n\t2.inorder\n\t3.preorder\n\t4.postorder\n\t5.Display\n\t6.Exit\n"; cont: cout<<"\nEnter your option :"; cin>>op; switch(op)

{ case 1: cout<<"\n Enter the element to be inserted:"; cin>>item; root=insert(root,item); break;

case 2: cout<<"\n INORDER: "; inorder(root); break; case 3: cout<<"\n PREORDER: "; preorder(root); break; case 4: cout<<"\n POSTORDER: "; postorder(root); break; case 5: cout<<"\n Elements are:"; display(root,cl,rw,wid); break; case 6: exit(0); break; default: cout<<"\n Try again"; } cout<<"\n Do you want to continue(y/n)?"; cin>>ch;

if((ch=='y')||(ch=='Y')) goto cont; else exit(0); getch(); } struct tree *insert(struct tree *root,int item) { if(root==NULL) { root=(struct tree *)malloc(sizeof(struct tree)); root->item=item; root->rchild=NULL; root->lchild=NULL; return(root); } else { if(item<root->item) root->lchild=insert(root->lchild,item); else root->rchild=insert(root->rchild,item); return(root); } } void display(struct tree *root,int cl,int rw,int wid) { int item; struct tree *temp=root; if(temp!=NULL) {

gotoxy(cl,rw); cout<<temp->item; if(temp->lchild!=NULL) display(temp->lchild,cl-wid,rw+2,wid/2); if(temp->rchild!=NULL) display(temp->rchild,cl+wid,rw+2,wid/2); } else cout<<" \n NULL"; } void preorder(struct tree *root) { int item; struct tree *temp=root; if(temp!=NULL) { cout<<"\t"<<temp->item; preorder(temp->lchild); preorder(temp->rchild); } } void postorder(struct tree *root) { int item; struct tree *temp=root; if(temp!=NULL) { preorder(temp->lchild); preorder(temp->rchild); cout<<"\t"<<temp->item; }

} void inorder(struct tree *root) { int item; struct tree *temp=root; if(temp!=NULL) { preorder(temp->lchild); cout<<"\t"<<temp->item; preorder(temp->rchild); } }

OUTPUT BINARY TREE TRAVERSAL ---------------------MENU 1.insert 2.inorder 3.preorder 4.postorder 5.Display 6.Exit

Enter your option :1

Enter the element to be inserted:10

Do you want to continue(y/n)?y

Enter your option :1

Enter the element to be inserted:12

Do you want to continue(y/n)?y

Enter your option :1

Enter the element to be inserted:14

Do you want to continue(y/n)?y

Enter your option :5

Elements are

12

10

14

Do you want to continue(y/n)?y

Enter your option :2

INORDER:

10

12

14

Do you want to continue(y/n)?y

Enter your option :3

PREORDER:

12

10

14

Do you want to continue(y/n)?y

Enter your option :4

POSTORDER:

10

14

12

Do you want to continue(y/n)? N RESULT The program is done successfully and output is displayed

TRANSITIVE CLOSURE USING WARSHALLS ALGORITHM

#include<iostream.h> #include<conio.h> class wars { public: void war(int n,int w[10][10]); }wa; void wars::war(int n,int w[10][10]) { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(w[i][j]==1||w[i][k]==1&&w[k][j]==1) w[i][j]=1; else w[i][j]=0; }

} } cout<<"\n\t Transitive Closure Using Warshall's Algorithm\n "; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cout<<"\t"<<w[i][j]; cout<<"\n\n"; } } void main() { int i,j,n,w[10][10]; char ch; do { clrscr(); cout<<"\n Transitive Closure"; cout<<"\n\n Enter the no.of vertices :"; cin>>n; cout<<"\n Enter the vertex values :"; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cin>>w[i][j];

} cout<<"\n Adjacency Matrix\n "; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cout<<"\t"<<w[i][j]; cout<<"\n"; } wa.war(n,w); cout<<"\n Do you want to continue(y/n)?"; cin>>ch; }while(ch=='y'||ch=='Y'); getch(); }

OUTPUT Transitive Closure Enter the no.of vertices :4 Enter the vertex values : 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 Adjacency Matrix 0 1 0 0 0 1 0 0 0 0 0 0

Transitive Closure Using Warshall's Algorithm

0 1 1 1

0 0 1 1

0 0 0 1

0 0 0 0

Do you want to continue(y/n)?N

RESULT The program is done successfully and output is displayed

SHORTEST PATH USING DIJKSTRAS ALGORITHM

#include<iostream.h> #include<conio.h> #define inf 999 int adj[20][20],n,src,dest; class shortest { public: int shrt(int src,int dest); } sh; int shortest::shrt(int src,int dest) { int small,perm[10],current,start,dist[10],rewd,temp,i,k=1; for(i=1;i<=n;i++) { perm[i]=0; dist[i]=inf; } perm[src]=1; dist[src]=0; current=src; while(current!=dest)

{ small=inf; start=dist[current]; for(i=1;i<=n;i++) { if(perm[i]==0) { rewd=start+adj[current][i]; if(rewd<dist[i]) dist[i]=rewd; if(dist[i]<small) { small=dist[i]; temp=i; } } } current=temp; perm[current]=i; k++; } return(small); } void main() {

int i,j; clrscr(); cout<<"\n\n\t DIJKSTRA'S ALGORITHM"; cout<<"\n\t----------------------"; cout<<"\n Enter the no.of vertices :"; cin>>n; cout<<"\n Enter the weight of nodes \n Enter 999 if infinity "; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout<<"\n Enter the weight of node "<<i<<" to "<<j<<"\t:"; cin>>adj[i][j]; } } cout<<"\n Enter the source :"; cin>>src; cout<<"\n Enter the destination : "; cin>>dest; cout<<"\n Shortest path= "<<sh.shrt(src,dest); getch(); }

OUTPUT

DIJKSTRA'S ALGORITHM -----------------------------------

Enter the no.of vertices :3

Enter the weight of nodes Enter 999 if infinity

Enter the weight of node 1 to 1 Enter the weight of node 1 to 2 Enter the weight of node 1 to 3 Enter the weight of node 2 to 1 Enter the weight of node 2 to 2 Enter the weight of node 2 to 3 Enter the weight of node 3 to 1 Enter the weight of node 3 to 2 Enter the weight of node 3 to 3 Enter the source :1 Enter the destination : 3 Shortest path= 4 RESULT

:999 :3 :4 :3 :999 :5 :4 :5 :999

The program is done successfully and output is displayed

MINIMUM SPANNING TREE USING PRIM'S ALGORITHM

#include<iostream.h> #include<conio.h> #define INF 999 class prim { public: void op(int,int[10][10]); }; void prim::op(int n,int w[10][10]) { int i,j,p,q,adj[10][10],dist[10],sum=0,min; for(i=1;i<=n;i++) { min=INF; for(j=1;j<=n;j++) { adj[i][j]=0; if(min>w[j][i]) { min=w[i][j]; p=i;q=j; }

} dist[i]=min; adj[p][q]=1; w[p][q]=INF; } for(i=1;i<=n;i++) { if(dist[i+1]>dist[i]) p=i+1; } for(i=1;i<=n;i++) { sum=sum+dist[i]; adj[p][j]=0; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(adj[i][j]==1) cout<<"\n("<<i<<"->"<<j<<")"<<dist[i]; } cout<<"\n"; } cout<<"\n Weight of minimum spanning tree :"<<sum;

} void main() { prim r; int i,j,n,w[10][10],dist[10],adj[10][10]; clrscr(); cout<<"\n\t MINIMUM SPANNING TREE USING PRIM'S ALGORITHM"; cout<<"\n\t-----------------------------------------------"; cout<<"\n Enter the no.of vertices:"; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout<<"\n Weight of vertex "<<i<<" to "<<j<<" : "; cin>>w[i][j]; } } cout<<"\n Weight of matrix :"<<endl; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout<<"\t"<<w[i][j]; }

cout<<"\n"; } r.op(n,w); getch(); }

OUTPUT

MINIMUM SPANNING TREE USING PRIM'S ALGORITHM -------------------------------------------------------------------------------Enter the no.of vertices:3 Weight of vertex 1 to 1 : 999 Weight of vertex 1 to 2 : 2 Weight of vertex 1 to 3 : 4 Weight of vertex 2 to 1 : 1 Weight of vertex 2 to 2 : 999 Weight of vertex 2 to 3 : 3 Weight of vertex 3 to 1 : 4 Weight of vertex 3 to 2 : 5 Weight of vertex 3 to 3 : 999 Weight of matrix : 999 1 4 (1->2)2 (2->3)3 (3->1)4 Weight of minimum spanning tree :9 2 999 5 4 3 999

RESULT The program is done successfully and output is displayed

KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING

#include<iostream.h> #include<conio.h> int i,j; class knap { public: void knapsack(int n,int w,int val[10],int wt[10],int v[10][10]); }k; void knap::knapsack(int n,int w,int val[10],int wt[10],int v[10][10]) { for(i=1;i<=n;i++) { for(j=1;j<=w;j++) { if(j>=wt[i]) { if((v[i-1][j])>(val[i]+v[i-1][j-wt[i]])) { v[i][j]=v[i-1][j]; } else {

v[i][j]=val[i]+v[i-1][j-wt[i]]; } } else { v[i][j]=v[i-1][j]; } } } } void main() { int n,no,w,val[10],wt[10],v[10][10],knapsackitem[10]; clrscr(); cout<<"\n\t KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING"; cout<<"\n\t -------------------------------------------"; cout<<"\n Enter the no.of items:"; cin>>no; n=no; for(i=1;i<=n;i++) { cout<<"\n Weight of item "<<i<<" :"; cin>>wt[i]; cout<<"\n Value of item "<<i<<":"; cin>>val[i];

} cout<<"\n Enter the capacity of knapsack :"; cin>>w; for(i=0,j=0;i<=n,j<=w;i++,j++) { v[i][0]=0; v[0][j]=0; } k.knapsack(n,w,val,wt,v); cout<<"\n DYNAMIC PROGRAMMING TABLE\n\n+"; for(i=0;i<=n;i++) { for(j=0;j<=w;j++) { cout<<"\t"<<v[i][j]; } cout<<endl; } i--; j--; int k=0; while(i>0) { if(v[i][j]!=v[i-1][j]) {

knapsackitem[k]=i; j-=wt[i]; i--; k++; } else i--; } k--; cout<<"\n Item in knapsack :"; for(i=k;i>=0;i--) cout<<knapsackitem[i]<<" "; getch(); }

OUTPUT KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING --------------------------------------------------------------------------------Enter the no.of items:4 Weight of item 1 :2 Value of item 1:12

Weight of item 2 :1 Value of item 2:10

Weight of item 3 :3 Value of item 3:20

Weight of item 4 :2 Value of item 4:15 Enter the capacity of knapsack :5 DYNAMIC PROGRAMMING TABLE 0 0 0 0 0 0 0 10 10 10 0 12 12 12 15 0 12 22 22 25 0 12 22 30 30 0 12 22 32 37

Item in knapsack :1 2 4 RESULT The program is done successfully and output is displayed

N-QUEEN PROBLEM

#include<iostream.h> #include<conio.h> #include<stdlib.h> int nquees=8,queen=64,count=1; class queens { char ch; public: void check(int,int,char[8][8]); void print(char[8][8]); }; int main() { queens q; int col; char board[8][8]; clrscr(); cout<<"\n\t QUEENS PROBLEM"; cout<<"\n\t --------------"; for(col=0;col<nquees;col++) { q.check(0,col,board);

} getch(); return(0); } void queens::check(int r,int c,char board[8][8]) { int i,j,p,h; if((r==nquees)&&(c==0)) { cout<<"\n\n"<<count++<<" solution \n"; print(board); ch=(char)getch(); if(ch=='e') exit(0); } for(i=0;i<r;i++) { if(board[i][c]==queen) return; } for(j=0;j<c;j++) { if(board[r][i]==queen) return; }

i=r; j=c; do { if(board[i][j]==queen) return; i--; j--; }while(i>=0&&j>=0); i=r; j=c; do { if(board[i][j]==queen) return; i--; j++; }while(i>=0&&j<nquees); board[r][c]=queen; r++; for(p=0;p<nquees;p++) check(r,p,board); for(h=0;h<nquees;h++) board[r-1][h]='-'; }

void queens::print(char board[8][8]) { int i,j; cout<<"\n\n"; for(i=0;i<nquees;i++) { for(j=0;j<nquees;j++) { if(board[i][j]==queen) { cout<<"Q"; } else { cout<<"."<<char(22); } } cout<<"\n\n"; } cout<<"Press e to exit"; }

OUTPUT

QUEENS PROBLEM ----------------------------

1 solution .Q...... ...Q.... .....Q.. .......Q ..Q..... Q....... ......Q. ....Q...

Press e to exit E

RESULT The program is done successfully and output is displayed

TRAVELLING SALESMAN PROBLEM #include<iostream.h> #include<conio.h> int dist[10][10],a[10][10],n; int firstlowerbound() { int i,j,k,temp,lb; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) if(a[k][i]<a[k][j]) { temp=a[k][i]; a[k][i]=a[k][j]; a[k][j]=temp; } } lb=0; for(i=1;i<=n;i++) lb=lb+a[i][1]+a[i][2]; return(lb/2); } int nextlowerbound(int i,int j) {

int x,y,min[5][5],sum=0; for(x=1;x<=n;x++) for(y=1;y<=2;y++) min[x][y]=a[x][y]; if((dist[i][j]!=min[i][1])&&(dist[i][j]!=min[i][2])) min[i][2]=dist[i][j]; if((dist[j][i]!=min[j][1])&&(dist[j][i]!=min[i][2])) min[j][2]=dist[j][i]; for(x=1;x<=n;x++) for(y=1;y<=2;y++) sum=sum+min[x][y]; return(sum/2); } int main() { int i,m,j,v[10],minbnd,minver; clrscr(); cout<<"\n\t TRAVELLING SALESMAN PROBLEM"; cout<<"\n\t ---------------------------"; cout<<"\n Enter the no.of vertices :"; cin>>n; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { cout<<"\n Enter the distance between "<<i<<" and "<<j<<" :";

cin>>dist[i][j]; a[i][j]=dist[i][j]; } firstlowerbound(); v[1]=1; minbnd=nextlowerbound(1,2); minver=2; for(i=3;i<=n;i++) m=nextlowerbound(1,i); if(m<minbnd) { minbnd=m; minver=i; cout<<" Text "<<i<<"x"; } v[2]=minver; if(minver==2) if(nextlowerbound(minver,3)<nextlowerbound(minver,4)) { v[3]=3; v[4]=4; } else { v[3]=4;

v[4]=3; } if(minver==3) if(nextlowerbound(minver,2)<nextlowerbound(minver,4)) { v[3]=2; v[4]=4; } else { v[3]=4; v[4]=4; } if(minver==4) if(nextlowerbound(minver,2)<nextlowerbound(minver,3)) { v[3]=2; v[4]=4; } else { v[3]=4; v[4]=3; } for(int k=1;k<=n;k++)

cout<<v[k]<<" "; cout<<" 1"; getch(); return(0); }

OUTPUT

RESULT The program is done successfully and output is displayed

You might also like