Recursive Definitions and Structural Induction
Recursive Definitions and Structural Induction
Recursion
Recursion means defining something,
such as a function, in terms of itself
Recursion example
Rosen, section 3.4, question 1
a)
b)
Recursion example
Rosen, section 3.4, question 1
c)
d)
Fractals
A fractal is a pattern that uses recursion
Fractals
Fibonacci sequence
Definition of the Fibonacci sequence
Non-recursive:
1 5 1 5
F ( n)
Recursive:
or:
5 2n
F(1) = 1, F(2) = 1
Note that some will use F(0) = 1, F(1) = 1
7
Recursion definition
From The Hackers Dictionary:
recursion n. See recursion.
tail recursion.
See also
f(0) = 1
f(n) = 1 + f(n-2)
What is f(1)?
Consider:
f(0) = 1
f(n) = 1+f(-n)
What is f(1)?
10
Basis step: 1 S
Recursive step: if x S, then x+1 S
11
b)
c)
1S
If x S, then x+2 S
3S
If x S, then 3*x S
0S
If p(x) S, then p(x) + cxn S
c Z, n Z and n 0
12
Base step: *
If w * and x , then wx *
Thus, * s the set of all the possible strings that can
be generated with the alphabet
Is this countably infinite or uncountably infinite?
13
14
Example: l(aaa)
l(aaa) = l(aa) + 1
l(aa) = l(a) + 1
l(a) = l() + 1
l() = 0
Result: 3
15
Todays demotivators
16
Basis step: P
18
Recursion pros
Easy to program
Easy to understand
19
Recursion cons
Consider the recursive Fibonacci generator
How many recursive calls does it make?
F(1): 1
F(2): 1
F(3): 3
F(4): 5
F(5): 9
F(10): 109
F(20): 13,529
F(30): 1,664,079
F(40): 204,668,309
F(50): 25,172,538,049
F(100): 708,449,696,358,523,830,149 7 * 1020
At 1 billion recursive calls per second (generous), this would take over
22,000 years
But that would also take well over 1012 Gb of memory!
20
Trees
Rooted trees:
21
Rooted trees
Recursive definition:
22
23
27
the
recursive
definition
f(0) = 1
Base case
f(n) = n * f(n-1)
The step
for
{ 3, 6, 9, 12, 15, }
{ x | x = 3k and k Z+ }
Recursive definition:
Basis step: 3 S
Recursive step: If x S and y S, then x+y S
29
Two parts:
Show that S A
Let P(n) = 3n S
Base case: P(1) = 3*1 S
3*(k+1) = 3k+3
3k S by the inductive hypothesis
3 S by the base case
Thus, 3k+3 S by the recursive definition
Show that A S
30
i.e., 3k+3 S
Structural induction
A more convenient form of induction for
recursively defined things
Used in conjunction with the recursive definition
Three parts:
h(T) = 0
n(T) = 1
n(T) 2h(T) + 1
1 2*0 + 1
11
34
Recursive step:
Let T = T1 T2
Here the operator means creating a new tree with a root note r and subtrees
T1 and T2
New element is T
Therefore:
n(T) = 1 + n(T1) + n(T2)
1 + 2h(T1) + 1 + 2h(T2) + 1
1 + 2*max ( h(T1), h(T2) ) the sum of two non-neg #s is at least
as large as the larger of the two
= 1 + 2*h(T)
35
Where x and w *
Note that x is a bit: either 0 or 1
36
Where s, t * and x
New element is ones(stx)
ones (stx) = ones ((st)x))
= x+ones(st)
= x + ones(s) + ones(t)
= ones(s) + (x + ones(t))
= ones(s) + ones(tx)
Proven!
by associativity of concatenation
by recursive definition
by inductive hypothesis
by commutativity and assoc. of +
by recursive definition
37
Quick survey
a)
b)
c)
d)
38
Human stupidity
39
Used for
Assumption
Weak
mathematical
Strong
Mathematical
Usually
formulae
Assume P(k)
What to
prove
True for
P(k+1)
Step 1
called
Base case
Base case
Step 3
called
Inductive step
Inductive step
Structural
Only things defined
via recursion
Assume statement is
true for some "old"
elements
Statement is true for
some "new" elements
created with "old"
elements
Basis step
Recursive step
40
Fibonacci definition:
F(1) = 1 < 21 = 2
F(2) = 1 < 22 = 4
41
42
Recursive step:
But wait!
In this example, the structural induction proof
was essentially the same as the weak or strong
mathematical induction proof
A bit of humor
47
a) an = 4n 2
b) an = 1 + (-1)n
Terms: 0, 2, 0, 2, 0, 2, etc.
a1 = 0, a2 = 2
an = an-2
c) an = n(n+1)
d) an = n2
48
f12 = f1f2
12 = 1*1
f1 = f2*1
1=1
f1 + f2 + f3 + + f2k-1 = f2k
51
f1 + f2 + f3 + + f2k-1 = f2k
Basis step: 1 S
Recursive step: s + t S when s S and t S
Z+ = { 1, 2, 3, }
1 is a positive integer
For any arbitrary n that is a positive integer, n+1 is also a positive integer
wR = y(vR)
Parentheses are for our benefit
54
Quick survey
a)
b)
c)
d)
55
Quick survey
a)
b)
c)
d)
56
Quick survey
a)
b)
c)
d)
57
Todays demotivators
58