Discrete MathematicsRecursion and Recurrences3 of 4
Recursion and Recurrences · 3

Setting Up Recurrences from Counting Problems

When a counting problem has too many possibilities to list by hand, we can build a recurrence relation by looking at the choices for the very first step and seeing what size problem is left behind.

1

Tiling a 2×n2 \times n Board with Dominoes

How many ways an\newterm{a_n} can we completely tile a 2×n2 \times n board using 2×12 \times 1 dominoes?

Tiling a 2 × n Board with Dominoes
Ways(10)=  ?\htmlClass{lc-pink}{\text{Ways}(10)} = \; \htmlClass{lc-orange}{?}
Vertical start\text{Vertical start}
2×(n−1)\htmlClass{lc-blue}{2 \times (n - 1)}
an−1\htmlClass{lc-blue}{a_{n-1}}
Horizontal start\text{Horizontal start}
2×(n−2)\htmlClass{lc-blue}{2 \times (n - 2)}
an−2\htmlClass{lc-blue}{a_{n-2}}
Text solution
Start with a vertical dominoPlacing one vertical domino fills the first column completely and leaves an unrestricted 2×(n−1)\leftover{2 \times (n - 1)} board.
Vertical start\displaystyle \start{\text{Vertical start}} =an−1\displaystyle = \leftover{a_{n-1}}
Start with a horizontal dominoPlacing a horizontal domino on the top-left traps the square underneath it, forcing a second horizontal domino and leaving an unrestricted 2×(n−2)\leftover{2 \times (n - 2)} board.
Horizontal start\displaystyle \start{\text{Horizontal start}} =an−2\displaystyle = \leftover{a_{n-2}}
Add the two casesSince the top-left corner must be covered either vertically or horizontally, the total number of tilings for n≥3n \ge 3 is the sum of the two cases.
an\displaystyle \newterm{a_n} =an−1+an−2\displaystyle = \leftover{a_{n-1}} + \leftover{a_{n-2}}
Add terms from the starting boardsCounting a1=1\initial{a_1 = 1} and a2=2\initial{a_2 = 2} by hand lets us add each pair of terms to reach a10\newterm{a_{10}}.
1,  2,  3,  5,  8,  13,  21,  34,  55,  89\displaystyle \initial{1},\; \initial{2},\; 3,\; 5,\; 8,\; 13,\; 21,\; 34,\; 55,\; 89

So there are 8989 ways to tile a 2×102 \times 10 board.

Here is how the two starting cases divide up all the tilings of a 2×52 \times 5 board without overlap.

Each starting move only locks in tiles until a clean rectangle is left, and any extra move is already counted inside that rectangle.
2

Binary Strings Without Forbidden Runs

Now let's find the number of binary strings an\newterm{a_n} of length nn that contain no two consecutive zeros (00\forbidden{00}).

Binary Strings Without Forbidden Runs
an\htmlClass{lc-pink}{a_n}
0    10\;\;1
nn
no 00\text{no } \htmlClass{lc-orange}{00}
Text solution
Start with 1\start{1}Placing 1\start{1} in the first slot puts no restriction on the next slot, leaving n−1\leftover{n - 1} clean slots.
1_  _  ⋯  _⏟n−1 clean slots  ⟶  an−1\displaystyle \start{1}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{n-1\text{ clean slots}}} \;\longrightarrow\; \leftover{a_{n-1}}
Start with 0\start{0}Placing 0\start{0} in the first slot forces a 1\start{1} into the second slot to avoid 00\forbidden{00}, leaving n−2\leftover{n - 2} clean slots.
0 1_  _  ⋯  _⏟n−2 clean slots  ⟶  an−2\displaystyle \start{0\,1}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{n-2\text{ clean slots}}} \;\longrightarrow\; \leftover{a_{n-2}}
Add the cases for no 00\forbidden{00}Every valid string starts with either 1\start{1} or 01\start{01}, giving the same recurrence as the domino problem.
an\displaystyle \newterm{a_n} =an−1+an−2\displaystyle = \leftover{a_{n-1}} + \leftover{a_{n-2}}
Extend to no 000\forbidden{000}When three consecutive zeros are forbidden, a valid string can start with zero, one, or two zeros before its first 1\start{1}, and after that 1\start{1} the remaining slots are a clean slate that still follows the no 000\forbidden{000} rule.
an=an−1⏟starts 1+an−2⏟starts 01+an−3⏟starts 001\displaystyle \begin{aligned} \newterm{a_n} &= \underbrace{\leftover{a_{n-1}}}_{\text{starts } \start{1}} + \underbrace{\leftover{a_{n-2}}}_{\text{starts } \start{01}} + \underbrace{\leftover{a_{n-3}}}_{\text{starts } \start{001}} \end{aligned}

