Project Euler 215 - Solving with Sparse Matrices and Vector Multiplication












2












$begingroup$


I wrote a program to solve Project Euler Problem #215 (see below for description) using memoization, and when I got access to the PE forums, I saw everyone else wrote programs that also used dynamic programming/memoization techniques.



However, from this stackoverflow answer, Dietrich Epp solved it in Haskell using a sparse matrix of NxN dimensions (an adjacency matrix) and then multiplied the matrix by a 1xN vector to arrive at the solution.



Quote from the poster Dietrich Epp:




You compute each possible way to tile a row. Let's say there are N ways to tile a row. Make an NxN matrix. Element i,j is 1 if row i can appear next to row j, 0 otherwise. Start with a vector containing N 1s. Multiply the matrix by the vector a number of times equal to the height of the wall minus 1, then sum the resulting vector.




How come the matrix x vector solution is viable for this problem, as in what property is it exploiting to solve the question?



Thanks in advance!




Problem Description



Consider the problem of building a wall out of 2x1 and 3x1 bricks (horizontal and vertical dimensions) such that, for extra strength, the gaps between horizontally-adjacent bricks never line up in consecutive layers, i.e. never form a "running crack".



For example, the following 9x3 wall is not acceptable due to the running crack shown in red:




PE 215 Crack-free wall




There are eight ways of forming a crack-free 93 wall, written W(9,3) = 8.



Calculate W(32,10).











share|cite|improve this question











$endgroup$












  • $begingroup$
    See my answer at stackoverflow.com: stackoverflow.com/questions/7788462/project-euler-215-in-java/…
    $endgroup$
    – Thomas Andrews
    Oct 21 '11 at 3:14
















2












$begingroup$


I wrote a program to solve Project Euler Problem #215 (see below for description) using memoization, and when I got access to the PE forums, I saw everyone else wrote programs that also used dynamic programming/memoization techniques.



However, from this stackoverflow answer, Dietrich Epp solved it in Haskell using a sparse matrix of NxN dimensions (an adjacency matrix) and then multiplied the matrix by a 1xN vector to arrive at the solution.



Quote from the poster Dietrich Epp:




You compute each possible way to tile a row. Let's say there are N ways to tile a row. Make an NxN matrix. Element i,j is 1 if row i can appear next to row j, 0 otherwise. Start with a vector containing N 1s. Multiply the matrix by the vector a number of times equal to the height of the wall minus 1, then sum the resulting vector.




How come the matrix x vector solution is viable for this problem, as in what property is it exploiting to solve the question?



Thanks in advance!




Problem Description



Consider the problem of building a wall out of 2x1 and 3x1 bricks (horizontal and vertical dimensions) such that, for extra strength, the gaps between horizontally-adjacent bricks never line up in consecutive layers, i.e. never form a "running crack".



For example, the following 9x3 wall is not acceptable due to the running crack shown in red:




PE 215 Crack-free wall




There are eight ways of forming a crack-free 93 wall, written W(9,3) = 8.



Calculate W(32,10).











share|cite|improve this question











$endgroup$












  • $begingroup$
    See my answer at stackoverflow.com: stackoverflow.com/questions/7788462/project-euler-215-in-java/…
    $endgroup$
    – Thomas Andrews
    Oct 21 '11 at 3:14














2












2








2





$begingroup$


I wrote a program to solve Project Euler Problem #215 (see below for description) using memoization, and when I got access to the PE forums, I saw everyone else wrote programs that also used dynamic programming/memoization techniques.



However, from this stackoverflow answer, Dietrich Epp solved it in Haskell using a sparse matrix of NxN dimensions (an adjacency matrix) and then multiplied the matrix by a 1xN vector to arrive at the solution.



Quote from the poster Dietrich Epp:




You compute each possible way to tile a row. Let's say there are N ways to tile a row. Make an NxN matrix. Element i,j is 1 if row i can appear next to row j, 0 otherwise. Start with a vector containing N 1s. Multiply the matrix by the vector a number of times equal to the height of the wall minus 1, then sum the resulting vector.




How come the matrix x vector solution is viable for this problem, as in what property is it exploiting to solve the question?



Thanks in advance!




Problem Description



Consider the problem of building a wall out of 2x1 and 3x1 bricks (horizontal and vertical dimensions) such that, for extra strength, the gaps between horizontally-adjacent bricks never line up in consecutive layers, i.e. never form a "running crack".



For example, the following 9x3 wall is not acceptable due to the running crack shown in red:




PE 215 Crack-free wall




There are eight ways of forming a crack-free 93 wall, written W(9,3) = 8.



Calculate W(32,10).











share|cite|improve this question











$endgroup$




I wrote a program to solve Project Euler Problem #215 (see below for description) using memoization, and when I got access to the PE forums, I saw everyone else wrote programs that also used dynamic programming/memoization techniques.



However, from this stackoverflow answer, Dietrich Epp solved it in Haskell using a sparse matrix of NxN dimensions (an adjacency matrix) and then multiplied the matrix by a 1xN vector to arrive at the solution.



Quote from the poster Dietrich Epp:




You compute each possible way to tile a row. Let's say there are N ways to tile a row. Make an NxN matrix. Element i,j is 1 if row i can appear next to row j, 0 otherwise. Start with a vector containing N 1s. Multiply the matrix by the vector a number of times equal to the height of the wall minus 1, then sum the resulting vector.




How come the matrix x vector solution is viable for this problem, as in what property is it exploiting to solve the question?



Thanks in advance!




