Download Today`s topics §3.4: Recursive Definitions Recursion Recursively

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Today’s topics
§3.4: Recursive Definitions
• Recursion
• In induction, we prove all members of an infinite set
satisfy some predicate P by:
–
–
–
Recursively defined functions
Recursively defined sets
Structural Induction
– proving the truth of the predicate for larger members in terms of
that of smaller members.
• In recursive definitions,
definitions, we similarly define a function, a
predicate, a set, or a more complex structure over an
infinite domain (universe of discourse) by:
• Reading:
Reading: Sections 3.4
• Upcoming
– defining the function, predicate value, set membership, or
structure of larger elements in terms of those of smaller ones.
– Counting
CompSci 102
• In structural induction,
induction, we inductively prove properties of
recursively-defined objects in a way that parallels the
objects’
objects’ own recursive definitions.
© Michael Frank
10.1
CompSci 102
© Michael Frank
Recursion
Recursively Defined Functions
• Recursion is the general term for the practice of defining
an object in terms of itself
• Simplest case: One way to define a
function f:N!S (for any set S) or series
an=f(n) is to:
– or of part of itself
– This may seem circular, but it isn’
isn’t necessarily.
• An inductive proof establishes the truth of P(n+1)
recursively in terms of P(n).
• There are also recursive algorithms,
algorithms, definitions,
definitions, functions,
functions,
sequences,
sequences, sets,
sets, and other structures.
CompSci 102
© Michael Frank
10.3
10.2
– Define f(0).
– For n>0, define f(n) in terms of f(0),…
(0),…,f(n!1).
• E.g.: Define the series an :" 2n recursively:
– Let a0 :" 1.
– For n>0, let an :" 2an-1.
CompSci 102
© Michael Frank
10.4
Another Example
Recursive definition of Factorial
• Suppose we define f(n) for all n#N
recursively by:
• Give an inductive (recursive) definition of
the factorial function,
F(n) :" n! :" &1#i"n i = 1"2"…"n.
– Let f(0)=3
– For all n#N, let f(n+1)=2
f(n+1)=2ff(n)+3
– Base case: F(0) :" 1
– Recursive part: F(n) :" n " F(n!1).
• What are the values of the following?
– f(1)= 9
CompSci 102
• F(1)=1
• F(2)=2
• F(3)=6
f(2)= 21 f(3)= 45 f(4)= 93
© Michael Frank
10.5
CompSci 102
© Michael Frank
10.6
More Easy Examples
The Fibonacci Series
• Write down recursive definitions for:
• The Fibonacci series fn%0 is a famous series
defined by:
f0 :" 0, f1 :" 1, fn%2 :" fn!1 + fn!2
i+n (i
(i integer, n natural) using only s(i) = i+1.
a·n (a
(a real, n natural) using only addition
an (a real, n natural) using only multiplication
!0#i"n ai (for an arbitrary series of numbers {a
{ai})
#0#i"n ai (for an arbitrary series of numbers {a
{ai})
0
1 1
2 3
58
13
$0#i"n Si (for an arbitrary series of sets {S
{Si})
CompSci 102
© Michael Frank
10.7
CompSci 102
Leonardo Fibonacci
1170-1250
© Michael Frank
10.8
Inductive Proof about Fib. series
A lower bound on Fibonacci series
Implicitly for all n#N
• Theorem: fn < 2n.
• Proof: By induction.
Note use of
Base cases:
f0 = 0 < 20 = 1 base cases of
f1 = 1 < 21 = 2 recursive def’n.
Inductive step: Use 2nd principle of induction
(strong induction). Assume $k<n, fk < 2k.
Then fn = fn!1 + fn!2 is
< 2n!1 + 2n!2 < 2n!1 + 2n!1 = 2n. (
• Theorem. For all integers n % 3, fn > $n!2, where
$ = (1+51/2)/2 ' 1.61803.
• Proof. (Using strong induction.)
CompSci 102
© Michael Frank
10.9
– Let P(n) = (f
(fn > $n!2).
– Base cases: For n=3, note that $ < 2 = f3. For n=4, $2
= (1+2·51/2+5)/4 = (3+51/2)/2 ' 2.61803 < 3 = f4.
– Inductive step: For k%4, assume P(j) for 3#
3#j#k, prove
P(k+1). Note $2 = $+1. Thus, $k!1 = ($
($+1)$
+1)$k!3 = $k!2 +
$k!3. By inductive hypothesis, fk!1 > $k!3 and fk > $k!2.
So, fk+1 = fk + fk!1 > $k!2 + $k!3 = $k!1. Thus P(k+1). (
CompSci 102
© Michael Frank
10.10
Lamé’s Theorem
Proof of Lamé’s Theorem
• Theorem: $a,b#N, a%b>0,
the number of steps in Euclid’
Euclid’s
algorithm to find gcd(a
gcd(a,b) is
# 5k
5k, where k = %log10 b&+1 is the
number of decimal digits in b.
• Consider the sequence of divisionalgorithm equations used in Euclid’s alg.:
– Thus, Euclid’
Euclid’s algorithm is lineartime in the number of digits in b.
• Proof:
– Uses the Fibonacci sequence!
– See next 2 slides.
Gabriel Lamé
1795-1870
r0 = r1q1 + r2
r1 = r2q2 + r3
…
rn!2 = rn!1qn!1 + rn
rn!1 = rnqn + rn+1
with 0 # r2 < r1
with 0 # r3 < r2
Where a =
r0 ,
b = r1, and
gcd(a,b)=rn.
with 0 # rn < rn!1
with rn+1 = 0 (terminate)
• The number of divisions (iterations) is n.
Continued on next slide…
CompSci 102
© Michael Frank
10.11
CompSci 102
© Michael Frank
10.12
Lamé Proof, continued
Recursively Defined Sets
• Since r0 % r1 > r2 > … > rn, each quotient qi " %ri!1/ri& % 1.
• Since rn!1 = rnqn and rn!1 > rn, qn % 2.
• So we have the following relations between r and f:
• An infinite set S may be defined
recursively, by giving:
– A small finite set of base elements of S.
– A rule for constructing new elements of S from
previously-established elements.
– Implicitly, S has no other elements but these.
rn % 1 = f2
rn!1 % 2r
2rn % 2f
2f2 = f3
rn!2 % rn!1 + rn % f2 + f3 = f4
…
r2 % r3 + r4 % fn!1 + fn!2 = fn
b = r1 % r2 + r3 % fn + fn!1 = fn+1.
• Thus, if n>2 divisions are used, then b % fn+1 >
• Example: Let 3#S, and let x+y#S if x,y#S.
What is S?
$n!1.
– Thus, log10 b > log10($n!1) = (n
(n!1)log
!1)log10 $ ' (n
(n!1)0.208
!1)0.208 > (n
(n!1)/5.
!1)/5.
– If b has k decimal digits, then log10 b < k, so n!1 < 5k
5k, so n " 5k.
CompSci 102
© Michael Frank
10.13
CompSci 102
© Michael Frank
The Set of All Strings
Other Easy String Examples
• Given an alphabet ,, the set ,* of all
strings over , can be recursively defined by:
- # ,* (- :" “”, the empty string) Book
uses .
*
*
w # , ' x # , ) wx # ,
• Exercise: Prove that this definition is
equivalent to our old one:
"
n
• Give recursive definitions for:
– The concatenation of strings w1·w2.
– The length l(w) of a string w.
– Well-formed formulae of propositional logic
involving T, F, propositional variables, and
operators in {¬, ', (, ), *}.
*}.
# :$ U #
– Well-formed arithmetic formulae involving
variables, numerals, and ops in {+, !, *, +}.
+}.
n!N
CompSci 102
© Michael Frank
10.15
10.14
CompSci 102
© Michael Frank
10.16
Rooted Trees
Illustrating Rooted Tree Def’n.
• Trees will be covered in CompSci 130.
130.
• How rooted trees can be combined to form
a new rooted tree… r
– Briefly, a tree is a graph in which there is exactly one
undirected path between each pair of nodes.
– An undirected graph can be represented as a set of
unordered pairs (called arcs)
arcs) of objects called nodes.
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,
then another rooted tree is {{r
{{r,r1},…
},…,{r
,{r,rn}})
}})T1)…)
Tn.
T1
r1
T2
r2
…
Tn
rn
Draw some examples…
CompSci 102
© Michael Frank
10.17
CompSci 102
© Michael Frank
10.18
Extended Binary Trees
Full Binary Trees
• A special case of rooted trees.
• Recursive definition of EBTs:
• A special case of extended binary trees.
• Recursive definition of FBTs:
FBTs:
– A single node r is a full binary tree.
– The empty set * is an extended binary tree.
– If T1,T2 are disjoint EBTs,
EBTs, then e1)e2) T1)T2
is an EBT, where e1 = * if T1 = *, and e1 =
{(r
{(r,r1)} if T1/ * and has root r1, and similarly
for e2.
• Note this is different from the EBT base case.
– If T1,T2 are disjoint FBTs,
FBTs, then e1)e2)T1)T2, where e1 = * if T1
= *, and e1 = {(r
{(r,r1)} if T1/ *and has root r1, and similarly for e2.
• Note this is the same as the EBT recursive case!
– Can simplify it to “If T1,T2 are disjoint FBTs with roots r1 and r2, then
{(r
{(r, r1),(r
),(r,r2)})
)}) T1)T2 is an FBT.”
FBT.”
Draw some examples…
CompSci 102
© Michael Frank
10.19
Draw some examples…
CompSci 102
© Michael Frank
10.20
Structural Induction
Example continued
• Proving something about a recursively
defined object using an inductive proof
whose structure mirrors the object’s
definition.
• Example problem: Let 3#S, and let x+y#
S if x,y#S. Show that S = {n#Z+| (3|n)}
(the set of positive multiples of 3).
CompSci 102
© Michael Frank
10.21
• Let 3#
3#S, and let x+y#S if x,y#S. Let A = {n#Z+| (3|n
(3|n)}.
• Theorem: A=S. Proof: We show that A+S and S+A.
– To show A+S, show [n
[n#Z+ ' (3|n
(3|n)])
)]) n#S.
• Inductive proof. Let P(n) :"
:" n#S. Induction over positive multiples
of 3. Base case: n=3, thus 3#
3#S by def’
def’n. of S. Inductive step: Given
P(n), prove P(n+3). By inductive hyp.,
hyp., n#S, and 3#
3#S, so by def’
def’n of
S, n+3#
+3#S.
– To show S+A: let n#S, show n#A.
• Structural inductive proof. Let P(n):"
):"n#A. Two cases: n=3 (base
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<n and
y<n, and so we know x and y are in A, by strong inductive hypothesis.
Since 3|x
3|x and 3|y
3|y, we have 3|(x
3|(x+y), thus x+y # A.
CompSci 102
© Michael Frank
10.22
Related documents