2

The task is to find all N×N matrices M with integer values, when the sums along the cols and rows are fixed.

For N=3 this means: Find the matrix M

m11 m12 m13
m21 m22 m23
m31 m32 m33

so that

m11 + m12 + m13 = r1
m21 + m22 + m23 = r2 
m31 + m32 + m33 = r3

and

m11 + m21 + m31 = c1
m12 + m22 + m32 = c2 
m13 + m23 + m33 = c3

holds.

e.g.: For

r1=3, r2=6, r3=9,
c1=5, c2=6, c3=7

one solution would be:

0 1 2
1 2 3
4 3 2

The only algorithm I came up is to calculate straightforward all possible partitions for the rows, start with the first partition (p11, p12, ..., p1N) of the first column, select all compatible partitions (p21, p22, ..., p2N) of the second row by checking p11+p21 <= c1, ..., p1N+p2N <= cN, and proceed with the third row. If one wants to visit all possible solutions this scales obviously very badly with N.

The 1D problem - the partition of a number A into N parts - is solved by a number of algorithms. One basic idea is to calculate all possibilities to split a string of A '+' by (N-1) '|' and count the numbers of + between the | (e.g.: +++|++|+| yields 3,2,1,0). I wonder if it is possible to extend this ansatz to the 2D problem. Also, I suspect that it might be possible to calculate a solution based on the preceding solution and iterate over all solutions.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
hbar
  • 43
  • 2

0 Answers0