Number of closed loops in a square grid












6












$begingroup$


QUESTION: Given an $mtimes n$ grid of squares, is a formula known for the number of closed loops that can be drawn along the perimeters of these squares? For a better description of what I mean, see the link below.



MOTIVATION: I have been considering the puzzle game "Slitherlink" in which the solution to the puzzle consists of a closed loop drawn in a square lattice grid satisfying certain constraints. So, given an $mtimes n$ grid of squares, I would like to calculate the number of closed loops that one can draw in that grid.



Does anyone know of a formula that computes this, or a quick algorithm that one can use to determine this number in terms of $m,n$? Are there any special cases that are easier to determine than others? Where can I read more about this problem?



An equivalent problem: given a graph $G$, how can one calculate the number of cyclic subgraphs it contains?










share|cite|improve this question









$endgroup$












  • $begingroup$
    What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    $endgroup$
    – SmileyCraft
    Jan 3 at 22:29












  • $begingroup$
    @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    $endgroup$
    – Frpzzd
    Jan 3 at 22:30










  • $begingroup$
    An interesting question. Have you attempted to count the possible loops in small grids?
    $endgroup$
    – Daniel Mathias
    Jan 3 at 23:01






  • 2




    $begingroup$
    How about this?
    $endgroup$
    – Jens
    Jan 3 at 23:08


















6












$begingroup$


QUESTION: Given an $mtimes n$ grid of squares, is a formula known for the number of closed loops that can be drawn along the perimeters of these squares? For a better description of what I mean, see the link below.



MOTIVATION: I have been considering the puzzle game "Slitherlink" in which the solution to the puzzle consists of a closed loop drawn in a square lattice grid satisfying certain constraints. So, given an $mtimes n$ grid of squares, I would like to calculate the number of closed loops that one can draw in that grid.



Does anyone know of a formula that computes this, or a quick algorithm that one can use to determine this number in terms of $m,n$? Are there any special cases that are easier to determine than others? Where can I read more about this problem?



An equivalent problem: given a graph $G$, how can one calculate the number of cyclic subgraphs it contains?










share|cite|improve this question









$endgroup$












  • $begingroup$
    What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    $endgroup$
    – SmileyCraft
    Jan 3 at 22:29












  • $begingroup$
    @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    $endgroup$
    – Frpzzd
    Jan 3 at 22:30










  • $begingroup$
    An interesting question. Have you attempted to count the possible loops in small grids?
    $endgroup$
    – Daniel Mathias
    Jan 3 at 23:01






  • 2




    $begingroup$
    How about this?
    $endgroup$
    – Jens
    Jan 3 at 23:08
















6












6








6





$begingroup$


QUESTION: Given an $mtimes n$ grid of squares, is a formula known for the number of closed loops that can be drawn along the perimeters of these squares? For a better description of what I mean, see the link below.



MOTIVATION: I have been considering the puzzle game "Slitherlink" in which the solution to the puzzle consists of a closed loop drawn in a square lattice grid satisfying certain constraints. So, given an $mtimes n$ grid of squares, I would like to calculate the number of closed loops that one can draw in that grid.



Does anyone know of a formula that computes this, or a quick algorithm that one can use to determine this number in terms of $m,n$? Are there any special cases that are easier to determine than others? Where can I read more about this problem?



An equivalent problem: given a graph $G$, how can one calculate the number of cyclic subgraphs it contains?










share|cite|improve this question









$endgroup$




QUESTION: Given an $mtimes n$ grid of squares, is a formula known for the number of closed loops that can be drawn along the perimeters of these squares? For a better description of what I mean, see the link below.



MOTIVATION: I have been considering the puzzle game "Slitherlink" in which the solution to the puzzle consists of a closed loop drawn in a square lattice grid satisfying certain constraints. So, given an $mtimes n$ grid of squares, I would like to calculate the number of closed loops that one can draw in that grid.



Does anyone know of a formula that computes this, or a quick algorithm that one can use to determine this number in terms of $m,n$? Are there any special cases that are easier to determine than others? Where can I read more about this problem?



An equivalent problem: given a graph $G$, how can one calculate the number of cyclic subgraphs it contains?







combinatorics graph-theory puzzle






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Jan 3 at 22:19









FrpzzdFrpzzd

23k841111




