首页 文章

从和来重建矩阵

提问于
浏览
2

我想重建一个包含所有正整数元素的矩阵,给定每行和每列的总和 .

a0 a1 a2 .. aN | Σa
b0 b1 b2 .. bN | Σb
.. .  .  .  .. | ..
..  .  .  . .. | ..
z0 z1 z2 .. zN | Σz
---------------+----
Σ0 Σ1 Σ2 .. ΣN |

在给定行和列总和的情况下,是否有算法可以找到所有可能的矩阵元素组合 .

非常感谢任何参考 .

1 回答

  • 1

    是 . 你拥有的是system of linear equations,每行一个,每列一个; m * n个变量和m n个方程 .

    如何计算(和表示)解决此类系统的解决方案集取决于您的环境 .

    EDIT: 我明白了 . 有关此问题的大型实例,请参阅integer programming .

    但如果矩阵和行/列总和很小,则可以通过backtracking找到所有解 . 非常高级的伪代码:

    function SOLVE(partially filled M) {
    
        if (M has no empty entries) {
    
            M is a solution
    
        } else {
    
            ij <- one empty position of M
            // in practice, try picking one that reduces the number of
            // iterations of the following loop
    
            for (each possible value v of M[ij], subject to the constraints) {
                M' <- a copy of M
                M'[ij] = v
                SOLVE(M')
        }
    }
    
    
    M0 <- an empty Matrix of correct size
    
    SOLVE(M0)
    

相关问题