Java Implementation of Quick Union Union-Find Algorithm, Study notes of Algorithms and Programming

The java code implementation of the quick union union-find algorithm, which is a data structure used for determining if two objects belong to the same set and for unioning two sets into one. The algorithm is commonly used in graph theory and other areas of computer science.

Typology: Study notes

2014/2015

Uploaded on 03/03/2015

Ayush.Agrawal
Ayush.Agrawal 🇮🇳

8 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
import java.util.Scanner;
class QuickUnionUF
{
int id[];
QuickUnionUF(int n)
{
id=new int[n];
for(int i=0;i<n;i++)
id[i]=i;
}
private int root (int i)
{
while(i!=id[i])
i=id[i];
return i;
}
public boolean connected(int a, int b)
{
return root(a)==root(b);
}
public void union(int a,int b)
{
id[root(a)]=root(b); // Note: The roots have the same id as their index number
}
}

Partial preview of the text

Download Java Implementation of Quick Union Union-Find Algorithm and more Study notes Algorithms and Programming in PDF only on Docsity!

import java.util.Scanner;

class QuickUnionUF { int id[];

QuickUnionUF(int n) { id=new int[n]; for(int i=0;i<n;i++) id[i]=i; }

private int root (int i) { while(i!=id[i]) i=id[i]; return i; }

public boolean connected(int a, int b) { return root(a)==root(b); }

public void union(int a,int b) { id[root(a)]=root(b); // Note: The roots have the same id as their index number

}