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 8633187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:27:19+00:00 2026-06-12T09:27:19+00:00

There is a Weighted Quick-Union with Path Compression algorithm. The code: public class WeightedQU

  • 0

There is a “Weighted Quick-Union with Path Compression” algorithm.

The code:

public class WeightedQU 
{
    private int[] id;
    private int[] iz;

    public WeightedQU(int N)
    {
        id = new int[N];
        iz = new int[N];
        for(int i = 0; i < id.length; i++)
        {
            iz[i] = i;
            id[i] = i;
        }
    }

    public int root(int i)
    {
        while(i != id[i])
        {
            id[i] = id[id[i]];   // this line represents "path compression"
            i = id[i];
        }
        return i;
    }

    public boolean connected(int p, int q)
    {
        return root(p) == root(q);
    }

    public void union(int p, int q)   // here iz[] is used to "weighting"
    {
        int i = root(p);
        int j = root(q);
        if(iz[i] < iz[j])
        {
            id[i] = j;
            iz[j] += iz[i];
        }
        else
        {
            id[j] = i;
            iz[i] += iz[j];
        }
    }
}

Questions:

  1. How does the path compression work? id[i] = id[id[i]] means that we reach only the second ancester of our node, not the root.

  2. iz[] contains integers from 0 to N-1. How does iz[] help us know the number of elements in the set?

Can someone clarify this for me?

  • 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-06-12T09:27:20+00:00Added an answer on June 12, 2026 at 9:27 am

    First understand that id is a forest. id[i] is the parent of i. If id[i] == i it means that i is a root.

    For some root i (where id[i] == i) then iz[i] is the number of elements in the tree rooted at i.

    public int root(int i)
    {
        while(i != id[i])
        {
            id[i] = id[id[i]];   // this line represents "path compression"
            i = id[i];
        }
        return i;
    }
    

    How does the path compression work? id[i] = id[id[i]] means that we reach only the second ancester of our node, not the root.

    As we are ascending the tree to find the root we move nodes from their parents to their grandparents. This partially flattens the tree. Notice that this operation doesn’t change which tree the node is a member of, this is all we are interested in. This is the path compression technique.

    (You did notice the loop right? while(i == id[i]) terminates once i is a root node)

    iz[] contains integers from 0 to N-1. How does iz[] help us know the number of elements in the set?

    There is a transcription error in the code:

    for(int i = 0; i < id.length; i++)
    {
        iz[i] = i; // WRONG
        id[i] = i;
    }
    

    This is the correct version:

    for(int i = 0; i < id.length; i++)
    {
        iz[i] = 1; // RIGHT
        id[i] = i;
    }
    

    iz[i] is the number of elements for a tree rooted at i (or if i is not a root then iz[i] is undefined). So it should be initialized to 1, not i. Initially each element is a seperate “singleton” tree of size 1.

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

Sidebar

Related Questions

Is there an algorithm that will allow me to traverse a weighted graph in
There is any way to set the generic type (T) of class in the
Is there a freely available implementation of finding a maximum weight clique in weighted
Is there an algorithm that allows to rank items based on the difference of
I'm trying to come up with a weighted algorithm for an application. In the
I' m trying to implement a weighted graph. I know that there are two
I need to write algorithms finding the lightest path tree in directed and weighted
Is there an objective way to validate the output of a clustering algorithm? I'm
I have an undirected weighted graph and derive a new graph based on groups
I have a weighted choice algorithm that works, but I'd like to improve it

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.