Created
July 26, 2018 03:27
-
-
Save paullewallencom/453026aec384c5df62c5e3a838ac5ad0 to your computer and use it in GitHub Desktop.
Quick-Union Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QuickUnionUF | |
{ | |
private int[] parent; // parent[i] = parent of i | |
private int count; // number of components | |
public QuickUnionUF( int n ) | |
{ | |
parent = new int[ n ]; | |
count = n; | |
for ( int i = 0; i < n; i++ ) | |
{ | |
parent[ i ] = i; | |
} | |
} | |
public int count() { return count; } | |
public int find( int p ) | |
{ | |
validate( p ); | |
while ( p != parent[ p ] ) | |
p = parent[ p ]; | |
return p; | |
} | |
private void validate( int p ) | |
{ | |
int n = parent.length; | |
if ( p < 0 || p >= n ) | |
{ | |
throw new IllegalArgumentException( "index " + p + " is not between 0 and " + ( n - 1 ) ); | |
} | |
} | |
public boolean connected( int p, int q ) | |
{ | |
return find( p ) == find( q ); | |
} | |
public void union( int p, int q ) | |
{ | |
int rootP = find( p ); | |
int rootQ = find( q ); | |
if ( rootP == rootQ ) return; | |
parent[ rootP ] = rootQ; | |
count--; | |
} | |
public static void main( String[] args ) | |
{ | |
int n = StdIn.readInt(); | |
QuickUnionUF uf = new QuickUnionUF( n ); | |
while ( !StdIn.isEmpty() ) | |
{ | |
int p = StdIn.readInt(); | |
int q = StdIn.readInt(); | |
if ( uf.connected( p, q ) ) continue; | |
uf.union( p, q ); | |
StdOut.println( p + " " + q ); | |
} | |
StdOut.println( uf.count() + " components" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment