Papers
Topics
Authors
Recent
Search
2000 character limit reached

Binary Magic Squares: Theory & Constructions

Updated 4 July 2026
  • Binary Magic Squares are binary matrices with each row and column summing to a constant, serving as incidence matrices for regular bipartite graphs.
  • Constructive generation methods use wrapped-band matrices and column-by-column algorithms that guarantee optimal O(n²) performance.
  • Alternate definitions include parity patterns from classical magic squares, highlighting distinct combinatorial and symmetry-based interpretations.

Searching arXiv for recent work on Binary Magic Squares and related constrained/binary magic-square literature. Binary Magic Squares (BMS) are binary matrices M{0,1}n×nM \in \{0,1\}^{n\times n} for which there exists an integer kk such that every row sum and every column sum equals kk; the same framework extends to rectangular matrices M{0,1}m×nM \in \{0,1\}^{m\times n} with constant row sum aa and constant column sum bb (Riou, 1 Nov 2025). In this formulation, a BMS is exactly the incidence or adjacency matrix of a regular bipartite graph with constant degree on each side. A distinct, secondary usage also exists in the magic-square literature, where “binary magic square” denotes the $0/1$ parity pattern obtained from an ordinary magic square by mapping odd entries to $1$ and even entries to $0$ (Fahimi, 2023). The modern algorithmic literature on BMS is centered on the first meaning: equal-margin binary matrices with provable existence criteria and efficient constructive generation.

1. Definitions and combinatorial interpretation

In the square case, a Binary Magic Square is a matrix

M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}

such that there exists kk0 with

kk1

kk2

Thus each row and each column contains exactly kk3 ones. The same paper also treats the generated objects as random binary matrices of this type when the construction uses randomized subset selection (Riou, 1 Nov 2025).

The rectangular generalization replaces the single degree parameter kk4 by a row target kk5 and a column target kk6. A matrix

kk7

is a non-square BMS if there exist integers kk8 and kk9 such that every row sum equals kk0 and every column sum equals kk1. This is the constant-margin case of a binary matrix with prescribed row and column sums.

The graph-theoretic interpretation is exact. In the square case, the matrix is the incidence matrix of a kk2-regular bipartite graph on two parts of size kk3; in the rectangular case, it is the adjacency matrix of a bipartite graph with left part of size kk4, right part of size kk5, all left-degrees equal to kk6, and all right-degrees equal to kk7 (Riou, 1 Nov 2025). This places BMS at the intersection of regular graph realization and constrained binary matrix generation.

2. Existence theory for square and rectangular BMS

For square matrices, existence is unconditional once kk8 and kk9 satisfy M{0,1}m×nM \in \{0,1\}^{m\times n}0. The constructive theorem states that for all M{0,1}m×nM \in \{0,1\}^{m\times n}1 and all M{0,1}m×nM \in \{0,1\}^{m\times n}2, there exists a valid M{0,1}m×nM \in \{0,1\}^{m\times n}3 BMS with row and column sums equal to M{0,1}m×nM \in \{0,1\}^{m\times n}4 (Riou, 1 Nov 2025). The proof uses a wrapped-band matrix: M{0,1}m×nM \in \{0,1\}^{m\times n}5 with indices taken in M{0,1}m×nM \in \{0,1\}^{m\times n}6. Intuitively, each row contains a block of M{0,1}m×nM \in \{0,1\}^{m\times n}7 consecutive ones, and the block may wrap around the matrix boundary. Direct row- and column-sum calculations show that every row and every column sums to M{0,1}m×nM \in \{0,1\}^{m\times n}8. The construction is presented as a special case of the Gale–Ryser theorem (Riou, 1 Nov 2025).

The rectangular case is more restrictive. If M{0,1}m×nM \in \{0,1\}^{m\times n}9 has every row sum equal to aa0 and every column sum equal to aa1, then counting the total number of ones by rows and by columns yields

aa2

The characterization theorem states that such a matrix exists if and only if

