2 dimensional array algorithm question

Z

Zarathustra

I'm sure there must be a simple algorithm for giving the number of
"ones" that are connected, for example in an array like this:
int a[5][5] = {0};
a[0][0] = a[0][1] = a[1][1] = a[2][1] = a[0][4] = a[3][4]= a[4][4] =
a[4][0] = 1;
with all the rest still equal to zero.

1 0 0 1 1
0 0 0 0 0
0 0 0 0 0
1 1 1 0 0
1 0 0 0 1

such that the output in this case, can give 1 group of size 4, 1 group
of size 2, and 2 groups of size 1.

It looks simple, but everything I've tried so far hasn't caught all
possible shapes of groups of "1"s.

Any ideas would be greatly appreciated.
 
P

Paul Mesken

I'm sure there must be a simple algorithm for giving the number of
"ones" that are connected, for example in an array like this:
int a[5][5] = {0};
a[0][0] = a[0][1] = a[1][1] = a[2][1] = a[0][4] = a[3][4]= a[4][4] =
a[4][0] = 1;
with all the rest still equal to zero.

1 0 0 1 1
0 0 0 0 0
0 0 0 0 0
1 1 1 0 0
1 0 0 0 1

such that the output in this case, can give 1 group of size 4, 1 group
of size 2, and 2 groups of size 1.

It looks simple, but everything I've tried so far hasn't caught all
possible shapes of groups of "1"s.

Any ideas would be greatly appreciated.

Jburgy mentioned in thread "Clustering in C" the Hoshen-Kopelman
algorithm. It's very simple. Just use it and then count the number of
cells for each cluster.

Here's a link :

http://splorg.org:8080/people/tobin/projects/hoshenkopelman/hoshenkopelman.html
 
Z

Zarathustra

Paul said:
I'm sure there must be a simple algorithm for giving the number of
"ones" that are connected, for example in an array like this:
int a[5][5] = {0};
a[0][0] = a[0][1] = a[1][1] = a[2][1] = a[0][4] = a[3][4]= a[4][4] =
a[4][0] = 1;
with all the rest still equal to zero.

1 0 0 1 1
0 0 0 0 0
0 0 0 0 0
1 1 1 0 0
1 0 0 0 1

such that the output in this case, can give 1 group of size 4, 1 group
of size 2, and 2 groups of size 1.

It looks simple, but everything I've tried so far hasn't caught all
possible shapes of groups of "1"s.

Any ideas would be greatly appreciated.

Jburgy mentioned in thread "Clustering in C" the Hoshen-Kopelman
algorithm. It's very simple. Just use it and then count the number of
cells for each cluster.

Here's a link :

http://splorg.org:8080/people/tobin/projects/hoshenkopelman/hoshenkopelman.html

Thanks very much. That was exactly what I was looking for.
 
G

googmeister

It suffices to use depth-first search or breadth-first
search to label all of the clusters. Union-find is
probably overkill here since your graph is fixed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top