π Lecture 21 β Recursion I
π Overview: This lecture introduces the fundamental concept of recursion, where an object is defined in terms of smaller versions of itself. It explains the structure of recursive definitions (base and recursion), demonstrates how to evaluate recursively defined functions, and explores key applications including the factorial function, Fibonacci sequence, and the Tower of Hanoi puzzle. Understanding recursion is critical for computer science as it provides elegant solutions to many algorithmic problems.
ποΈ Topics Covered
The lecture covers the definition and structure of recursion with examples like odd positive integers, evaluation of recursively defined functions using base values and recurrence relations, the factorial function defined recursively, sum of first n integers, a quotient-finding recursive function, the Fibonacci sequence, recurrence relations with initial conditions, verifying sequences satisfy recurrence relations, and the Tower of Hanoi puzzle as a classic recursive problem.
π Lecture Summary
Recursion
First, instead of giving the definition of Recursion, we give you an example. You already know the Set of Odd numbers. Here we give the new definition of the same set that is the set of Odd numbers.
Definition for odd positive integers may be given as:
BASE: 1 is an odd positive integer.
RECURSION: If k is an odd positive integer, then k + 2 is an odd positive integer.
Now, 1 is an odd positive integer by the definition base. With k = 1, 1 + 2 = 3, so 3 is an odd positive integer. With k = 3, 3 + 2 = 5, so 5 is an odd positive integer and so, 7, 9, 11, ... are odd positive integers.
REMARK: Recursive definitions can be used in a "generative" manner.
RECURSION: The process of defining an object in terms of smaller versions of itself is called recursion. A recursive definition has two parts:
- BASE: An initial simple definition which cannot be expressed in terms of smaller versions of itself.
- RECURSION: The part of definition which can be expressed in terms of smaller versions of itself.
Recursively Defined Functions
A function is said to be recursively defined if the function refers to itself such that:
- There are certain arguments, called base values, for which the function does not refer to itself.
- Each time the function does refer to itself, the argument of the function must be closer to a base value.
EXAMPLE: Suppose that f is defined recursively by f(0) = 3 and f(n + 1) = 2 f(n) + 3. Find f(1), f(2), f(3) and f(4).
SOLUTION: From the recursive definition it follows that f(1) = 2 f(0) + 3 = 2(3) + 3 = 6 + 3 = 9. In evaluating f(1) we use the formula given in the example and we note that it involves f(0) and we are also given the value of that which we use to find out the functional value at 1. Similarly we will use the preceding value in evaluating the next values of the functions as we did below:
- f(2) = 2 f(1) + 3 = 2(9) + 3 = 18 + 3 = 21
- f(3) = 2 f(2) + 3 = 2(21) + 3 = 42 + 3 = 45
- f(4) = 2 f(3) + 3 = 2(45) + 3 = 90 + 3 = 93
EXERCISE: Find f(2), f(3), and f(4) if f is defined recursively by f(0) = -1, f(1)=2 and for n = 1, 2, 3, ... f(n+1) = f(n) + 3 f(n - 1).
SOLUTION: From the recursive definition it follows that:
- f(2) = f(1) + 3 f(1-1) = f(1) + 3 f(0) = 2 + 3(-1) = -1
- f(3) = f(2) + 3 f(2-1) = f(2) + 3 f(1) = (-1) + 3(2) = 5
- f(4) = f(3) + 3 f(3-1) = f(2) + 3 f(2) = 5 + 3(-1) = 2
The Factorial of a Positive Integer
For each positive integer n, the factorial of n denoted n! is defined to be the product of all the integers from 1 to n: n! = nΒ·(n-1)Β·(n-2)Β·...Β·3Β·2Β·1. Zero factorial is defined to be 1: 0! = 1.
EXAMPLE: 0! = 1, 1! = 1, 2! = 2Β·1 = 2, 3! = 3Β·2Β·1 = 6, 4! = 4Β·3Β·2Β·1 = 24, 5! = 5Β·4Β·3Β·2Β·1 = 120, 6! = 6Β·5Β·4Β·3Β·2Β·1 = 720, 7! = 7Β·6Β·5Β·4Β·3Β·2Β·1 = 5040.
REMARK: 5! = 5Β·4Β·3Β·2Β·1 = 5Β·(4Β·3Β·2Β·1) = 5Β·4!. In general, n! = n(n-1)! for each positive integer n.
The Factorial Function Defined Recursively
We can define the factorial function F(n) = n! recursively by specifying the initial value of this function, namely, F(0) = 1, and giving a rule for finding F(n) from F(n-1). {(n! = n(n-1)!)} Thus, the recursive definition of factorial function F(n) is:
- F(0) = 1
- F(n) = n F(n-1)
EXERCISE: Let S be the function such that S(n) is the sum of the first n positive integers. Give a recursive definition of S(n).
SOLUTION: The initial value of this function may be specified as S(0) = 0. Since S(n) = n + (n-1) + (n-2) + ... + 3 + 2 + 1 = n + [(n-1) + (n-2) + ... + 3 + 2 + 1] = n + S(n-1), which defines the recursive step. Accordingly S may be defined as:
- S(0) = 0
- S(n) = n + S(n-1) for n β₯ 1
EXERCISE: Let a and b denote positive integers. Suppose a function Q is defined recursively as follows: Q(a, b) = {0 if a < b; Q(a-b, b) + 1 if b β€ a}. Find the value of Q(2,3) and Q(14,3). What does this function do? Find Q(3355, 7).
SOLUTION: (a) Q(2,3) = 0 since 2 < 3. For Q(14,3): Q(14,3) = Q(11,3) + 1 = [Q(8,3) + 1] + 1 = Q(8,3) + 2 = [Q(5,3) + 1] + 2 = Q(5,3) + 3 = [Q(2,3) + 1] + 3 = Q(2,3) + 4 = 0 + 4 = 4.
(b) Each time b is subtracted from a, the value of Q is increased by 1. Hence Q(a,b) finds the integer quotient when a is divided by b. Thus Q(3355, 7) = 479.
π Definition β Integer Quotient: The number of times one integer (b) can be subtracted from another (a) before the remainder becomes less than b; equivalently, the result of integer division a Γ· b.
The Fibonacci Sequence
The Fibonacci sequence is defined as follows:
- Fβ = 1, Fβ = 1
- Fβ = Fβββ + Fβββ for all integers k β₯ 2
Fβ = Fβ + Fβ = 1 + 1 = 2; Fβ = Fβ + Fβ = 2 + 1 = 3; Fβ = Fβ + Fβ = 3 + 2 = 5; Fβ = Fβ + Fβ = 5 + 3 = 8; and so on.
π‘ Why this matters: The Fibonacci sequence appears in nature, art, and algorithm analysis, making it a fundamental recursive pattern.
Recurrence Relation
A recurrence relation for a sequence aβ, aβ, aβ, ..., is a formula that relates each term aβ to certain of its predecessors aβββ, aβββ, ..., aββα΅’, where i is a fixed integer and k is any integer greater than or equal to i. The initial conditions for such a recurrence relation specify the values of aβ, aβ, aβ, ..., aα΅’ββ.
EXERCISE: Find the first four terms of the following recursively defined sequence: bβ = 2; bβ = bβββ + 2Β·k, for all integers k β₯ 2.
SOLUTION: bβ = 2 (given in base step); bβ = bβ + 2Β·2 = 2 + 4 = 6; bβ = bβ + 2Β·3 = 6 + 6 = 12; bβ = bβ + 2Β·4 = 12 + 8 = 20.
EXERCISE: Find the first five terms of the following recursively defined sequence: tβ = -1, tβ = 1; tβ = tβββ + 2Β·tβββ, for all integers k β₯ 2.
SOLUTION: tβ = -1, tβ = 1 (given in base step); tβ = tβ + 2Β·tβ = 1 + 2Β·(-1) = 1 - 2 = -1; tβ = tβ + 2Β·tβ = -1 + 2Β·1 = -1 + 2 = 1; tβ = tβ + 2Β·tβ = 1 + 2Β·(-1) = 1 - 2 = -1.
EXERCISE: Define a sequence bβ, bβ, bβ, ... by the formula bβ = 5βΏ, for all integers n β₯ 0. Show that this sequence satisfies the recurrence relation bβ = 5bβββ, for all integers k β₯ 1.
SOLUTION: The sequence is given by the formula bβ = 5βΏ. Substituting k for n we get bβ = 5α΅ (1). Substituting k-1 for n we get bβββ = 5α΅β»ΒΉ (2). Multiplying both sides of (2) by 5 we obtain 5Β·bβββ = 5Β·5α΅β»ΒΉ = 5α΅ = bβ using (1). Hence bβ = 5bβββ as required.
EXERCISE: Show that the sequence 0, 1, 3, 7, ..., 2βΏ - 1, ..., for n β₯ 0, satisfies the recurrence relation dβ = 3dβββ - 2dβββ, for all integers k β₯ 2.
SOLUTION: The sequence is given by the formula dβ = 2βΏ - 1 for n β₯ 0. Substituting k-1 for n we get dβββ = 2α΅β»ΒΉ - 1. Substituting k-2 for n we get dβββ = 2α΅β»Β² - 1. We want to prove that dβ = 3dβββ - 2dβββ. R.H.S. = 3(2α΅β»ΒΉ - 1) - 2(2α΅β»Β² - 1) = 3Β·2α΅β»ΒΉ - 3 - 2Β·2α΅β»Β² + 2 = 3Β·2α΅β»ΒΉ - 2α΅β»ΒΉ - 1 = (3-1)Β·2α΅β»ΒΉ - 1 = 2Β·2α΅β»ΒΉ - 1 = 2α΅ - 1 = dβ = L.H.S.
The Tower of Hanoi
The puzzle was invented by a French Mathematician Edouard Lucas in 1883. It is well known to students of Computer Science since it appears in virtually any introductory text on data structures or algorithms. There are three poles on first of which are stacked a number of disks that decrease in size as they rise from the base. The goal is to transfer all the disks one by one from the first pole to one of the others, but they must never place a larger disk on top of a smaller one.
Let mβ be the minimum number of moves needed to move a tower of n disks from one pole to another. Then mβ can be obtained recursively as follows:
- mβ = 1
- mβ = 2mβββ + 1
mβ = 2Β·mβ + 1 = 2Β·1 + 1 = 3; mβ = 2Β·mβ + 1 = 2Β·3 + 1 = 7; mβ = 2Β·mβ + 1 = 2Β·7 + 1 = 15; mβ = 2Β·mβ + 1 = 2Β·15 + 1 = 31; mβ = 2Β·mβ + 1 = 2Β·31 + 1 = 65.
Note that mβ = 2βΏ - 1. For 64 disks, mββ = 2βΆβ΄ - 1 = 18,446,744,073,709,551,615 moves = 1.844 Γ 10ΒΉβΉ moves.
Use of Recursion
At first recursion may seem hard or impossible, may be magical at best. However, recursion often provides elegant, short algorithmic solutions to many problems in computer science and mathematics.
Examples where recursion is often used:
- math functions
- number sequences
- data structure definitions
- data structure manipulations
- language definitions
π Formula: Tower of Hanoi β mβ = 2βΏ - 1 β The minimum number of moves required to transfer n disks from one pole to another, following the rule that no larger disk may ever be placed on top of a smaller disk.
π Example: For 5 disks, mβ = 2β΅ - 1 = 32 - 1 = 31 moves.
β Key Takeaways
A recursive definition must always have a base case (simple definition that cannot be expressed in smaller terms) and a recursive part (definition in terms of smaller versions of itself). To evaluate recursively defined functions, start from the given base values and work forward, each time substituting previous results into the recurrence formula. The factorial function is defined recursively as F(0)=1 and F(n)=nF(n-1), and the Fibonacci sequence requires two base values (Fβ=1, Fβ=1) with the recurrence Fβ=Fβββ+Fβββ. A recurrence relation relates each term to its predecessors, and initial conditions provide the starting values needed to generate the sequence. The Tower of Hanoi puzzle demonstrates that the minimum number of moves for n disks follows the recurrence mβ=2mβββ+1 with closed form mβ=2βΏ-1.
π§ Quick Revision Questions
- What are the two essential parts of any recursive definition? Provide an example from the lecture.
- If f is defined recursively by f(0)=3 and f(n+1)=2f(n)+3, what is f(3)?
- Write the recursive definition of the factorial function and use it to compute 4!.
- What is the recurrence relation for the Fibonacci sequence? List the first 6 terms starting from Fβ.
- For the Tower of Hanoi puzzle with 6 disks, what is the minimum number of moves required? What is the closed-form formula for mβ?
π Lecture 22 β Recursion II
π Overview: This lecture continues the study of recursive definitions by applying them to various mathematical structures including Boolean expressions, strings, parentheses, and arithmetic expressions. The key insight is that recursive definitions provide a systematic way to build complex objects from simpler ones using base cases and recursion rules, which is essential for understanding formal languages and computational structures.
ποΈ Topics Covered
This lecture formally defines recursive definitions with BASE, RECURSION, and RESTRICTION rules, then applies this framework to Boolean expressions, strings over an alphabet, parenthesis structures, arithmetic expressions, and recursive definitions of summation, union, and intersection of sets. Each topic includes derivations showing how specific examples can be constructed using the recursive rules.
π Lecture Summary
Recursive Definition Structure
A recursive definition for a set consists of three rules: I. BASE β a statement that certain objects belong to the set. II. RECURSION β a collection of rules indicating how to form new set objects from those already known to be in the set. III. RESTRICTION β a statement that no objects belong to the set other than those coming from I and II.
EXERCISE: Let S be a set defined recursively by I. BASE: 5 β S. II. RECURSION: If x β S and y β S, then x + y β S. III. RESTRICTION: S contains no elements other than those obtained from rules I and II. Show that S is the subset of all positive integers divisible by 5.
SOLUTION: Let A be the set of all positive integers divisible by 5. Then A = {5n | n β N}. We need to prove that S β A. 5 is divisible by 5 since 5 = 5 Γ 1 β 5 β A. Now consider x β A and y β A, we show that x + y β A. x β A β 5 | x so that x = 5 Β· p for some p β N. y β A β 5 | y so that y = 5 Β· q for some q β N. Hence x + y = 5 Β· p + 5 Β· q = 5 Β· (p + q) β 5 | (x + y) and so (x + y) β A. Thus, S is a subset of A.
RECURSIVE DEFINITION OF BOOLEAN EXPRESSIONS
I. BASE: Each symbol of the alphabet is a Boolean expression. II. RECURSION: If P and Q are Boolean Expressions, then so are (a) (P β§ Q) (b) (P β¨ Q) and (c) ~ P. III. RESTRICTION: There are no Boolean expressions over the alphabet other than those obtained from I and II.
EXERCISE: Show that ((p β¨ q) β¨ ~ ((p β§ ~ s) β§ r)) is a Boolean expression over the English alphabet.
SOLUTION: (1) p, q, r, and s are Boolean expressions by I. (2) ~ s is a Boolean expression by (1) and II(c). (3) (p β§ ~ s) is a Boolean expression by (1), (2) and II(a). (4) ((p β§ ~ s) β§ r) is a Boolean expression by (1), (3) and II(a). (5) ~ ((p β§ ~ s) β§ r) is a Boolean expression by (4) and II(c). (6) (p β¨ q) is a Boolean expression by (1) and II(b). (7) ((p β¨ q) β¨ ~ ((p β§ ~ s) β§ r)) is a Boolean expression by (5), (6) and II(b).
RECURSIVE DEFINITION OF THE SET OF STRINGS OVER AN ALPHABET
Consider a finite alphabet Ξ£ = {a, b}. The set of all finite strings over Ξ£, denoted Ξ£*, is defined recursively as follows: I. BASE: Ξ΅ is in Ξ£*, where Ξ΅ is the null string. II. RECURSION: If s β Ξ£*, then (a) sa β Ξ£* and (b) sb β Ξ£*, where sa and sb are concatenations of s with a and b respectively. III. RESTRICTION: Nothing is in Ξ£* other than objects defined in I and II above.
EXERCISE: Give derivations showing that abb is in Ξ£*. SOLUTION: (1) Ξ΅ β Ξ£* by I. (2) a = Ξ΅a β Ξ£* by (1) and II(a). (3) ab β Ξ£* by (2) and II(b). (4) abb β Ξ£* by (3) and II(b).
EXERCISE: Give a recursive definition of all strings of 0βs and 1βs for which all the 0βs precede all the 1βs. SOLUTION: Let S be the set of all strings of 0βs and 1βs for which all the 0βs precede all the 1βs. The following is a recursive definition of S. I. BASE: The null string Ξ΅ β S. II. RECURSION: If s β S, then (a) 0s β S and (b) s1 β S. III. RESTRICTION: Nothing is in S other than objects defined in I and II above.
PARENTHESIS STRUCTURE
Let P be the set of grammatical configurations of parentheses. The following is a recursive definition of P. I. BASE: ( ) is in P. II. RECURSION: (a) If E is in P, so is (E). (b) If E and F are in P, so is EF. III. RESTRICTION: No configurations of parentheses are in P other than those derived from I and II above.
EXERCISE: Derive the fact that ( ( ( ) ) ( ) ) is in the set P of grammatical configuration of parentheses. SOLUTION: (1) ( ) is in P, by I. (2) ( ( ) ) is in P, by 1 and II(a). (3) ( ( ) ) ( ) is in P, by 2, I and II(b). (4) ( ( ( ) ) ( ) ) is in P, by 3, and II(a).
SET OF ARITHMETIC EXPRESSIONS
The set of arithmetic expressions over the real numbers can be defined recursively as follows. I. BASE: Each real number r is an arithmetic expression. II. RECURSION: If u and v are arithmetic expressions, then the following are also arithmetic expressions: a. (+ u) b. (β u) c. (u + v) d. (u β v) e. (u Β· v) f. (u/v) III. RESTRICTION: There are no arithmetic expressions other than those obtained from I and II above.
EXERCISE: Give derivations showing that ( (9Β·(6.1+2)) / ((4β7)Β·6) ) is an arithmetic expression. SOLUTION: (1) 9, 6.1, 2, 4, 7, and 6 are arithmetic expressions by I. (2) (6.1 + 2) is an arithmetic expression by (1) and II(c). (3) (9Β·(6.1+2)) is an arithmetic expression by (1), (2) and II(e). (4) (4 β 7) is an arithmetic expression by (1) and II(d). (5) ((4β7)Β·6) is an arithmetic expression by (1), (4) and II(e). (6) ( (9Β·(6.1+2)) / ((4β7)Β·6) ) is an arithmetic expression by (3), (5) and II(f).
RECURSIVE DEFINITION OF SUM
Given numbers aβ, aβ, ..., aβ, where n is a positive integer, the summation from i = 1 to n of the aα΅’, denoted Ξ£α΅’βββΏ aα΅’, is defined as follows: BASE: Ξ£α΅’ββΒΉ aα΅’ = aβ RECURSION: Ξ£α΅’βββΏ aα΅’ = (Ξ£α΅’βββΏβ»ΒΉ aα΅’) + aβ
RECURSIVE DEFINITION OF UNION OF SETS
Given sets Aβ, Aβ, ..., Aβ, where n is a positive integer, the union of Aα΅’ from i = 1 to n, denoted βͺα΅’βββΏ Aα΅’, is defined by: BASE: βͺα΅’ββΒΉ Aα΅’ = Aβ RECURSION: βͺα΅’βββΏ Aα΅’ = (βͺα΅’βββΏβ»ΒΉ Aα΅’) βͺ Aβ
RECURSIVE DEFINITION OF INTERSECTION OF SETS
Given sets Aβ, Aβ, ..., Aβ, where n is a positive integer, the intersection of Aα΅’ from i = 1 to n, denoted β©α΅’βββΏ Aα΅’, is defined by: BASE: β©α΅’ββΒΉ Aα΅’ = Aβ RECURSION: β©α΅’βββΏ Aα΅’ = (β©α΅’βββΏβ»ΒΉ Aα΅’) β© Aβ
β Key Takeaways
Every recursive definition requires three components: a base case that provides initial elements, recursion rules that build new elements from existing ones, and a restriction that excludes everything not generated by the first two rules. To verify that an expression belongs to a recursively defined set, always start with the innermost components using the base case, then apply recursion rules step by step from the inside out. The same recursive pattern β base case for the first element and recursion relating the nth case to the (n-1)th case β applies to operations like summation, union, and intersection. Recursive definitions are fundamental for specifying formal languages and mathematical structures precisely, and mastering them requires practice with derivations.
π§ Quick Revision Questions
- What are the three rules that must be included in any recursive definition of a set?
- How would you derive that the string "010" belongs to the set defined by BASE: Ξ΅ β Ξ£*, RECURSION: if s β Ξ£* then 0s β Ξ£* and 1s β Ξ£*?
- If S is defined recursively by BASE: 3 β S and RECURSION: if x, y β S then x+y β S, is 12 in S? Show your derivation.
- Using the recursive definition of Boolean expressions, is (p β§ (q β¨ ~r)) a valid Boolean expression? Show your steps.
- Write the recursive definition for the union of n sets, stating both the base and recursion cases.