23k841111












  • $begingroup$
    What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    $endgroup$
    – SmileyCraft
    Jan 3 at 22:29












  • $begingroup$
    @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    $endgroup$
    – Frpzzd
    Jan 3 at 22:30










  • $begingroup$
    An interesting question. Have you attempted to count the possible loops in small grids?
    $endgroup$
    – Daniel Mathias
    Jan 3 at 23:01






  • 2




    $begingroup$
    How about this?
    $endgroup$
    – Jens
    Jan 3 at 23:08




















  • $begingroup$
    What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    $endgroup$
    – SmileyCraft
    Jan 3 at 22:29












  • $begingroup$
    @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    $endgroup$
    – Frpzzd
    Jan 3 at 22:30










  • $begingroup$
    An interesting question. Have you attempted to count the possible loops in small grids?
    $endgroup$
    – Daniel Mathias
    Jan 3 at 23:01






  • 2




    $begingroup$
    How about this?
    $endgroup$
    – Jens
    Jan 3 at 23:08


















$begingroup$
What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
$endgroup$
– SmileyCraft
Jan 3 at 22:29






$begingroup$
What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
$endgroup$
– SmileyCraft
Jan 3 at 22:29














$begingroup$
@SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
$endgroup$
– Frpzzd
Jan 3 at 22:30




$begingroup$
@SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
$endgroup$
– Frpzzd
Jan 3 at 22:30












$begingroup$
An interesting question. Have you attempted to count the possible loops in small grids?
$endgroup$
– Daniel Mathias
Jan 3 at 23:01




$begingroup$
An interesting question. Have you attempted to count the possible loops in small grids?
$endgroup$
– Daniel Mathias
Jan 3 at 23:01




2




2




$begingroup$
How about this?
$endgroup$
– Jens
Jan 3 at 23:08






$begingroup$
How about this?
$endgroup$
– Jens
Jan 3 at 23:08












2 Answers
2






active

oldest

votes


















4












$begingroup$

EDIT: I would like to note that OEIS mentions all largest known values are found by means of a transfer matrix method. Hence, I like to believe the algorithm I present here is asymptotically very close to the best known algorithm.



Here is an $mathcal{O}(f(n)^3log m)$ time algorithm where $f(n)=mathcal{O}(3^n/n)$. The idea is to define an $f(n)times f(n)$ transition matrix $M$ and calculate $M^m$. This takes $mathcal{O}(f(n)^3log m)$ time using the standard matrix multiplication algorithm, but can be sped up to $mathcal{O}(f(n)^{2.807}log m)$ using Strassen's algorithm. There are asymptotically faster matrix multiplication algorithms to speed this up to $mathcal{O}(f(n)^{2.373}log m)$, but don't bother with that.



Note that this means we can calculate the answer for a $5times 10^{18}$ grid in mere seconds, while the answer for a $10times10$ grid would take forever. Pretty funny, if you ask me :) This does assume that multiplication is a constant time operation, which is far from true with such big numbers, unless you only want to know the last few digits of the answer.



So the idea is to use a transition matrix. The state we will consider is the set of horizontal segments that the closed path uses in a certain strip. Consider the following image.



state image



The state of this closed path is $110110$ in the second column and $111010$ in the fourth. What you need to do is define a matrix $M$ where every row and every column represents a state. The entry at row $x$ and column $y$ should be $1$ if you can go from state $y$ to state $x$ and $0$ otherwise.



The point of this technique is described by the following fact. The entry at row $x$ and column $y$ of $M^i$ is the amount of ways to go from state $y$ to state $x$ in $i$ columns. This concludes the theory. The rest of the work is in the implementation. I will note some very important pitfalls for this technique.



Pitfall 1: You might have notices the purple stars in the image. The point is that the set of horizontal segments that the closed path uses in a certain strip is not enough information. Some horizontal segments are allowed to be combined, while others are not. This is to prevent to get two disjoint closed paths count as one. In the image I drew stars between segments that are not allowed to be combined. You need to come up with a way to encode this in your state.



Pitfall 2: Actually calculating $M$ is not easy. For example, note that you can not go from state $1100$ to state $0011$ because vertical line segments will need to cross.



