TITLE: A sequence with dominoes
QUESTION [18 upvotes]: My friend ran into this problem years ago. He asked for my help, but I couldn't solve it either. The setting is as follows.
Dominoes are placed in a triangular formation. Something like this...
 ㅁ
ㅁㅁ  (n=2)

Rules: 1. If a domino is hit, it may fall or it may not. 2. If a domino is not hit, it does not fall.
Problem: $D_n$ is the number of scenarioes possible for a given $n$. Find the explicit formula of $D_n$.
Example: For n=2, The domino on the top may or may not fall. If it does not fall, the two dominoes under it do not fall. If it does, the two dominoes may or may not fall. Therefore $D_2=5$.
Clarification: The rule is: the top domino can fall; for any non-top domino, it can fall only if at least one domino above it falls.
Please help. Thank you very much.

REPLY [2 votes]: I've computed the first $n=22$ domino coefficients based on ideas from Vepir. The algorithm has time complexity $O(4^n)$ and space complexity $O(2^n)$ (computation for $n=22$ took me around $5$ hours), because we don't store the matrix $M$ and compute its values whenever needed (the check is performed in the if statement, it has a simple form thanks to implementing bitwise operations). The code is in C++ and I am using boost to be able to use bigger data types rather than just $64$ bit long int (in fact, $256$ bits is the smallest size that can be used to compute the first $22$ coefficients and even it will run out soon - $D_{22} \approx 1.04159*10^{53}$ so the sequence indeed grows rapidly).
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <vector>

int main() {
    const int n = 22;
    std::vector<boost::multiprecision::int256_t> v(1 << n), w = v;
    v[0] = 1;
    v[1] = 1;
    std::cout << "0: " << 1 << "\n1: " << 2 << "\n";
    for (int k = 2; k <= n; k++) {
        for (int j = 0; j < 1 << k; j++) {
            w[j] = 0;
            for (int i = 0; i < 1 << (k - 1); i++)
                if (!(j & ~i & ~(i << 1)))
                    w[j] += v[i];
        }
        boost::multiprecision::int256_t s = 0;
        for (int j = 0; j < 1 << k; j++) {
            v[j] = w[j];
            s += v[j];
        }
        std::cout << k << ": " << s << "\n";
    }
    return 0;
}

