<<

Today’s topics §3.4: Recursive

• In induction, we prove all members of an infinite satisfy some predicate P by: – Recursively defined functions – proving the truth of the predicate for larger members in terms of – Recursively defined sets that of smaller members. – Structural Induction • In recursive definitions, we similarly define a , a predicate, a set, or a more complex structure over an • Reading: Sections 3.4 infinite domain (universe of discourse) by: • Upcoming – defining the function, predicate value, set membership, or structure of larger elements in terms of those of smaller ones. – Counting • In structural induction, we inductively prove properties of recursively-defined objects in a way that parallels the objects’ own recursive definitions.

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.1 10.2

Recursion Recursively Defined Functions

• Recursion is the general term for the practice of defining • Simplest case: One way to define a an object in terms of itself function f:N!S (for any set S) or series – or of part of itself a =f(n) is to: – This may seem circular, but it isn’t necessarily. n • An inductive proof establishes the truth of P(n+1) – Define f(0). recursively in terms of P(n). – For n>0, define f(n) in terms of f(0),…,f(n!1). • There are also recursive algorithms, definitions, functions, n • E.g.: Define the series an :" 2 recursively: , sets, and other structures. – Let a0 :" 1. – For n>0, let an :" 2an-1.

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.3 10.4 Another Example Recursive of

• Suppose we define f(n) for all n#N • Give an inductive (recursive) definition of recursively by: the factorial function, – Let f(0)=3 F(n) :" n! :" &1#i"n i = 1"2"…"n. – For all n#N, let f(n+1)=2f(n)+3 – Base case: F(0) :" 1 • What are the values of the following? – Recursive part: F(n) :" n " F(n!1). • F(1)=1 – f(1)= 9 f(2)= 2 1 f(3)= 4 5 f(4)= 93 • F(1)=1 • F(2)=2 • F(3)=6

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.5 10.6

More Easy Examples The Fibonacci Series

• Write down recursive definitions for: • The Fibonacci series fn%0 is a famous series i+n (i , n natural) using only s(i) = i+1. defined by: a·n (a real, n natural) using only f0 :" 0, f1 :" 1, fn%2 :" fn!1 + fn!2 an (a real, n natural) using only 0 !0#i"n ai (for an arbitrary series of numbers {ai}) 1 1 #0#i"n ai (for an arbitrary series of numbers {ai}) 2 3 5 8 $0#i"n Si (for an arbitrary series of sets {Si}) 13 Leonardo Fibonacci 1170-1250

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.7 10.8 Inductive Proof about Fib. series A lower bound on Fibonacci series

n n!2 • Theorem: fn < 2 . Implicitly for all n#N • Theorem. For all n % 3, fn > $ , where 1/2 • Proof: By induction. $ = (1+5 )/2 ' 1.61803. 0 Note use of • Proof. (Using strong induction.) Base cases: f0 = 0 < 2 = 1 base cases of – Let P(n) = (f > $n!2). 1 n f = 1 < 2 = 2 recursive def’n. 2 1 – Base cases: For n=3, note that $ < 2 = f3. For n=4, $ nd 1/2 1/2 Inductive step: Use 2 principle of induction = (1+2·5 +5)/4 = (3+5 )/2 ' 2.61803 < 3 = f4. (strong induction). Assume $k $ and fk > $ . n!1 n!2 n!1 n!1 n k!2 k!3 k!1 < 2 + 2 < 2 + 2 = 2 . ( So, fk+1 = fk + fk!1 > $ + $ = $ . Thus P(k+1). (

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.9 10.10

Lamé’s Theorem Proof of Lamé’s Theorem

• Theorem: $a,b#N, a%b>0, • Consider the of division- the number of steps in Euclid’s algorithm equations used in Euclid’s alg.: algorithm to find gcd(a,b) is r0 = r1q1 + r2 with 0 # r2 < r1 # 5k, where k = %log10 b&+1 is the Where a = r1 = r2q2 + r3 with 0 # r3 < r2 number of decimal digits in b. r0, – Thus, Euclid’s algorithm is linear- … b = r1, and time in the number of digits in b. gcd(a,b)=r . rn!2 = rn!1qn!1 + rn with 0 # rn < rn!1 n • Proof: Gabriel Lamé 1795-1870 rn!1 = rnqn + rn+1 with rn+1 = 0 (terminate) – Uses the Fibonacci sequence! – Uses the Fibonacci sequence! • The number of divisions (iterations) is n. – See next 2 slides. Continued on next slide… CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.11 10.12 Lamé Proof, continued Recursively Defined Sets