Pitfall 3: To prevent getting two disjoint closed paths, you will also need two extra states. A start state and an end state. They both have no horizontal line segments, but they need to be distinguished between to prevent getting two disjoint closed paths. To read the final answer, just look at the entry of $M^m$ at the start state row and end state column.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
    $endgroup$
    – Milo Brandt
    Jan 4 at 0:40








  • 1




    $begingroup$
    Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
    $endgroup$
    – Milo Brandt
    Jan 4 at 1:16






  • 1




    $begingroup$
    @MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
    $endgroup$
    – SmileyCraft
    Jan 4 at 1:57








  • 1




    $begingroup$
    I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:18








  • 1




    $begingroup$
    For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:19





















2












$begingroup$

The number of cycles with an exact length of $n$, meaning and max height of 2 is this:



$$ S(n,2) = 1 + sum_{m=1}^n sum_{k=1}^m binom{n-m-k+1}{k}binom{m}{k} cdot 2^k $$



Here, $m$ represents the number of columns with 1 square in them, and $k$ represents the number of groups of columns which have 1 square in them. It's very reminiscent of multichoose, except there's two options for each group of columns (either the squares are on the bottom, or top).



Thus, for the total number of cycles in a $n times 2$ board is:



$$ f(n,2) =sum_{i=1}^n (n-i+1) cdot S(i,2) $$



$S(n,3)$ is where things get ugly. Basically, you consider every possible configuration of columns with three squares in them, and then calculate the number of possible ways to connect these columns without creating another in between. I'd suggest a recursive formula dependent upon the squares visible at one end. Let $g(n,3)(b,b,b)$ be the number of cycles with exact width $n$, and the second part is binary conditions on where or not the $j$-th highest square in the rightmost column is in the cycle. The recursion would look something like this:



$$ g(n,3)(1,0,0) = g(n-1,3)(1,1,1)+g(n-1,3)(1,1,0) $$



We'd also want to include a clause about $g(n,3)(1,0,1)$, which sorta of depends on what cycles are allowed.






share|cite|improve this answer









$endgroup$









  • 1




    $begingroup$
    It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 4:50












  • $begingroup$
    the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
    $endgroup$
    – Zachary Hunter
    Jan 4 at 4:54






  • 1




    $begingroup$
    It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:01














Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3061074%2fnumber-of-closed-loops-in-a-square-grid%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

EDIT: I would like to note that OEIS mentions all largest known values are found by means of a transfer matrix method. Hence, I like to believe the algorithm I present here is asymptotically very close to the best known algorithm.



Here is an $mathcal{O}(f(n)^3log m)$ time algorithm where $f(n)=mathcal{O}(3^n/n)$. The idea is to define an $f(n)times f(n)$ transition matrix $M$ and calculate $M^m$. This takes $mathcal{O}(f(n)^3log m)$ time using the standard matrix multiplication algorithm, but can be sped up to $mathcal{O}(f(n)^{2.807}log m)$ using Strassen's algorithm. There are asymptotically faster matrix multiplication algorithms to speed this up to $mathcal{O}(f(n)^{2.373}log m)$, but don't bother with that.



Note that this means we can calculate the answer for a $5times 10^{18}$ grid in mere seconds, while the answer for a $10times10$ grid would take forever. Pretty funny, if you ask me :) This does assume that multiplication is a constant time operation, which is far from true with such big numbers, unless you only want to know the last few digits of the answer.



So the idea is to use a transition matrix. The state we will consider is the set of horizontal segments that the closed path uses in a certain strip. Consider the following image.



state image



The state of this closed path is $110110$ in the second column and $111010$ in the fourth. What you need to do is define a matrix $M$ where every row and every column represents a state. The entry at row $x$ and column $y$ should be $1$ if you can go from state $y$ to state $x$ and $0$ otherwise.



The point of this technique is described by the following fact. The entry at row $x$ and column $y$ of $M^i$ is the amount of ways to go from state $y$ to state $x$ in $i$ columns. This concludes the theory. The rest of the work is in the implementation. I will note some very important pitfalls for this technique.



Pitfall 1: You might have notices the purple stars in the image. The point is that the set of horizontal segments that the closed path uses in a certain strip is not enough information. Some horizontal segments are allowed to be combined, while others are not. This is to prevent to get two disjoint closed paths count as one. In the image I drew stars between segments that are not allowed to be combined. You need to come up with a way to encode this in your state.



Pitfall 2: Actually calculating $M$ is not easy. For example, note that you can not go from state $1100$ to state $0011$ because vertical line segments will need to cross.



