QuickFindUf: A Connected Component Algorithm, Study notes of Algorithms and Programming

The implementation of the quickfinduf class, a data structure used to determine if two elements are connected in a graph or an undirected graph. The class includes a constructor, a connected method, and a union method.

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
class QuickFindUf
{
private int[] id;
QuickFindUf(int n)
{
id=new int[n];
for(int i=0;i<n;i++)
id[i]=i;
}
public boolean connected(int a,int b)
{
return (id[a]==id[b]);
}
public void union(int a,int b)
{
int ida=id[a];
int idb=id[b];
int i;
for(i=0;i<id.length;i++)
{
if(id[i]==ida)
id[i]=idb;
}
}
}

Partial preview of the text

Download QuickFindUf: A Connected Component Algorithm and more Study notes Algorithms and Programming in PDF only on Docsity!

class QuickFindUf { private int[] id;

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

public boolean connected(int a,int b) { return (id[a]==id[b]); }

public void union(int a,int b) { int ida=id[a]; int idb=id[b];

int i;

for(i=0;i<id.length;i++) { if(id[i]==ida) id[i]=idb; } } }