0% found this document useful (0 votes)
39 views

Minimium Spanning Tree Using Prim'S Algorithm

The document describes code for implementing various tree traversal algorithms like preorder, inorder and postorder traversal on a binary tree data structure. It defines a tree node structure with left and right child pointers. A traversal class contains methods to insert nodes, display the tree, and perform the different traversal techniques by recursively visiting the nodes. The main function tests the code by building a sample binary tree and calling the traversal methods.

Uploaded by

Blessie Hudson
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Minimium Spanning Tree Using Prim'S Algorithm

The document describes code for implementing various tree traversal algorithms like preorder, inorder and postorder traversal on a binary tree data structure. It defines a tree node structure with left and right child pointers. A traversal class contains methods to insert nodes, display the tree, and perform the different traversal techniques by recursively visiting the nodes. The main function tests the code by building a sample binary tree and calling the traversal methods.

Uploaded by

Blessie Hudson
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

MINIMIUM SPANNING TREE USING PRIM'S ALGORITHM #include<iostream.h> #include<conio.

h> #define INF 999 class prims { int i,j,adj[10][10],dis[10],min,k[10][10]; public: void findpath(int n,int k[10][10]); }; void prims::findpath(int n,int k[10][10]) { int p,q,sum=0; for(i=1;i<=n;i++) { min=INF; for(j=i+1;j<=n;j++) { adj[i][j]=0; if(min>k[i][j]) { min=k[i][j]; p=i; q=j; } dis[i]=min; adj[p][q]=1; k[p][q]=INF; } } for(i=1;i<=n;i++) { if(dis[i+1]<dis[i]) p=i+1; } dis[p]=0; for(i=1;i<=n;i++) { sum=sum+dis[i];

adj[p][i]=0; } cout<<endl; cout<<"\t\tSelected Edges\n\t\t\t"<<endl; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(adj[i][j]==1) cout<<"\n\t\t("<<i<<","<<j<<")="<<dis[i]; } } cout<<"\n\n Weight of minimum spanning tree "<<sum; } void main() { int i,j,k,n,w[10][10]; clrscr(); prims p; cout<<"\t\tMINIMUM SPANNING TREE USING PRIM'S ALGORITHM"<<endl; cout<<"\t\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl; cout<<"\n\n\tEnter the number of vertex\n\n\t\t\t\t"; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cout<<"\n\n\tThe Weight of edge between"<<i<<"and"<<j<<":"; cin>>w[i][j]; } } cout<<"\n\n\t\tWeighted Matrix\n\n"; for(i=1;i<=n;i++) { cout<<"\t\t"; for(j=1;j<=n;j++) { cout<<w[i][j]<<"\t"; } cout<<endl; }

p.findpath(n,w); getch(); } /*OUTPUT MINIMUM SPANNING TREE USING PRIM'S ALGORITHM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Enter the number of vertex 3 The Weight of edge between1and1:0 The Weight of edge between1and2:3 The Weight of edge between1and3:7 The Weight of edge between2and1:3 The Weight of edge between2and2:0 The Weight of edge between2and3:5 The Weight of edge between3and1:7 The Weight of edge between3and2:5 The Weight of edge between3and3:0 Weighted Matrix 0 3 7 3 0 5 7 5 0 Selected Edges (1,2)=3 (2,3)=5 Weight of minimum spanning tree 8 */

NQUEEN PROGRAM

#include<iostream.h> #include<conio.h> #include<math.h> #include<process.h> int x[50]; class queen { public: int place(int); void nqueen(int); }; int queen:: place(int k) { int i,j=1; for(i=1;i<=k-1;i++) { if((x[i]==x[k]||abs(x[i]-x[k])==abs(i-k))) j=0; } return(j); } void queen::nqueen(int) { int k,j,i,n; x[1]=0; k=1; while(k>0) { x[k]=x[k]+1; while((x[k]<=n)&&(place(k)==0)) x[k]=x[k]+1; if(x[k]<=n) { if(k==n) { for(i=1;i<=n;i++) { cout<<"\t"<<i; } cout<<endl;