Each valid start up to the first 1\start{1} uses up a different number of slots and contributes one earlier term to the recurrence.

3

Where Coefficients Come From

What if more than one starting choice leaves the same number of clean slots behind? Let's find the number of strings an\newterm{a_n} of length nn formed from the letters AA, BB, and CC with no two AA's next to each other (AA\forbidden{AA}).

Where Coefficients Come From
an\htmlClass{lc-pink}{a_n}
A    B    CA\;\;B\;\;C
nn
no AA\text{no } \htmlClass{lc-orange}{AA}
BB ✓CC ✓BB\,\htmlClass{lc-green}{\checkmark}\qquad CC\,\htmlClass{lc-green}{\checkmark}
Text solution
Starts that leave n−1\leftover{n - 1} clean slotsPlacing B\start{B} or C\start{C} in the first slot puts no restriction on the second slot, giving 2\coef{2} separate ways to leave n−1\leftover{n - 1} clean slots.
B_  _  ⋯  _⏟n−1 clean slots  ⟶  an−1C_  _  ⋯  _⏟n−1 clean slots  ⟶  an−1}  2an−1\displaystyle \left.\begin{array}{l} \start{B}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{n-1\text{ clean slots}}} \;\longrightarrow\; \leftover{a_{n-1}} \\[10pt] \start{C}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{n-1\text{ clean slots}}} \;\longrightarrow\; \leftover{a_{n-1}} \end{array}\right\} \; \coef{2}\leftover{a_{n-1}}
Starts that leave n−2\leftover{n - 2} clean slotsPlacing A\start{A} in the first slot forces the second slot to be B\start{B} or C\start{C}, giving 2\coef{2} separate ways to leave n−2\leftover{n - 2} clean slots.
A B_  _  ⋯  _⏟n−2 clean slots  ⟶  an−2A C_  _  ⋯  _⏟n−2 clean slots  ⟶  an−2}  2an−2\displaystyle \left.\begin{array}{l} \start{A\,B}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{n-2\text{ clean slots}}} \;\longrightarrow\; \leftover{a_{n-2}} \\[10pt] \start{A\,C}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{n-2\text{ clean slots}}} \;\longrightarrow\; \leftover{a_{n-2}} \end{array}\right\} \; \coef{2}\leftover{a_{n-2}}
Combine all valid startsAdding the four starting cases gives the recurrence.
an\displaystyle \newterm{a_n} =2an−1+2an−2\displaystyle = \coef{2}\leftover{a_{n-1}} + \coef{2}\leftover{a_{n-2}}

So a coefficient in front of an−k\leftover{a_{n-k}} simply counts how many valid starts use kk slots to leave n−k\leftover{n - k} clean slots.

4

When a Move Satisfies the Rule

In the problems so far, the leftover slots always had to obey the original rule. Now let's flip the condition and find the number of binary strings an\newterm{a_n} of length nn that must contain at least one 00\required{00}.