Pitfall 3: To prevent getting two disjoint closed paths, you will also need two extra states. A start state and an end state. They both have no horizontal line segments, but they need to be distinguished between to prevent getting two disjoint closed paths. To read the final answer, just look at the entry of $M^m$ at the start state row and end state column.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
    $endgroup$
    – Milo Brandt
    Jan 4 at 0:40








  • 1




    $begingroup$
    Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
    $endgroup$
    – Milo Brandt
    Jan 4 at 1:16






  • 1




    $begingroup$
    @MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
    $endgroup$
    – SmileyCraft
    Jan 4 at 1:57








  • 1




    $begingroup$
    I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:18








  • 1




    $begingroup$
    For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:19


















4












$begingroup$

EDIT: I would like to note that OEIS mentions all largest known values are found by means of a transfer matrix method. Hence, I like to believe the algorithm I present here is asymptotically very close to the best known algorithm.



Here is an $mathcal{O}(f(n)^3log m)$ time algorithm where $f(n)=mathcal{O}(3^n/n)$. The idea is to define an $f(n)times f(n)$ transition matrix $M$ and calculate $M^m$. This takes $mathcal{O}(f(n)^3log m)$ time using the standard matrix multiplication algorithm, but can be sped up to $mathcal{O}(f(n)^{2.807}log m)$ using Strassen's algorithm. There are asymptotically faster matrix multiplication algorithms to speed this up to $mathcal{O}(f(n)^{2.373}log m)$, but don't bother with that.



Note that this means we can calculate the answer for a $5times 10^{18}$ grid in mere seconds, while the answer for a $10times10$ grid would take forever. Pretty funny, if you ask me :) This does assume that multiplication is a constant time operation, which is far from true with such big numbers, unless you only want to know the last few digits of the answer.



So the idea is to use a transition matrix. The state we will consider is the set of horizontal segments that the closed path uses in a certain strip. Consider the following image.



state image



The state of this closed path is $110110$ in the second column and $111010$ in the fourth. What you need to do is define a matrix $M$ where every row and every column represents a state. The entry at row $x$ and column $y$ should be $1$ if you can go from state $y$ to state $x$ and $0$ otherwise.



The point of this technique is described by the following fact. The entry at row $x$ and column $y$ of $M^i$ is the amount of ways to go from state $y$ to state $x$ in $i$ columns. This concludes the theory. The rest of the work is in the implementation. I will note some very important pitfalls for this technique.



Pitfall 1: You might have notices the purple stars in the image. The point is that the set of horizontal segments that the closed path uses in a certain strip is not enough information. Some horizontal segments are allowed to be combined, while others are not. This is to prevent to get two disjoint closed paths count as one. In the image I drew stars between segments that are not allowed to be combined. You need to come up with a way to encode this in your state.



Pitfall 2: Actually calculating $M$ is not easy. For example, note that you can not go from state $1100$ to state $0011$ because vertical line segments will need to cross.



Pitfall 3: To prevent getting two disjoint closed paths, you will also need two extra states. A start state and an end state. They both have no horizontal line segments, but they need to be distinguished between to prevent getting two disjoint closed paths. To read the final answer, just look at the entry of $M^m$ at the start state row and end state column.






share|cite|improve this answer











$endgroup$









  • 1




    $begingroup$
    Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
    $endgroup$
    – Milo Brandt
    Jan 4 at 0:40








  • 1




    $begingroup$
    Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
    $endgroup$
    – Milo Brandt
    Jan 4 at 1:16






  • 1




    $begingroup$
    @MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
    $endgroup$
    – SmileyCraft
    Jan 4 at 1:57








  • 1




    $begingroup$
    I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:18








  • 1




    $begingroup$
    For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:19
















4












4








4





$begingroup$

EDIT: I would like to note that OEIS mentions all largest known values are found by means of a transfer matrix method. Hence, I like to believe the algorithm I present here is asymptotically very close to the best known algorithm.



Here is an $mathcal{O}(f(n)^3log m)$ time algorithm where $f(n)=mathcal{O}(3^n/n)$. The idea is to define an $f(n)times f(n)$ transition matrix $M$ and calculate $M^m$. This takes $mathcal{O}(f(n)^3log m)$ time using the standard matrix multiplication algorithm, but can be sped up to $mathcal{O}(f(n)^{2.807}log m)$ using Strassen's algorithm. There are asymptotically faster matrix multiplication algorithms to speed this up to $mathcal{O}(f(n)^{2.373}log m)$, but don't bother with that.



