Title: 5-Recursion -1
1Strong Induction
- Normal Induction If we prove that 1)
P(n0) is true for some n0 (typically 0 or 1),
and 2) If P(k) is true for any kn0, then
P(k1) is also true.Then P(n) is true for all
nn0. - Strong Induction If we prove that 1)
Q(n0) is true for some n0 (typically 0 or 1),
and 2) If Q(j) is true for all j from n0 to k
(for any kn0), then Q(k1) is also
true.Then Q(k) is true for all kn0. - These 2 forms are equivalent Let P(n) n0jn
?Q(j) - Ref http//en.wikipedia.org/wiki/Strong_induction
2Proof by Strong Induction Jigsaw Puzzle
- Each step in assembling a jigsaw puzzle
consists of putting together 2 already assembled
blocks of pieces where each single piece is
considered a block itself. - P(n) It takes exactly n-1 steps to assemble a
jigsaw puzzle of n pieces. - Basis Step P(1) is (trivially) true.
- Inductive Step We assume P(k) true for kn and
well argue P(n1) - The last step in assembling a puzzle with n1
steps is to put together 2 blocks one of size
jgt0 and one of size n1-j. - By since 0ltj,n1-jn, P(j) and P(n1-j) are both
assumed true. - And so, the total number of steps to assemble a
puzzle with n1 pieces is 1 (j-1)
((n1-j)-1) n (n1)-1. - (this implies P(n1), and hence ends the
inductive part, and thus also the whole proof)
3More Examples of Theorems with easy Proofs using
Strong Induction
- Thm1 The second player always wins the following
game - Starting with 2 piles each containing the same
number of matches, players alternately remove any
non-zero number of matches from one of the piles. - The winner is the person who removes the last
match. - Thm2 Every ngt1 can be written as the product of
primes. - Thm3 Every postage amount of at least 18 cents
can be formed using just 4-cent and 7-cent stamps.
4Basis for Induction Integers are well ordered
- Induction is based on the fact that the integers
are well ordered in the following sense - Any non-empty set of integerswhich is bounded
below i.e. there exists b (not necessarily in
S) s.t. b x for all x in Scontains a least
element i.e. element e in S s.t. for all x in
S we have e x. - Fact The integers are well ordered.
- Why does well-ordering imply induction?
- Assume P(0) and P(k)?P(k1) for all k0.
- Well show that P(n) is true for all n0 (thus
showing that induction works) - Define S ngt0 P(n) is not true. Assume S
non-empty. - By well-ordering of N, there is a least element e
of S. - Let ke-10. If (not P(e)) then (not P(e-1)).
- We conclude that (not P(e-1)).
- e-1 cannot be equal to 0 because P(0) is true.
- But then e-1 must be in S, and so e is not the
least element of S! - gt Contradiction gt Therefore S must b empty.
- Therefore P(n) is true for all ngt0. Since P(0)
is also true, P(n) is true for all n0.
5Well Ordering can be used directly to prove
things (i.e. not necessarily via induction)
- Thrm If a and b are integers, not both 0, then
?s,t?Z (sa tb gcd(a,b)). - Proof For any a,b define S ngt0 ?s,t?Z n sa
tb. - By the well-ordering of Z, S has a smallest
element, call it d. - Choose s and t so that d sa tb.
- Claim d is a common divisor of a and b.
- Proof that da
- Writing dqar where 0rltd. If r0 then da.
- If rgt0 then since rd-qa(satb)-qa(s-q)atb
- we would have r?S.
- But since rltd it would mean that d is not the
smallest element of S - gt contradiction gt and therefore r0 (and hence
da). - Similarly, db.
- d is the greatest common divisor since any common
divisor of a and b must also divide satbd.
6Recursive (Inductive) Definitions
- A function fN?R is defined recursively by
specifying(1) f(0), its value at 0, and(2)
f(n), for ngt0, in terms of f(1),.,f(n-1), i.e
. in terms of f(k)s for kltn. - Examples
- f(n)n! can be specified as f(0)1 and, for ngt0,
f(n)nf(n-1). - For any a, fa(n)an, can be specified as fa(0)1
and,for ngt0, fa(n)afa(n-1) - Note In many cases, we specify f(k) explicitly
not only for f(0) but also for f(1), f(2), ,
f(m) for some mgt0, and then use a recursive
formula to define f(n) for all ngtm.
7Fibonacci Numbers are Recursively Defined
- The Fibonacci numbers f0,f1,f2,,fn,, are
defined by - (1) f00, f11
- (2) fnfn-1fn-2, for n2
- Subscripts can be a real pain and can interfere
with understanding.It is often convenient to use
F(n) instead of fn - The Fibonacci numbers are
- f00, f11, f21, f32, f43, f55, f68,
- f713, f821, f934, f1055, f1089,
- f11144, f12233, f13377, f14610,
- f15987, f111597, f122584,
- f134181, f146765, f1510946,
- f1617711, f1728657, f1846368,...Ref
http//en.wikipedia.org/wiki/Fibonacci_number
8Fibonacci numbers are related to a Golden Section
Ref http//en.wikipedia.org/wiki/Golden_ratio
9Fibonacci Growth
- Theorem Let f(1?5)/2and n3 then fn gt f n-2
- Proof by Strong Induction
- Basis Step (for n3 and 4)
- f3 2 gt f 1.618
- f4 3 gt f2 (12?55)/4 (3?5)/2 f1
2.168(Note Along the way, we showed that f2
f1) - Inductive Step Assume fkgtfk-2 for all k s.t.
3kn. - fn1 fnfn-1 gt fn-2fn-3 fn-3(f1) fn-3f2
f(n1)-2
10Recursively Defined Sets
- Always (1) Basis Step and (2) Recursive Step
- Multiples of 3(1) 3?S. (2) If x?S and y?S, then
xy ?S . - Strings S over an alphabet S. Let ? be the
empty string.(1) ?? S. (2) If w?S and x?S,
then wx?S . - Examples S0,1 S0,1,2,3,4,5,6,7,8,9
Sa,b,c,d,e,,x,y,z - Now recursively define (length) L S?N by (1)
L(?)0 (2)L(wx)L(x)1 - String Catenation ()(1) If w?S, then
w?w(2) If u,w?S and x?S, then u(wx)(uw)x
11Recursively Defined Sets
- Always (1) Basis Step and (2) Recursive Step
- Well-Formed Boolean Formulae
- T, F, and s, where s is a propositional variable
are all well-formed Boolean formulae (WFBF). - If E and F are WFBFs, then (E), (E?F), (E?F),
(E?F), and (E?F) are all WFBFs. - Well-Formed Arithmetic Expressions
- x is a well-formed arithmetic expression (WFAE)
if x is a numeral or a variable. - If F and G are WFAEs, then (FG), (F-G), (FG),
(F/G), and (E?F) are all WFAEs.
12Recursively Defined StructuresExample 1 Rooted
Trees
- Rooted Trees(1) A single vertex, r, is a rooted
tree with root r.(2) Suppose that T1,T2,,Tn are
rooted trees with roots r1,r2,,rn
respectively. Then the following graph is a
rooted tree with root r Take a free node r
and add an edge from r to each of root r1,,rn. - Basis Step 1
Step 2
13Recursively Defined StructuresExample 2
Extended Binary Trees
- Extended Binary Trees(1) The empty set is an
Extended Binary Tree (EBT)(2) If T1 and T2 are
EBTs, then the following tree, denoted T1T2 ,
is also an EBT Pick a new root, r, and attach
T1 as rs left subtree and attach T2 as rs
right subtree. - - Extended means that even an empty set is
considered a tree. - - Binary means that every node has at most 2
children. - Basis Step 1 Step 2 Step
3 - One element
- An Empty Set!
...
14Recursively Defined StructuresExample 3 Full
Binary Trees
- Full Binary Trees(1) A single vertex is an Full
Binary Tree (FBT)(2) If T1 and T2 are FBTs,
then the following tree, denoted T1T2 , is also
an FBT Pick a new root, r, and attach T1 as rs
left subtree and attach T2 as rs right
subtree. - - Unlike Extended BTs we dont count an empty
set in. - - In Full BTs each node has exactly 0 or 2
children - Base Step 1 Step 2 Step 3
...
15Recursive Definitions of Functions on recursively
defined objects (e.g. Full Binary Trees)
- Trees were defined recursively. Natural
definition of a function on trees will be
recursive as well! - Examples
- The Height function, h(T)(1) If T has a single
(root) node/vertex, h(T)0.(2) o/w, i.e. if
TT1T2,, then h(T) h(T1T2 )
1max(h(T1),h(T2 )) - The Number of vertices (function), n(T)(1) If T
has a single (root) node/vertex, n(T)1.(2) o/w,
i.e. if TT1T2,, then n(T) n(T1T2 )
1n(T1)n(T2 )
16Structural Induction
- If a set is recursively defined, to show a result
true for all elements in the set (1) Show the
result is true for all elements the basis
step includes.(2) Show that if the result is
true for each of the elements used to
construct a new element, it is also true for
that new element. - Thrm If T is a full binary tree, then
n(T)2h(T)1-1(proof on next slide) - Ref http//en.wikipedia.org/wiki/Structural_induc
tion
17Structural Induction Example
- Thrm If T is a full binary tree, then
n(T)2h(T)1-1 - Proof
- Basis Step If T is just the root vertex, n(T)1,
h(T)0 and n(T)11201-1 - Inductive Step When T T1T2 , we compute
- n(T)1n(T1)n(T2) Definition of n(T)
1(2h(T1)1-1)(2h(T2)1-1) Induction
hypothesis 2max(2h(T1)1,2h(T2)1)-1 Arithmeti
c - 221max(h(T1),h(T2)) -1 Arithmetic
- 22h(T)-1 Definition of h(T)
18Recursive Algorithms
- An algorithm is recursive if it solves a
problem by reducing it to an instance of the same
problem with smaller input. - Examples
- procedure factorial (n nonnegative integer)if
n0 then factorial(n) 1else factorial(n) n
factorial(n-1) - procedure power(a nonzero real, n nonnegative
integer)if n0 then power(a,n) 1else
power(a,n) apower(a,n-1) - procedure gcd(a,b nonnegative integers with
altb)if a0 then gcd(a,b) belse gcd(a,b)
gcd(b mod a, a) - procedure fibonacci (n nonnegative integer)if
n1 then fibonacci(n) 1else fibonacci(n)
fibonacci(n-1) fibonacci(n-2)