Recursion and Structural Induction: Mukulika Ghosh Fall 2018
Recursion and Structural Induction: Mukulika Ghosh Fall 2018
Mukulika Ghosh
Fall 2018
Recursive or Inductive step: Give a rule for finding its value at an integer
from its values at smaller integers.
Examples
Factorial
We can define the factorial function n! as follows:
Base step: 0! = 1
Recursive step: n! = n(n − 1)!
Fibonacci Numbers
The Fibonacci numbers fn are defined as follows:
Base step: f0 = 0 and f1 = 1
Recursive step: fn = fn−1 + fn−2 for n ≥ 2
One can define recursively defined functions for domains other than the
non-negative integers.
Exercise
Let F be the function such that F (n) is the sum of the first n positive
integers. Give a recursive definition of F (n).
Example:
Natural numbers {0, 1, 2, ...} can be defined as:
Basis: 0 ∈ N
Recursion: If n ∈ N then n + 1 ∈ N
Example
Consider the set {0, 1, 3, 5, 7, ...} can represent either set of odd primes or
set of odd integers based on the contents so far.
Another Example
Basis: 0 ∈ S
Recursion: If n ∈ S, then 2n + 1 ∈ S.
Exercise
Applications: Lists
We can define the set L of finite lists of integers as follows.
Basis: <>∈ B.
Recursion: If L, R ∈ B and x ∈ A, then < L, x, R >∈ B.
Structural Induction
Structural induction asserts a property about elements of an inductively
defined set. The proof method directly exploits the inductive definition of
the set.
The method is more powerful than strong induction in the sense that
one can prove statements that are difficult (or impossible) to prove with
strong induction. Typically, though, it is simply used because it is more
convenient than (strong) induction.
Basis: Every element in the basis of the definition of S satisfies the property
P.
We can now prove that every binary tree has a property P by arguing that
Basis: P (<>) is true.
Induction: For all binary trees L and R and x ∈ A, if P (L) and P (R),
then P (< L, x, R >)
Proof
Basis: The empty tree has no leaves, so f (<>) = 0 is correct.
Induction: Let L, R be trees in B, x ∈ A.
Induction Hypothesis: Suppose that f (L) and f (R) denote the number
of leaves of L and R, respectively.
If L = R =<>, then < L, x, R >=<<>, x, <>> has one leaf, namely
x, so f (< L, x, R >) = 1 is correct.
If L and R are not both empty, then the number of leaves of the tree
< L, x, R > is equal to the number of leaves of L and R. Hence, by
induction hypothesis, we get
f (< L, x, R >) = f (L) + f (R) as claimed.
Recursive Definition
Basis: There is a complete binary tree consisting of a single vertex r
Recursion: If T 1 and T 2 are disjoint complete binary trees and r ∈ A is a
node, then < T 1, r, T 2 > is a complete binary tree with root r and left
subtree T 1 and right subtree T 2.
The difference between binary trees and complete binary trees is in the
basis step.
Recursion: If L and R are complete binary trees and r ∈ A, then the tree
< L, r, R > has height
h(< L, r, R >) = 1 + max(h(L), h(R))
Let n(T ) denote the number of nodes of a complete binary tree over an
alphabet A. Then
Proof:
Basis step: For r in A, we have n(r) = 1 and h(r) = 0, therefore, we
have n(r) = 1 ≤ 2(0+1) − 1 = 2h(r)+1 − 1, as claimed.
Inductive step: Suppose that L and R are complete binary trees that
satisfy n(L) ≤ 2h(L)+1 − 1 and n(R) ≤ 2h(R)+1 − 1.
Then the tree T =< L, r, R > satisfies:
Exercise
Let l(T ) be the number of leaves of a binary tree T , and i(T ), the number
of internal vertices of T .
Use structural induction to show that l(T ) = i(T )+1 holds for all complete
binary trees.