Note that this means we can calculate the answer for a $5times 10^{18}$ grid in mere seconds, while the answer for a $10times10$ grid would take forever. Pretty funny, if you ask me :) This does assume that multiplication is a constant time operation, which is far from true with such big numbers, unless you only want to know the last few digits of the answer.



So the idea is to use a transition matrix. The state we will consider is the set of horizontal segments that the closed path uses in a certain strip. Consider the following image.



state image



The state of this closed path is $110110$ in the second column and $111010$ in the fourth. What you need to do is define a matrix $M$ where every row and every column represents a state. The entry at row $x$ and column $y$ should be $1$ if you can go from state $y$ to state $x$ and $0$ otherwise.



The point of this technique is described by the following fact. The entry at row $x$ and column $y$ of $M^i$ is the amount of ways to go from state $y$ to state $x$ in $i$ columns. This concludes the theory. The rest of the work is in the implementation. I will note some very important pitfalls for this technique.



Pitfall 1: You might have notices the purple stars in the image. The point is that the set of horizontal segments that the closed path uses in a certain strip is not enough information. Some horizontal segments are allowed to be combined, while others are not. This is to prevent to get two disjoint closed paths count as one. In the image I drew stars between segments that are not allowed to be combined. You need to come up with a way to encode this in your state.



Pitfall 2: Actually calculating $M$ is not easy. For example, note that you can not go from state $1100$ to state $0011$ because vertical line segments will need to cross.



Pitfall 3: To prevent getting two disjoint closed paths, you will also need two extra states. A start state and an end state. They both have no horizontal line segments, but they need to be distinguished between to prevent getting two disjoint closed paths. To read the final answer, just look at the entry of $M^m$ at the start state row and end state column.






share|cite|improve this answer











$endgroup$



EDIT: I would like to note that OEIS mentions all largest known values are found by means of a transfer matrix method. Hence, I like to believe the algorithm I present here is asymptotically very close to the best known algorithm.



Here is an $mathcal{O}(f(n)^3log m)$ time algorithm where $f(n)=mathcal{O}(3^n/n)$. The idea is to define an $f(n)times f(n)$ transition matrix $M$ and calculate $M^m$. This takes $mathcal{O}(f(n)^3log m)$ time using the standard matrix multiplication algorithm, but can be sped up to $mathcal{O}(f(n)^{2.807}log m)$ using Strassen's algorithm. There are asymptotically faster matrix multiplication algorithms to speed this up to $mathcal{O}(f(n)^{2.373}log m)$, but don't bother with that.



Note that this means we can calculate the answer for a $5times 10^{18}$ grid in mere seconds, while the answer for a $10times10$ grid would take forever. Pretty funny, if you ask me :) This does assume that multiplication is a constant time operation, which is far from true with such big numbers, unless you only want to know the last few digits of the answer.



So the idea is to use a transition matrix. The state we will consider is the set of horizontal segments that the closed path uses in a certain strip. Consider the following image.



state image



The state of this closed path is $110110$ in the second column and $111010$ in the fourth. What you need to do is define a matrix $M$ where every row and every column represents a state. The entry at row $x$ and column $y$ should be $1$ if you can go from state $y$ to state $x$ and $0$ otherwise.



The point of this technique is described by the following fact. The entry at row $x$ and column $y$ of $M^i$ is the amount of ways to go from state $y$ to state $x$ in $i$ columns. This concludes the theory. The rest of the work is in the implementation. I will note some very important pitfalls for this technique.



Pitfall 1: You might have notices the purple stars in the image. The point is that the set of horizontal segments that the closed path uses in a certain strip is not enough information. Some horizontal segments are allowed to be combined, while others are not. This is to prevent to get two disjoint closed paths count as one. In the image I drew stars between segments that are not allowed to be combined. You need to come up with a way to encode this in your state.



Pitfall 2: Actually calculating $M$ is not easy. For example, note that you can not go from state $1100$ to state $0011$ because vertical line segments will need to cross.



Pitfall 3: To prevent getting two disjoint closed paths, you will also need two extra states. A start state and an end state. They both have no horizontal line segments, but they need to be distinguished between to prevent getting two disjoint closed paths. To read the final answer, just look at the entry of $M^m$ at the start state row and end state column.







share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited Jan 4 at 2:36

























answered Jan 3 at 23:31