When a Move Satisfies the Rule
an\htmlClass{lc-pink}{a_n}
0    10\;\;1
nn
at least one 00\text{at least one } \htmlClass{lc-green}{00}
Text solution
Starts that still need 00\required{00}Starting with 1\start{1} (using 11 slot) or 01\start{01} (using 22 slots) has not produced 00\required{00} yet, so the remaining slots must still contain at least one 00\required{00}.
1_  _  ⋯  _⏟still needs 00  ⟶  an−1,0 1_  _  ⋯  _⏟still needs 00  ⟶  an−2\displaystyle \begin{aligned} \start{1}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{\text{still needs } \required{00}}} \;&\longrightarrow\; \leftover{a_{n-1}}, \\[6pt] \start{0\,1}\leftover{\underbrace{\_\;\_\;\cdots\;\_}_{\text{still needs } \required{00}}} \;&\longrightarrow\; \leftover{a_{n-2}} \end{aligned}
Start that satisfies the ruleStarting with 00\start{00} completes the requirement right away, leaving 2\free{2} unrestricted choices (00 or 11) for each of the remaining n−2\free{n - 2} slots.
0 0_  _  ⋯  _⏟no restriction left  ⟶  2n−2\displaystyle \start{0\,0}\free{\underbrace{\_\;\_\;\cdots\;\_}_{\text{no restriction left}}} \;\longrightarrow\; \free{2^{n-2}}
Add the three casesCombining the two unfinished cases with the completed case gives the recurrence.
an\displaystyle \newterm{a_n} =an−1+an−2+2n−2\displaystyle = \leftover{a_{n-1}} + \leftover{a_{n-2}} + \free{2^{n-2}}

Because the 00\start{00} start already satisfies the rule, its leftover is counted directly as 2n−2\free{2^{n-2}} instead of an−2\notterm{a_{n-2}}.

Compare what happens to the remaining slots when a start still needs 00\required{00} versus when it already has 00\required{00}.

The opening digits decide whether the leftover slots still need 00 and give an earlier term, or already have 00 and can be filled freely.
5

Finding Initial Conditions

A recurrence relation only works once nn is large enough to look back at earlier terms, so we pair it with initial conditions counted by hand for the smallest values of nn.

Finding Initial Conditions
initial conditions\htmlClass{lc-green}{\text{initial conditions}}
an−1\htmlClass{lc-blue}{a_{n-1}}
a1a_{1}
a2a_{2}
a3a_{3}
a4a_{4}
an−1, an−2\htmlClass{lc-blue}{a_{n-1},\ a_{n-2}}
a1a_{1}
a2a_{2}
a3a_{3}
a4a_{4}
an−1, an−2, an−3\htmlClass{lc-blue}{a_{n-1},\ a_{n-2},\ a_{n-3}}
a1a_{1}
a2a_{2}
a3a_{3}
a4a_{4}
Text solution
Match the number of initial conditions to the furthest step backBoth an=2an−1+2an−2\newterm{a_n} = \coef{2}\leftover{a_{n-1}} + \coef{2}\leftover{a_{n-2}} and an=an−1+an−2+2n−2\newterm{a_n} = \leftover{a_{n-1}} + \leftover{a_{n-2}} + \free{2^{n-2}} reach 22 steps back to an−2\leftover{a_{n-2}}, so the formulas start at n=3n = 3 and need a1\initial{a_1} and a2\initial{a_2}.
Reaches back to an−k  ⟶  find a1,a2,…,ak by hand\displaystyle \text{Reaches back to } \leftover{a_{n-k}} \;\longrightarrow\; \text{find } \initial{a_1, a_2, \dots, a_k} \text{ by hand}
Count a1\initial{a_1} and a2\initial{a_2} for A,B,CA, B, C strings with no AA\forbidden{AA}All 33 single letters work for n=1n = 1, and 88 of the 99 two-letter pairs work for n=2n = 2 (all except AA\forbidden{AA}).
a1=3,a2\displaystyle \initial{a_1 = 3}, \qquad \initial{a_2} =3×3−1\displaystyle = 3 \times 3 - \forbidden{1} =8\displaystyle = \initial{8}
Count a1\initial{a_1} and a2\initial{a_2} for binary strings that must contain 00\required{00}A 11-slot string cannot contain two zeros, and only 00\required{00} out of the 44 two-slot strings contains 00\required{00}.
a1=0,a2=1\displaystyle \initial{a_1 = 0}, \qquad \initial{a_2 = 1}

Plugging n=3n = 3 into an=an−1+an−2+2n−2\newterm{a_n} = \leftover{a_{n-1}} + \leftover{a_{n-2}} + \free{2^{n-2}} gives a3=1+0+21=3\newterm{a_3} = \leftover{1} + \leftover{0} + \free{2^1} = 3, matching the three valid strings 000000, 001001, and 100100.

Key concepts

