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

Prims

This document implements Kruskal's algorithm for finding the minimum spanning tree of a graph. It includes structs to represent graph nodes and edges, a Kruskal class with a KruskalsAlgorithm method, and main() to test it on a sample graph. The algorithm initializes data structures, inputs a graph, builds adjacency lists, then iteratively finds minimum cost edges and adds them to the MST if they don't form cycles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Prims

This document implements Kruskal's algorithm for finding the minimum spanning tree of a graph. It includes structs to represent graph nodes and edges, a Kruskal class with a KruskalsAlgorithm method, and main() to test it on a sample graph. The algorithm initializes data structures, inputs a graph, builds adjacency lists, then iteratively finds minimum cost edges and adds them to the MST if they don't form cycles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <iostream>

#include<string.h>
using namespace std;
#define max 20
struct adjacency_list
{
char name;
int cost;
struct adjacency_list *next;
}*l[max];
class kruskal
{
public:
void KruskalsAlgorithm(int [max][max],char [max][max]);
};
int total;
const int infinity=999;

void kruskal::KruskalsAlgorithm(int adjacent_vertex[max][max],char


country[max][max])
{
int
visited[max],cost[max][max],i,j,min=0,min2,pre,pos,least=0,start=0,t
otal_cost=0;
//here cost is the distance between each country
for(i=0;i<total;i++)
visited[i]=0;//ini alizing visited to 0

for(i=0;i<total;i++)
{
for(j=0;j<total;j++)
{
if(i==j)
cost[i][j]=infinity;//making same country to
same country distance infinity
else
{
cost[i][j]=adjacent_vertex[i][j];//storing in cost
}
}
}

pos=start;
visited[start]=1;
cout<<"\n"<<country[pos];
total_cost=0;
for(i=0;i<total-1;i++)
{
min=cost[pos][0];
for(j=0;j<total;j++)
{
if(cost[pos][j]<=min && visited[j]==0 &&
cost[pos][j]!=999)//first comparing that do we got any minimum
{
min=cost[pos][j];
least=j;

}
for(j=0;j<total;j++)
{ //if any test cases fail try cost<min instead
cost<=min ,in above if condi on also do same

if(cost[pre][j]<=min&&visited[j]==0&&cost[pre][j]!=999)//again
comparing same as prim on paper
{
min=cost[pre][j];//if we got less distance than
minimum than reolace
least=j;
}
}
total_cost=total_cost+min;
visited[least]=1;
pre=pos;//storing previous node
pos=least;//storing next node
cout<<"==>"<<country[least];

cout<<"\nTotal Cost is:-"<<total_cost;


}
int main()
{
int repeat;
do{
kruskal k;
int i,j,adjacency_matrix[max][max],pos;
char country[max][max],start[max];
cout<<"Enter no of vertex :-";
cin>>total;

//taking different countries names


for(i=0;i<total;i++)
{
cout<<"\nEnter "<<i+1<<" Vertex name :-";
cin>>country[i];
}

cout<<"\nPlease use 999 as distance between two non


adjacent or unconnected ver ces\n";

//taking input for adjacency matrix i.e. distance between


each other
//ini alizing it with infinity
for(i=0;i<total;i++)
{
for(j=0;j<total;j++)
adjacency_matrix[i][j]=infinity;
}
for(i=0;i<total;i++)
{

for(j=i;j<total;j++)
{
if(i==j)
adjacency_matrix[i][j]=infinity;//in prims
it get to infinity in short it doesnt ma er
else
{
cout<<"Enter distance between
"<<country[i]<<" and "<<country[j]<<" :-";
cin>>adjacency_matrix[i][j];

adjacency_matrix[j][i]=adjacency_matrix[i][j];//j i=i j making


them same
}
}
}

for(i=0;i<total;i++)
{
adjacency_list *curr;
for(j=0;j<total;j++)
{
if(adjacency_matrix[i][j]!=999)
{
adjacency_list *temp=new
adjacency_list;
temp->cost=adjacency_matrix[i][j];
temp->next=NULL;
temp->name=country[j][0];

if(l[i]==NULL)
l[i]=temp;
else
{
curr=l[i];
while(curr->next!=NULL)
curr=curr->next;
curr->next=temp;

}
}
}
for(i=0;i<total;i++)
{
adjacency_list *curr;
curr=l[i];
cout<<"\n"<<country[i]<<"==>\t";
while(curr->next!=NULL)
{
cout<<curr->name<<"\t";
curr=curr->next;
}
cout<<curr->name<<"\t";
}
k.KruskalsAlgorithm(adjacency_matrix,country);
cout<<"\nPress 1 to do it again\n";
cin>>repeat;
}while(repeat==1);
return 0;
}

/*
===========================================OUTPUT======
============================================

Enter no of vertex :-5

Enter 1 Vertex name :-A

Enter 2 Vertex name :-B

Enter 3 Vertex name :-C

Enter 4 Vertex name :-D

Enter 5 Vertex name :-E


Please use 999 as distance between two non adjacent or
unconnected ver ces
Enter distance between A and B :-20
Enter distance between A and C :-5
Enter distance between A and D :-4
Enter distance between A and E :-6
Enter distance between B and C :-8
Enter distance between B and D :-9
Enter distance between B and E :-7
Enter distance between C and D :-4
Enter distance between C and E :-1
Enter distance between D and E :-2

A==> B C D E
B==> A C D E
C==> A B D E
D==> A B C E
E==> A B C D
A==>D==>E==>C==>C
Total Cost is:-12
Press 1 to do it again
0*/

You might also like