PIXELBANKv9.1.0
Menu

Problem Statement

In image segmentation, connected component labeling is essential for identifying distinct objects. Union-Find (Disjoint Set Union) is an efficient data structure for this task.

Given a 2D binary grid where 1 represents foreground pixels and 0 represents background, count the number of connected foreground regions using Union-Find.

Two pixels are connected if they are adjacent horizontally or vertically (4-connectivity).

Applications

  • Object counting in binary images
  • Blob detection
  • Particle analysis in microscopy

Constraints

  • 1≤rows,cols≤1001 \leq rows, cols \leq 100
  • Grid contains only 0s and 1s

Example:

Input:
grid = [[1, 1, 0], [0, 1, 0], [1, 0, 1]]
Output:
3
Reasoning:

Three separate regions: top-left blob (3 pixels), bottom-left single pixel, bottom-right single pixel.

solution.py

Test Results

0/0
Run code to see test results.
Count Connected Regions - Medium | PixelBank