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 whether a
and b
are already connected (if they are, it won't add the edge).
Your four given lines do just that check whether a
and b
are already connected.
To understand this completely, you have to know the following: Initially u
and v
are set to a
and b
, respectively. The array p
stores the connected components. That is p[x] = y
means: x
lies in the connected component of y
. Note that initially each vertex represents its own connected component, indicated by p[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 whether u
is representant of a component (u
is a representant iff p[u] == 0
, which causes the while loop to stop). So, if u
is the representant of a component, it stops.
The more interesting part is the following: If u
is not a representant, the algorithm looks up p[u]
, i.e. it looks up which node is the representant of the component of u
. Then it updates u
accordingly (u=p[u]
).
You can consider this whole game as a graph. Consider the following table representing connected components:
u | 1 2 3 4 5 6 7 8 9
p[u] | 2 0 2 3 2 1 0 9 0
This means, that node 1
belongs to component represented by 2
. 4
belongs to component represented by 3
, which itself belongs to component represented by 2
. Note that 2
is a representant because it has entry 0
.
You can visualize this as a graph:
2 7 9
/|\ |
1 3 5 8
| |
6 4
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 representant 2
in 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.