The resulting coefficients are:  
$D_{0\;} = 1$ 
$D_{1\;} = 2$ 
$D_{2\;} = 5$ 
$D_{3\;} = 18$ 
$D_{4\;} = 97$ 
$D_{5\;} = 802$ 
$D_{6\;} = 10565$ 
$D_{7\;} = 228850$ 
$D_{8\;} = 8289217$ 
$D_{9\;} = 506526530$ 
$D_{10 } = 52501381765$ 
$D_{11 } = 9260170733266$ 
$D_{12 } = 2784551512218145$ 
$D_{13 } = 1429063630276963426$ 
$D_{14 } = 1252517782851235507141$ 
$D_{15 } = 1875484239084442842046130$ 
$D_{16 } = 4798818821638537354534159233$ 
$D_{17 } = 20984654018757393270224583817858$ 
$D_{18 } = 156836820191186149182649796402611461$ 
$D_{19 } = 2003511561206789377122778581243926768018$ 
$D_{20 } = 43746600983206157305472349537031148375171041$ 
$D_{21 } = 1632720578386908900755016228846582339781524154786$ 
$D_{22 } = 104159355027420388820987121345303301272029996166458949$ 
$D_{23 } = 11358102129403917231432165494822142898736362479589146061682$ 
$D_{24 } = 2117071962387596476689143406365411645227548761608194942194112321$ 
$D_{25 } = 674510308152303132671249373016113929650762465949837199637457187249602$ 
$D_{26 } = 367337379241994889203376240956745359336562483947308610993230043730270521221$ 
The logarithm of $D_n$ seems to be growing quadratically (which can even be seen looking at the shape of the above list). We have approximately:
$$D_n \approx 2^{0.386712 n^2 - 0.667456 n + 3.63662} = \exp(0.268048 n^2 - 0.462645 n + 2.52072)$$
But I don't know how accurate this approximation is nor what the real coefficients could be. And it also doesn't tell much about the potential recursive formula - it still seems to be out of reach.  
This problem is a special case of the more general one - given directed graph $G$, how many ways there are to assign numbers $0$ and $1$ to its vertices such that the arrows always point in the nondecreasing direction (i.e. not from $1$ to $0$)? We can without loss of generality assume the graph is connected (otherwise the answer is just a product of the answers for the components) and that it is acyclic (the cycle implies that all its have to have the same number, thus we can collapse them into one vertex). There is an easy algorithm for answering this problem in case of a directed tree (just the recursion from the leaves up) but with vertices interacting in a more complex way, such as in this problem, it seems hard if not impossible, to come up with a fast algorithm, especially taking into account, how fast these coefficients grow (though, for example, the formula for perfect binary trees of depth $n$ grows even faster and has a simple recursion $B_{n+1}=B_n^2+1$ with $B_0=1$). 
EDIT 
The first $5$ vectors $v_i$ generating these $D_i$ are (trailing zeros omitted): 
$v_0=(0,1)$ 
$v_1=(1,1)$ 
$v_2=(2, 1, 1, 1)$ 
$v_3=(5, 2, 3, 2, 2, 1, 2, 1)$ 
$v_4=(18, 6, 11, 6, 11, 4, 9, 4, 6, 2, 4, 2, 6, 2, 4, 2)$ 
$v_5=(97, 28, 56, 28, 65, 20, 48, 20, 56, 16, 33, 16, 48, 14, 31, 14, 28, 8, 16, 8, 20, 6, 14, 6, 28, 8, 16, 8, 20, 6, 14, 6)$ 
We can find interpretation of every coefficient in this vector based on the fractal properties of $M$ - for example the first element of vector $v$ is just the sum of all elements of the previous vector (and thus it's just $D_{n-1}$. The second element is the sum of every element on the odd position (if we count positions from the left starting at $0$) and the third is the sum in every position not divisible by $4$. However, as we go further the patterns get more complex, but by analyzing them one can maybe obtain faster algorithm (for example instead of summing $\frac{3}{4}$ of the elements of the previous vector to get the third element one can just subtract from the previously computed first element the sum of the $\frac{1}{4}$ elements with indices divisible by $4$, though this enhancement can yield at most twice the speed).  
EDIT 2 
I was able to compute more terms of the sequence (up to $26$ instead of $22$) by utilizing the following algorithm:
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <vector>

int main() {
    int k = 0, n = 26;
    std::vector<boost::multiprecision::int256_t> v(1 << n - 2, 1), a(n + 1, 1);
    for (int i = 2; i < a.size(); i++)
        a[i] = 3 * a[i - 1] + 2 * a[i - 2];
    for (int i = 0; i < v.size(); i++)
        for (int j = i | (i << 1); j; j >>= 1) {
            k += j & 1;
            if (!(j & 1 && j - 1)){
                v[i] *= a[k + 1];
                k = 0;
            }
        }
    for (int i = n - 3; i > 0; i--) {
        std::vector<boost::multiprecision::int256_t> w(1 << i, 0);
        for (int j = 0; j < 1 << i; j++)
            for (int k = 0; k < 1 << i + 1; k++)
                if (!(~(j | j << 1) & k))
                    w[j] += v[k];
        v = w;
        std::cout << n - i + 1 << " " << v[1] + 1 << "\n";
    }
}

Which can be easily parallelized speeding up the process even more. The way of computing the numbers is also different. Let $D(s,n)$ denote the valid domino configuration with binary string $s$ at the top and having $n$ layers - the initial problem is to evaluate $D('0',n)+D('1',n)=D('1',n)+1$. To obtain $D(s,n)$ we note that for $n=1$ they all equal one and for bigger $n$ we can consider all strings $s'$ directly below $n$, recursively compute all values $D(s',n-1)$ and sum them. The asymptotic complexity is the same but we can speed up the program by starting from $D(s,2)$ which is equal to $2$ to the power of the number of ones in $s$, or (as in the above algorithm) from $D(s,3)$ by noting that the number $D(s_k,3)$ where $s_k$ is the string consisting of $k$ ones is equal to OEIS A007483 which has a simple recursion and that for all other $s$ it's just the product of these numbers for each consecutive block of ones in $s$. $a_k=D(s_k,4)$, $b_k=D(s_k,5)$ and $c_k=(s_k,6)$ also have their recurrences:
$$a_{k+3}=4a_{k+2}+12a_{k+1}+8a_k$$
$$b_{k+5}=5b_{k+4}+48b_{k+3}+108b_{k+2}+64b_{k+1}-32b_k$$
$$c_{k+8}=6c_{k+7}+164c_{k+6}+896c_{k+5}+1584c_{k+4}-992c_{k+3}-3328c_{k+2}+3584c_{k+1}-1024c_k$$
With $a_0=b_0=c_0=1$, $a_1=18=D_3$, $a_2=96=D_4-1$, $b_1=97=D_4$, $b_2=801=D_5-1$, $b_3=8961$, $b_4=93697$, $c_1=802=D_5$, $c_2=10564=D_6-1$, $c_3=207720$, $c_4=3692192$, $c_5=66924256$, $c_6=1209149120$, $c_7=21854550528$. For $D(s_k,7)$ I haven't found such a series - either it doesn't exist or (more probably) there are not enough terms to detect it. Also I haven't found a way of computing $D(s,n)$ for any $n \ge 4$ for all strings $s$, that's why the algorithm makes use of only $D(s,3)$ - if one is able to compute them directly one can obtain bigger terms of the sequence. I just want to mention that $D_{26} \approx 2^{247.7}$ is the biggest term below $2^{255}$ so one will have to use bigger integer types when attempting to go higher.