DisjSets - */ public void union( int root1, int root2 ) {...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
package DataStructures; // DisjSets class // // CONSTRUCTION: with int representing initial number of sets // // ******************PUBLIC OPERATIONS********************* // void union( root1, root2 ) --> Merge two sets // int find( x ) --> Return set containing x // ******************ERRORS******************************** // No error checking is performed /** * Disjoint set class. (Package friendly so not used accidentally) * Does not use union heuristics or path compression. * Elements in the set are numbered starting at 0. * @author Mark Allen Weiss * @see DisjSetsFast */ class DisjSets { /** * Construct the disjoint sets object. * @param numElements the initial number of disjoint sets. */ public DisjSets( int numElements ) { s = new int [ numElements ]; for( int i = 0; i < s.length; i++ ) s[ i ] = -1; } /** * Union two disjoint sets. * For simplicity, we assume root1 and root2 are distinct * and represent set names. * @param root1 the root of set 1. * @param root2 the root of set 2.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: */ public void union( int root1, int root2 ) { s[ root2 ] = root1; } /** * Perform a find. * Error checks omitted again for simplicity. * @param x the element being searched for. * @return the set containing x. */ public int find( int x ) { if( s[ x ] &lt; 0 ) return x; else return find( s[ x ] ); } private int [ ] s; // Test main; all finds on same output line should be identical public static void main( String [ ] args ) { int numElements = 128; int numInSameSet = 16; DisjSets ds = new DisjSets( numElements ); int set1, set2; for( int k = 1; k &lt; numInSameSet; k *= 2 ) { for( int j = 0; j + k &lt; numElements; j += 2 * k ) { set1 = ds.find( j ); set2 = ds.find( j + k ); ds.union( set1, set2 ); } } for( int i = 0; i &lt; numElements; i++ ) { System.out.print( ds.find( i )+ &quot;*&quot; ); if( i % numInSameSet == numInSameSet - 1 ) System.out.println( ); } System.out.println( ); } }...
View Full Document

Page1 / 2

DisjSets - */ public void union( int root1, int root2 ) {...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online