for(i=1;i<=n;i++) cout<<"\t"<<"-"; cout<<endl; for(i=1;i<=n;i++) { for(j=1;j<=x[i]-1;j++) cout<<"\t"; cout<<"\t"<<"Q"<<i; for(j=x[i]+1;j<=n;j++) cout<<"\t"; cout<<endl; cout<<endl; } getch(); exit(0); } else { k=k+1; x[k]=0; } } else k=k-1; } } void main() { clrscr(); queen q; int x; cout<<"\t\t\tN QUEEN\n"; cout<<"\t\t\t^^^^^^^\n\n"; cout<<"\tEnter the value: "; cin>>x; if(x!=4&&x!=8) cout<<"Not Possible"; else q.nqueen(x); getch(); }

/* OUTPUT N QUEEN ^^^^^^^ Enter the value: 4 1 2 3 4 Q1 Q2 Q3 Q4 */

MINIMUM COST OF TRAVELLING SALES PERSON #include<iostream.h> #include<conio.h> class Sales { public: void tsperson(); }; void Sales::tsperson() { int i,j,c[5][5],q[5][5],m1,m2,r,k,s[10],result; cout<<"Enter the cost value"<<endl; for(i=1;i<=4;i++) for(j=1;j<=4;j++) if(i==j) c[i][j]=0; else cin>>c[i][j]; m1=3; m2=4; i=1; for(j=2;j<=4;j++) { r=c[j][m1]+c[m1][m2]+c[m2][m1]; k=c[j][m2]+c[m2][m1]+c[m1][i]; if(r>k) s[j]=k; else s[j]=r; if(j==2) { m1=2; m2=4; } if(j==3) { m1=2; m2=3; }

} s[i]=c[1][i+1]+s[i+1]; result=s[i]; cout<<"Minimum cost of travelling sales person problem is:"<<result; getch(); } void main() { clrscr(); Sales s; cout<<"\t\tTRAVELLING SALES PERSON"<<endl; cout<<"\t\t^^^^^^^^^^^^^^^^^^^^"<<endl; s.tsperson(); } /*OUTPUT TRAVELLING SALES PERSON ^^^^^^^^^^^^^^^^^^^^^^^ Enter the cost value 2 3 7 13 6 5 7 9 8 11 1 4 Minimum cost of travelling sales person problem is:18 */

DEPTH FIRST SEARCH #include<iostream.h>

#include<conio.h> int a[50][50],visit[50],n; void dfs(int x) { int i; visit[x]=1; for(i=0;i<n;i++) { if(a[x][i]==1 && visit[i]==0) { cout<<"->"<<i+1; dfs(i); } } } void main() { int e,j,i,c,d; clrscr(); cout<<"Enter the no of vertices:"; cin>>n; cout<<"enter the no of edges:"; cin>>e; for(i=0;i<n;i++) { for(j=0;j<n;j++) a[i][j]=0; visit[i]=0; } for(i=1;i<=e;i++) { cout<<"Enter two vertices for edge"<<i<<":"; cin>>c>>d; a[c-1][d-1]=a[d-1][c-1]=1; } cout<<"Enter the start vertices:"; cin>>c; cout<<"DFS:-"<<c; dfs(c-1); getch(); }

/*OUTPUT Enter the no of vertices:6 enter the no of edges:8 Enter two vertices for edge1:1 4 Enter two vertices for edge2:1 3 Enter two vertices for edge3:1 5 Enter two vertices for edge4:4 3 Enter two vertices for edge5:3 6 Enter two vertices for edge6:5 6 Enter two vertices for edge7:5 2 Enter two vertices for edge8:6 2 Enter the start vertices:1 DFS:-1->3->4->6->2->5 */