To set up a recurrence for an\newterm{a_n}, look only at the choices for the first spot on the left, fill in anything that choice forces, and check the rule on the remaining space:

Clean smaller problem (c an−k\coef{c}\,\leftover{a_{n-k}})

Stop locking in steps as soon as the leftover space is a clean smaller copy of the original problem, with no extra restriction from the start. If c\coef{c} different starts use kk spots and leave n−k\leftover{n - k} spots that still obey the original rule, they contribute c an−k\coef{c}\,\leftover{a_{n-k}} (for strings from A,B,CA, B, C with no AA\forbidden{AA}, starts AB\start{AB} and AC\start{AC} give 2an−2\coef{2}\leftover{a_{n-2}}).

When not to use an earlier term an−k\notterm{a_{n-k}}

If the first kk spots already satisfy the condition for the entire object, there is no restriction left on the remaining n−k\free{n - k} spots, so count them directly instead of writing an−k\notterm{a_{n-k}} (for binary strings that must contain 00\required{00}, starting with 00\start{00} contributes 2n−2\free{2^{n-2}}, not an−2\notterm{a_{n-2}}).

Initial conditions

If the recurrence reaches back to an−k\leftover{a_{n-k}}, count a1,a2,…,ak\initial{a_1, a_2, \dots, a_k} by hand and apply the recurrence for n≥k+1n \ge k + 1.

6

Practice

1

Let an\newterm{a_n} be the number of ways to tile a 2×n2 \times n board using 2×12 \times 1 dominoes (which can be placed vertically or horizontally) and 2×22 \times 2 square tiles. Set up a recurrence relation and initial conditions for an\newterm{a_n}, and use them to find a5\newterm{a_5}.

How many ways can you cover the first column using 11 column vs. 22 columns without leaving a trapped corner?

Cover 11 columnA single vertical 2×12 \times 1 domino fills column 11 and leaves a clean 2×(n−1)\leftover{2 \times (n - 1)} board.
1-column start\displaystyle \start{\text{1-column start}} =an−1\displaystyle = \leftover{a_{n-1}}
Cover 22 columnsWe can fill the first two columns with two stacked horizontal 2×12 \times 1 dominoes or with one 2×22 \times 2 square tile, giving 2\coef{2} ways to leave a clean 2×(n−2)\leftover{2 \times (n - 2)} board. (Watch out: do not count two side-by-side vertical dominoes here. Those start with a vertical domino in column 11 and are already counted inside an−1\leftover{a_{n-1}}. Including them gives an=an−1+3an−2a_n = a_{n-1} + 3a_{n-2} and the wrong answer a5=33a_5 = 33.)
2-column starts\displaystyle \start{\text{2-column starts}} =2an−2\displaystyle = \coef{2}\leftover{a_{n-2}}
Recurrence and initial conditionsThe recurrence reaches 22 steps back, so we count a1\initial{a_1} and a2\initial{a_2} by hand (remembering to include the 2×22 \times 2 square tile in a2\initial{a_2}; leaving it out gives a2=2a_2 = 2 and a5=16a_5 = 16).
an\displaystyle \newterm{a_n} =an−1+2an−2for n\displaystyle = \leftover{a_{n-1}} + \coef{2}\leftover{a_{n-2}} \quad \text{for } n ≥3,a1=1,a2=3\displaystyle \ge 3, \qquad \initial{a_1 = 1}, \quad \initial{a_2 = 3}
Compute up to a5\newterm{a_5}
a3=3+2(1)=5a4=5+2(3)=11a5=11+2(5)=21\displaystyle \begin{aligned} \newterm{a_3} &= \leftover{3} + \coef{2}(\leftover{1}) = 5 \\ \newterm{a_4} &= \leftover{5} + \coef{2}(\leftover{3}) = 11 \\ \newterm{a_5} &= \leftover{11} + \coef{2}(\leftover{5}) = 21 \end{aligned}
2

Let an\newterm{a_n} be the number of ways to tile a 3×n3 \times n board using 3×13 \times 1 trominoes, which can be oriented vertically (3×13 \times 1) or horizontally (1×31 \times 3). Set up a recurrence relation and initial conditions for an\newterm{a_n}.

If you place a 1×31 \times 3 horizontal tromino across the top-left, what is forced in the two rows underneath it?