Problem Description



Consider the problem of building a wall out of 2x1 and 3x1 bricks (horizontal and vertical dimensions) such that, for extra strength, the gaps between horizontally-adjacent bricks never line up in consecutive layers, i.e. never form a "running crack".



For example, the following 9x3 wall is not acceptable due to the running crack shown in red:




PE 215 Crack-free wall




There are eight ways of forming a crack-free 93 wall, written W(9,3) = 8.



Calculate W(32,10).








linear-algebra matrices project-euler






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Dec 22 '18 at 20:19









Glorfindel

3,41381930




3,41381930










asked Oct 20 '11 at 20:01









wklwkl

1136




1136












  • $begingroup$
    See my answer at stackoverflow.com: stackoverflow.com/questions/7788462/project-euler-215-in-java/…
    $endgroup$
    – Thomas Andrews
    Oct 21 '11 at 3:14


















  • $begingroup$
    See my answer at stackoverflow.com: stackoverflow.com/questions/7788462/project-euler-215-in-java/…
    $endgroup$
    – Thomas Andrews
    Oct 21 '11 at 3:14
















$begingroup$
See my answer at stackoverflow.com: stackoverflow.com/questions/7788462/project-euler-215-in-java/…
$endgroup$
– Thomas Andrews
Oct 21 '11 at 3:14




$begingroup$
See my answer at stackoverflow.com: stackoverflow.com/questions/7788462/project-euler-215-in-java/…
$endgroup$
– Thomas Andrews
Oct 21 '11 at 3:14










1 Answer
1






active

oldest

votes


















2












$begingroup$

This is a nice way of keeping track of the linked recurrence relations. The vector initially starts out with all $1$'s indicating there is one way to make a height $1$ wall with each type of layer on the bottom. After one multiplication by the matrix position $j$ in the vector is the number of ways to have a wall of height $2$ with the bottom layer of type $j$. Each successive multiply adds one layer because position $j$ will have the sum of all the other types of layer that can come above it. So after $n-1$ multiplies you have walls of height $n$. Then at the end you sum over all the types of bottom layer.






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Great, thanks for the explanation!
    $endgroup$
    – wkl
    Oct 21 '11 at 0:46












Your Answer








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%2f74388%2fproject-euler-215-solving-with-sparse-matrices-and-vector-multiplication%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2












$begingroup$

This is a nice way of keeping track of the linked recurrence relations. The vector initially starts out with all $1$'s indicating there is one way to make a height $1$ wall with each type of layer on the bottom. After one multiplication by the matrix position $j$ in the vector is the number of ways to have a wall of height $2$ with the bottom layer of type $j$. Each successive multiply adds one layer because position $j$ will have the sum of all the other types of layer that can come above it. So after $n-1$ multiplies you have walls of height $n$. Then at the end you sum over all the types of bottom layer.






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Great, thanks for the explanation!
    $endgroup$
    – wkl
    Oct 21 '11 at 0:46
















2












$begingroup$

This is a nice way of keeping track of the linked recurrence relations. The vector initially starts out with all $1$'s indicating there is one way to make a height $1$ wall with each type of layer on the bottom. After one multiplication by the matrix position $j$ in the vector is the number of ways to have a wall of height $2$ with the bottom layer of type $j$. Each successive multiply adds one layer because position $j$ will have the sum of all the other types of layer that can come above it. So after $n-1$ multiplies you have walls of height $n$. Then at the end you sum over all the types of bottom layer.






share|cite|improve this answer









$endgroup$













  • $begingroup$
    Great, thanks for the explanation!
    $endgroup$
    – wkl
    Oct 21 '11 at 0:46














2












2








2





$begingroup$

This is a nice way of keeping track of the linked recurrence relations. The vector initially starts out with all $1$'s indicating there is one way to make a height $1$ wall with each type of layer on the bottom. After one multiplication by the matrix position $j$ in the vector is the number of ways to have a wall of height $2$ with the bottom layer of type $j$. Each successive multiply adds one layer because position $j$ will have the sum of all the other types of layer that can come above it. So after $n-1$ multiplies you have walls of height $n$. Then at the end you sum over all the types of bottom layer.






share|cite|improve this answer









$endgroup$



This is a nice way of keeping track of the linked recurrence relations. The vector initially starts out with all $1$'s indicating there is one way to make a height $1$ wall with each type of layer on the bottom. After one multiplication by the matrix position $j$ in the vector is the number of ways to have a wall of height $2$ with the bottom layer of type $j$. Each successive multiply adds one layer because position $j$ will have the sum of all the other types of layer that can come above it. So after $n-1$ multiplies you have walls of height $n$. Then at the end you sum over all the types of bottom layer.







share|cite|improve this answer












share|cite|improve this answer



share|cite|improve this answer










answered Oct 21 '11 at 0:00









Ross MillikanRoss Millikan

302k24200375




302k24200375












  • $begingroup$
    Great, thanks for the explanation!
    $endgroup$
    – wkl
    Oct 21 '11 at 0:46


















  • $begingroup$
    Great, thanks for the explanation!
    $endgroup$
    – wkl
    Oct 21 '11 at 0:46
















$begingroup$
Great, thanks for the explanation!
$endgroup$
– wkl
Oct 21 '11 at 0:46




$begingroup$
Great, thanks for the explanation!
$endgroup$
– wkl
Oct 21 '11 at 0:46


















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%2f74388%2fproject-euler-215-solving-with-sparse-matrices-and-vector-multiplication%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

Plaza Victoria

In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...