-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmark.java
More file actions
86 lines (74 loc) · 3.81 KB
/
Benchmark.java
File metadata and controls
86 lines (74 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Performance Benchmark Suite.
* Pits the Legacy Recursive DFS approach against the Optimized Union-Find approach.
* Tests multiple grid sizes to demonstrate Time Complexity scaling and Stack Overflow limits.
*/
public class Benchmark {
public static void main(String[] args) {
// We test increasingly large grids.
// Warning: Legacy DFS will likely StackOverflow at N=200 or N=500.
int[] gridSizes = {10, 50, 100, 250, 500, 1000};
System.out.println("=========================================================================");
System.out.println(" PERCOLATION ALGORITHM BENCHMARK: LEGACY (DFS) vs OPTIMIZED (UF) ");
System.out.println("=========================================================================");
System.out.printf("%-10s | %-15s | %-20s | %-20s%n", "Grid (NxN)", "Strikes Needed", "Legacy Time (ms)", "Union-Find Time (ms)");
System.out.println("-------------------------------------------------------------------------");
for (int n : gridSizes) {
// 1. Generate a deterministic list of random strikes so both algorithms face the exact same scenario
List<int[]> strikes = generateRandomStrikes(n);
// 2. Setup instances
LegacyPercolation legacyGrid = new LegacyPercolation(n);
Percolation optimizedGrid = new Percolation(n);
long legacyTime = -1;
long optimizedTime = -1;
boolean legacyCrashed = false;
int strikesToPercolate = 0;
// 3. Test Optimized Union-Find
long startTimeOpt = System.currentTimeMillis();
for (int[] strike : strikes) {
optimizedGrid.strike(strike[0], strike[1]);
if (optimizedGrid.percolates()) {
strikesToPercolate = strikes.indexOf(strike) + 1;
break;
}
}
optimizedTime = System.currentTimeMillis() - startTimeOpt;
// 4. Test Legacy Recursive DFS
long startTimeLeg = System.currentTimeMillis();
try {
for (int[] strike : strikes) {
legacyGrid.strike(strike[0], strike[1]);
if (legacyGrid.percolates()) break;
}
legacyTime = System.currentTimeMillis() - startTimeLeg;
} catch (StackOverflowError e) {
// DFS goes too deep on large grids and crashes the JVM stack
legacyCrashed = true;
}
// 5. Print Results Formatting
String legacyStr = legacyCrashed ? "STACK_OVERFLOW ☠️" : String.format("%d ms", legacyTime);
String optStr = String.format("%d ms", optimizedTime);
System.out.printf("%-10s | %-15d | %-20s | %-20s%n",
n + "x" + n, strikesToPercolate, legacyStr, optStr);
}
System.out.println("=========================================================================");
System.out.println("* NOTE: Legacy approach uses deep recursion, causing memory limit crashes on large grids.");
System.out.println("* NOTE: Optimized approach scales linearly thanks to Path Compression in the Disjoint-Set.");
}
/**
* Generates a fully shuffled list of all possible coordinates in an NxN grid.
*/
private static List<int[]> generateRandomStrikes(int n) {
List<int[]> strikes = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
strikes.add(new int[]{i, j});
}
}
Collections.shuffle(strikes);
return strikes;
}
}