Algorithm is required for Finding the group of 1s in a matrix, but the group of 1s should contain only vertical entry
Asked
Active
Viewed 143 times
-5
-
4You mean you can't figure out how to search columns in a matrix to find adjacent 1's? Stack Overflow really isn't about us doing your work for you. It's about us helping you improve your own work. Please read https://stackoverflow.com/help/how-to-ask, and edit your question accordingly. – Jim Mischel Jun 30 '18 at 05:16
1 Answers
0
This is more in the brain-teaser category.
For small matrices, we just access it column by column.
Assuming input is a 2 dimension array of integer, and output is a list of the following class:
class GroupIdentifier {
int col;
int rowStart;
int colStart;
/* the corresponding getter/setter/constructor etc */
}
Here is the function you are looking for:
int FIRST_ONE = 2;
int IN_GROUP = 1;
int OUT_GROUP = 0;
public List<GroupIdentifier> findVerticalGroupsOf1 (int[][] a,
int numRow, int numCol) {
List<GroupIdentifier> answer = new ArrayList<GroupIdentifier>();
for (col = 0; col < numCol; col = col + 1) {
// let's create the tempArr holding the current col
int[] tempArr = new int[numRow];
for (row = 0; row < numRow; row = row + 1 =) {
tempArr[row] = a[row][col];
}
// let's find the groups
// first, init the state
int state = (tempArr[0] == 1) ? FIRST_ONE : OUT_GROUP;
int start = (state == FIRST_ONE) ? 0 : -1;
// I see this problem as a simple state-machine
// We have three states: FIRST_ONE encountered (0 to 1),
// still tracking the 1s IN_GROUP ... (1 to 1) and
// get OUT_GROUP (1 to 0).
// the switch case in the following loop does exactly that.
// So, whenever we get an OUT_GROUP event, we get an answer.
for (int i = 1; i < numRow; i = i + 1) { // edit: changed to numRow as
// it was a typo error
switch (state) :
case FIRST_ONE :
if (tempArr[i] == 0)
state = OUT_GROUP;
else
state = IN_GROUP;
break;
case IN_GROUP :
if (tempArr[i] == 0) {
GroupIdentifier gi = new GroupIdentifier (col, start, i - 1);
answer.add(gi);
}
break;
case OUT_GROUP :
if (tempArr[i] == 1) {
start = i;
state = FIRST_ONE;
}
break;
}
}
// since this question looks like homework,
// i will leave out the boundary case handling
// here. it's not that hard; just copy/paste the
// switch statement and fondle around.
return answer;
}
As a computer scientist myself, I think of ways to optimize it.
For larger matrices, what I would do is to precompute the all combinations of tempArr and store them as Integer --> List map. Then, I will traverse the columns without computing again.

Edward Aung
- 3,014
- 1
- 12
- 15
-
@EdwardAung can you please explain this code. i am confuse in the logic being used for vertical grouping. It will be much more better if you could write the full code in Java. Thanks – user8409309 Jul 02 '18 at 06:49
-
@user8409309 ... edited the answer to include comments explaining the code (as well as corrected a typo error). And my code is in pure Java (not even use any known libraries). – Edward Aung Jul 02 '18 at 07:57
-
can you please send me the full java code?. i worked around this function but i couldnt implement it. Thank you – user8409309 Jul 10 '18 at 04:01