After performing connected component analysis, we often want to find the largest object (region with most pixels).
Given a 2D binary grid, use Union-Find to identify all connected foreground regions and return the size of the largest region (count of 1s in that region).
Connected Component Analysis identifies groups of foreground pixels (1s) that are connected in a binary grid. Two pixels are connected if they share an edge (4-connectivity) or corner (8-connectivity). Union-Find (Disjoint Set Union - DSU) efficiently tracks these equivalence classes by merging connected sets and finding set representatives.
Union-Find Structure maintains:
parent[i]: parent of node i
size[i]: size of component rooted at i (for union-by-size)
Find: Path-compressed root finding with nearly-constant amortized time O(\alpha(n)), where α is the inverse Ackermann function.
Union: Merges smaller trees into larger ones, preserving size tracking.