How to construct a binary matrix with given row and column distribution

coding-theorycombinatorial-designscombinatoricsgraph theorymatrices

I need to construct an $m \times n$ binary matrix $B$ from a given row and columns distribution.
What algorithms can be used for this? As a concrete example : $B$ a $12 \times 63$ matrix with row distribution $12 r^{19}$ and column distribution $12 c^2 + 39 c^3 + 9 c^7 + 3 c^8$. That is
all $12$ rows have weight $19$; $12$ columns have weight $2$, $39$ columns have weight $3$, $\cdots$, $3$ columns have weight $8$. The weight of binary row or column is its sum or equivalently the number of nonzero entries. (This comes up in LDPC code constructions; the example is from LDPC Code Designs (2017), page 38)

Best Answer

One approach is to use integer linear programming (ILP) as follows. Let $r_i$ and $c_j$ be the desired row and column sums, respectively. Let binary decision variable $B_{i,j}$ be the entry in row $i$ and column $j$. The constraints are \begin{align} \sum_j B_{i,j} &= r_i &&\text{for all $i$}\\ \sum_i B_{i,j} &= c_j &&\text{for all $j$}\\ \end{align} Branch-and-cut is the most common algorithm for ILP, but it turns out that for your problem the linear programming relaxation always yields an integer solution. This particular problem is known as the transportation problem, with a supply node for row $i$ and a demand node for column $j$, and the variable $B_{i,j}$ represents the "flow" from node $i$ to node $j$. Usually the transportation problem has a linear objective function $\sum_{i,j} c_{i,j} B_{i,j}$ to be minimized, but yours is a feasibility problem; you can think of this as taking $c_{i,j}=0$ as the cost.

For your sample data, here is one such matrix:

1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 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