C++ Code for Kruskal's Algorithm with explanation
C++ Code for Kruskal's Algorithm with explanation
Example: Node numbers are 1 2 3 4 5
Weight is 3 between 1 and 3.
Weight is 4 between 1 and 2.
Weight is 9 between 2 and 3.
Weight is 1 between 2 and 4.
Weight is 7 between 5 and 3.
At first sort the edge list according to weight.
Weight is 1 between 2 and 4.
Weight is 2 between 4 and 5.
Weight is 3 between 1 and 3.
Weight is 4 between 1 and 2.
Weight is 7 between 5 and 3.
Weight is 9 between 2 and 3.
Take array for node's parent. And initialize parent value equal to node value.
parent[1]=1
parent[2]=2
parent[3]=3
parent[4]=4
parent[5]=5
Select the most small weighted edge. Here it is : Weight is 1 between 2 and 4.
check the parents of two nodes are equal or not. If parents are not equal, Then store it in answer container. And set 2nd's node parent is 1st node's parent.
parent of node 2 is 1.
parent of node 4 is 2.
parents are not equal. put this pair in answer container.
And set node 4's is 2. i.e parent[4]=2;
Then take next sorted edge which is : Weight is 2 between 4 and 5.
Here, parent[4]=2 and parent[5]=5. parents are not equal. Then store it in answer container.
Set, parent[5]=2.
Then take next sorted edge which is : Weight is 3 between 1 and 3.
Here, parent[1]=1 and parent[3]=3. parents are not equal. Then store it in answer container.
Set, parent[3]=1.
Then take next sorted edge which is : Weight is 4 between 1 and 2.
Here, parent[1]=1 and parent[2]=2. parents are not equal. Then store it in answer container.
Set, parent[2]=1.
Then take next sorted edge which is : Weight is 7 between 5 and 3.
Here, parent[3]=1. parent[5]=parent[2]=1. parents are equal. So, it is not our answer. It creates circle. So, ignore it.
Then take next sorted edge which is : Weight is 9 between 2 and 3.
Here, parent[3]=1. parent[2]=1. parents are equal. So, it is not our answer. It creates circle. So, ignore it.
Traversing is completed.
Code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> Pair_Int_Int;
typedef pair<int,Pair_Int_Int> Pair_Int_Pair_Int_Int;
vector<Pair_Int_Pair_Int_Int> myvec,result;
int *parent;
bool mysort(Pair_Int_Pair_Int_Int p1, Pair_Int_Pair_Int_Int p2)
{
return p1.first<p2.first;
}
int getParent(int x)
{
int y;
if(parent[x]==x)
return x;
y=parent[x];
parent[y]=getParent(y);
return parent[y];
}
int main()
{
freopen("file.txt","r",stdin);
int node,edge,i,u,v,w,parent_u,parent_v;
Pair_Int_Int p1;
Pair_Int_Pair_Int_Int p2;
cin>>node>>edge;
for(i=0;i<edge;i++)
{
cin>>u>>v>>w;
p1=pair<int,int>(u,v);
p2=pair<int,Pair_Int_Int>(w,p1);
myvec.push_back(p2);
}
sort(myvec.begin(),myvec.end(),mysort);
parent= new int[node+1];
for(i=1;i<=node;i++)
parent[i]=i;
for(i=0;i<edge;i++)
{
p2=myvec[i];
w=p2.first;
p1=p2.second;
u=p1.first;
v=p1.second;
parent[u]=getParent(u);
parent[v]=getParent(v);
if(parent[u]!=parent[v])
{
result.push_back(p2);
parent[parent[v]]=parent[u];
}
}
delete[] parent;
for(i=0;i<result.size();i++)
{
p2=myvec[i];
p1=p2.second;
cout<<p1.first<<" "<<p1.second<<endl;
}
return 0;
}
Input:
5
6
1 3 3
1 2 4
2 3 9
2 4 1
4 5 2
3 5 7
Output:
2 4
4 5
1 3
1 2
Comments
Post a Comment