I was studying Kruskal’s algorithm for finding the MST for a given graph and i understand the basic concept that you have to consider all the vertices as a forest initially. After that you have to find the minimum edge and joining the vertices of the edge into one tree. And doing this recursively until only one tree containing all the vertices is left.
And i came across the following implementation of this algorithm.
#include<iostream.h>
int p[10];
void kruskal(int w[10][10],int n)
{
int min,sum=0,ne=0,i,j,u,v,a,b;
for(i=1;i<=n;i++)
p[i]=0;
while(ne<n-1)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(w[i][j]<min)
{
min=w[i][j];
u=a=i;
v=b=j;
}
}
while(p[u])
u=p[u];
while(p[v])
v=p[v];
if(u!=v)
{
ne++;
sum+=min;
cout<<"\nedge "<<a<<"-->"<<b<<" is "<<min;
p[v]=u;
}
w[a][b]=w[b][a]=999;
}
cout<<"\nmin cost spanning tree= "<<sum;
}
void main()
{
int w[10][10],n,i,j;
clrscr();
cout<<"enter no.of vertices\n";
cin>>n;
cout<<"enter weight matrix\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>w[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(w[i][j]==0)
w[i][j]=999;
kruskal(w,n);
}
What i don’t understand is the need for:
while(p[u])
u=p[u];
while(p[v])
v=p[v];
What exactly do those two while loops do?
edit: and also the necessity of-
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(w[i][j]==0)
w[i][j]=999;
Kruskals algorithm wants to add a certain edge
(a, b). However, before doing so, it has to check whetheraandbare already connected (if they are, it won’t add the edge).Your four given lines do just that check whether
aandbare already connected.To understand this completely, you have to know the following: Initially
uandvare set toaandb, respectively. The arraypstores the connected components. That isp[x] = ymeans:xlies in the connected component ofy. Note that initially each vertex represents its own connected component, indicated byp[n1] = 0, p[n2] = 0, ...(i.e. the component is set to 0).Moreover, note that each connected component is represented by one vertex.
So, here we go:
while(p[u])checks whetheruis representant of a component (uis a representant iffp[u] == 0, which causes the while loop to stop). So, ifuis the representant of a component, it stops.The more interesting part is the following: If
uis not a representant, the algorithm looks upp[u], i.e. it looks up which node is the representant of the component ofu. Then it updatesuaccordingly (u=p[u]).You can consider this whole game as a graph. Consider the following table representing connected components:
This means, that node
1belongs to component represented by2.4belongs to component represented by3, which itself belongs to component represented by2. Note that2is a representant because it has entry0.You can visualize this as a graph:
You see, we have currently 3 components represented by 2, 7 and 9, respectively.
If we now want to add a new edge
(6,7), we have to “go up the trees” until we find the representants 2 and 7, respectively. As we see, the representants are not the same, we add the edge.Now another example: We want to add edge
(6, 5), so we “go up the tree” and find representant2in both cases. Thus, we don’t add the edge.“Going up in the trees” is done by the lines that were explicitly stated by you.