aa3

equivalently, if there exist integers aa4 such that

aa5

Here aa6 and aa7 (Riou, 1 Nov 2025).

The sufficiency proof is constructive. One first builds a aa8 square BMS with row and column sum aa9, then tiles that pattern bb0 times vertically and bb1 times horizontally. Each row of the resulting bb2 matrix consists of bb3 copies of a row of the bb4 pattern, and each column consists of bb5 copies of a column, so the row sums become bb6 and the column sums become bb7 (Riou, 1 Nov 2025).

A notable corollary concerns coprime dimensions. If bb8, then the only non-square BMS are the trivial all-zero and all-one matrices. This sharply separates the square case, where every bb9 is feasible, from the rectangular case, where degree compatibility is the central constraint (Riou, 1 Nov 2025).

3. Constructive generation and inductive correctness

The principal generation procedure is column-by-column. For the square case, the input is $0/1$0 and $0/1$1; the output is $0/1$2 with all row and column sums equal to $0/1$3 (Riou, 1 Nov 2025). The algorithm maintains partial row sums

$0/1$4

together with the invariant

$0/1$5

The upper bound prevents any row from exceeding its final target, while the lower bound guarantees that enough capacity remains in later columns for the row to reach exactly $0/1$6.

At each column $0/1$7, rows are partitioned into three sets: $0/1$8

$0/1$9

$1$0

Rows in $1$1 are at the critical lower bound and must be selected in the current column. Rows in $1$2 are already saturated and must not be selected. The algorithm therefore sets

$1$3

then writes ones in column $1$4 precisely on the rows in $1$5. The only randomness lies in the choice of the subset of $1$6 (Riou, 1 Nov 2025).

Correctness is proved by induction. One lemma shows that the invariant

$1$7

holds for all rows and all steps; at $1$8, the lower and upper bounds coincide, so every row sum is exactly $1$9. A second lemma shows that $0$0 at every column, hence each column contains exactly $0$1 ones. Together these imply that any matrix returned by the algorithm is a valid BMS (Riou, 1 Nov 2025).

The non-square variant keeps the same structure but replaces the square target $0$2 by row target $0$3 and column target $0$4. The row invariant becomes

$0$5

and the selection rule becomes

$0$6

The proof of column correctness mirrors the square argument but additionally uses the global identity $0$7. The result is a slight variant of the original algorithm that provably generates all feasible rectangular constant-margin instances (Riou, 1 Nov 2025).

4. Complexity, optimality, and implementations

The asymptotic work of the square generator is $0$8. There are $0$9 columns, and at each column the algorithm scans the M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}0 rows to determine the sets M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}1, then writes at most M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}2 ones and updates the corresponding row sums. This gives

M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}3

The paper calls this optimal theoretical complexity because explicitly writing or storing all entries of an M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}4 matrix already requires M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}5 operations or memory accesses (Riou, 1 Nov 2025).

A parallel model is also discussed. If M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}6 rows can be processed simultaneously, then one column costs M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}7 parallel time, so the total becomes

M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}8

This yields wall-clock behavior of order M=(mij)i,j{0,1}n×nM=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}9, up to overheads (Riou, 1 Nov 2025).

Two Python implementations are publicly released as pip-installable packages hosted on GitHub: a standard CPU version and a PyTorch version that can use GPU acceleration. The implementation strategy is a direct translation of the algorithmic specification: represent kk00 as a NumPy or PyTorch tensor, maintain a vector of partial row sums, compute masks for kk01, apply random_subset, and update the selected entries (Riou, 1 Nov 2025).

The GPU-oriented implementation exploits parallelism in two ways. First, operations within a single BMS are vectorized as elementwise or boolean-tensor operations. Second, several BMS can be generated in parallel by batching tensors of shape kk02 together with partial-sum arrays of shape kk03. The paper does not include detailed timing tables or graphs in the LaTeX text, but it notes that many BMS can be generated in parallel and that this enables significant speedups compared to a naive CPU implementation (Riou, 1 Nov 2025).

