Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:00:57+00:00 2026-05-26T23:00:57+00:00

I was studying Kruskal’s algorithm for finding the MST for a given graph and

  • 0

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; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:00:57+00:00Added an answer on May 26, 2026 at 11:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Studying compilers course, I am left wondering why use registers at all. It is
While studying about JMX, I have seen one of the important feature of it
After studying TCP/UDP difference all week, I just can't decide which to use. I
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
While studying the Collection API, we find that some methods ( add , remove
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
I started studying Haskell one week ago and have one strange problem. I created
Studying STL I have tried to negate a functor with not2 but encontered problems.
Im studying inline functions in C++ and have come to a section concerning limitations
While studying the Qt framework, I've noticed that it's littered with hundreds of overloaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.