Start with a vertical trominoPlacing one vertical 3×13 \times 1 tromino in column 11 leaves a clean 3×(n−1)\leftover{3 \times (n - 1)} board.
Vertical start\displaystyle \start{\text{Vertical start}} =an−1\displaystyle = \leftover{a_{n-1}}
Start with a horizontal trominoPlacing a horizontal 1×31 \times 3 tromino across the top of columns 11, 22, and 33 traps the two squares underneath it in column 11, forcing two more horizontal trominoes below it and leaving a clean 3×(n−3)\leftover{3 \times (n - 3)} board. (Watch out: even though this block uses 33 horizontal trominoes, all three are forced together as 11 single choice, so it contributes an−3\leftover{a_{n-3}}, not 3an−3\notterm{3a_{n-3}}.)
Horizontal start\displaystyle \start{\text{Horizontal start}} =an−3\displaystyle = \leftover{a_{n-3}}
Recurrence and initial conditionsBecause the recurrence reaches 33 steps back to an−3\leftover{a_{n-3}}, we need 33 initial conditions a1,a2,a3\initial{a_1, a_2, a_3} (for n=1n = 1 and n=2n = 2, horizontal trominoes cannot fit, so only vertical trominoes work).
an\displaystyle \newterm{a_n} =an−1+an−3for n\displaystyle = \leftover{a_{n-1}} + \leftover{a_{n-3}} \quad \text{for } n ≥4,a1=1,a2=1,a3=2\displaystyle \ge 4, \qquad \initial{a_1 = 1}, \quad \initial{a_2 = 1}, \quad \initial{a_3 = 2}
3

Let an\newterm{a_n} be the number of ternary strings of length nn (formed from the digits 00, 11, and 22) that do not contain three consecutive zeros (000\forbidden{000}). Set up a recurrence relation and initial conditions for an\newterm{a_n}.

A valid string starts with zero, one, or two 00's before its first non-zero digit. How many choices are there for that first non-zero digit?

Zero 00's at the startStarting with 1\start{1} or 2\start{2} (2\coef{2} choices) leaves n−1\leftover{n - 1} clean slots.
Starts 1 or 2  ⟶  2an−1\displaystyle \text{Starts } \start{1} \text{ or } \start{2} \;\longrightarrow\; \coef{2}\leftover{a_{n-1}}
One or two 00's at the startStarting with 0\start{0} followed by a non-zero digit (01\start{01} or 02\start{02}) leaves n−2\leftover{n - 2} clean slots, and starting with 00\start{00} followed by a non-zero digit (001\start{001} or 002\start{002}) leaves n−3\leftover{n - 3} clean slots. (Watch out: unlike binary strings where "not 00" must be 11, here the non-zero digit has 2\coef{2} choices, 1\start{1} or 2\start{2}. Forgetting that factor of 2\coef{2} gives the wrong recurrence an=2an−1+an−2+an−3a_n = 2a_{n-1} + a_{n-2} + a_{n-3}.)
Starts 01,02  ⟶  2an−2,Starts 001,002  ⟶  2an−3\displaystyle \text{Starts } \start{01}, \start{02} \;\longrightarrow\; \coef{2}\leftover{a_{n-2}}, \qquad \text{Starts } \start{001}, \start{002} \;\longrightarrow\; \coef{2}\leftover{a_{n-3}}
Recurrence and initial conditionsReaching back to an−3\leftover{a_{n-3}} requires a1,a2,a3\initial{a_1, a_2, a_3}; for n=3n = 3, only 000\forbidden{000} out of the 33=273^3 = 27 strings is forbidden.
an\displaystyle \newterm{a_n} =2an−1+2an−2+2an−3for n\displaystyle = \coef{2}\leftover{a_{n-1}} + \coef{2}\leftover{a_{n-2}} + \coef{2}\leftover{a_{n-3}} \quad \text{for } n ≥4,a1=3,a2=9,a3=26\displaystyle \ge 4, \qquad \initial{a_1 = 3}, \quad \initial{a_2 = 9}, \quad \initial{a_3 = 26}
4

Let an\newterm{a_n} be the number of strings of length nn formed from the letters AA, BB, and CC that must contain at least one pair of adjacent AA's (AA\required{AA}). Set up a recurrence relation and initial conditions for an\newterm{a_n}.

