Question: How to identify and count the clusters ( 4 direction connected components) in a 2D array? The way to label (identify) the clusters is like
How to identify and count the clusters ( 4 direction connected components) in a 2D array?
The way to label (identify) the clusters is like the flood fill, if it connects to their neighbor by up-down-left-right direction, they will be grouping in the same cluster. we go from top left to bottom right. Please help me solve this problem. Thanks!!! (javascript code example would be great)

The color version is: 
for example, I have 2d array like this: 1 2 2 2 2 1 1 1 1 2 1 1 2 1 1 1 2 1 2 2 1 1 2 1 2 2 1 2 1 1 2 2 2 2 1 1 1 1 1 2 1 2 1 2 2 1 1 2 1 1 2 1 2 1 2 1 2 2 2 2 1 1 2 2 2 2 1 2 1 2 1 1 2 1 1 2 1 1 2 2 1 1 1 2 And I want to label the connected components (4 directions to be like this: 1 2 2 2 2 3 3 3 3 4 5 5 6 7 7 7 2 3 8 8 3 3 9 5 6 6 7 10 3 3 8 8 8 8 5 5 7 7 7 10 3 11 12 8 8 5 5 8 7 7 13 14 13 15 16 17 8 8 8 8 7 7 13 13 13 13 18 19 20 8 20 20 21 7 7 13 22 22 19 19 20 20 20 23 for example, I have 2d array like this: 1 2 2 2 2 1 1 1 1 2 1 1 2 1 1 1 2 1 2 2 1 1 2 1 2 2 1 2 1 1 2 2 2 2 1 1 1 1 1 2 1 2 1 2 2 1 1 2 1 1 2 1 2 1 2 1 2 2 2 2 1 1 2 2 2 2 1 2 1 2 1 1 2 1 1 2 1 1 2 2 1 1 1 2 And I want to label the connected components (4 directions to be like this: 1 2 2 2 2 3 3 3 3 4 5 5 6 7 7 7 2 3 8 8 3 3 9 5 6 6 7 10 3 3 8 8 8 8 5 5 7 7 7 10 3 11 12 8 8 5 5 8 7 7 13 14 13 15 16 17 8 8 8 8 7 7 13 13 13 13 18 19 20 8 20 20 21 7 7 13 22 22 19 19 20 20 20 23
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
