Skip to main content

3.prims

File: /home/saiesh/ADS/Assignment No 3/ads3.cpp
Page 1 of 3
#include<iostream>
#include<stdlib.h>
using namespace std;
class prims
{
private:
int nodes;
int w[10][10];
int visited[10];
public:
void accept();
void prim();
};
void prims::accept()
{
int i,j;
cout<<"\nHow many number of nodes do you want to accept? ";
cin>>nodes;
for(i=0;i<nodes;i++)
{
for(j=i+1;j<nodes;j++)
{
cout<<"\nEnter the value of ["<<i<<"]["<<j<<"]:\t";
cin>>w[i][j];
w[j][i]=w[i][j];
}
}
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
if(i==j)
w[i][j]=0;
}
}
for(i=0;i<nodes;i++)
{
cout<<"\n";
for(j=0;j<nodes;j++)
cout<<"\t"<<w[i][j];
}
}
void prims::prim()
{
int i,j,totalcost=0,min=9999,k,l,m,st;
cout<<"\nEnter the starting vertex: ";
cin>>st;
for(i=0;i<nodes;i++)
visited[i]=0;
visited[st]=1;
for(j=0;j<nodes;j++)
{
if(w[st][j]!=0&&w[st][j]<min)
{
min=w[st][j];
k=st;
l=j;
}
}
cout<<"\nEdge ("<<k<<","<<l<<") is having minimum cost: "<<min;
visited[k]=visited[l]=1;
totalcost=min;
for(m=0;m<nodes-2;m++)
{File: /home/saiesh/ADS/Assignment No 3/ads3.cpp
Page 2 of 3
min=9999;
for(i=0;i<nodes;i++)
{
if(visited[i]==1)
{
for(j=0;j<nodes;j++)
{
if(visited[j]!=1)
{
if(w[i][j]!=0&&w[i][j]<min)
{
min=w[i][j];
k=i;
l=j;
}
}
}
}
}
cout<<"\nEdge ("<<k<<","<<l<<") is having minimum cost: "<<min;
visited[k]=visited[l]=1;
totalcost=totalcost+min;
}
cout<<"\nTotal cost is: "<<totalcost;
}
int main()
{
prims obj;
int ch;
char ans;
do
{
cout<<"\n\tMENU";
cout<<"\n1.Accept the graph";
cout<<"\n2.Minimum spanning tree";
cout<<"\n3.Exit";
cout<<"\nEnter your choice:\t";
cin>>ch;
switch(ch)
{
case 1: obj.accept();
break;
case 2: obj.prim();
break;
case 3: exit(0);
}
cout<<"\nDo you want to continue? ";
cin>>ans;
} while(ans=='y'||ans=='Y');
return 0;
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads3.cpp
saiesh@saiesh :~$ ./a.out
MENU
1.Accept the graph
2.Minimum spanning tree
3.Exit
Enter your choice:
1
How many number of nodes do you want to accept? 4
Enter the value of [0][1]:
1File: /home/saiesh/ADS/Assignment No 3/ads3.cpp
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
value
value
value
value
value
of
of
of
of
of
[0][2]:
[0][3]:
[1][2]:
[1][3]:
[2][3]: 0
7
5
0
2
0
1
0
1
0
5
0
5
0
7
0
2
Do you want to continue? y 7
0
2
0
MENU
1.Accept the graph
2.Minimum spanning tree
3.Exit
Enter your choice:
2
Enter the starting vertex: 0
Edge (0,1) is having minimum cost: 1
Edge (1,2) is having minimum cost: 5
Edge (2,3) is having minimum cost: 2
Total cost is: 8
Do you want to continue? y
MENU
1.Accept the graph
2.Minimum spanning tree
3.Exit
Enter your choice:
2
Enter the starting vertex: 1
Edge (1,0) is having minimum cost: 1
Edge (1,2) is having minimum cost: 5
Edge (2,3) is having minimum cost: 2
Total cost is: 8
Do you want to continue? y
MENU
1.Accept the graph
2.Minimum spanning tree
3.Exit
Enter your choice:
2
Enter the starting vertex: 2
Edge (2,3) is having minimum cost: 2
Edge (2,1) is having minimum cost: 5
Edge (1,0) is having minimum cost: 1
Total cost is: 8
Do you want to continue? y
MENU
1.Accept the graph
2.Minimum spanning tree
3.Exit
Enter your choice:
2
Enter the starting vertex: 3
Edge (3,2) is having minimum cost: 2
Edge (2,1) is having minimum cost: 5
Edge (1,0) is having minimum cost: 1
Total cost is: 8
Do you want to continue? y
MENU
1.Accept the graph
2.Minimum spanning tree
3.Exit
Enter your choice:
3
saiesh@saiesh :~$ */
Page 3 of 3

Comments

Popular posts from this blog

PPL 1

NEW Unit III STRUCTURING OF PROGRAM MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of ‘Data Abstraction’? (a) Reduce Complexity (b) Binds together code and data (c) Parallelism (d) None of the mentioned Answer : a Explanation : An essential element of Object Oriented Programming is ‘Data Abstraction’ which means hiding things. Complexity is managed through abstraction. 2. Which of the following mechanisms is/are provided by Object Oriented Language to implement Object Oriented Model? (a) Encapsulation (b) Inheritance (c) Polymorphism (d) All of the mentioned Answer : d Explanation : None. 3. Which of these is the functionality of ‘Encapsulation’? (a) Binds together code and data (b) Using single interface for general class of actions. (c) Reduce Complexity (d) All of the mentioned Answer : a Explanation : ‘Encapsulation’ acts as protective wrapper that prevents code and data from being accessed by other code defined outside the...

13.interface

File: /home/student/saiesh/ADS/Assignment No 13/ads13_2A.java package pack; interface ip { public public public public } void void void void add(int sub(int mul(int div(int a, a, a, a, int int int int b); b); b); b); public class A implements ip { public void add(int a, int b) { System.out.println("Addition is\t\t: "+(a+b)); } public void sub(int a,int b) { System.out.println("Substraction is\t\t: "+(a-b)); } public void mul(int a, int b) { System.out.println("Multiplication is\t: "+(a*b)); } public void div(int a, int b) { System.out.println("Division is\t\t: "+(a/b)); } } Page 1 of 1 ....................... File: /home/student/saiesh/ADS/Assignment No 13/ads13_2B.java package mypack; import pack.*; import java.util.Scanner; public class B { public static void main(String arg[]) { A obj= new A(); Scanner sc=new Scanner(System.in); System.out.println("\nEnter first number\t: "); int a=sc.nextInt(); System.out.println("\nEnter ...

2.BST_dictionary

File: /home/saiesh/ADS/Assignment No 2/ads2.cpp #include<iostream> #include<stdlib.h> #include<string.h> using namespace std; typedef struct node { char word[30]; char meaning[50]; node *left,*right; }bstnode; bstnode *root; class BST { public: BST() { root=NULL; } bstnode *getnode(); void create(); void inorder(bstnode *T); void comparison(bstnode *root); void update(bstnode *root); bstnode *deletenode(bstnode *root,char word[30]); }; bstnode *BST::getnode() { bstnode *temp; temp=new bstnode; cout<<"\nEnter the word:\t\t"; cin>>temp->word; cout<<"Enter the meaning:\t"; cin>>temp->meaning; temp->left=temp->right=NULL; return temp; } void BST::create() { char ch; bstnode *temp, *ptr; do { temp=getnode(); if(root==NULL) root=temp; else { ptr=root; while(1) { if(strcmp(temp->word,ptr->word)<0) { if(ptr->left==NULL) { ptr->left=temp; break; } else ptr=ptr->left; } else Page 1 of 7File: /home/saiesh...