DSA 1_10
DSA 1_10
1
PROGRAM CODE:-
#include<iostream>
#include<string.h>
using namespace std;
typedef struct student
{
int roll_num;
char name [20];
float marks;
}stud;
void create(stud s[20],int n);
void display(stud s[20],int n);
void bubble_sort(stud s[20],int n);
void insertionSort(stud s[20],int n);
void quick_sort(stud s[20],int,int);
int partition(stud s[20],int,int);
void search(stud s[20],int n,int key);
int bsearch(stud s[20], char x[20],int low,int high);
int main()
{
stud s[20];
int ch,n,key,result;
char x[20];
do
{
cout<<"\n 1) Create Student Database ";
cout<<"\n 2) Display Student Records ";
cout<<"\n 3) Bubble Sort ";
cout<<"\n 4) Insertion Sort ";
cout<<"\n 5) Quick Sort ";
cout<<"\n 6) Linear search ";
cout<<"\n 7) Binary search ";
cout<<"\n 8) Exit ";
cout<<"\n Enetr Your Choice:=";
cin>>ch;
switch(ch)
{case 1:
cout<<"\n Enter The Number Of Records:=";
cin>>n;
create(s,n);
break;
case 2:
display(s,n);
break;
case 3:
bubble_sort(s,n);
break;
case 4:
insertionSort(s,n);
break;
case 5:
quick_sort(s,0,n-1);
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\t"<< "Marks";
for(int i=n-1; i>=n-10; i--)
{ cout<<"\n";
cout<<"\t "<< s[i].roll_num<<"\t "<<s[i].name<<"\t "<<s[i].marks;}
break;
case 6:
cout<<"\n Enter the marks which u want to search:=";
cin>>key;
search(s,n,key);
break;
case 7:
cout<<"\n Enter the name of student which u want to search:=";
cin>>x;
insertionSort(s,n);
result=bsearch(s,x,0,(n-1));
if(result==-1)
{cout<<" \n Student name you want to search for is not present ! \n";}
else {cout<<" \n The student is present :\t" << s[result].name;}
break;
case 8:return 0;
default:cout<<"\n Invalid choice !! Please enter your choice again."<<endl;}
}while(ch!=8);}
void create(stud s[20],int n)
{int i;
for(i=0;i<n;i++)
{cout<<"\n Enter the roll number:=";
cin>>s[i].roll_num;
cout<<"\n Enter the Name:=";
cin>>s[i].name;
cout<<"\n Enter the marks:=";
cin>>s[i].marks;
}}
void display(stud s[20],int n)
{int i;
cout<<"\n"<< "\t"<< "Roll No"<< "\t"<<" Name" <<"\t"<< "Marks";
for(i=0;i<n;i++)
{cout<<"\n";
cout<<"\t "<< s[i].roll_num<<"\t "<<s[i].name<<"\t "<<s[i].marks;
}}
//bubble sort to sort in ascending order on roll number
void bubble_sort(stud s[20],int n)
{
int i,j;
stud temp;
for(i=1;i<n;i++)
{for(j=0;j<n-i;j++)
{if(s[j].roll_num>s[j+1].roll_num)
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}}}}
// insertion sort to sort on names in ascending order
void insertionSort(stud s[20], int n)
{ int i, j;
stud key;
for (i = 1; i < n; i++){
key= s[i]; j = i - 1;
/* Move elements of arr[0..i-1], that are
{
cout<<"\n\t "<< s[i].roll_num<<"\t "<<s[i].name<<"\t "<<s[i].marks;
}}}
int bsearch(stud s[20], char x[20],int low,int high)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(x,s[mid].name)==0)
{
return mid;
}
else if(strcmp(x,s[mid].name)<0)
{
high=mid-1;
}
else
{
low=mid+1;
}}
return -1;}
OUTPUT:
1) Create Student Database
2) Display Student Records
3) Bubble Sort
4) Insertion Sort
5) Quick Sort
6) Linear search
7) Binary search
8) Exit
Enetr Your Choice:=1
Enter The Number Of Records:=3
Enter the roll number:=4
Enter the Name:=sanju
Enter the marks:=78
Enter the roll number:=1
Enter the Name:=shantanu
Enter the marks:=90
Enter the roll number:=10
Enter the Name:=omkar
Enter the marks:=99
6) Linear search
7) Binary search
8) Exit
Enter Your Choice:=2
} s->next=NULL;
delete(temp);
}
void info::count()
{ temp=head; ct=0;
while(temp->next!=NULL)
{ temp=temp->next; ct++; }
ct++;
cout<<" Count of members is:"<<ct;
}
void info::reverse()
{ rev(head); }
void info::rev(node *temp)
{ if(temp==NULL)
{ return; }
else
{ rev(temp->next); }
cout<<" "<<temp->prn<<" "<<temp->rollno<<" "<<temp->name;
}
void info::concat()
{ int k,j;
cout<<"enter no. of members in list1";
cin>>k;
head=NULL;
for(i=0;i<k;i++)
{ insertm();
head1=head;
} head=NULL;
cout<<"enter no. of members in list2";
cin>>j;
for(i=0;i<j;i++)
{ insertm();
head2=head;
} head=NULL;
temp1=head1;
while(temp1->next!=NULL)
{ temp1=temp1->next; }
temp1->next=head2;
temp2=head1;
cout<<" prn rolln0 NAME \n";
while(temp2->next!=NULL)
{
cout<<"\n "<<temp2->prn<<"
temp2=temp2->next;
"<<temp2->rollno<<" "<<temp2->name<<"\n";;
}
cout<<"\n "<<temp2->prn<<" "<<temp2->rollno<<" "<<temp2->name<<"\n";
}
int main()
{ info s;
int i;
char ch;
do{
cout<<"\n choice the options";
cout<<"\n 1. To insert president ";
cout<<"\n 2. To insert member ";
cout<<"\n 3. To insert secretary ";
cout<<"\n 4. To delete president ";
cout<<"\n 5. To delete member ";
cout<<"\n 6. To delete secretary ";
cout<<"\n 7. To display data ";
cout<<"\n 8. Count of members";
cout<<"\n 9. To display reverse of string ";
cout<<"\n 10.To concatenate two strings ";
cin>>i;
switch(i)
{ case 1: s.insertp();
break;
case 2: s.insertm();
break;
case 3: s.insertm();
break;
case 4: s.delp();
break;
case 5: s.delm();
break;
case 6: s.dels();
break;
case 7: s.display();
break;
case 8: s.count();
break;
case 9: s.reverse();
break;
case 10: s.concat();
break;
default: cout<<"\n unknown choice";
}
cout<<"\n do you want to continue enter y/Y \n";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}
OUTPUT:-
choice the options
1. To insert president
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 9
2. To insert member
3. To insert secretary
4. To delete president
5. To delete member
6. To delete secretary
7. To display data
8. Count of members
9. To display reverse of string
10. To concatenate two strings 3
enter name of student hari
enter prn no. of student
4444
enter student rollno23
do you want to continue enter y/Y
y
8. Count of members
9. To display reverse of string
10. To concatenate two strings 10
enter no. of members in list11
enter name of student wama
enter prn no. of student
23456
enter student rollno22
enter no. of members in list21
enter name of student qqq
enter prn no. of student
2223
enter student rollno34
prn rolln0 NAME
23456 22 wama
2223 34 qqq
continue;
}
/* push the operator into the stack */
if (data != -1) {
push(data);
}
if (str[i] == ‘+’ || str[i] == ‘-‘
|| str[i] == ‘*’ || str[i] == ‘/’) {
/*
* if the i/p character is an operator,
* then pop two elements from the stack,
* apply operator and push the result into
* the stack
*/
operand2 = pop();
operand1 = pop();
if (operand1 == -1 || operand2 == -1)
break;
switch (str[i]) {
case ‘+’:
result = operand1 + operand2;
/* pushing result into the stack */
push(result);
break;
case ‘-‘:
result = operand1 – operand2;
push(result);
break;
case ‘*’:
result = operand1 * operand2;
push(result);
break;
case ‘/’:
result = operand1 / operand2;
push(result);
break;
}
}
data = -1;
}
if (top != NULL && top->next == NULL)
cout<<“Output:%d\n”, top->data;
else
cout<<“u ve entered wrong expression\n”;
return 0;
}
OUTPUT:-
obj.display();
break;
case 3: if (!obj.isempty())
{ cout<<"\n Queue contains:";
obj.display();
}
else
break;
cout<<"\n Queue is empty";
case 4: cout<<"\n Exit";
}
}while(ch!=4);
return 0;
}
OUTPUT :-
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:34
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:64
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:84
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:1
Enter data:93
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:3
Queue contains:
34 64 84 93
1. insert job
2. delete job
3.display
4.Exit
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:3
Queue contains:
64 84 93
1. insert job
2. delete job
3.display
4.Exit
Enter your choice:4
Exit
EXPERIMENT NO. 5
PROGRAM:-
#include<iostream>
//#include
//#include
using namespace std;
#define SIZE 5
class dequeue
{
int a[10],front,rear,count;
public:
dequeue();
void add_at_beg(int);
void add_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void display();
};
dequeue::dequeue()
{
front=-1;
rear=-1;
count=0;
}
void dequeue::add_at_beg(int item)
{
int i;
if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
}
else
{
cout<<"\nInsertion is not possible,overflow!!!!";
for(i=count;i>=0;i--)
{
a[i]=a[i-1];
}
a[i]=item;
count++;
rear++;
}
}
void dequeue::add_at_end(int item)
{
if(front==-1)
{
front++;
rear++;
a[rear]=item;
count++;
}
else if(rear>=SIZE-1)
{
}
else
{
}}
cout<<"\nInsertion is not possible,overflow!!!";
return;
a[++rear]=item;
void dequeue::display()
{
for(int i=front;i<=rear;i++)
{
cout<<a[i]<<" "; }
}
void dequeue::delete_fr_front()
{
if(front==-1)
{
}
else
{
cout<<"Deletion is not possible:: Dequeue is empty";
return;
if(front==rear)
{
front=rear=-1;
return;
}
cout<<"The deleted element is "<<a[front];
front=front+1;
}}
void dequeue::delete_fr_rear()
{
if(front==-1)
{
}
else
{
cout<<"Deletion is not possible:Dequeue is empty";
return;
if(front==rear)
{
front=rear=-1;
}
cout<<"The deleted element is "<< a[rear];
rear=rear-1;
}}
int main()
{
int c,item;
dequeue d1;
do
{
cout<<"\n\n****DEQUEUE OPERATION****\n";
cout<<"\n1-Insert at beginning";
cout<<"\n2-Insert at end";
cout<<"\n3_Display";
cout<<"\n4_Deletion from front";
cout<<"\n5-Deletion from rear";
cout<<"\n6_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>c;
switch(c)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_beg(item);
break;
case 2:
cout<<"Enter the element to be inserted:";
cin>>item;
d1.add_at_end(item);
break;
case 3:
d1.display();
break;
case 4:
d1.delete_fr_front();
break;
case 5:
d1.delete_fr_rear();
break;
case 6:
exit(1);
break;
default:
cout<<"Invalid choice";
break;
}
}while(c!=7);
return 0;}
OUTPUT :-
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:1
Enter the element to be inserted:45
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:2
Enter the element to be inserted:46
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:3
45 46
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:4
The deleted element is 45
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:3
46
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:5
The deleted element is 0
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:3
****DEQUEUE OPERATION****
1- Insert at beginning
2- Insert at end
3_Display
4_Deletion from front
5-Deletion from rear
6_Exit
Enter your choice<1-4>:
EXPERIMENT NO. 6
PROGRAM:-
#include <iostream>
# include <cstdlib>
# include <string.h>
using namespace std;
/*
* Node Declaration
*/
struct node
{
char label[10];
int ch_count;
struct node *child[10];
}*root;
/*
* Class Declaration
*/
class BST
{
public:
void create_tree();
void display(node * r1);
BST()
{
root = NULL;
}
};
void BST::create_tree()
{
int tbooks,tchapters,i,j,k;
root = new node();
cout<<"Enter name of book";
cin>>root->label;
cout<<"Enter no. of chapters in book";
cin>>tchapters;
root->ch_count = tchapters;
for(i=0;i<tchapters;i++)
{
root->child[i] = new node;
cout<<"Enter Chapter name\n";
cin>>root->child[i]->label;
cout<<"Enter no. of sections in Chapter: "<<root->child[i]->label;
cin>>root->child[i]->ch_count;
for(j=0;j<root->child[i]->ch_count;j++)
{
root->child[i]->child[j] = new node;
cout<<"Enter Section "<<j+1<<"name\n";
cin>>root->child[i]->child[j]->label;
//cout<<"Enter no. of subsections in "<<r1->child[i]->child[j]->label;
//cin>>r1->child[i]->ch_count;
}
}
}
void BST::display(node * r1)
{
int i,j,k,tchapters;
if(r1 != NULL)
{
cout<<"\n-----Book Hierarchy---";
cout<<"Wrong choice"<<endl;
}
}
}
OUTPUT :-
1.Create
2.Display
3.Quit
Enter your choice : 1
Enter name of bookC++
Enter no. of chapters in book2
Enter Chapter name
Operators
Enter no. of sections in Chapter: Operators1
Enter Section 1name
Arithmetic
Enter Chapter name
Functions
Enter no. of sections in Chapter: Functions1
Enter Section 1name
FunctionDefine
-----Book Hierarchy---
Book title : C++
Chapter 1 Operators
Sections
Arithmetic
Chapter 2 Functions
Sections
Function Define
………………… Book Tree Creation……………
1. Create
2. Display
3.Quit
EXPERIMENT NO. 7
PROGRAM:-
#include <iostream>
using namespace std;
#include <conio.h>
struct tree
{
tree *l, *r;
int data;
}*root = NULL, *p = NULL, *np = NULL, *q;
void create()
{
int value,c = 0;
while (c < 7)
{
if (root == NULL)
{
root = new tree;
cout<<"enter value of root node\n";
cin>>root->data;
root->r=NULL;
root->l=NULL;
}
else
{
p = root;
cout<<"enter value of node\n";
cin>>value;
while(true)
{
if (value < p->data)
{
if (p->l == NULL)
{
p->l = new tree;
p = p->l;
p->data = value;
p->l = NULL;
p->r = NULL;
cout<<"value entered in left\n";
break;
}
else if (p->l != NULL)
{
p = p->l;
}
}
else if (value > p->data)
{
if (p->r == NULL)
{
p->r = new tree;
p = p->r;
p->data = value;
p->l = NULL;
p->r = NULL;
cout<<"value entered in right\n";
break;
}
else if (p->r != NULL)
{
p = p->r;
}
}
}
}
c++;
}
}
void inorder(tree *p)
{
if (p != NULL)
{
inorder(p->l);
cout<<p->data<<endl;
inorder(p->r);
}
}
void preorder(tree *p)
{
if (p != NULL)
{
cout<<p->data<<endl;
preorder(p->l);
preorder(p->r);
}
}
void postorder(tree *p)
{
if (p != NULL)
{
postorder(p->l);
postorder(p->r);
cout<<p->data<<endl;
}
}
int main()
{
create();
cout<<"printing traversal in inorder\n";
inorder(root);
cout<<"printing traversal in preorder\n";
preorder(root);
cout<<"printing traversal in postorder\n";
postorder(root);
getch();
}
OUTPUT :-
enter value of root node
7
enter value of node
8
value entered in right
enter value of node
4
value entered in left
enter value of node
6
value entered in right
enter value of node
3
value entered in left
enter value of node
5
value entered in left
enter value of node
2
value entered in left
printing traversal in inorder
2
3
4
5
6
7
8
printing traversal in preorder
7
4
3
2
6
5
8
printing traversal in postorder
2
3
5
6
4
8
7
EXPERIMENT NO. 8
PROGRAM:-
#include<iostream>
using namespace std;
struct node
{
int data;
node *L;
node *R;
};
node *root,*temp;
int count,key;
class bst
{
public:
void create();
void insert(node*,node*);
void disin(node*);
void dispre(node*);
void dispost(node*);
void search(node*,int);
int height(node*);
void mirror(node*);
void min(node*);
bst()
{
root=NULL;
count=0;
}
};
void bst::create()
{
char ans;
do
{
temp=new node;
cout<<"Enter the data : ";
cin>>temp->data;
temp->L=NULL;
temp->R=NULL;
if(root==NULL)
{
root=temp;
}
else
insert(root,temp);
cout<<"Do you want to insert more value : "<<endl;
cin>>ans;
count++;
cout<<endl;
}while(ans=='y');
cout<<"The Total no.of nodes are:" <<count;
}
void bst::insert(node *root,node* temp)
{
if(temp->data>root->data)
{
if(root->R==NULL)
{
root->R=temp;
}
else
insert(root->R,temp);
}
else
{
if(root->L==NULL)
{
root->L=temp;
}
else
insert(root->L,temp);
}
}
void bst::disin(node *root)
{
if(root!=NULL)
{
disin(root->L);
cout<<root->data<<"\t";
disin(root->R);
count++;
}
}
void bst::dispre(node *root)
{
if(root!=NULL)
{
cout<<root->data<<"\t";
dispre(root->L);
dispre(root->R);
}
}
void bst::dispost(node *root)
{
if(root!=NULL)
{
dispost(root->L);
dispost(root->R);
cout<<root->data<<"\t";
}
}
void bst::search(node * root,int key)
{
int flag=0;
cout<<"\nEnter your key : "<<endl;
cin>>key;
temp=root;
while(temp!=NULL)
{
if(key==temp->data)
{
cout<<"KEY FOUND\n";
flag=1;
break;
} node *parent=temp; if(key>parent->data)
{
temp=temp->R;
}
else
{
temp=temp->L;
}
}
if(flag==0)
{
cout<< "KEY NOT FOUND " <<endl;
}
} int bst::height(node *root)
{ int hl,hr;
if(root==NULL)
{ return 0; }
else if(root->L==NULL && root->R==NULL)
{
return 0;
}
cout<<endl; hr=height(root->R);
hl=height(root->L);
if(hr>hl)
{
return(1+hr);
}
else
{
return(1+hl);
}
}
void bst::min(node *root)
{
temp=root;
cout<<endl; while(temp->L!=NULL)
{
temp=temp->L;
}
cout<<root->data;
}
void bst::mirror(node *root)
{
temp=root;
if(root!=NULL)
{
mirror(root->L);
mirror(root->R);
temp=root->L;
root->L=root->R;
root->R=temp;
}
}
int main()
{
bst t;
int ch;
char ans;
do
{
cout<<"\n1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search
6) inorder
7) preorder 8) postorder" <<endl;
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
cout<<"\"n Number of nodes in longest path:"<<(1+(t.height(root)));
break;
case 3:
cout<<"\nThe min element is:";
t.min(root);
break;
case 4:
t.mirror(root);
cout<<"\nThe mirror of tree is: ";
t.disin(root);
break;
case 5:
t.search(root,key);
break;
case 6:
cout<<"\n***************INORDER**************" <<endl;
t.disin(root);
break;
case 7:
cout<<"\n***************PREORDER**************"<<endl;
t.dispre(root);
break;
case 8:
cout<<"\n*******************POSTORDER**************"<<endl;
t.dispost(root);
break;
}
cout<<"\nDo you want to continue :"; cin>>ans;
}while(ans=='y');
return 0;
}
OUTPUT:-
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
1
Enter the data : 3
Do you want to insert more value :
y
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
2
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
3
1)Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7) preorder 8) postorder
4
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
5
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
6
***************INORDER**************
976431
Do you want to continue :y
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
7
***************PREORDER**************
346971
Do you want to continue :y
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
8
*******************POSTORDER**************
796413
Do you want to continue :y
1) Insert new node 2)number of nodes in longest path 3) minimum 4) mirror 5) search 6) inorder
7)
preorder 8) postorder
EXPERIMENT NO. 9
PROGRAM:-
class Graph {
// Number of vertex
int v;
// Number of edges
int e;
// Adjacency matrix
int** adj;
public:
{
// Visited vector to so that
// a vertex is not visited more than once
// Initializing the vector to false as no
// vertex is visited at the beginning
vector<bool> visited(v, false);
vector<int> q;
q.push_back(start);
int vis;
while (!q.empty()) {
vis = q[0];
// Set
visited[i] = true;
}
}
}
}
// Driver code
int main()
{
int v = 5, e = 4;
G.BFS(0);
}
OUTPUT:-
0123
EXPERIMENT NO. 10
PROGRAM:-
#include <iostream>
#include<iomanip>
using namespace std;
public:
PhoneGraph(int num)
{
n=num;
}
void readgraph();
void printGraph();
int mincost(int cost[],bool visited[]);
int prim();
void kruskal(EdgeList &spanlist);
int find(int belongs[], int vertexno);
void unionComp(int belongs[], int c1,int c2);
};
void PhoneGraph::readgraph()
{
cout<<"Enter Adjacency(Cost) Matrix: \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n; j++)
cin>>data[i][j];
}
}
void PhoneGraph::printGraph()
{
cout<<"\nAdjacency (COST) Matrix: \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<setw(3)<<data[i][j];
}
cout<<endl;
}
}
int PhoneGraph::mincost(int cost[],bool visited[]) //finding vertex with minimum cost
{
int min=9999,min_index; //initialize min to MAX value(ANY) as temporary
for(int i=0;i<n;i++)
{
if(visited[i]==0 && cost[i]<min)
{
min=cost[i];
min_index=i;
}
}
return min_index; //return index of vertex which is not visited and having minimum cost
}
int PhoneGraph::prim()
{
bool visited[MAX];
int parents[MAX];
int cost[MAX]; //saving minimum cost
for(int i=0;i<n;i++)
{
cost[i]=9999; //set cost as infinity/MAX_VALUE
visited[i]=0; //initialize visited array to false
}
cost[0]=0; //starting vertex cost
parents[0]=-1; //make first vertex as a root
for(int i=0;i<n-1;i++)
{
int k=mincost(cost,visited);
visited[k]=1;
for(int j=0;j<n;j++)
{
if(data[k][j] && visited[j]==0 && data[k][j] < cost[j])
{
parents[j]=k;
cost[j]=data[k][j];
}
}
}
cout<<"Minimum Cost Telephone Map:\n";
for(int i=1;i<n;i++)
{
cout<<i<<" -- "<<parents[i]<<" = "<<cost[i]<<endl;
}
int mincost=0;
for (int i = 1; i < n; i++)
mincost+=cost[i]; //data[i][parents[i]];
return mincost;
}
// ------- Kruskal's Algorithm
void PhoneGraph::kruskal(EdgeList &spanlist)
{
int belongs[MAX]; //Separate Components at start (No Edges, Only vertices)
int cno1,cno2; //Component 1 & 2
EdgeList elist;
for(int i=1;i<n;i++)
for(int j=0;j<i;j++)
{
if(data[i][j]!=0)
{
elist.data[elist.n]=Edge(i,j,data[i][j]); //constructor for initializing edge
elist.n++;
}
}
elist.sort(); //sorting in increasing weight order
for(int i=0;i<n;i++)
belongs[i]=i;
for(int i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u); //find set of u
cno2=find(belongs,elist.data[i].v); ////find set of v
if(cno1!=cno2) //if u & v belongs to different sets
{
spanlist.data[spanlist.n]=elist.data[i]; //ADD Edge to spanlist
spanlist.n=spanlist.n+1;
unionComp(belongs,cno1,cno2); //ADD both components to same set
}
}
}
void PhoneGraph::unionComp(int belongs[],int c1,int c2)
{
for(int i=0;i<n;i++)
{
if(belongs[i]==c2)
belongs[i]=c1;
}
}
int PhoneGraph::find(int belongs[],int vertexno)
{
return belongs[vertexno];
}
// MAIN PROGRAM
int main() {
int vertices,choice;
EdgeList spantree;
cout<<"Enter Number of cities: ";
cin>>vertices;
PhoneGraph p1(vertices);
p1.readgraph();
do
{
cout<<"\n1.Find Minimum Total Cost(By Prim's Algorithm)"
<<"\n2.Find Minimum Total Cost(by Kruskal's Algorithms)"
<<"\n3.Re-Read Graph(INPUT)"
<<"\n4.Print Graph"
<<"\n0. Exit"
OUTPUT :-