SmileyCraftSmileyCraft

3,776519




3,776519








  • 1




    $begingroup$
    Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
    $endgroup$
    – Milo Brandt
    Jan 4 at 0:40








  • 1




    $begingroup$
    Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
    $endgroup$
    – Milo Brandt
    Jan 4 at 1:16






  • 1




    $begingroup$
    @MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
    $endgroup$
    – SmileyCraft
    Jan 4 at 1:57








  • 1




    $begingroup$
    I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:18








  • 1




    $begingroup$
    For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:19
















  • 1




    $begingroup$
    Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
    $endgroup$
    – Milo Brandt
    Jan 4 at 0:40








  • 1




    $begingroup$
    Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
    $endgroup$
    – Milo Brandt
    Jan 4 at 1:16






  • 1




    $begingroup$
    @MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
    $endgroup$
    – SmileyCraft
    Jan 4 at 1:57








  • 1




    $begingroup$
    I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:18








  • 1




    $begingroup$
    For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:19










1




1




$begingroup$
Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
$endgroup$
– Milo Brandt
Jan 4 at 0:40






$begingroup$
Aside from start and end states, the states are probably best encoded by a set of edges along with a pairing of edges based on which are already connected in the previously built portion. The number of states corresponding to a vertical slice with $2n$ edges would the the $n^{th}$ Catalan number (noting that these are plane graphs in a natural way).
$endgroup$
– Milo Brandt
Jan 4 at 0:40






1




1




$begingroup$
Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
$endgroup$
– Milo Brandt
Jan 4 at 1:16




$begingroup$
Well - assuming we're moving left-to-right in our transitions - every edge in a vertical slice is connected to exactly one other by a path lying to the left of the slice (since every vertex to the left has degree $2$). These paths do not intersect or even share vertices - so they end up forming a sort of nested structure. You can then represent the connectivity by an expression of matched parenthesis - for instance, with four edges, they could be connected as ()() or (()) - the former being two small arcs, the latter one large one enclosing a small one. These are counted by $C_n$.
$endgroup$
– Milo Brandt
Jan 4 at 1:16




1




1




$begingroup$
@MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
$endgroup$
– SmileyCraft
Jan 4 at 1:57






$begingroup$
@MiloBrandt Awesome! But that only counts the number of states where every horizontal edge is used. With $n$ horizontal edges we would therefore get $$2+sum_{k=1}^{lfloor n/2rfloor}{nchoose 2k}C_k$$ states in total. So $$f(n)=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!(2k)!}{(2k)!(n-2k)!(k+1)(k!)^2}=2+sum_{k=1}^{lfloor n/2rfloor}frac{n!}{k!(n-2k)!(k+1)!}=mathcal{O}left(frac{3^n}{n+1}right)$$ by Stirlin's approximation. Here I bound $$frac{n!}{k!(n-2k)!(k+1)!}leqfrac1{n+1}frac{(n+1)!}{((n+1)/3)!^3}=mathcal{O}(frac1{n+1}frac{3^n}n)$$.
$endgroup$
– SmileyCraft
Jan 4 at 1:57






1




1




$begingroup$
I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
$endgroup$
– Milo Brandt
Jan 4 at 5:18






$begingroup$
I did some work, identifying some states via reflectional symmetry. I made the matrices have the last state be the end state, the second to last state be the start state. For $n=2$, we get the number of cycles in a $2times m$ grid as follows: $$left( begin{array}{cccc} 0 & 0 & 0 & 1 end{array} right)cdot left( begin{array}{cccc} 1 & 2 & 2 & 0 \ 1 & 1 & 1 & 0 \ 0 & 0 & 1 & 0 \ 1 & 1 & 0 & 1 \ end{array} right)^mcdot left( begin{array}{c} 0 \ 0 \ 1 \ 0 end{array} right)$$
$endgroup$
– Milo Brandt
Jan 4 at 5:18






1




1




$begingroup$
For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
$endgroup$
– Milo Brandt
Jan 4 at 5:19






$begingroup$
For $n=3$, the matrix is $$left( begin{array}{cccccccc} 1 & 1 & 2 & 0 & 0 & 2 & 2 & 0 \ 1 & 2 & 2 & 2 & 0 & 0 & 2 & 0 \ 1 & 1 & 1 & 1 & 1 & 0 & 1 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 \ 1 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \ 1 & 1 & 1 & 1 & 0 & 1 & 0 & 1 \ end{array} right).$$ (I checked these against the OEIS table - they give the right results)
$endgroup$
– Milo Brandt
Jan 4 at 5:19













