Skip to main content

Complexity and Memory

LetΒ N=widthΓ—height\text{Let } N = width \times height

Time​

The flood-fill visits each pixel once and tests 4 neighbours (4β‹…N4 \cdot N), so the dominant cost is O(N)O(N) with a small constant (neighbour checks and byte comparisons). The merge pass is another similar O(N)O(N) scan, so overall O(N)O(N).

Memory​

The implementation allocates a labels array of NN integers and a regions vector whose size equals number of components (at most NN). So memory is O(N)O(N) additional to the image buffer.

Cache behaviour​

BFS queue can cause random access inside large components; using a scanline two-pass connected-component algorithm (or union-find) can improve cache locality and throughput on big images.