Created
July 15, 2022 20:13
-
-
Save mrcsxsiq/1c2cf2038d017e420bf062a6c7d04af4 to your computer and use it in GitHub Desktop.
Union Find Path Compression
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
10 | |
4 3 | |
3 8 | |
6 5 | |
9 4 | |
2 1 | |
8 9 | |
5 0 | |
7 2 | |
6 1 | |
1 0 | |
6 7 |
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 QuickUnionPathCompressionUF { | |
private int[] id; | |
private int count; | |
public QuickUnionPathCompressionUF(int n) { | |
count = n; | |
id = new int[n]; | |
for (int i = 0; i < n; i++) { | |
id[i] = i; | |
} | |
} | |
public int count() { | |
return count; | |
} | |
public int find(int p) { | |
int root = p; | |
while (root != id[root]) | |
root = id[root]; | |
while (p != root) { | |
int newp = id[p]; | |
id[p] = root; | |
p = newp; | |
} | |
return root; | |
} | |
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; | |
id[rootP] = rootQ; | |
count--; | |
} | |
public static void main(String[] args) { | |
int n = StdIn.readInt(); | |
QuickUnionPathCompressionUF uf = new QuickUnionPathCompressionUF(n); | |
while (!StdIn.isEmpty()) { | |
int p = StdIn.readInt(); | |
int q = StdIn.readInt(); | |
if (uf.find(p) == uf.find(q)) continue; | |
uf.union(p, q); | |
System.out.println(p + " " + q); | |
} | |
System.out.println(uf.count() + " components"); | |
} | |
} | |
// javac QuickUnionPathCompressionUF.java | |
// java QuickUnionPathCompressionUF < input.txt |
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; | |
private int count; | |
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.find(p) == uf.find(q)) continue; | |
uf.union(p, q); | |
System.out.println(p + " " + q); | |
} | |
System.out.println(uf.count() + " components"); | |
} | |
} | |
// javac QuickUnionUF.java | |
// java QuickUnionUF < input.txt |
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
import java.util.Scanner; | |
import java.util.regex.Pattern; | |
public final class StdIn { | |
private StdIn() {} | |
private static Scanner scanner; | |
private static final String charsetName = "UTF-8"; | |
private static final java.util.Locale usLocale = new java.util.Locale("en", "US"); | |
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\p{javaWhitespace}+"); | |
private static final Pattern EMPTY_PATTERN = Pattern.compile(""); | |
private static final Pattern EVERYTHING_PATTERN = Pattern.compile("\\A"); | |
public static boolean isEmpty() { | |
return !scanner.hasNext(); | |
} | |
public static boolean hasNextLine() { | |
return scanner.hasNextLine(); | |
} | |
public static boolean hasNextChar() { | |
scanner.useDelimiter(EMPTY_PATTERN); | |
boolean result = scanner.hasNext(); | |
scanner.useDelimiter(WHITESPACE_PATTERN); | |
return result; | |
} | |
public static String readLine() { | |
String line; | |
try { line = scanner.nextLine(); } | |
catch (Exception e) { line = null; } | |
return line; | |
} | |
public static char readChar() { | |
scanner.useDelimiter(EMPTY_PATTERN); | |
String ch = scanner.next(); | |
assert (ch.length() == 1) : "Internal (Std)In.readChar() error!" | |
+ " Please contact the authors."; | |
scanner.useDelimiter(WHITESPACE_PATTERN); | |
return ch.charAt(0); | |
} | |
public static String readAll() { | |
if (!scanner.hasNextLine()) | |
return ""; | |
String result = scanner.useDelimiter(EVERYTHING_PATTERN).next(); | |
scanner.useDelimiter(WHITESPACE_PATTERN); | |
return result; | |
} | |
public static String readString() { | |
return scanner.next(); | |
} | |
public static int readInt() { | |
return scanner.nextInt(); | |
} | |
public static double readDouble() { | |
return scanner.nextDouble(); | |
} | |
public static float readFloat() { | |
return scanner.nextFloat(); | |
} | |
public static long readLong() { | |
return scanner.nextLong(); | |
} | |
public static short readShort() { | |
return scanner.nextShort(); | |
} | |
public static byte readByte() { | |
return scanner.nextByte(); | |
} | |
public static boolean readBoolean() { | |
String s = readString(); | |
if (s.equalsIgnoreCase("true")) return true; | |
if (s.equalsIgnoreCase("false")) return false; | |
if (s.equals("1")) return true; | |
if (s.equals("0")) return false; | |
throw new java.util.InputMismatchException(); | |
} | |
public static String[] readAllStrings() { | |
String[] tokens = WHITESPACE_PATTERN.split(readAll()); | |
if (tokens.length == 0 || tokens[0].length() > 0) | |
return tokens; | |
String[] decapitokens = new String[tokens.length-1]; | |
for (int i=0; i < tokens.length-1; i++) | |
decapitokens[i] = tokens[i+1]; | |
return decapitokens; | |
} | |
public static int[] readAllInts() { | |
String[] fields = readAllStrings(); | |
int[] vals = new int[fields.length]; | |
for (int i = 0; i < fields.length; i++) | |
vals[i] = Integer.parseInt(fields[i]); | |
return vals; | |
} | |
public static double[] readAllDoubles() { | |
String[] fields = readAllStrings(); | |
double[] vals = new double[fields.length]; | |
for (int i = 0; i < fields.length; i++) | |
vals[i] = Double.parseDouble(fields[i]); | |
return vals; | |
} | |
private static void resync() { | |
setScanner(new Scanner(new java.io.BufferedInputStream(System.in), | |
charsetName)); | |
} | |
private static void setScanner(Scanner scanner) { | |
StdIn.scanner = scanner; | |
StdIn.scanner.useLocale(usLocale); | |
} | |
static { | |
resync(); | |
} | |
public static int[] readInts() { | |
return readAllInts(); | |
} | |
public static double[] readDoubles() { | |
return readAllDoubles(); | |
} | |
public static String[] readStrings() { | |
return readAllStrings(); | |
} | |
public static void main(String[] args) { | |
System.out.println("Type a string: "); | |
String s = StdIn.readString(); | |
System.out.println("Your string was: " + s); | |
System.out.println(); | |
System.out.println("Type an int: "); | |
int a = StdIn.readInt(); | |
System.out.println("Your int was: " + a); | |
System.out.println(); | |
System.out.println("Type a boolean: "); | |
boolean b = StdIn.readBoolean(); | |
System.out.println("Your boolean was: " + b); | |
System.out.println(); | |
System.out.println("Type a double: "); | |
double c = StdIn.readDouble(); | |
System.out.println("Your double was: " + c); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment