Complexity and Memory
Timeβ
The flood-fill visits each pixel once and tests 4 neighbours (), so the dominant cost is with a small constant (neighbour checks and byte comparisons). The merge pass is another similar scan, so overall .
Memoryβ
The implementation allocates a labels array of integers and a regions vector whose size equals number of components
(at most ). So memory is 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.