BINARY TREE TRAVERSAL #include<iostream.h> #include<conio.h>

#include<stdlib.h> #include<alloc.h> struct tree { int item; struct tree *rchild,*lchild; }; class traversal { int item; public: struct tree *insert(struct tree*,int); void preorder(struct tree*); void inorder(struct tree*); void postorder(struct tree*); void display(struct tree*,int,int,int); }; struct tree *traversal::insert(struct tree *root,int item) { if(root==NULL) { root=(struct tree*)malloc(sizeof(struct tree)); root->item=item; root->lchild=NULL; root->rchild=NULL; } else { if(item<root->item) root->lchild=insert(root->lchild,item); else root->rchild=insert(root->rchild,item); } return(root); } void traversal::display(struct tree *root,int cl,int rw,int wid) { int i,n; struct tree *temp=root; if(temp!=NULL) {

gotoxy(cl,rw); cout<<"\t"<<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); } return; } void traversal::preorder(struct tree *root) { struct tree *temp=root; if(temp!=NULL) { cout<<"\t"<<temp->item<<""; preorder(temp->lchild); preorder(temp->rchild); } } void traversal::inorder(struct tree *root) { struct tree *temp=root; if(temp!=NULL) { inorder(temp->lchild); cout<<"\t"<<temp->item<<""; inorder(temp->rchild); } } void traversal::postorder(struct tree *root) { struct tree *temp=root; if(temp!=NULL) { postorder(temp->lchild); postorder(temp->rchild); cout<<"\t"<<temp->item<<""; } } void main() {

struct tree *root; traversal t; int op,item; char ch; root=NULL; clrscr(); cout<<"\t\tBINARY TREE TRAVERSAL"<<endl; cout<<"\t\t^^^^^^^^^^^^^^^^^^^^^"<<endl; cout<<"\tmenu\n\n 1.insert\n 2.display\n 3.preorder\n 4.inorder\n 5.postorder\n 6.exit\n"; cont:cout<<"enter your choice:"; cin>>op; switch(op) { case 1: cout<<"enter the item:"; cin>>item; root=t.insert(root,item); cout<<"the item inserted is:"<<item<<endl; break; case 2: clrscr(); t.display(root,32,8,10); cout<<"\n\n\n\n\n\n\n\n\n\n\n\n"; break; case 3: cout<<"\n \t PRE ORDER:\n"; t.preorder(root); break; case 4: cout<<"\n \t IN ORDER:\n"; t.inorder(root); break; case 5: cout<<"\n \t POST ORDER:\n"; t.postorder(root); break; case 6: return; default: cout<<"try Again"; break;

} cout<<"\nDo you want to continue:"; cin>>ch; if(ch=='y'||ch=='n') goto cont; getch(); } /*OUTPUT BINARY TREE TRAVERSAL ^^^^^^^^^^^^^^^^^^^^^ MENU 1.Insert 2.Display 3.Preorder 4.Inorder 5.Postorder 6.Exit enter your choice:1 enter the item:56 The item inserted is 56 Do you want to continue:y enter your choice:1 enter the item:32 the item inserted is:32 Do you want to continue:y enter your choice:1 enter the item:62 the item inserted is:62 Do you want to continue:y enter your choice:1 enter the item:1 the item inserted is:1 Do you want to continue:y enter your choice:1 enter the item:5 the item inserted is:5

Do you want to continue:y enter your choice:1 enter the item:40 the item inserted is:40 Do you want to continue:y enter your choice:1 enter the item:45 the item inserted is:45 Do you want to continue:y enter your choice:2 56 32 1 5 Do you want to continue:y enter your choice:3 PRE ORDER 56 32 1 5 Do you want to continue:y enter your choice:4 IN ORDER 1 5 32 40 Do you want to continue:y enter your choice:5 POST ORDER 5 1 45 40 Do you want to continue:y enter your choice:6 */ 40 45 62

40

45

62

45

56

62

32

62

56

You might also like