You are on page 1of 26

Recursion and Structural Induction

Mukulika Ghosh

Fall 2018

Based on slides by Dr. Hyunyoung Lee


Recursively Defined Functions

Recursively Defined Functions


Suppose we have a function with the set of non-negative integers as its
domain.

We can specify the function as follows:

Basis step: Specify the value of the function at 0.

Recursive or Inductive step: Give a rule for finding its value at an integer
from its values at smaller integers.

This is called a recursive or inductive definition.

Mukulika Ghosh | Parasol Lab - Texas A&M University 2/26


Recursively Defined Functions

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

Mukulika Ghosh | Parasol Lab - Texas A&M University 3/26


Recursively Defined Functions

One can define recursively defined functions for domains other than the
non-negative integers.

In general, a function f is called recursively defined if and only if at least


one value f (x) is defined in terms of another value f (y), where x and y
are distinct elements.

Mukulika Ghosh | Parasol Lab - Texas A&M University 4/26


Recursively Defined Functions

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).

Mukulika Ghosh | Parasol Lab - Texas A&M University 5/26


Recursively Defined Sets

Recursively Defined Sets


An recursive definition of a set S has the following form:

1. Basis: Specify one or more initial elements of S.

2. Recursion/Induction: Give one or more rules for constructing new


elements of S from old elements of S.

3. Closure: The set S consists of exactly the elements that can be


obtained by starting with the initial elements of S and applying the
rules for constructing new elements of S.

Mukulika Ghosh | Parasol Lab - Texas A&M University 6/26


Recursively Defined Sets

The definition is similar to steps taken inductions.

The closure condition is usually omitted, since it is always assumed in


recursive definitions.

Example:
Natural numbers {0, 1, 2, ...} can be defined as:
Basis: 0 ∈ N
Recursion: If n ∈ N then n + 1 ∈ N

Mukulika Ghosh | Parasol Lab - Texas A&M University 7/26


Why Recursive Definition

Why Recursive Definition


Recursive definition is popular in computer science. With recursive defini-
tion, there is no ambiguities about the contents of a set.

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.

But a recursive definition: 0 ∈ S as basis and recursion as n + 2 ∈ S when


n ∈ S remove such ambiguities.

Mukulika Ghosh | Parasol Lab - Texas A&M University 8/26


Another Example

Another Example

Let S be the set defined as follows:

Basis: 0 ∈ S

Recursion: If n ∈ S, then 2n + 1 ∈ S.

The set can also be described as,


S = {0, 1, 3, 7, 15, 31, ...} = {2n − 1| n is a non-negative integer }
since 20 − 1 = 0 and 2n+1 − 1 = 2(2n − 1) + 1

Mukulika Ghosh | Parasol Lab - Texas A&M University 9/26


Exercise

Exercise

Give the recursive definition of the sequence {an }, n = 1, 2, 3, ...


if an = n2

Mukulika Ghosh | Parasol Lab - Texas A&M University 10/26


Applications: Well-formed Formulas

Applications: Well-formed Formulas


We can define the set of well-formed formulas consisting of variables,
numerals, and operators from the set {+, −, ∗, /} as follows:

Basis: x is a well-formed formula if x is a numeral or a variable.

Recursion: If F and G are well-formed formulas, then (F + G), (F −


G), (F ∗ G), and (F/G) are well-formed formulas.

Examples: 42, x, (x + 42), (x − y), (3/0), (x ∗ (y + z))

Application: Definition of grammars, logic etc.

Mukulika Ghosh | Parasol Lab - Texas A&M University 11/26


Applications: Lists

Applications: Lists
We can define the set L of finite lists of integers as follows.

Basis: The empty list () is contained in L

Recursion: If i is an integer, and l is a list in L, then (cons i l) is in L.


Note: This is the Lisp style of lists, where (cons i l) prepends the data
item i at the front of the list l

Example: (cons 1 (cons 2 (cons 3 () ))) is the list (1 2 3) in Lisp.

Application: Data structure

Mukulika Ghosh | Parasol Lab - Texas A&M University 12/26


Applications: Binary Trees

Applications: Binary Trees


We can define the set B of binary trees over an alphabet A as follows:

Basis: <>∈ B.
Recursion: If L, R ∈ B and x ∈ A, then < L, x, R >∈ B.

