Graphs & Connectivity Hard
Union-Find: Flood-Fill Connectivity
The pinned GlobalWarming implementation labels connected land cells with recursive four-neighbor flood fills, then answers component queries from the labels.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(n²) preprocessing for an n×n grid
AverageO(n²) preprocessing for an n×n grid
WorstO(n²) preprocessing for an n×n grid
SpaceO(n²) labels and up to O(n²) recursive depth
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Cells at or below the water level are submerged. Count the remaining islands and answer whether pairs of grid coordinates share one, connecting only horizontal and vertical neighbors.
- The catalog groups this lesson under union-find, but the pinned source uses recursive depth-first flood fill and a flat component-label array.
- Altitude equal to the water level is submerged; only strictly higher cells are land.
- Diagonal contact does not connect two islands; source neighbor order is down, up, right, then left.
- A cell is marked before its neighbors are visited, which stops recursive cycles and repeated work.
- The constructor scans row-major and increments the component count only when the starting cell still carries the current island id.
- onSameIsland returns false when its first point is submerged, including when both points refer to the same underwater cell.
- The source expects square grids and valid point coordinates; the lesson enforces both constraints and bounds grid size for safe traces.
Algorithm code
JavaGlobalWarming.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1