Table of Contents
Fetching ...

Efficient Generation of Binary Magic Squares

Alain Riou

TL;DR

This work introduces an efficient, provably correct method to generate Binary Magic Squares, i.e. $n\times n$ binary matrices with uniform row and column sums $k$. It develops a column-by-column randomized construction that maintains a per-row invariant and uses a partition-based selection to guarantee exactly $k$ ones per column and per-row sums of $k$, achieving $\mathcal{O}(n^2)$ time. The method extends to non-square $m\times n$ matrices under an am=bn feasibility condition, with a constructive replication approach guided by gcd decompositions. The authors provide open-source Python and PyTorch (GPU) implementations, enabling fast, parallel generation and broad applicability to problems in combinatorial optimization and graph constructions.

Abstract

We propose a simple algorithm for generating Binary Magic Squares (BMS), i.e., square binary matrices where the sum of all rows and all columns are equal. We show by induction that our algorithm always returns valid BMS with optimal theoretical complexity. We then extend our study to non-square Binary Magic Squares, formalize conditions on the sum of rows and columns for these BMS to exist, and show that a slight variant of our first algorithm can generate provably generate them. Finally, we publicly release two implementations of our algorithm as Python packages, including one that can generate several BMS in parallel using GPU acceleration.

Efficient Generation of Binary Magic Squares

TL;DR

This work introduces an efficient, provably correct method to generate Binary Magic Squares, i.e. binary matrices with uniform row and column sums . It develops a column-by-column randomized construction that maintains a per-row invariant and uses a partition-based selection to guarantee exactly ones per column and per-row sums of , achieving time. The method extends to non-square matrices under an am=bn feasibility condition, with a constructive replication approach guided by gcd decompositions. The authors provide open-source Python and PyTorch (GPU) implementations, enabling fast, parallel generation and broad applicability to problems in combinatorial optimization and graph constructions.

Abstract

We propose a simple algorithm for generating Binary Magic Squares (BMS), i.e., square binary matrices where the sum of all rows and all columns are equal. We show by induction that our algorithm always returns valid BMS with optimal theoretical complexity. We then extend our study to non-square Binary Magic Squares, formalize conditions on the sum of rows and columns for these BMS to exist, and show that a slight variant of our first algorithm can generate provably generate them. Finally, we publicly release two implementations of our algorithm as Python packages, including one that can generate several BMS in parallel using GPU acceleration.

Paper Structure

This paper contains 12 sections, 8 theorems, 16 equations, 2 figures, 2 algorithms.

Key Result

Theorem 1

For all $n \in \mathbb{N}$, for all $k \in \{0, \dots, n\}$, there exists $M \in \{0, 1\}^{n \times n}$ such that $M$ is a valid BMS whose sum of rows and columns equals $k$.

Figures (2)

  • Figure 1: A Binary Magic Square with $n = 5$ and $k = 3$.
  • Figure 2: Illustration of our algorithm for a BMS of size $6 \times 6$ with $k = 4$. At the end of step $t$, we compute the current sum of each row. Then, indices are partitioned based on their value. At step $t+1$, indices in $A_1$ must be selected to create a valid BMS, while indices from $A_3$ must not. Finally, some indices in $A_2$ are randomly selected (only one in this example) to ensure that the sum of each column equals $k$.

Theorems & Definitions (17)

  • Theorem 1
  • proof
  • Lemma 4.1
  • proof
  • Lemma 4.2
  • proof
  • Theorem 2
  • proof
  • Theorem 3
  • proof
  • ...and 7 more