+ 2

How I can find a number of all connecting cells which filled with 1 in matrix NxN? (Python).

Problem connecting cells. For example matrix 4x4 a = [[1,0,0,1] [0,0,1,1] [0,0,0,0] [1,1,0,0]] In this matrix a number of connecting cells equal 3. 1st: a[0][0] 2nd:a[0][3], a[1][2], a[1][3] 3rd: a[2][0], a[2][1]

6th Aug 2020, 3:05 PM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
21 Answers
+ 3
Rustam, the first part adds extra row of 0s and flattens the matrix to one list. the second part searches for all spaces around each node except the space directly above and on the top left. but, why that works i honestly dont know! im sorry! i just kept messing with it until it worked 🤔
10th Aug 2020, 5:16 AM
madeline
madeline - avatar
+ 2
Rustam, no need for sorry it's ok 👌 Can you perhaps show another example, maybe a 3x3 matrix or something. I ask because I'm still unsure what output is expected and how to format the output. Actually, I still don't quite understand the term "connecting cells" : )
7th Aug 2020, 3:11 PM
Ipang
+ 2
Madeline . Yes
7th Aug 2020, 3:37 PM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
+ 2
Oh okay 👌 Looks pretty tough Rustam, maybe I'll get back if I come up with something. Right now, no idea ...
7th Aug 2020, 3:47 PM
Ipang
+ 2
Thanks
7th Aug 2020, 4:31 PM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
+ 2
there is quite a bit of theory on this problem. it is called “find thd number of islands” problem and there are ideas about depth first search and breadth first search. just fyi! thanks!!!
10th Aug 2020, 2:32 PM
madeline
madeline - avatar
+ 1
What you mean connecting cells?!
6th Aug 2020, 5:17 PM
george
george - avatar
+ 1
Rustam find in inet percolation algorithm.
6th Aug 2020, 7:51 PM
george
george - avatar
+ 1
did you want all connecting 1s or only the longest group of them?
6th Aug 2020, 7:58 PM
madeline
madeline - avatar
+ 1
i guess i need a clearer question lol
6th Aug 2020, 8:19 PM
madeline
madeline - avatar
+ 1
How I can find a number of all connecting cells which filled with 1 in matrix NxN? (Python). I mean , Problem connecting cells. For example matrix 4x4 a = [[1,0,0,1] [0,0,1,1] [0,0,0,0] [1,1,0,0]] In this matrix a number of connecting cells equal 3. 1st: a[0][0] 2nd:a[0][3], a[1][2], a[1][3] 3rd: a[2][0], a[2][1]
7th Aug 2020, 6:11 AM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
+ 1
oh i gotcha! you want the number of groups of 1s. i’ll work on that. thank you for the challenge. but... i can already see where it will be tough!!!
7th Aug 2020, 12:42 PM
madeline
madeline - avatar
+ 1
Rustam, Are you sure about the 3rd a[2][0], a[2][1] a[2] contains zero only ... I think maybe you mean a[3][0], a[3][1] because there are two values of 1 there.
7th Aug 2020, 1:13 PM
Ipang
+ 1
Ipang ok. This is 3x3 matrix [0][1][0] [0][0][1] [1][1][0] In this matrix a number of connected cells are 1. Because all cells which filled with 1 are neighbors each other.
7th Aug 2020, 3:15 PM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
+ 1
George thank you
7th Aug 2020, 3:19 PM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
+ 1
Rustam Correct me if I'm wrong understanding ... In 3x3 matrix above, the 3rd row [1, 1, 0] is the only one connected cell because it contains two adjacent value of 1. Did I get it right this time?
7th Aug 2020, 3:23 PM
Ipang
+ 1
Ipang. No you're wrong. Because [2][1] cell is neighbour with [1][2] and [1][2] cell is neighbour with [0][1] cell. All of them filled with 1 and neighbors each other. All of them connected with each other.
7th Aug 2020, 3:34 PM
Rustam Bazarbayev
Rustam Bazarbayev - avatar
+ 1
he is also counting diagonals it looks like
7th Aug 2020, 3:36 PM
madeline
madeline - avatar
+ 1
i am working on it now haha and its rly hard to say the least...
7th Aug 2020, 3:48 PM
madeline
madeline - avatar
+ 1
Madeline thank you. It's working. Can you explain me this code?
10th Aug 2020, 3:29 AM
Rustam Bazarbayev
Rustam Bazarbayev - avatar