Skip to main content
Logo image

Activities to Accompany Applied Combinatorics

Worksheet 3.1 Recursive Counting

1.

Consider coloring the squares of a \(1\times n\) checkerboard white and gold. Let \(h(n)\) be the number of such colorings that do not have two adjacent squares colored gold.

(a)

Draw pictures to determine \(h(1)\text{,}\) \(h(2)\text{,}\) \(h(3)\text{,}\) and \(h(4)\text{.}\)
Solution.
Pictures are omitted, but you should get \(h(1) = 2\text{,}\) \(h(2) = 3\text{,}\) \(h(3) = 5\text{,}\) and \(h(4) = 8\text{.}\)

(b)

Now let’s think about the full \(1\times n\) checkerboard. If the first square is colored white, how many squares are there left to color? Use the function \(h\) to describe the number of ways that you can color those squares without making two consecutive gold squares.
Solution.
When the first square is colored white, the remaining \(n-1\) spaces can be colored in any of the \(h(n-1)\) ways that avoid two consecutive gold squares.

(c)

Continuing to think about the full \(1\times n\) checkerboard, what do you know about the color of the second square if the first square is colored gold? Use the function \(h\) to describe the number of ways that you can color the remaining squares without making two consecutive gold squares.
Solution.
When the first square is colored gold, we must color the second square white in order to avoid two consecutive gold squares. But then the remaining \(n-2\) squares can be colored in any of the \(h(n-2)\) ways that avoid two consecutive gold squares.

(d)

What recursive formula do you thus have for \(h(n)\text{?}\) Be sure to specify enough initial conditions to get started!
Solution.
Since starting with a white square or starting with a gold square are the only options, we have \(h(n) = h(n-1)+h(n-2)\) as our recursive formula for \(n\geq 3\text{.}\) We also have \(h(1) = 2\) and \(h(2) = 3\text{.}\)

2.

Recall that a ternary string is a string in which the allowed symbols are \(0\text{,}\) \(1\text{,}\) and \(2\text{.}\) Call a ternary string β€œgood” if it does not contain a \(1\) followed immediately by a \(2\text{.}\) Let \(g(n)\) be the number of good strings of length \(n\text{.}\) Find a recursive formula for \(g(n)\text{.}\)
Solution.
If the first digit is a 0 or a 2, then the remaining positions can be any good string of length \(n-1\text{.}\) This accounts for \(2g(n-1)\) good strings. When the first digit of the string is 1, we can follow with any good string of length \(n-1\) that does not start with a 2. There are \(g(n-1)\) good strings of length \(n-1\text{,}\) so we subtract off those that start with a 2. There are \(g(n-2)\) good strings of length \(n-1\) that start with a 2, since the 2 can be followed by any good string of length \(n-2\text{.}\) Thus, in the case of starting with a 1, we have \(g(n-1)-g(n-2)\) good strings. Adding these together to account for all cases, the total number of good strings is therefore given by \(g(n) = 3g(n-1) - g(n-2)\) for \(n\geq 3\text{.}\) We also have \(g(1) = 3\) since all strings of length 1 are good and the only string of length 2 that is not good is 12, so \(g(2) = 8\text{.}\)

3.

The Towers of Hanoi puzzle consists of \(n\) discs of different sizes and three pegs lined up in a row. At the beginning, all of the discs are on the leftmost peg. The discs are arranged in order of increasing size with the largest disc on the bottom and the smallest disc on top. A β€œmove” consists of picking one disc up and placing it on another peg. However, the rules do not allow you to ever place a larger disc on top of a smaller disc. The goal of the game is to move all the discs from the leftmost peg to a different peg while following the rules. Let \(t(n)\) be the smallest number of moves in which you can accomplish this goal. Find a recursive formula for \(t(n)\text{.}\)
Solution.
First note that you can move one disc with one move, so \(t(1)=1\text{.}\) To establish the recursive formula, notice that in order to move a stack of \(n\) discs to from the leftmost peg to the rightmost peg, we can first move the top \(n-1\) discs from the leftmost peg to the middle peg. This requires \(t(n-1)\) moves. We then move the largest disc from the leftmost peg to the rightmost peg with a single move. Now we move the stack of \(n-1\) discs from the middle peg to the rightmost peg, which requires \(t(n-1)\) moves again. Therefore, \(t(n) = 2t(n-1)+1\) for \(n\geq 2\) with \(t(1) = 1\text{.}\)

