TLDR: A new, efficient algorithm is proposed for generating Binary Magic Squares (binary matrices with equal row and column sums), proven to be correct with optimal complexity. The method is extended to non-square matrices, with conditions for their existence formalized. Python implementations, including a GPU-accelerated version, are publicly released.
Binary Magic Squares (BMS) are fascinating mathematical constructs that have practical applications in fields like combinatorial optimization. Imagine a square grid, much like a Sudoku puzzle, but instead of numbers, each cell contains either a 0 or a 1. The unique property of a Binary Magic Square is that the sum of all the numbers in each row and each column is exactly the same constant value. These squares are not just mathematical curiosities; they are closely linked to graph theory, representing the adjacency matrices of regular bipartite graphs.
Historically, researchers have explored the existence and computability of binary matrices with specific row and column sum constraints, with BMS being a special instance. Various methods, including exhaustive generation of k-regular graphs and Markov chain Monte Carlo algorithms, have been proposed for sampling such matrices. However, a recent paper introduces a novel and highly efficient algorithm specifically designed for generating Binary Magic Squares.
Authored by Alain Riou, the research paper titled “Efficient Generation of Binary Magic Squares” presents a straightforward algorithm that consistently produces valid BMS with optimal theoretical complexity. The core idea behind this algorithm is to construct the Binary Magic Square column by column. It starts with an empty matrix (all zeros) and then, for each column, strategically places ‘1’s. The key is to ensure that as ‘1’s are added, certain constraints on the sum of the rows are continuously maintained. By the time the last column is filled, both row and column sums are guaranteed to be equal to the desired constant, ‘k’.
The algorithm cleverly partitions the candidate rows into three groups at each step, based on their current sum. This allows for a systematic selection of where to place the ‘1’s in the current column, ensuring that the final matrix meets the BMS criteria. The paper rigorously proves the correctness of this method, demonstrating that it always yields a valid Binary Magic Square.
Beyond square matrices, the study extends to non-square Binary Magic Squares. The paper formalizes the conditions under which these non-square BMS can exist, specifically stating that the ratio between the sum of rows and the sum of columns must equal the ratio between the number of columns and rows (am=bn). A slight modification of the original algorithm is then shown to be capable of generating these non-square variants, provided these conditions are met.
A significant aspect of this research is the public release of two Python package implementations of the algorithm. One of these implementations is particularly noteworthy as it leverages GPU acceleration, allowing for the parallel generation of multiple Binary Magic Squares, which can drastically improve performance for large-scale applications. This open-source contribution makes the efficient generation of BMS accessible to a wider audience of researchers and developers. For more details, you can refer to the full research paper here.
Also Read:
- Examining LLM Contributions to Bin Packing: Interpretability and Generalization
- Advancing Numeric Planning: A Lifted Approach to Action Generation
In conclusion, this work addresses a gap in the availability of simple yet efficient algorithms for generating Binary Magic Squares. By providing a theoretically validated algorithm and practical, high-performance implementations, it offers a valuable tool for various applications in combinatorial optimization and graph theory.


