Discrete Structures

# Lecture Notes, 15 Nov 2010

 Last Update: 17 November 2010 Note: or material is highlighted

## §4.3: Recursive Definitions

1. Consider f:NN s.t. f(n) = 5n+1

nf(n)
01
16
211
316
421
n5n+1

1. "5n+1" characterizes f's O/P in terms of its I/P n

2. But the sequence of its O/Ps exhibits another, related pattern:

• Consider the differences between f(n) & f(n+1):

f(1) = f(0)+5
f(2) = f(1)+5

3. In general, f(n) = f(n–1) + 5

2. Defined this way, f is defined in terms of (an earlier version of) itself!

• Actually, f is defined in terms of its previous O/P

• Such a definition (plus one other feature that we'll see later) is called recursive

• …because previous values "recur" (= occur again)

3. But now we have 2 ways to describe the function:

Is f(n), which = f(n–1)+5, also = 5n+1?

1. i.e.) is f(current I/P) = f(previous O/P)+5 = 5(current I/P)+1?

2. i.e.) we seem to have two algorithms for computing f(n):

• compute f(previous O/P); then add 5
• multiply current I/P by 5; then add 1

Do these algorithms have the same I/O behavior?

3. Let's prove that:

the function f defined non-recursively (or "explicitly")
=
the function f defined recursively, for all n

4. To avoid confusion,

let's call the non-recursive function "f"
& the recursive function "h".

• i.e.) f,h:NN s.t.:

f(n) = 5n+1
h(n) = h(n–1)+5

5. So: f=h?

4. Let's check a few cases first:

• f(1) = 5*1 + 1 = 6
h(1) = h(0) + 5 = 1+5 = 6
∴ f(1) = h(1)

• We can use this to prove f(2)=h(2):

• h(2) = h(1)+5
= f(1)+5, by previous computation
= (5*1 + 1) + 5, by def of f(1)
= 5*1 + (1 + 5), by assoc. law for +
= 5*1 + (5 + 1), by comm. law for +
= (5*1 + 5) + 1, by assoc law
= 5(1+1) + 1, by dist law
= 5*2 + 1
= f(2)

• We can use h(2)=f(2) to prove h(3)=f(3):

• h(3) = h(2)+5
= f(2)+5, by previous computation
= ((5*2)+1)+5, by def of f(2)
= 5*2 + 5 + 1
= 5(2+1) + 1
= 5*3 + 1
= f(3)

• In general, can use h(k)=f(k) to prove h(k+1)=f(k+1):

• Just like the inductive case of an inductive proof!

• h(k+1) = h(k) + 5
= f(k) + 5
= (5k+1)+5
= (5k+5)+1
= 5(k+1)+1
= f(k+1)

5. Have we proved ∀n[h(n) = f(n)]?

• Not yet!

• What happens to a row of dominoes?

• "They all fall down in a chain reaction."
• But only if you push the first one!

• What about h(0) = f(0)?

• Have to prove this directly:

• h(0) =def f(0) = 5*0+1 = 1; easy!

• Note: If h(0) or f(0) had been different, then so would f(n) and h(n)!

• e.g.) f′(n) = 5n+10
h′(0)=10; h′(n) = h′(n–1)+5 (∀n>0)

• But note that the general case for both h and h′ are the same;
they only differ in their base case!

6. Another e.g. of the ugly but explicit/non-recursive definition vs. the simple recursive definition:
The Fibonacci sequence:

• 0, 1, 1, 2, 3, 5, 8, 13, 21, …

is most easily described not by giving a formula for each term:

• Fib(n) = …n…

e.g.) Fib(n) = (φ1n – φ2n)/√5, where φ1 = (1+√5)/2 and φ2 = (1–√5)/2 (!!)

• By the way φ1 is called the "golden ratio" and is ≈ 1.618033989…
• φ2 = –1/φ1 ≈ –0.618033989…
• And φ1–1 = 1/φ2

Rather, it is most easily described by showing you the first 2 terms

• "giving them to you as free samples"

& then giving you a formula for computing the next term
based on the 2 previous terms:

• Fib(0) =def 0
Fib(1) =def 1
Fib(n) =def Fib(n–1) + Fib(n–2)

7. This last kind of definition is called a recursive (or inductive) definition

1. It consists of "initial conditions" = "base case"
& a "general rule" = "recurrence relation"

1. Gives an ∞ definition in a finite way
2. Can be understood by a computer
3. Easier for humans :-)

3. E.g.) n! = n * (n–1) * (n–2) * … * 3 * 2 * 1

• We need a way to eliminate the "…",
in order to make it more precise
i.e.) more algorithmic

• Is 3! = 3*2*1*…*3*2*1 ;???? No!

Could try:

i=n
n! = Π i
i=1

• Now ∃ no "…",
but no longer algorithmic:
n! = # ways you can arrange n terms;
∴ 0! = # ways you can arrange 0 terms, namely, 1 way (?!)

4. Better:

• base case:
0! =def 1 (special case; if 0!=0, this rec def wouldn't work!)
recursive case:
(n+1)! =def (n+1)*n!

• i.e.) S(n)! = S(n)*n!

• 1! = (0+1)*0! = 1*1 = 1
2! = (1+1)*1! = 2*1! = 2*1 = 2
3! = (2+1)*2! = 3*2! = 3*2*1 = 6
4! = (3+1)*3! = 4*3! = 4*3*2*1 = 24, etc.

• Alternative recursive case (same thing, different notation):

• n! =def n*(n–1)!

5. E.g.) "+" defined recursively, in terms of the successor function:

• base case:
m + 0 =def m
recursive case:
m + S(n) =def S(m+n)
i.e.)
m + (n+1) = (m+n) + 1

• Importantly, "+" must be defined in terms of the successor function S
(which is a "recursive" definition),
not in terms of "+1",
which would be a circular definition!

• And S is "defined" in terms of Peano's axioms!

• S(n) is the "next" integer after n

• e.g.)

6+4 = 6+S(3), by def of 4 as S(3)
= S(6+3), by rec def of 6+S(3)
= S(6+S(2)), by def of 3 as S(2)
= S(S(6+2)), by rec def of 6+S(2)
= S(S(6+S(1))), by def of 2 as S(1)
= S(S(S(6+1))), by rec def of 6+S(1)
= S(S(S(6+S(0)))), by def of 1 as S(0)
= S(S(S(S(6+0)))), by rec def of 6+S(0)
= S(S(S(S(6)))), by rec def of 6+0, base case
= S(S(S(7))), by def of S(6)
= S(S(8)), by def of S(7)
= S(9), by def of S(8)
= 10, by def of S(9)