Which starts still need AAAA in the remaining slots, and what happens to the rule when the string starts with AAAA?

Starts that still need AA\required{AA}Starting with B\start{B} or C\start{C} (2\coef{2} ways) leaves n−1\leftover{n - 1} slots that still need AA\required{AA}, and starting with AB\start{AB} or AC\start{AC} (2\coef{2} ways) stops the A\start{A} from forming AA\required{AA} right away and leaves n−2\leftover{n - 2} slots that still need AA\required{AA}.
Starts B,C  ⟶  2an−1,Starts AB,AC  ⟶  2an−2\displaystyle \text{Starts } \start{B}, \start{C} \;\longrightarrow\; \coef{2}\leftover{a_{n-1}}, \qquad \text{Starts } \start{AB}, \start{AC} \;\longrightarrow\; \coef{2}\leftover{a_{n-2}}
Start that satisfies the ruleStarting with AA\start{AA} (11 way) fulfills the rule, leaving all 3\free{3} letters (AA, BB, or CC) available for each of the remaining n−2\free{n - 2} slots. (Watch out: do not write an−2\notterm{a_{n-2}} once AA\start{AA} is complete, and use base 3\free{3} for the 3\free{3} letters rather than carrying over 2n−22^{n-2} from binary strings.)
Start AA  ⟶  3n−2\displaystyle \text{Start } \start{AA} \;\longrightarrow\; \free{3^{n-2}}
Recurrence and initial conditions
an\displaystyle \newterm{a_n} =2an−1+2an−2+3n−2for n\displaystyle = \coef{2}\leftover{a_{n-1}} + \coef{2}\leftover{a_{n-2}} + \free{3^{n-2}} \quad \text{for } n ≥3,a1=0,a2=1\displaystyle \ge 3, \qquad \initial{a_1 = 0}, \quad \initial{a_2 = 1}
5

A 1×n1 \times n strip is tiled using red 1×11 \times 1 squares, blue 1×11 \times 1 squares, and white 1×21 \times 2 dominoes, with the rule that no two red squares can be placed next to each other. Set up a recurrence relation and initial conditions for the number of valid tilings an\newterm{a_n}.

If the first tile is a red 1×11 \times 1 square, what are all the valid tiles that can come immediately after it?

Start with a blue square or white dominoA blue 1×11 \times 1 square leaves a clean 1×(n−1)1 \times (n - 1) strip (an−1\leftover{a_{n-1}}), and a white 1×21 \times 2 domino leaves a clean 1×(n−2)1 \times (n - 2) strip (an−2\leftover{a_{n-2}}).
Blue start\displaystyle \text{Blue start} =an−1,White start\displaystyle = \leftover{a_{n-1}}, \qquad \text{White start} =an−2\displaystyle = \leftover{a_{n-2}}
Start with a red squareThe tile right after a red 1×11 \times 1 square cannot be red, so it must be either a blue 1×11 \times 1 square (using 22 spots total and leaving n−2n - 2) or a white 1×21 \times 2 domino (using 33 spots total and leaving n−3n - 3). (Watch out: overlooking the white 1×21 \times 2 domino after the red square drops the an−3\leftover{a_{n-3}} term and gives the wrong recurrence an=an−1+2an−2a_n = a_{n-1} + 2a_{n-2}.)
Red then Blue\displaystyle \text{Red then Blue} =an−2,Red then White\displaystyle = \leftover{a_{n-2}}, \qquad \text{Red then White} =an−3\displaystyle = \leftover{a_{n-3}}
Recurrence and initial conditionsAdding the four cases reaches 33 steps back, so we count a1,a2,a3\initial{a_1, a_2, a_3} by hand.
an\displaystyle \newterm{a_n} =an−1+2an−2+an−3for n\displaystyle = \leftover{a_{n-1}} + \coef{2}\leftover{a_{n-2}} + \leftover{a_{n-3}} \quad \text{for } n ≥4,a1=2,a2=4,a3=9\displaystyle \ge 4, \qquad \initial{a_1 = 2}, \quad \initial{a_2 = 4}, \quad \initial{a_3 = 9}