Search code examples
arraysmathmatrixsumbranch-and-bound

How to build a binary matrix from sums


I have two decimal number variables, colSum and rowSum, using those I want to build a matrix of binary values based on those sums, the rowSum array variable is the result of adding all the 1's for each row, the same goes for colSum array.

So ,if you have

rowSum = [0,1,2]
colSum = [1,1,1]

you will have to build properly the following array

matrix = [
 [0,0,0],
 [0,0,1],
 [1,1,0]
]

I'm using this method in PHP, that works for a 3x3 matrix, but not for a bigger one, like 8x8. First ,fill all the 1's in the rows using the rowSum value. Then ,try to find a wrong sum value of 2 columns, with a pivot I inter-change them (1 with a cero value) in the same row, until i get the correct value of colSum. But it will not work because I need some control of the criteria to change the 1 and 0 in the same row for two columns...

This is the method I'm using.

Let's say we have this Matrix (N=3 -> NxN):

0  0  0
0  0  1
1  1  0

then we have the following arrays

R0 = {0,1,2} //--> result of sums of each rows: ( 0+0+0, 0+0+1 , 1+1+0 )
C0 = {1,1,1} // ->sums of each columns

Step 1

Create and fill a NxN array using as many 1's as R0(i) in each row:

0  0  0
1  0  0
1  1  0 

compute sums of this new matrix now: R1 = {0,1,2} C1 = {2,1,0}

Step 2

Check if for all the elements of the column sums of the created matrix has the same value as C0 (origin)

for ( i=0, N-1) do

 if C0(i)!=C1(i) then
   ReplaceColumn(i)
 end

end

To replace a column we have to dig inside the conditions. C0(0) = 1 != C1(0) = 2 the first column sum does meet the condition to call the replace ,so

Step 3

Choose criteria for apply the branch & bound method and find the best row to change column that satisfy the global condition (all column sums).

The amount of changes for a difference between columns sums is:

|C0(i)-C1(i)| 

for this example, |C0(0)-C1(0)| = 1 change. Go back condition must be if the change generates a greater difference between the total sum of columns.

Σi,N(|C0(i)-C1(i)|)

So, could this method really work?


Solution

  • Is the goal to construct the matrix that satisfies the row and column sums or a matrix that satisfies them? It's not clear from the question, but if it's the former ("the" case) then it's not going to possible.

    Suppose it were the case that you could uniquely represent any m × m matrix of bits in this way. Then consider the following hypothetical compression algorithm.

    • Take 22n bits of data
    • Treat it as 2n × 2n bits
    • To describe the data, use 2 × 2n row and column sums, each using at most log2(2n) = n bits
    • The data is compressed to 2 × n × 2n bits

    Since 2 × n × 2n << 22n and this process could just keep being repeated, the supposition that you can uniquely represent any m × m matrix of bits by only its row and column sums is false.