Example: <<>, 1, <>> is a tree with one node (1)


Example: <<<>, 1, <>>, r, <<>, 2, <>>> is tree with root r and two
children (1 and 2).

Application: Data structure

Mukulika Ghosh | Parasol Lab - Texas A&M University 13/26


Structural Induction

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.

Mukulika Ghosh | Parasol Lab - Texas A&M University 14/26


Structural Induction

In structural induction, the proof of the assertion that every element of an


inductively defined set S has a certain property P proceeds by showing
that:

Basis: Every element in the basis of the definition of S satisfies the property
P.

Induction: Assuming that every argument of a constructor has property


P , show that the constructed element has the property P .

Mukulika Ghosh | Parasol Lab - Texas A&M University 15/26


Example: Binary Trees

Example: Binary Trees

The set B of binary trees over an alphabet A is defined as follows:


Basis: <>∈ B.
Recursion: If L, R ∈ B and x ∈ A, then < L, x, R >∈ B

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 >)

Mukulika Ghosh | Parasol Lab - Texas A&M University 16/26


Example: Binary Trees

Let f : B → N be a function defined by:


f (<>) = 0 
1, if L = R =<>
f (< L, x, R >) =
f (L) + f (R), otherwise

Let T in B be a binary tree. Then f (T ) yields the number of leaves of T .

Mukulika Ghosh | Parasol Lab - Texas A&M University 17/26


Example: Binary Trees

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.

Mukulika Ghosh | Parasol Lab - Texas A&M University 18/26


Example: Complete Binary Trees

Example: Complete Binary Trees


A binary tree is complete if and only if each node is either a leaf or has
precisely two children.

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.

Mukulika Ghosh | Parasol Lab - Texas A&M University 19/26


Example: Complete Binary Trees

Mukulika Ghosh | Parasol Lab - Texas A&M University 20/26


Height of Complete Binary Tree

Height of Complete Binary Tree

Let T be a complete binary tree over an alphabet A. We define the height


h(T ) of a complete binary tree as follows:

Basis: For r ∈ A, we define h(r) = 0; that is, the height of a complete


binary tree with just a single node is 0.

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))

Mukulika Ghosh | Parasol Lab - Texas A&M University 21/26


Number of Nodes in Complete Binary Tree

Number of Nodes in Complete Binary Tree

Let n(T ) denote the number of nodes of a complete binary tree over an
alphabet A. Then

Basis: For r ∈ A, we have n(r) = 1.

Recursion: If L and R are complete binary trees and r ∈ A, then the


number of nodes of < L, r, R > is given by n(< L, r, R >) = 1 + n(L) +
n(R)

Mukulika Ghosh | Parasol Lab - Texas A&M University 22/26


Example of Structural Induction

Example of Structural Induction

Let T be a complete binary tree over an alphabet A. Then we have


n(T ) ≤ 2h(T )+1 − 1.

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.

Mukulika Ghosh | Parasol Lab - Texas A&M University 23/26


Example of Structural Induction

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:

n(T ) = 1 + n(L) + n(R)


≤ 1 + 2h(L)+1 − 1 + 2h(R)+1 − 1, by Induction Hypothesis
≤ 2max(2h(L)+1 , 2h(R)+1 ) − 1, since a + b ≤ 2max(a, b)
= 2.2max(h(L),h(R))+1 − 1 = 2.2h(T ) − 1 = 2h(T )+1 − 1

Mukulika Ghosh | Parasol Lab - Texas A&M University 24/26


Complete Binary Tree: Internal Vertices and Leaves

Complete Binary Tree: Internal Vertices and Leaves


The set of leaves and the set of internal vertices of
a complete binary tree is defined recursively:
Base: The root r is a leaf of the complete binary
tree with exactly one vertex r. This tree has no
internal vertices.
Recursion: The set of leaves of the tree T =<
L, r, R > is the union of the sets of leaves of L and
of R.
The internal vertices of T are the root r of T and
the union of the set of internal vertices of L and the
set of internal vertices of R.

Mukulika Ghosh | Parasol Lab - Texas A&M University 25/26


Exercise

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.

Mukulika Ghosh | Parasol Lab - Texas A&M University 26/26

You might also like