MSTDisjointSets - if ( sizeI > sizeJ ) { //...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
class DisjointSets { int [] arr; public DisjointSets(int n) { arr = new int[n]; for (int i = 0; i < arr.length; i++) arr[i] = -1; } public void union(int i, int j) { /* Union the sets whose roots are i and j */ int sizeI = - arr[i]; int sizeJ = - arr[j];
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if ( sizeI &amp;gt; sizeJ ) { // the tree at i is larger arr[j] = i; arr[i] = - (sizeI + sizeJ); } else { arr[i] = j; arr[j] = - (sizeI + sizeJ); } } public int find(int x) { while (arr[x] &amp;gt; 0) { x = arr[x]; } return x; } }...
View Full Document

This note was uploaded on 02/18/2011 for the course 22C 031 taught by Professor Kasturi during the Spring '11 term at University of Iowa.

Ask a homework question - tutors are online