2












$begingroup$

The number of cycles with an exact length of $n$, meaning and max height of 2 is this:



$$ S(n,2) = 1 + sum_{m=1}^n sum_{k=1}^m binom{n-m-k+1}{k}binom{m}{k} cdot 2^k $$



Here, $m$ represents the number of columns with 1 square in them, and $k$ represents the number of groups of columns which have 1 square in them. It's very reminiscent of multichoose, except there's two options for each group of columns (either the squares are on the bottom, or top).



Thus, for the total number of cycles in a $n times 2$ board is:



$$ f(n,2) =sum_{i=1}^n (n-i+1) cdot S(i,2) $$



$S(n,3)$ is where things get ugly. Basically, you consider every possible configuration of columns with three squares in them, and then calculate the number of possible ways to connect these columns without creating another in between. I'd suggest a recursive formula dependent upon the squares visible at one end. Let $g(n,3)(b,b,b)$ be the number of cycles with exact width $n$, and the second part is binary conditions on where or not the $j$-th highest square in the rightmost column is in the cycle. The recursion would look something like this:



$$ g(n,3)(1,0,0) = g(n-1,3)(1,1,1)+g(n-1,3)(1,1,0) $$



We'd also want to include a clause about $g(n,3)(1,0,1)$, which sorta of depends on what cycles are allowed.






share|cite|improve this answer









$endgroup$









  • 1




    $begingroup$
    It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 4:50












  • $begingroup$
    the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
    $endgroup$
    – Zachary Hunter
    Jan 4 at 4:54






  • 1




    $begingroup$
    It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:01


















2












$begingroup$

The number of cycles with an exact length of $n$, meaning and max height of 2 is this:



$$ S(n,2) = 1 + sum_{m=1}^n sum_{k=1}^m binom{n-m-k+1}{k}binom{m}{k} cdot 2^k $$



Here, $m$ represents the number of columns with 1 square in them, and $k$ represents the number of groups of columns which have 1 square in them. It's very reminiscent of multichoose, except there's two options for each group of columns (either the squares are on the bottom, or top).



Thus, for the total number of cycles in a $n times 2$ board is:



$$ f(n,2) =sum_{i=1}^n (n-i+1) cdot S(i,2) $$



$S(n,3)$ is where things get ugly. Basically, you consider every possible configuration of columns with three squares in them, and then calculate the number of possible ways to connect these columns without creating another in between. I'd suggest a recursive formula dependent upon the squares visible at one end. Let $g(n,3)(b,b,b)$ be the number of cycles with exact width $n$, and the second part is binary conditions on where or not the $j$-th highest square in the rightmost column is in the cycle. The recursion would look something like this:



$$ g(n,3)(1,0,0) = g(n-1,3)(1,1,1)+g(n-1,3)(1,1,0) $$



We'd also want to include a clause about $g(n,3)(1,0,1)$, which sorta of depends on what cycles are allowed.






share|cite|improve this answer









$endgroup$









  • 1




    $begingroup$
    It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 4:50












  • $begingroup$
    the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
    $endgroup$
    – Zachary Hunter
    Jan 4 at 4:54






  • 1




    $begingroup$
    It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:01
















2












2








2





$begingroup$

The number of cycles with an exact length of $n$, meaning and max height of 2 is this:



$$ S(n,2) = 1 + sum_{m=1}^n sum_{k=1}^m binom{n-m-k+1}{k}binom{m}{k} cdot 2^k $$



Here, $m$ represents the number of columns with 1 square in them, and $k$ represents the number of groups of columns which have 1 square in them. It's very reminiscent of multichoose, except there's two options for each group of columns (either the squares are on the bottom, or top).



Thus, for the total number of cycles in a $n times 2$ board is:



$$ f(n,2) =sum_{i=1}^n (n-i+1) cdot S(i,2) $$



$S(n,3)$ is where things get ugly. Basically, you consider every possible configuration of columns with three squares in them, and then calculate the number of possible ways to connect these columns without creating another in between. I'd suggest a recursive formula dependent upon the squares visible at one end. Let $g(n,3)(b,b,b)$ be the number of cycles with exact width $n$, and the second part is binary conditions on where or not the $j$-th highest square in the rightmost column is in the cycle. The recursion would look something like this:



$$ g(n,3)(1,0,0) = g(n-1,3)(1,1,1)+g(n-1,3)(1,1,0) $$



We'd also want to include a clause about $g(n,3)(1,0,1)$, which sorta of depends on what cycles are allowed.






share|cite|improve this answer









$endgroup$



The number of cycles with an exact length of $n$, meaning and max height of 2 is this:



$$ S(n,2) = 1 + sum_{m=1}^n sum_{k=1}^m binom{n-m-k+1}{k}binom{m}{k} cdot 2^k $$



Here, $m$ represents the number of columns with 1 square in them, and $k$ represents the number of groups of columns which have 1 square in them. It's very reminiscent of multichoose, except there's two options for each group of columns (either the squares are on the bottom, or top).



Thus, for the total number of cycles in a $n times 2$ board is:



$$ f(n,2) =sum_{i=1}^n (n-i+1) cdot S(i,2) $$



$S(n,3)$ is where things get ugly. Basically, you consider every possible configuration of columns with three squares in them, and then calculate the number of possible ways to connect these columns without creating another in between. I'd suggest a recursive formula dependent upon the squares visible at one end. Let $g(n,3)(b,b,b)$ be the number of cycles with exact width $n$, and the second part is binary conditions on where or not the $j$-th highest square in the rightmost column is in the cycle. The recursion would look something like this:



$$ g(n,3)(1,0,0) = g(n-1,3)(1,1,1)+g(n-1,3)(1,1,0) $$



We'd also want to include a clause about $g(n,3)(1,0,1)$, which sorta of depends on what cycles are allowed.







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered Jan 4 at 1:20









Zachary HunterZachary Hunter

1,065314




1,065314








  • 1




    $begingroup$
    It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 4:50












  • $begingroup$
    the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
    $endgroup$
    – Zachary Hunter
    Jan 4 at 4:54






  • 1




    $begingroup$
    It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:01
















  • 1




    $begingroup$
    It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
    $endgroup$
    – Milo Brandt
    Jan 4 at 4:50












  • $begingroup$
    the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
    $endgroup$
    – Zachary Hunter
    Jan 4 at 4:54






  • 1




    $begingroup$
    It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
    $endgroup$
    – Milo Brandt
    Jan 4 at 5:01










1




1




$begingroup$
It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
$endgroup$
– Milo Brandt
Jan 4 at 4:50






$begingroup$
It's worth noting that the recurrence relation suggested in for height $3$ is not exactly correct - it would fail to track connectivity - but it can be applied to the case of $n=2$ for a much cleaner result (analogous to SmileyCraft's answer): the recurrence relation for $S(n,2)$ is degree $2$ and solves as $$S(n,2)=frac{1}2left((1+sqrt{2})^{1+n}+(1-sqrt{2})^{1+n}right).$$ You can use this to find a closed form for $f(n,2)$: $$f(n,2)=frac{1}{4} left(left(7-5 sqrt{2}right) left(1-sqrt{2}right)^n-8 n+left(5 sqrt{2}+7right) left(sqrt{2}+1right)^n-14right).$$
$endgroup$
– Milo Brandt
Jan 4 at 4:50














$begingroup$
the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
$endgroup$
– Zachary Hunter
Jan 4 at 4:54




$begingroup$
the recurrence I believe is right, a (1,0,1)-column cannot happen in between 3-columns, only on the sides.
$endgroup$
– Zachary Hunter
Jan 4 at 4:54




1




1




$begingroup$
It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
$endgroup$
– Milo Brandt
Jan 4 at 5:01






$begingroup$
It's tricky to actually capture that condition separately - really, there are two kinds of (1,0,1) columns - in a sequence of the form (1,1,1), (1,0,1), (1,0,1), ... , (1,0,1), one can't follow by (1,1,1). In a sequence like (1,0,0), (1,0,1), (1,0,1), ... , (1,0,1) one can't follow by (1,0,0), but could be followed by (1,1,1). This distinction really needs to be baked into the recurrence - it's not so easy to add retrospectively as this answer suggests (because the rule is really that any contiguous segment of (1,0,1)'s must be flanked on exactly one side by (1,1,1) - which is tricky)
$endgroup$
– Milo Brandt
Jan 4 at 5:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematics Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3061074%2fnumber-of-closed-loops-in-a-square-grid%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas