# How to recreate a matrix (0-1) given the number of 1s in each row and column?

advertisements

The first part to this program was randomly generating a matrix with dimensions between 2 and 6. Then I had to fill this matrix up with 1s and 0s randomly. Using this matrix, I made 2 one-dimensional arrays containing the number of 1's in each row and column. The index of the matrix representing the row number, and the number in the cell representing the count. I made 2 of these arrays: one for row count and one for column count. Here was my code for that.

``````    public static void count(int[][] matrix, int[] rowcount, int[] colcount)
{
for(int x = 0; x < rowcount.length; x++)
for(int y = 0; y < colcount.length; y++)
{
if (matrix[x][y] == 1)
{
rowcount[x] = rowcount[x] + 1;
colcount[y] = colcount[y] + 1;
}
}
}
```
```

Now the problem I'm facing is to recreate this matrix using these counts. By recreate, I mean just create another matrix that satisfies the the counts of the 1-D arrays, it is not necessary to generate the exact matrix that these counts were derived from. Here is my code for that so far, I've been working on this for program for 2 days and I can't find an algorithm to generate a matrix for all cases.

Here are the methods for that

``````    public static void re_create(int[] rowcount, int[] colcount)
{
int[][] recreated = new int[rowcount.length][colcount.length];
recur(recreated, rowcount, colcount, 0, 0);
}
private static void recur(int[][] m, int[] rowcount, int[] colcount, int r, int c) //recursive helper method
{
if(compare(m, rowcount, colcount))    //base case: if new matrix works
{
System.out.println();
System.out.println("RECREATED");
display(m, rowcount, colcount);    //we're done!
System.exit(0);
}
else
{
int[] temp_r = new int[m.length];
int[] temp_c = new int[m[0].length];
count(m, temp_r, temp_c);
if(rowcount[r] > temp_r[r] && colcount[c] > temp_c[c])
m[r][c] = 1;
if(r+1 < m.length)
recur(m,rowcount,colcount,r+1,c);
if(rowcount[r] < temp_r[r] || colcount[c] < temp_c[c])
m[r][c] = 0;
if(c+1 < m[0].length)
recur(m,rowcount,colcount,r,c+1);
}
}
private static boolean compare(int[][] m, int[] rowcount, int[] colcount)
{
int[] temp_r = new int[m.length];
int[] temp_c = new int[m[0].length];
count(m, temp_r, temp_c);

for (int x = 0; x < temp_r.length; x++)
{
if(temp_r[x] != rowcount[x])
return false;
}

for (int y = 0; y < temp_c.length; y++)
{
if(temp_c[y] != colcount[y])
return false;
}

return true;
}
```
```

The program is from school, so I was already given the method header and the base case for the recursive method, so those must stay the same. Everything else was written by me. I just can't find a good algorithm to generate these matrices. I think I'm supposed to generate every permutation of 1s and 0s in the matrix until one matches the base case, but I don't understand how that would work given the arguments in the recur method.

There is no solution that will work for all cases. Consider:

``````0 1 0 0 | 1    0 0 1 0 | 1
1 0 1 0 | 2    0 1 0 1 | 2
0 1 0 1 | 2    1 0 1 0 | 2
0 0 1 0 | 1    0 1 0 0 | 1
-------        -------
1 2 2 1        1 2 2 1
```
```