Lecture 11: Recursion
- Introduction: Recursion in Computer Science
- Recursion
- Recursive Data Structures
- Recursive Programming
- Solving Recurrences
Introduction: Recursion in Computer Science
Fundamental concept in Computer Science
- Defining complex objects from simpler ones
- Unbounded complexity with a finite description
Recursive Data Structures:
Finite definitions of arbitrarily large objects
- Natural numbers
- Words
- Linked lists
- Formulas
- Binary trees
Recursive Algorithms:
Solving problems/calculations by reducing to smaller cases
- Factorial
- Euclidean gcd algorithm
- Towers of Hanoi
- Mergesort, Quicksort
Analysis of Recursion:
Reasoning about recursive objects
- Induction, Structural Induction
- Recursive sequences (e.g. Fibonacci sequence)
- Asymptotic analysis of recursive functions
Recursion
Consists of a basis (B) and recursive process (R).
A sequence/object/algorithm is recursively defined when (typically)
(B) some initial terms are specified, perhaps only the first one;
(R) later terms stated as functional expressions of the earlier
terms.
Take Notice
(R) also called recurrence formula (especially when dealing with sequences)
Example: Factorial
$Factorial$:
(B) $0! =1$
(R) $(n+1)! = (n+1)·n!$
$fact(n)$:
(B) $if(n = 0): 1$
(R) $else: n ∗ fact(n −1)$
TAKE NOTICE: $0! =1$
Example: Euclid’s gcd algorithm
Example: Towers of Hanoi
- There are 3 towers (pegs)
- n disks of decreasing size placed on the first tower
- You need to move all disks from the first tower to the last tower
- Larger disks cannot be placed on top of smaller disks
- The third tower can be used to temporarily hold disks
Questions:
- Describe a general solution for n disks
- How many moves does it take?
move 1 disks
One step obviously.
move 2 disks
Three steps obviously.
move 3 disks
We move 3 towers that takes 7 steps. —-(1)
move 4 disks
And if we need to move 4 towers, we move the top 3 towers into the middle pillar ((1) move to the second pillar rather not third one, but takes the same steps), 7 steps
then take 1 step to move the biggest disk to the third pillar —- (2)
then take 7 steps to the third pillar —- (3)
hence 4 disks take $7+1+7=15$ steps.
disks | steps |
---|---|
1 | 1 |
2 | 3 |
3 | 7 |
4 | 15 |
Hence the recursion formula is
Recursive Data Structures
Example: Natural numbers
A natural number is either 0 (B) or one more than a natural number (R).
Formal definition of N:
- (B) $0 ∈ \mathbb{N}$
- (R) If $n ∈ \mathbb{N} \quad then (n+1) ∈ \mathbb{N}$
Example: Odd/Even numbers
The set of even numbers can be defined as:
- (B) 0 is an even number
- (R) If $n$ is an even number then $n+2$ is an even number
The set of odd numbers can be defined as:
- (B) 1 is an odd number
- (R) If $n$ is an odd number then $n+2$ is an odd number
Example: Fibonacci numbers
The Fibonacci sequence starts 0, 1, 1, 2, 3, … where, after 0, 1, each term is the sum of the previous two terms.
Formally, the sequence of Fibonacci numbers: $F_0, F_1, F_2, \dots$ where the n-th Fibonacci number $F_n$ is defined as:
Take Notice
Could also define the Fibonacci sequence as a function
$FIB : \mathbb{N} \to \mathbb{F}.$
the $\mathbb{F}$ represents the set of Fibonacci numbers.
Example: Linked lists
A linked list is zero or more linked list nodes:
We can view the linked list structure abstractly. A linked list is either:
- (B) an empty list, or
- (R) an ordered pair (Data,List).
Example: Words over $Σ$
A word over an alphabet $Σ$ is either $λ(B)$ or a symbol from $Σ$ followed by a word (R).
Formal definition of $Σ^∗$:
- $(B) λ ∈ Σ^∗$
- $(R) \text{If } w ∈ Σ^∗ \text{then } aw ∈ Σ^∗ \text{ for all } a ∈ Σ$
Take Notice
This matches the recursive definition of a Linked List data type.
Example: Expressions in the Proof Assistant
(B) $A,B,…,Z,a,b,…z$ are expressions \
(B) $∅$ and $U$ are expressions \
(R) If $E$ is an expression then so is $(E)$ and $E^c$ \
(R) If $E_1$ and $E_2$ are expressions then:
Example: Propositional formulas
A well-formed formula (wff) over a set of propositional variables, $Prop$ is defined as:
(B) $⊤$ is a wff
(B) $⊥$ is a wff
(B) $p$ is a wff for all $p ∈ Prop$
(R) If $φ$ is a wff then $¬φ$ is a wff
(R) If $φ$ and $ψ$ are wffs then:
- $(φ ∧ψ)$,
- $(φ ∨ψ)$,
- $(φ →ψ)$, and
- $(φ ↔ψ)$ are wffs.
Exercises
(a) Give a recursive definition for the sequence
To generate $an = 2^{2^n}$ use $a_n = (a{n−1})^2$.
(The related “Fermat numbers” $F_n = 2^{2^n} + 1$ are used in cryptography.)
(b) Give a recursive definition for the sequence
To generate a “stack” of n 2’s use $bn = 2^{b{n−1}}$.
Recursive Programming
Programming over recursive datatypes
Recursive datatypes make recursive programming/functions easy.
The factorial function:
Summing the first n natural numbers:
Summing elements of a linked list:
Sorting elements of a linked list (insertion sort):
Concatenation of words (defining wv):
For all $w,v ∈ Σ^∗$ and $a ∈ Σ$ :
(B) $λv =v$
(R) $(aw)v = a(wv)$
Length of words:
(B) length(λ) = 0
(R) length(aw) = 1 + length(w)
Exercise
Let $Σ$ be a finite set.
Define append : $Σ^∗ \times Σ \to Σ^∗$ by
Give a (direct) definition of append [i.e. only concatenates symbols on the left].
For all $w ∈ Σ^∗$ and $a,x ∈ Σ$:
(B) append(λ, x) = x
(R) append(aw, x) = a append(w, x)
Pitfall: Correctness of Recursive Definition
A recurrence formula is correct if the computation of any later term can be reduced to the initial values given in (B).
Example (Incorrect definition)
Function g(n) is defined recursively by
The definition of $g(n)$ is incomplete — the recursion may not terminate:
Attempt to compute $g(1)$ gives
When implemented, it leads to an overflow; most static analyses cannot detect this kind of ill-defined recursion.
However, the definition could be repaired. For example, we can add the specification specify $g(1) = 2$.
Then
Example (Incorrect definition)
Check your base cases!
Function f (n) is defined by
When evaluated for n = 1 it leads to
This one can also be repaired. For example, one could specify that
$f(1) = 1$.
This would lead to a constant function $f(n) = 1$ for all $n ≥ 0$.
Mutual Recursion
Sometimes recursive definitions use more than one function, with each calling each other.
Example (Fibonacci, again)
Recall:
Alternative, mutually recursive definition:
Solving Recurrences
Question: How can we (asymptotically) compare recursively defined functions?
Some practical approaches:
- Unwinding the recurrence
- Approximating with big-O
- The Master Theorem
Take Notice
Each approach gives an informal “solution”: ideally one should prove a solution is correct (using e.g. induction).
Example 1 (Unwinding)
Unwinding:
Example 2 (unwinding)
Unwinding:
Take notice! The answer above may be wrong, because my proof is here:
Example 3 (Approximating with big-O)
Note, the section is approximate big-O. If you want to unwind the fabonacci sequence, refer to unwind-recursion-formula.
Assuming $f(n)$ is increasing (The step is only to compare f(n)’s growth, to get its formula until a constant):
so
so (by unwinding):
so:
Master Theorem
The following result covers many recurrences that arise in practice (e.g. divide-and-conquer algorithms)
Let $a ≥ 1$ be an integer, $b > 1$ a real number, and $T(n)$ be the following recurrence
where $f(n) ∈ Θ(n^c(log \text{ }n)^k)$.
By denoting $d = log_b(a)$, then we have:
- Case 1: If $c < d$ then $T(n) = Θ(n^d)$
- Case 2: If $c = d$ then $T(n) = Θ(n^c(logn)^{k+1})$
- Case 3: If $c > d$ then $T(n) = Θ(f(n))$
It’s a little complicated to understand, please refer to wiki: Master theorem).
Example 1
Here a = 1, b = 2, c = 2, k = 0 and d = 0. So we have Case 3 and the solution is
Example 2
Mergesort has
for the number of comparisons.
Here a = b = 2, c = 1, k = 0 and d = 1. So we have Case 2, and the solution is
Example 3 wind
Unwinding example:
Here a = 1, b = 2, c = 0, k = 0, and d = 0. So we have Case 2, and the solution is
The Master Theorem: Pitfalls
Take Notice
- a, b, c, k have to be constants (not dependent on n).
- Only one recursive term.
- Recursive term is of the form $T(n/b)$, not $T(n − b)$.
- Solution is only an asymptotic bound.
Examples
The Master theorem does not apply to any of these:
First equation, the a $(2^n)$ is related to n; Second there are two T terms; Third the item is n-1 rather not n/?.
The Master Theorem: Linear differences
Take Notice
The Master Theorem applies to recurrences where T(n) is defined in terms of T(n/b); not in terms of T(n −1).
However, the following is a consequence of the Master Theorem:
Theorem
Suppose
Then
exercise
Solve $T(n) = 3^nT(\frac{n}{2})$ with $T(1) = 1$
Let $n ≥ 2$ be a power of 2 then
Welcome to point out the mistakes and faults!