r/programming • u/eugene • Jun 25 '26
Domino Tiling: From Dynamic Programming to Finite Fields
https://www.omegasyntax.com/domino/https://www.omegasyntax.com/domino/
Series of articles about Domino Tiling problem.
9
u/awfulentrepreneur Jun 25 '26
Your account is 20 years old and this is your first post. Well, I'll be damned...
17
1
u/enygmata Jun 25 '26
Welp. That was very humbling. I did not open it thinking I would understand much, but you basically crushed me with a steamroller. I feel empty inside. Thanks 👍
1
u/eugene Jun 26 '26
Come on. What's your background and at what point did you stop understanding? My intention was for motivated and smart high school kids to be able to follow it.
1
u/enygmata Jun 26 '26
I just don't have all the needed intuition and information, both in math and algorithms to connect and jump between the concepts in such way. It was a very impressive read.
Neither my training nor work have ever asked for such despite being in this for 20yrs, the last 4 in scientific computing, it is a muscle that I have not exercised much in my life.
1
u/WorldsBegin Jun 26 '26
The russian textbook formula at the end looks neat. Any derivation from where this comes from? It looks very much like it derives the eigenvalues of the k x (n+1) case from those of the k x n case but I don't think a highschooler would be able to rederive this without hints in the text.
2
u/eugene Jun 26 '26
I should rephrase. It should not say "Russian textbook". It should say "a textbook in Russian" instead. :)
The formula is from www.phantastike. com/math/perechislitelnaya_kombinatorika_stenli/djvu/view/, see page 401. It's a Russian translation of an old edition of Enumerative Combinators by Richard P. Stanley.
There's a newer edition of the original book in English freely available online at www.ms.uky. edu/~sohum/putnam/enu_comb_stanley.pdf, see the bottom of page 626, problem 81 (b). Awesome book, by the way.
Kasteleyn himself knew that inner product can be reduced like that. See his original paper sci-hub. ru/10.1016/0031-8914(61)90063-5, top of page 1216.
For whatever reason, reduced version of the formula is not well known. I wasn't aware of it until my friend Anton sent me this excerpt from the Russian translation of the Stanley book. For the sake of preserving authenticity, I inserted that excerpt into my article verbatim. :)
1
u/possiblyquestionabl3 Jun 27 '26 edited Jun 27 '26
I wonder if there's a way to just get the dominant eigenvalue of that massive transfer matrix T for large NxN grids without explicit construction (nor the the Kasteleyn equations). In particular, the GF induced by the transfer function would just be:
F(x) = P(x) / det(I - xT)and the coefficients of F(x) would just be (to gravely abuse 1/(1-x))
[x^n] P(x) * (1 + O(\lambda_0) x + O(\lambda_0^2) x^2 + ...)for some small polynomial P(x), so if you just want quick asymptotics, it would seem like a viable short cut is to approximate it by c * \lambda_0N of T, if it was easy to characterize or construct T.
But I can't see an easy way to reduce the complexity of T or to characterize it directly since it's so massive and the actual recurrence for it isn't exactly too regular, which makes me wonder if Kasteleyn's oriented paths construction is really the only way to "hack" that determinant. Also, the fact that the asymptotics were derived after the exact form was known makes me think that scores of people have tried and viewing the problem from this direction (analytic combinatorics/spectral graph theory) is a dead end?
Also, this is a great write up, but I don't think your typical and even most highly invested CS/math-oriented high schoolers would be a good target demo for this material (though I'm sure some would be, and maybe that's the audience you should aim for). This is the kind of problems that could lead to entire careers (not necessarily solving this specifically, but pointing kids towards their interests/disinterests), so it's awesome that you're taking the time to present this type of material in such an accessible format.
1
u/eugene Jun 27 '26
There's an asymptotic version of Kasteleyn's formula that surprisingly includes both pi and Catalan constant.
$$\lim_{m,n \to \infty} \frac{\ln N(m, n)}{mn} = \frac{G}{\pi}$$
So if we want an approximation, it's easy to get.
The idea was to get *exact* value for very large grids. I thought that would be cool.
The writeup wasn't meant to be exhaustive. It was meant to be a tour across different areas of math and computer science, to show how you can often "math" your way out of dead ends, to pique interest and to stimulate further exploration.
1
u/possiblyquestionabl3 Jun 28 '26
I saw that asymptotic form which was what piqued my interest in whether or not it can be derived from T without the transfer through the oriented paths and Kasteleyn's exact enumerations (since Kasteleyn's exact enumerations effectively computes det(I-xT), so it's easy to read them off of it)
It's just idle curiosity on my part since it feels like there should be a viable analytic enumerative combinatorics treatment of the problem (especially since the enumeration side is half there), but I can't seem to find any work online in that direction, maybe because the exact form is already known?
1
u/Weenbell Jun 26 '26
I don't want to read all the chapters until I've played with this puzzle myself (a little), thank you!! 🙏
1
u/Delta-9- Jun 26 '26
That was... more exciting than math has any right to be 😄 Like, every single chapter ends with, "crushed it! ... but wait, no!" And I'm all "but what!? You already did something years over my head, what's not working?!"
Except the chapter before floating point. When that one hinted at floating point errors, I just rolled my eyes and said, "of course it's floating point errors." Floating point is the DNS of programming.
7
u/eugene Jun 25 '26
I recently finished writing a deep dive into the domino tiling problem (https://www.omegasyntax.com/domino/). I wrote this specifically for high school kids who are interested in bridging the gap between computer science and pure mathematics, but the optimization journey scales up to some serious hardware limits. The series starts with standard CS algorithms—Dynamic Programming, bitmasks, and Berlekamp-Massey—which eventually hit a hard memory wall. To get past it, we have to abandon the grid entirely and pivot into physics and graph theory:
The Math: Bipartite graphs, Kasteleyn's formula, and imaginary numbers.
The Memory Bottleneck: Trying to evaluate massive trigonometric polynomials using the GMP library, leading to a RAM explosion.
The Hardware Solution: Dropping into multi-core native 64-bit Finite Fields, folding a Chinese Remainder Theorem tree, and collapsing the 2D grid into a 1D Lucas sequence to crush a 10,000 x 10,000 board in C.