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×n Board with Dominoes
How many ways an can we completely tile a 2×n board using 2×1 dominoes?
Tiling a 2 × n Board with Dominoes
Text solution
Start with a vertical dominoPlacing one vertical domino fills the first column completely and leaves an unrestricted 2×(n−1) board.
Vertical start=an−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) board.
Horizontal start=an−2
Add the two casesSince the top-left corner must be covered either vertically or horizontally, the total number of tilings for n≥3 is the sum of the two cases.
an=an−1+an−2
Add terms from the starting boardsCounting a1=1 and a2=2 by hand lets us add each pair of terms to reach a10.
1,2,3,5,8,13,21,34,55,89
So there are 89 ways to tile a 2×10 board.
Here is how the two starting cases divide up all the tilings of a 2×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 of length n that contain no two consecutive zeros (00).
Binary Strings Without Forbidden Runs
Text solution
Start with 1Placing 1 in the first slot puts no restriction on the next slot, leaving n−1 clean slots.
1n−1 clean slots__⋯_⟶an−1
Start with 0Placing 0 in the first slot forces a 1 into the second slot to avoid 00, leaving n−2 clean slots.
01n−2 clean slots__⋯_⟶an−2
Add the cases for no 00Every valid string starts with either 1 or 01, giving the same recurrence as the domino problem.
an=an−1+an−2
Extend to no 000When three consecutive zeros are forbidden, a valid string can start with zero, one, or two zeros before its first 1, and after that 1 the remaining slots are a clean slate that still follows the no 000 rule.
Each valid start up to the first 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 of length n formed from the letters A,B, and C with no two A's next to each other (AA).
Where Coefficients Come From
Text solution
Starts that leave n−1 clean slotsPlacing B or C in the first slot puts no restriction on the second slot, giving 2 separate ways to leave n−1 clean slots.
Combine all valid startsAdding the four starting cases gives the recurrence.
an=2an−1+2an−2
So a coefficient in front of an−k simply counts how many valid starts use k slots to leave 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 of length n that must contain at least one 00.
When a Move Satisfies the Rule
Text solution
Starts that still need 00Starting with 1 (using 1 slot) or 01 (using 2 slots) has not produced 00 yet, so the remaining slots must still contain at least one 00.
Start that satisfies the ruleStarting with 00 completes the requirement right away, leaving 2 unrestricted choices (0 or 1) for each of the remaining n−2 slots.
00no restriction left__⋯_⟶2n−2
Add the three casesCombining the two unfinished cases with the completed case gives the recurrence.
an=an−1+an−2+2n−2
Because the 00 start already satisfies the rule, its leftover is counted directly as 2n−2 instead of an−2.
Compare what happens to the remaining slots when a start still needs 00 versus when it already has 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 n is large enough to look back at earlier terms, so we pair it with initial conditions counted by hand for the smallest values of n.
Finding Initial Conditions
Text solution
Match the number of initial conditions to the furthest step backBoth an=2an−1+2an−2 and an=an−1+an−2+2n−2 reach 2 steps back to an−2, so the formulas start at n=3 and need a1 and a2.
Reaches back to an−k⟶find a1,a2,…,ak by hand
Count a1 and a2 for A,B,C strings with no AAAll 3 single letters work for n=1, and 8 of the 9 two-letter pairs work for n=2 (all except AA).
a1=3,a2=3×3−1=8
Count a1 and a2 for binary strings that must contain 00A 1-slot string cannot contain two zeros, and only 00 out of the 4 two-slot strings contains 00.
a1=0,a2=1
Plugging n=3 into an=an−1+an−2+2n−2 gives a3=1+0+21=3, matching the three valid strings 000,001, and 100.
Key concepts
To set up a recurrence for an, 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 (can−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 different starts use k spots and leave n−k spots that still obey the original rule, they contribute can−k (for strings from A,B,C with no AA, starts AB and AC give 2an−2).
When not to use an earlier term an−k
If the first k spots already satisfy the condition for the entire object, there is no restriction left on the remaining n−k spots, so count them directly instead of writing an−k (for binary strings that must contain 00, starting with 00 contributes 2n−2, not an−2).
Initial conditions
If the recurrence reaches back to an−k, count a1,a2,…,ak by hand and apply the recurrence for n≥k+1.
6
Practice
1
Let an be the number of ways to tile a 2×n board using 2×1 dominoes (which can be placed vertically or horizontally) and 2×2 square tiles. Set up a recurrence relation and initial conditions for an, and use them to find a5.
How many ways can you cover the first column using 1 column vs. 2 columns without leaving a trapped corner?
Cover 1 columnA single vertical 2×1 domino fills column 1 and leaves a clean 2×(n−1) board.
1-column start=an−1
Cover 2 columnsWe can fill the first two columns with two stacked horizontal 2×1 dominoes or with one 2×2 square tile, giving 2 ways to leave a clean 2×(n−2) board. (Watch out: do not count two side-by-side vertical dominoes here. Those start with a vertical domino in column 1 and are already counted inside an−1. Including them gives an=an−1+3an−2 and the wrong answer a5=33.)
2-column starts=2an−2
Recurrence and initial conditionsThe recurrence reaches 2 steps back, so we count a1 and a2 by hand (remembering to include the 2×2 square tile in a2; leaving it out gives a2=2 and a5=16).
an=an−1+2an−2for n≥3,a1=1,a2=3
Compute up to a5
a3a4a5=3+2(1)=5=5+2(3)=11=11+2(5)=21
2
Let an be the number of ways to tile a 3×n board using 3×1 trominoes, which can be oriented vertically (3×1) or horizontally (1×3). Set up a recurrence relation and initial conditions for an.
If you place a 1×3 horizontal tromino across the top-left, what is forced in the two rows underneath it?
Start with a vertical trominoPlacing one vertical 3×1 tromino in column 1 leaves a clean 3×(n−1) board.
Vertical start=an−1
Start with a horizontal trominoPlacing a horizontal 1×3 tromino across the top of columns 1,2, and 3 traps the two squares underneath it in column 1, forcing two more horizontal trominoes below it and leaving a clean 3×(n−3) board. (Watch out: even though this block uses 3 horizontal trominoes, all three are forced together as 1 single choice, so it contributes an−3, not 3an−3.)
Horizontal start=an−3
Recurrence and initial conditionsBecause the recurrence reaches 3 steps back to an−3, we need 3 initial conditions a1,a2,a3 (for n=1 and n=2, horizontal trominoes cannot fit, so only vertical trominoes work).
an=an−1+an−3for n≥4,a1=1,a2=1,a3=2
3
Let an be the number of ternary strings of length n (formed from the digits 0,1, and 2) that do not contain three consecutive zeros (000). Set up a recurrence relation and initial conditions for an.
A valid string starts with zero, one, or two 0's before its first non-zero digit. How many choices are there for that first non-zero digit?
Zero 0's at the startStarting with 1 or 2(2 choices) leaves n−1 clean slots.
Starts 1 or 2⟶2an−1
One or two 0's at the startStarting with 0 followed by a non-zero digit (01 or 02) leaves n−2 clean slots, and starting with 00 followed by a non-zero digit (001 or 002) leaves n−3 clean slots. (Watch out: unlike binary strings where "not 0" must be 1, here the non-zero digit has 2 choices, 1 or 2. Forgetting that factor of 2 gives the wrong recurrence an=2an−1+an−2+an−3.)
Starts 01,02⟶2an−2,Starts 001,002⟶2an−3
Recurrence and initial conditionsReaching back to an−3 requires a1,a2,a3; for n=3, only 000 out of the 33=27 strings is forbidden.
Let an be the number of strings of length n formed from the letters A,B, and C that must contain at least one pair of adjacent A's (AA). Set up a recurrence relation and initial conditions for an.
Which starts still need AA in the remaining slots, and what happens to the rule when the string starts with AA?
Starts that still need AAStarting with B or C(2 ways) leaves n−1 slots that still need AA, and starting with AB or AC(2 ways) stops the A from forming AA right away and leaves n−2 slots that still need AA.
Starts B,C⟶2an−1,Starts AB,AC⟶2an−2
Start that satisfies the ruleStarting with AA(1 way) fulfills the rule, leaving all 3 letters (A,B, or C) available for each of the remaining n−2 slots. (Watch out: do not write an−2 once AA is complete, and use base 3 for the 3 letters rather than carrying over 2n−2 from binary strings.)
Start AA⟶3n−2
Recurrence and initial conditions
an=2an−1+2an−2+3n−2for n≥3,a1=0,a2=1
5
A 1×n strip is tiled using red 1×1 squares, blue 1×1 squares, and white 1×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.
If the first tile is a red 1×1 square, what are all the valid tiles that can come immediately after it?
Start with a blue square or white dominoA blue 1×1 square leaves a clean 1×(n−1) strip (an−1), and a white 1×2 domino leaves a clean 1×(n−2) strip (an−2).
Blue start=an−1,White start=an−2
Start with a red squareThe tile right after a red 1×1 square cannot be red, so it must be either a blue 1×1 square (using 2 spots total and leaving n−2) or a white 1×2 domino (using 3 spots total and leaving n−3). (Watch out: overlooking the white 1×2 domino after the red square drops the an−3 term and gives the wrong recurrence an=an−1+2an−2.)
Red then Blue=an−2,Red then White=an−3
Recurrence and initial conditionsAdding the four cases reaches 3 steps back, so we count a1,a2,a3 by hand.