University of Aberdeen, Computing Science CS3511 Discrete

Document Sample

```					Module #16 - Recursion

University of Aberdeen, Computing Science

CS3511
Discrete Methods
Kees van Deemter
Slides adapted from Michael P. Frank’s
Course Based on the Text
Discrete Mathematics & Its Applications
(5th Edition)
by Kenneth H. Rosen

3/5/2012                  (c)2001-2003, Michael P. Frank   1
Module #16 - Recursion

Module #16:
Recursion

Rosen 5th ed., §§3.4-3.5
~34 slides, ~2 lectures

3/5/2012                 (c)2001-2003, Michael P. Frank   2
Module #16 - Recursion

• NB: This material was shortened compared
to Rosen, largely omitting algorithmic
aspects.
• Reason: algorithms & complexity will be
taught in CS4018 (Models of Computation)

3/5/2012                 (c)2001-2003, Michael P. Frank   3
Module #16 - Recursion

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

3/5/2012                        (c)2001-2003, Michael P. Frank                      4
Module #16 - Recursion

Recursion
• Recursion is the general term for the
practice of defining an object in terms of
itself (or of part of itself)
– This may seem circular, but it isn’t necessarily.
• There are recursive algorithms, recursively
defined functions, relations, sets, etc.

3/5/2012                   (c)2001-2003, Michael P. Frank        5
Module #16 - Recursion

Recursively Defined Functions
• Simplest case: One way to define a function
f:NS (for any set S) or series an=f(n) is to:
– Define f(0).
– For n>0, define f(n) in terms of f(0),…,f(n−1).
• E.g.: Define the series an :≡ 2n precisely
– Let a0 :≡ 1.
– For n>0, let an :≡ 2an-1.

3/5/2012                   (c)2001-2003, Michael P. Frank      6
Module #16 - Recursion

Another Example
• Suppose we define f(n) for all nN
recursively by:
– Let f(0)=3
– For all nN, let f(n+1)=2f(n)+3
• What are the values of the following?
– f(1)= 9     f(2)= 21 f(3)= 45 f(4)= 93

3/5/2012                    (c)2001-2003, Michael P. Frank   7
Module #16 - Recursion

Spurious examples
• It can take close attention to see whether a
set of equations constitute a recursive
definition. E.g., consider
• f(0)= 0 ,
f(n)= 2(f(n-2)) for n>=1
• f(0)=f(1)-1
f(n)= 2(f(n-1)) for n>=1

3/5/2012                 (c)2001-2003, Michael P. Frank   8
Module #16 - Recursion

Recursive definition of Factorial
• Give an inductive (recursive) definition of
the factorial function,
F(n) :≡ n! :≡ ∏1≤i≤n i = 12…n.

3/5/2012                 (c)2001-2003, Michael P. Frank   9
Module #16 - Recursion

Recursive definition of Factorial
• Give an inductive (recursive) definition of
the factorial function,
F(n) :≡ n! :≡ ∏1≤i≤n i = 12…n.
– Base case of definition: F(1) :≡ 1
– Recursive part: F(n) :≡ n  F(n−1).
• F(2)=2
• F(3)=6

3/5/2012                   (c)2001-2003, Michael P. Frank   10
Module #16 - Recursion

More Easy Examples
• Recursive definitions exist for:
i+n (i integer, n natural) using only s(i) = i+1.
a·n (a real, n natural) using only addition
an (a real, n natural) using only multiplication

3/5/2012                   (c)2001-2003, Michael P. Frank      11
Module #16 - Recursion

The Fibonacci Series
• The Fibonacci series fn≥0 is a famous series
defined by:
f0 :≡ 0, f1 :≡ 1, fn≥2 :≡ fn−1 + fn−2

0
1 1
2 3
58
13
Leonardo Fibonacci
1170-1250
3/5/2012                 (c)2001-2003, Michael P. Frank                  12
Module #16 - Recursion

An upper bound on Fib. series
• Theorem: fn < 2n.             Implicitly for all nN
• Proof: By induction.
0=1                 Note use of
Base cases: f0 = 0 < 2                      base cases of
f1 = 1 < 2 1=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 (because of IH)
< 2n−1 + 2n−2 < 2n−1 + 2n−1 = 2. 2n−1 =2n. ■

3/5/2012                    (c)2001-2003, Michael P. Frank            13
Module #16 - Recursion

(A lower bound on Fibonacci series
• Theorem. For all integers n ≥ 3, fn > αn−2, where α
= (1+51/2)/2 ≈ 1.61803.
• Proof. (Using strong induction.)
– Let P(n) = (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≤j≤k, prove
P(k+1). Note α2 = α+1. Thus, αk−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). ■)

3/5/2012                      (c)2001-2003, Michael P. Frank               14
Module #16 - Recursion

Recursively Defined Sets
• An infinite set S may be defined
recursively, by giving:
– A 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 than these.
• Example: Let 3S, and let x+yS if x,yS.
What is S?

3/5/2012                  (c)2001-2003, Michael P. Frank       15
Module #16 - Recursion

• Given an alphabet Σ, what is Σ*?
ε  Σ* (ε :≡ “”, the empty string)
Book
w  Σ*  x  Σ → wx  Σ*                      uses λ

3/5/2012                 (c)2001-2003, Michael P. Frank            16
Module #16 - Recursion

The Set of All Strings
• 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   :       n

nN

3/5/2012                 (c)2001-2003, Michael P. Frank   17
Module #16 - Recursion

Other Easy String Examples
• Give recursive definitions for:
– The concatenation of strings w1w2.
– The length (w) of a string w.
– Well-formed formulae of propositional logic
involving T, F, propositional variables, and
operators in {¬, , , →, ↔}.
– Well-formed arithmetic formulae involving
variables, numerals, and ops in {+, −, *}.

3/5/2012                  (c)2001-2003, Michael P. Frank    18
Module #16 - Recursion

Flashback: The language of
propositional logic defined
• Atoms: p1, p2, p3, ..
• Formulas:
– All atoms are formulas
– If  is a formula then ¬  is a formula
– If  and  are formulas then (  ) is a formula
• (Recall that negation and disjunction form a
functionally complete set of connectives)

3/5/2012                     (c)2001-2003, Michael P. Frank      19
Module #16 - Recursion

• This was a recursive definition of the
concept ‘formula’
• It can be used to prove theorems using an
induction that follows the structure of the
definition. E.g.,
• Theorem: “Every formula containing 
contains brackets around the arguments of
any occurrence of  ”

3/5/2012                 (c)2001-2003, Michael P. Frank   20
Module #16 - Recursion

• Theorem: “Every formula containing 
contains brackets around the arguments of
any occurrence of  ”
• Define complexity of a formula as the
number of rule applications needed to
construct the formula
• E.g., complexity(p0)=1
complexity(¬ p0) =2
complexity (p0  ¬p7) =4

3/5/2012                 (c)2001-2003, Michael P. Frank   21
Module #16 - Recursion

Complexity defined
recursively

• If  is an atom then
complexity()=1
• If  =  then
complexity()=complexity()+1
• If  = () then
complexity()=complexity()+complexity()+1

3/5/2012                 (c)2001-2003, Michael P. Frank   22
Module #16 - Recursion

• Theorem: “Every formula  containing 
contains brackets around its arguments”
• Proof using complexity of :
• Basis step: Complexity()=1. This means
that  is a proposition letter. Hence, it
contains no  and the property holds for 
automatically (i.e. trivially).

3/5/2012                 (c)2001-2003, Michael P. Frank   23
Module #16 - Recursion

•    Inductive step: IH=“property holds when
complexity()<n”. Consider formula 
whose complexity is n. The first case:
1. =, where complexity()<n
It follows by IH that the property holds for .
Consequently, it also holds for =
(because  contains the same disjunctions,
disjuncts, and brackets as ).

3/5/2012                  (c)2001-2003, Michael P. Frank        24
Module #16 - Recursion

•    Inductive step: IH=“property holds when
complexity()<n”. Consider formula 
whose complexity is n. The second case:
2. =(), where complexity() and
complexity() < n. It follows by IH that the
property holds for  and . It must then also
hold for =(), because no brackets are
removed from around arguments of  .

3/5/2012                  (c)2001-2003, Michael P. Frank      25
Module #16 - Recursion

• Why did we use the second (i.e., strong)
of the first principle?

3/5/2012                 (c)2001-2003, Michael P. Frank   26
Module #16 - Recursion

•    Why did we use the second (i.e., strong)
principle of mathematical induction,
instead of the first principle? Recall
2. =(), where complexity() and complexity() <
n. It follows by IH that the property holds for  and
. It must then also hold for =().
We don’t know that
complexity() = complexity() = n1.
(In fact, complexity() + complexity() = n1, and neither is equal to 0 )

3/5/2012                          (c)2001-2003, Michael P. Frank                         27
Module #16 - Recursion

Structural Induction
• We have seen that normal induction (1st or 2nd
principle) applies to recursively defined objects
• But we can also use a more dedicated principle,
called structural induction :
– Basis step: show that proposition holds for all elements
specified in basis step of the recursive definition
– Recursive step: show that IF proposition holds for each
of the elements used for constructing new elements in
the recursive step of the definition, THEN the
proposition holds for these new elements

3/5/2012                     (c)2001-2003, Michael P. Frank             28
Module #16 - Recursion

Structural induction applied to
earlier example
• Theorem: “Every formula containing 
contains brackets around the arguments of
any occurrence of  ”
• Basis step: proposition holds for ‘basis
elements’, since these are atoms.
• Recursive step: suppose prop. holds for
 and . Prove that it holds for ¬  and for
(  ). (Etc.)

3/5/2012                 (c)2001-2003, Michael P. Frank   29
Module #16 - Recursion

Structural Induction:
• 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. (Nothing else is an element of S.)
Prove: S = 3,6,9,12, ... = {nZ+| (3|n)}.

3/5/2012                 (c)2001-2003, Michael P. Frank   30
Module #16 - Recursion

Example continued
• Let 3S, and let x+yS if x,yS. Let A = {nZ+| (3|n)}.
• Theorem: A=S. Proof: We show that AS and SA.
– To show AS, show [nZ+  (3|n)]→ nS.
• Inductive proof. Let P(n) :≡ nS. Induction over positive multiples
of 3. Base case: n=3, thus 3S by def’n. of S. Inductive step:
nS (Induction Hyp); we also have 3S, so by definition of S, it
follows that n+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). For the recursive step,
suppose xA and yA, that is, 3|x and 3|y. It follows that 3|(x+y).
(For example, if x=3a and y=3b then x+y=3(a+b).) Thus x+y  A.

3/5/2012                         (c)2001-2003, Michael P. Frank                         31
Module #16 - Recursion

Recursive Algorithms (§3.5)
• An algorithm is recursive if it solves a
problem by reducing it to an instance of the
same problem with a smaller input.

• Example: A procedure to compute an.
procedure power(a≠0: real, nN)
if n = 0 then return 1
else return a · power(a, n−1)

3/5/2012                 (c)2001-2003, Michael P. Frank   32
Module #16 - Recursion

Recursive Algorithms (§3.5)
procedure power(2, 3)
return 2 · power(2, 3−1)
return 2 · power(2, 3−2)
return 2 · power(2, 3−3)=1

This calculates that 23 = 2.2.2.1 = 8.

3/5/2012                 (c)2001-2003, Michael P. Frank   33
Module #16 - Recursion

Recursive Algorithms (§3.5)
• This is just an algorithmic form of a
recursively defined function:

a0 = 1
an+1 = a.an

3/5/2012                 (c)2001-2003, Michael P. Frank   34
Module #16 - Recursion

Efficiency of Recursive Algorithms

• Recursive algorithms can be slow
• The time complexity of a recursive
algorithm may depend critically on the
number of recursive calls it makes.

3/5/2012                 (c)2001-2003, Michael P. Frank   35
Module #16 - Recursion

Recursive Fibonacci Algorithm
procedure fibonacci(n  N)
if n=0 return 0
if n=1 return 1
return fibonacci(n−1)+fibonacci(n−2)
• Is this an efficient algorithm?
• How many additions are performed?

3/5/2012                 (c)2001-2003, Michael P. Frank   36
Module #16 - Recursion

Example: computing fibonacci(4)

f(4)
f(3)     f(2)
f(2) f(1) f(1) f(0)
f(1) f(0)
• The problem: the same values are computed
again and again. (E.g., f(1) is computed
three times)

3/5/2012                  (c)2001-2003, Michael P. Frank   37
Module #16 - Recursion

Analysis of Fibonacci Procedure
• Theorem: The preceding procedure for
– Proof: By strong structural induction over n, based on
the procedure’s own recursive definition.
• Base cases: fibonacci(0) performs 0 additions, and f0+1−1 =
1 − 1 = 0. Likewise, fibonacci(1) performs 0 additions, and
f1+1−1 = 1−1 = 0.
• Inductive step: For n>1, by strong inductive hypothesis,
fibonacci(n−1) and fibonacci(n−2) perform fn−1 and fn−1−1
(fn−1)+ (fn−1−1)+1 = fn+fn−11 =(def of fib)= fn+11. ■

3/5/2012                        (c)2001-2003, Michael P. Frank                   38
Module #16 - Recursion

• The latter was a sneak preview of next’s year’s
Models of Computation course:
– Complexity:
• “How long does a given algorithm take?”
[This is where induction can be used]
• “What’s the fastest possible algorithm for a given problem?”
– Computability (“How do I know whether a problem can
be solved at all using an algorithm?”)

3/5/2012                      (c)2001-2003, Michael P. Frank                   39
Module #16 - Recursion

• Some types of structures that are naturally
defined using recursion:
– Rooted trees
– (Extended Binary Trees)
– (Full Binary Trees)

3/5/2012                 (c)2001-2003, Michael P. Frank   40
Module #16 - Recursion

Rooted Trees
• Trees are covered in more depth in chapter 9.
– Briefly, a tree is a graph in which there is
exactly one undirected path between each pair
of nodes.

3/5/2012                  (c)2001-2003, Michael P. Frank    41
Module #16 - Recursion

Rooted Trees
• Recursive definition of the set of (rooted)
trees:
– Basis step: Any single node r is a tree
with root r.
– Recursive step: If T1, …, Tn are disjoint trees
with roots r1, …, rn, and r is a node not in any
of the Ti’s, then the graph consisting of r and
edged from r to each of r1, …, rn is a tree with
root r.

3/5/2012                  (c)2001-2003, Michael P. Frank        42
Module #16 - Recursion

Illustrating Rooted Tree Def’n.
• How rooted trees can be combined to form
a new rooted tree… r

T1
r1
T2
r2
…       Tn
rn

3/5/2012                 (c)2001-2003, Michael P. Frank             43
Module #16 - Recursion

• Theorem: in a tree there is exactly one path
between each pair of nodes.

3/5/2012                 (c)2001-2003, Michael P. Frank   44
Module #16 - Recursion

• Theorem: in a tree there is exactly one undirected
path between each pair of nodes. Proof:
• Base case: (of course) the theorem is true in the
case of a tree consisting of just one node
• Induction step: suppose the theorem holds for
disjoint trees T1, …, Tn with roots r1, …, rn .
Then it also holds for the graph consisting of r
and edged from r to each of r1, …, rn (see over)

3/5/2012                 (c)2001-2003, Michael P. Frank       45
Module #16 - Recursion

proving the induction step
There is only one path from ri to rj, Given IH, there is
only one path between any two nodes within any ri .
Consequently, there is         r only one path from any
node in the whole tree to any other node in the tree.

T1
r1
T2
r2
…       Tn
rn

3/5/2012                      (c)2001-2003, Michael P. Frank             46
Module #16 - Recursion

There is much more ...
See Rosen, chapter 3.4 if you’re interested
in theorems relating the height of a tree to
the number of nodes in it. (Proofs with
induction.)

3/5/2012                 (c)2001-2003, Michael P. Frank   47

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 4 posted: 3/5/2012 language: English pages: 47
How are you planning on using Docstoc?