• Since r0 % r1 > r2 > … > rn, each quotient qi " %ri!1/ri& % 1. • An infinite set S may be defined • Since r = r q and r > r , q % 2. • Since rn!1 = rnqn and rn!1 > rn, qn % 2. recursively, by giving: • So we have the following relations between r and f: – A small finite set of base elements of S. rn % 1 = f2

rn!1 % 2rn % 2f2 = f3 – A rule for constructing new elements of S from rn!2 % rn!1 + rn % f2 + f3 = f4 previously-established elements. … – Implicitly, S has no other elements but these. r2 % r3 + r4 % fn!1 + fn!2 = fn b = r1 % r2 + r3 % fn + fn!1 = fn+1. • Example: Let 3#S, and let x+y#S if x,y#S. n!1 • Thus, if n>2 divisions are used, then b % fn+1 > $ . What is S? n!1 – Thus, log10 b > log10($ ) = (n!1)log10 $ ' (n!1)0.208 > (n!1)/5.

– If b has k decimal digits, then log10 b < k, so n!1 < 5k, so n " 5k.

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.13 10.14

The Set of All Strings Other Easy String Examples

• Given an alphabet ,, the set ,* of all • Give recursive definitions for:

strings over , can be recursively defined by: – The concatenation of strings w1·w2. - ,* (- :" “”, the empty string) Book # uses . – The length l(w) of a string w. w # ,* ' x # , ) wx # ,* – Well-formed formulae of propositional logic • Exercise: Prove that this definition is involving T, F, propositional variables, and operators in {¬, ', (, ), *}. equivalent to our old one: " n # :$ # – Well-formed arithmetic formulae involving nU!N variables, numerals, and ops in {+, !, *, +}.

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.15 10.16 Rooted Trees Illustrating Rooted Def’n.

• Trees will be covered in CompSci 130. • How rooted trees can be combined to form – Briefly, a tree is a graph in which there is exactly one undirected path between each pair of nodes. a new rooted tree… r – An undirected graph can be represented as a set of unordered pairs (called arcs) of objects called nodes. • Definition of the set of rooted trees: – Any single node r is a rooted tree.

– If T1, …, Tn are disjoint rooted trees with respective roots r1, …, rn, and r is a node not in any of the Ti’s, r1 r2 rn then another rooted tree is {{r,r1},…,{r,rn}})T1)…) T1 T2 … Tn Tn.

Draw some examples… CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.17 10.18

Extended Binary Trees Full Binary Trees

• A special case of rooted trees. • A special case of extended binary trees. • Recursive definition of FBTs: • Recursive definition of EBTs: – A single node r is a full binary tree. – The empty set * is an extended binary tree. • Note this is different from the EBT base case. – If T1,T2 are disjoint FBTs, then e1)e2)T1)T2, where e1 = * if T1 – If T1,T2 are disjoint EBTs, then e1)e2) T1)T2 = *, and e1 = {(r,r1)} if T1/ *and has root r1, and similarly for e2. is an EBT, where e = if T = , and e = 1 * 1 * 1 • Note this is the same as the EBT recursive case!

{(r,r1)} if T1/ * and has root r1, and similarly – Can simplify it to “If T1,T2 are disjoint FBTs with roots r1 and r2, then {(r, r1),(r,r2)}) T1)T2 is an FBT.” for e2.

Draw some examples… Draw some examples… CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.19 10.20 Structural Induction Example continued

• Let 3#S, and let x+y#S if x,y#S. Let A = {n#Z+| (3|n)}. • Proving something about a recursively • Theorem: A=S. Proof: We show that A+S and S+A. defined object using an inductive proof – To show A+S, show [n#Z+ ' (3|n)]) n#S. • Inductive proof. Let P(n) :" n#S. Induction over positive multiples whose structure mirrors the object’s of 3. Base case: n=3, thus 3#S by def’n. of S. Inductive step: Given P(n), prove P(n+3). By inductive hyp., n#S, and 3#S, so by def’n of definition. S, n+3#S. • Example problem: Let 3#S, and let x+y# – To show S+A: let n#S, show n#A. + • Structural inductive proof. Let P(n):"n#A. Two cases: n=3 (base S if x,y#S. Show that S = {n#Z | (3|n)} case), which is in A, or n=x+y (recursive step). We know x and y are positive, since neither rule generates negative numbers. So, x

CompSci 102 © Michael Frank CompSci 102 © Michael Frank 10.21 10.22