4.

Find a recursive formula for the number of binary strings of length \(n\) not containing \(101\text{.}\)
Solution.
Let \(b(n)\) be the number of binary strings of length \(n\) not containing 101. This means that all bit strings of length 1 and 2 satisfy the criterion, and only one bit string (101) of length 3 fails to satisfy it. This gives us initial conditions of
\begin{equation*} b(1) = 2\qquad b(2) = 4 \qquad b(3) = 7\text{.} \end{equation*}
If the first digit of a bit string of length \(n\) is 0, then the remaining \(n-1\) positions can be filled with any of the \(b(n-1)\) bit strings of length \(n-1\) that do not contain 101. If the first digit of a 101-avoiding bit string of length \(n\) is a 1, then the remaining \(n-1\) positions can be filled with any 101-avoiding bit string of length \(n-1\) that does not start with 01. There are \(b(n-1)\) bit strings of length \(n-1\) that avoid 101. Since we are only thinking now about those bit strings that we know don’t contain 101, we are looking to subtract those that start with 01.
Our task now is to count the 101-avoiding bit strings of length \(n-1\) that start 01, so we can subtract as discussed in the previous paragraph. To do this, notice that there is a one-to-one correspondence between 101-avoiding bit strings of length \(n-1\) that start 01 and 101-avoiding bit strings of length \(n-2\) that start with 1. (Removing the 0 from the length \(n-1\) bit string gives a length \(n-2\) bit string of the type required, and taking a length \(n-2\) bit string that avoids 101 and starts with 1 and adding a 0 to the front gives a 101-avoiding bit string of length \(n-1\) that starts 01.)
OK, so now what we really want to count (so we can subtract it!) is the number of 101-avoiding bit strings of length \(n-2\) that start with 1. Since every 101-avoiding bit string of length \(n-2\) either starts with a 0 or a 1, adding the number that start with 0 to the number that start with 1 yields \(b(n-2)\text{.}\) Alternatively, the number of 101-avoiding bit strings of length \(n-2\) that start with 1 can be found by taking \(b(n-2)\) and subtracting the number of 101-avoiding bit strings of length \(n-2\) that start with 0.
OK, so what we really need to count (and then we’ll see how to use it!) is the number of 101-avoiding bit strings of length \(n-2\) that start with 0. But as we saw before, we can turn any 101-avoiding bit string of length \(n-2\) that starts with 0 and turn it into a 101-avoiding bit string of length \(n-3\) by removing the leading 0. Conversely, we can take any 101-avoiding bit string of length \(n-3\) and put a 0 on its front without creating 101, so then we have a 101-avoiding bit string of length \(n-2\) that starts with 0. Therefore, the number of 101-avoiding bit strings of length \(n-2\) that start with 0 is the same as the number of 101-avoiding bit strings of length \(n-3\text{,}\) which we know to be \(b(n-3)\text{.}\) Progress!
Alright, so here’s a summary of where we are:
\begin{align*} b(n) \amp = b(n-1) + \left[b(n-1) - (\text{length }n-1\text{ start }01)\right]\\ (\text{length }n-1\text{ start }01)\amp = (\text{length }n-2\text{ start }1) \\ (\text{length }n-2\text{ start }1)\amp = b(n-2) - (\text{length }n-2\text{ start }0)\\ (\text{length }n-2\text{ start }0) \amp = b(n-3)\text{.} \end{align*}
Now we’ve got all the information we need to substitute in, so we have
\begin{align*} (\text{length }n-2\text{ start }1)\amp = b(n-2) - b(n-3)\\ (\text{length }n-1\text{ start }01)\amp = b(n-2) - b(n-3) \\ b(n) \amp = b(n-1) + \left[b(n-1) - (b(n-2) - b(n-3) )\right]\\ \amp = 2b(n-1) - b(n-2) + b(n-3)\text{.} \end{align*}
Thus, \(b(n) =2b(n-1) - b(n-2) + b(n-3) \) for \(n\geq 4\) with \(b(1)=2\text{,}\) \(b(2) = 4\text{,}\) and \(b(3)=7\text{.}\)