Number of closed loops in a square grid












6














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






















  • What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    – SmileyCraft
    6 hours ago












  • @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    – Frpzzd
    6 hours ago










  • An interesting question. Have you attempted to count the possible loops in small grids?
    – Daniel Mathias
    5 hours ago






  • 2




    How about this?
    – Jens
    5 hours ago


















6














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






















  • What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    – SmileyCraft
    6 hours ago












  • @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    – Frpzzd
    6 hours ago










  • An interesting question. Have you attempted to count the possible loops in small grids?
    – Daniel Mathias
    5 hours ago






  • 2




    How about this?
    – Jens
    5 hours ago
















6












6








6







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













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 6 hours ago









Frpzzd

22.1k839107




22.1k839107












  • What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    – SmileyCraft
    6 hours ago












  • @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    – Frpzzd
    6 hours ago










  • An interesting question. Have you attempted to count the possible loops in small grids?
    – Daniel Mathias
    5 hours ago






  • 2




    How about this?
    – Jens
    5 hours ago




















  • What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
    – SmileyCraft
    6 hours ago












  • @SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
    – Frpzzd
    6 hours ago










  • An interesting question. Have you attempted to count the possible loops in small grids?
    – Daniel Mathias
    5 hours ago






  • 2




    How about this?
    – Jens
    5 hours ago


















What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
– SmileyCraft
6 hours ago






What kind of time complexity is desired? I believe I know an $mathcal{O}(6^nlog m)$ time algorithm.
– SmileyCraft
6 hours ago














@SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
– Frpzzd
6 hours ago




@SmileyCraft I'll take whatever you have. I can't find any resources online for this problem, strangely.
– Frpzzd
6 hours ago












An interesting question. Have you attempted to count the possible loops in small grids?
– Daniel Mathias
5 hours ago




An interesting question. Have you attempted to count the possible loops in small grids?
– Daniel Mathias
5 hours ago




2




2




How about this?
– Jens
5 hours ago






How about this?
– Jens
5 hours ago












2 Answers
2






active

oldest

votes


















3














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



















  • 1




    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).
    – Milo Brandt
    3 hours ago












  • @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
    – SmileyCraft
    3 hours ago






  • 1




    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$.
    – Milo Brandt
    3 hours ago






  • 1




    @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)$$.
    – SmileyCraft
    2 hours ago





















2














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








New contributor




Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    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









    3














    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



















    • 1




      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).
      – Milo Brandt
      3 hours ago












    • @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
      – SmileyCraft
      3 hours ago






    • 1




      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$.
      – Milo Brandt
      3 hours ago






    • 1




      @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)$$.
      – SmileyCraft
      2 hours ago


















    3














    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



















    • 1




      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).
      – Milo Brandt
      3 hours ago












    • @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
      – SmileyCraft
      3 hours ago






    • 1




      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$.
      – Milo Brandt
      3 hours ago






    • 1




      @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)$$.
      – SmileyCraft
      2 hours ago
















    3












    3








    3






    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














    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 1 hour ago

























    answered 5 hours ago









    SmileyCraft

    2,844415




    2,844415








    • 1




      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).
      – Milo Brandt
      3 hours ago












    • @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
      – SmileyCraft
      3 hours ago






    • 1




      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$.
      – Milo Brandt
      3 hours ago






    • 1




      @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)$$.
      – SmileyCraft
      2 hours ago
















    • 1




      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).
      – Milo Brandt
      3 hours ago












    • @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
      – SmileyCraft
      3 hours ago






    • 1




      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$.
      – Milo Brandt
      3 hours ago






    • 1




      @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)$$.
      – SmileyCraft
      2 hours ago










    1




    1




    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).
    – Milo Brandt
    3 hours ago






    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).
    – Milo Brandt
    3 hours ago














    @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
    – SmileyCraft
    3 hours ago




    @MiloBrandt Interesting! Can you elaborate just a bit more on why we get the Catalan numbers?
    – SmileyCraft
    3 hours ago




    1




    1




    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$.
    – Milo Brandt
    3 hours ago




    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$.
    – Milo Brandt
    3 hours ago




    1




    1




    @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)$$.
    – SmileyCraft
    2 hours ago






    @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)$$.
    – SmileyCraft
    2 hours ago













    2














    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








    New contributor




    Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2














      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








      New contributor




      Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        2












        2








        2






        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








        New contributor




        Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        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








        New contributor




        Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|cite|improve this answer



        share|cite|improve this answer






        New contributor




        Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 3 hours ago









        Zachary Hunter

        4268




        4268




        New contributor




        Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Zachary Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.


            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

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei