Discrete Structures
Lecture Notes, 15 Nov 2010
Last Update: 17 November 2010
Note:
or
material is highlighted
|
Note: A username and password may be required to access certain
documents.
Please
contact
Bill Rapaport.
Index to all lecture notes
…Previous lecture
§4.3: Recursive Definitions
- Consider f:N→N s.t. f(n) = 5n+1
n | f(n) |
0 | 1 |
1 | 6 |
2 | 11 |
3 | 16 |
4 | 21 |
… | … |
n | 5n+1 |
- "5n+1" characterizes f's O/P in terms of its I/P n
- 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
-
In general, f(n) = f(n–1) + 5
- 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)
- But now we have 2 ways to describe the function:
Is f(n), which = f(n–1)+5, also = 5n+1?
- i.e.) is f(current I/P) = f(previous O/P)+5 = 5(current
I/P)+1?
- 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?
- Let's prove that:
the function f defined non-recursively
(or "explicitly")the function f defined recursively, for all n
- To avoid confusion,
let's call the non-recursive function "f"
& the recursive function "h".
- i.e.) f,h:N→N s.t.:
f(n) = 5n+1
h(n) = h(n–1)+5
- So: f=h?
- 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)
- 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!
- Another e.g. of the ugly but explicit/non-recursive definition vs. the
simple recursive definition:
- 0, 1, 1, 2, 3, 5, 8, 13, 21, …
is most easily described not by giving a formula for each
term:
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)
- This last kind of definition is called a recursive
(or inductive) definition
- It consists of "initial conditions" = "base case"
& a "general rule" = "recurrence relation"
- Advantages:
-
Gives an ∞ definition in a finite way
- Can be understood by a computer
- Easier for humans :-)
- 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:
- Now ∃ no "…",
but no longer algorithmic:
& what about n=0?
n! = # ways you can arrange n terms;
∴ 0! = # ways you can arrange 0 terms, namely, 1 way (?!)
- Better:
- base case:
0! =def 1 (special case; if 0!=0, this rec def wouldn't work!)
recursive case:
- 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):
- E.g.) "+" defined recursively, in terms of the successor
function:
- base case:
recursive case:
i.e.)
- 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)
Next lecture…
Text copyright © 2010 by William J. Rapaport
(rapaport@buffalo.edu)
Cartoon links and screen-captures appear here for your enjoyment.
They are not meant to infringe on any copyrights held by the creators.
For more information on any cartoon, click on it, or contact me.
http://www.cse.buffalo.edu/~rapaport/191/F10/lecturenotes-20101115.html-20101117