5. Alternative meanings of “binary magic square” and symmetry-based encodings

A separate literature uses “binary magic square” in a different sense: not as a matrix with equal binary margins, but as the parity pattern of an ordinary magic square. In that definition, if kk04 is a classical magic square, then the associated binary matrix kk05 is given by

kk06

The analysis is then performed up to the eight symmetries of the square, namely the dihedral group kk07 of rotations and reflections (Fahimi, 2023).

Using that parity-based meaning, exhaustive datasets exhibit strong compression under symmetry. Among all 880 magic squares of order kk08, only kk09 unique binary patterns are needed; among all kk10 associative magic squares of order kk11, only kk12 unique patterns occur; and among all kk13 Franklin magic squares of order kk14, only kk15 unique patterns occur when patterns are identified up to rotation and reflection (Fahimi, 2023). The corresponding work also studies the same binary encodings geometrically by flattening them into vectors in kk16, then applying PCA and LDA.

The PCA analysis identifies kk17 distinct patterns for the kk18 family, kk19 for the kk20 associative family, and kk21 for the Franklin kk22 family; the two-dimensional PCA plots show kk23, kk24, and kk25 regions, respectively (Fahimi, 2023). LDA, using the dihedral-equivalence classes as labels, yields “7 pencil-like fingerprints” for the kk26 case, and for the kk27 associative case the histograms of kk28 and kk29 are reported to exhibit an approximate normal distribution (Fahimi, 2023).

This terminology is materially different from the equal-margin definition. In the parity-coded usage, the binary matrix is a shadow of an ordinary magic square and need not have constant row and column sums. In the equal-margin usage, the binary matrix itself is the primary combinatorial object. The coexistence of these meanings is a source of terminological ambiguity in the literature.

6. Constrained-digit relatives, limitations, and open directions

A broader adjacent literature studies magic squares built from restricted digit alphabets rather than from binary matrices with fixed margins. One example constructs upside-down, bimagic, and palindromic squares using only the digits kk30, kk31, and kk32 in digital form, motivated by the palindromic date kk33 (Taneja, 2011). In that setting, a magic square retains the classical requirement that all row sums, column sums, and the two main diagonals have the same value, and additional properties such as bimagicity, pan-diagonality, or kk34 rotational consistency may also be imposed.

The paper gives kk35, kk36, and kk37 examples with magic sums

kk38

satisfying

kk39

and also describes kk40, kk41, and kk42 bimagic constructions with block structure and restricted-digit palindromic entries (Taneja, 2011). This is not strict BMS theory in the equal-margin sense, but it is relevant as constrained-digit magic-square research. The same source explicitly notes that the digit set kk43 is a superset of the binary set kk44, and that many ideas—rotational symmetry, palindromic concatenation, and block structures—are combinatorial patterns that can be adapted by restricting the alphabet (Taneja, 2011).

Within the equal-margin BMS line, several limitations are explicit. The generation algorithm proves correctness and efficiency for producing some BMS with given parameters, but it does not claim uniform sampling over all BMS for fixed kk45 or kk46, and it does not address enumeration or counting (Riou, 1 Nov 2025). Potential future directions mentioned implicitly or naturally suggested include studying the distribution induced by random_subset, integrating the generator into MCMC schemes, and exploring additional structural constraints beyond constant margins (Riou, 1 Nov 2025).

In the parity-based line of work, the open problems are different. The unresolved questions include the general enumeration of binary parity patterns for higher orders, characterization of which binary patterns are realizable for specific structured families such as Franklin squares, analytic explanations of the observed PCA and LDA geometry, and the effect of binary encodings other than parity (Fahimi, 2023). Taken together, these directions show that “Binary Magic Squares” is now best understood as a bifurcated topic: one branch studies constant-margin binary matrices as regular bipartite graph realizations, and another studies binary encodings of classical magic-square families under symmetry.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Binary Magic Squares (BMS).