473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

2 dimensional array algorithm question

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.

Nov 14 '05 #1
3 2023
On 12 Jun 2005 11:16:16 -0700, "Zarathustra"
<hu************@hotmail.com> wrote:
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/...nkopelman.html
Nov 14 '05 #2


Paul Mesken wrote:
On 12 Jun 2005 11:16:16 -0700, "Zarathustra"
<hu************@hotmail.com> wrote:
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/...nkopelman.html


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

Nov 14 '05 #3
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.

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
6
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
5
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.