university of aberdeen, computing science cs3511 discrete methods kees van deemter

47
06/21/22 (c)2001-2003, Michael P. Frank 1 odule #16 - Recursion University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter Slides adapted from Michael P. Slides adapted from Michael P. Frank’s Frank’s Course Based on the Text Course Based on the Text Discrete Mathematics & Its Applications Discrete Mathematics & Its Applications (5 (5 th th Edition) Edition) by Kenneth H. Rosen by Kenneth H. Rosen

Upload: tamar

Post on 08-Feb-2016

24 views

Category:

Documents


0 download

DESCRIPTION

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 (5 th Edition) by Kenneth H. Rosen. Module #16: Recursion. Rosen 5 th ed., §§3.4-3.5 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 1

Module #16 - Recursion

University of Aberdeen, Computing Science

CS3511Discrete Methods

Kees van Deemter Slides adapted from Michael P. Frank’s Slides adapted from Michael P. Frank’s

Course Based on the TextCourse Based on the TextDiscrete Mathematics & Its ApplicationsDiscrete Mathematics & Its Applications

(5(5thth Edition) Edition)by Kenneth H. Rosenby Kenneth H. Rosen

Page 2: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 2

Module #16 - Recursion

Module #16:Recursion

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

Page 3: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 3

Module #16 - Recursion

• NB: This material was shortened compared NB: This material was shortened compared to Rosen, largely omitting algorithmic to Rosen, largely omitting algorithmic aspects.aspects.

• Reason: algorithms & complexity will be Reason: algorithms & complexity will be taught in CS4018 (Models of Computation)taught in CS4018 (Models of Computation)

Page 4: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 4

Module #16 - Recursion

§3.4: Recursive Definitions• In induction, we prove that all members of an infinite set In induction, we prove that all members of an infinite set

satisfy some predicate satisfy some predicate PP by: by:– proving the truth of the predicate for larger members based on that proving the truth of the predicate for larger members based on that

for smaller members.for smaller members.• In In recursive definitionsrecursive definitions, we similarly , we similarly definedefine a function, a a function, a

predicate, a set, or a more complex structure over an predicate, a set, or a more complex structure over an infinite domain (universe of discourse) by:infinite domain (universe of discourse) by:– defining the function, predicate value, set membership, or structure defining the function, predicate value, set membership, or structure

of larger elements in terms of those of smaller ones.of larger elements in terms of those of smaller ones.• In In structural inductionstructural induction, we inductively prove properties of , we inductively prove properties of

recursively-defined objects in a way that parallels the recursively-defined objects in a way that parallels the objects’ own recursive definitions.objects’ own recursive definitions.

Page 5: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 5

Module #16 - Recursion

Recursion

• RecursionRecursion is the general term for the is the general term for the practice of defining an object in terms of practice of defining an object in terms of itselfitself (or of part of itself) (or of part of itself)– This may seem circular, but it isn’t necessarily.This may seem circular, but it isn’t necessarily.

• There are recursive There are recursive algorithmsalgorithms, recursively , recursively defined defined functionsfunctions, , relations, setsrelations, sets, etc., etc.

Page 6: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 6

Module #16 - Recursion

Recursively Defined Functions

• Simplest case: One way to define a function Simplest case: One way to define a function ff::NNSS (for any set (for any set SS) or series ) or series aann==ff((nn) is to:) is to:– Define Define ff(0).(0).– For For nn>0, define >0, define ff((nn) in terms of ) in terms of ff(0),…,(0),…,ff((nn−−1).1).

• E.g.E.g.: Define the series : Define the series aan n ::≡≡ 22nn precisely precisely (instead of writing “1,2,4,8,…”):(instead of writing “1,2,4,8,…”):– Let Let aa0 0 ::≡≡ 1.1.– For For nn>0, let >0, let aann ::≡≡ 22aann-1-1..

Page 7: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 7

Module #16 - Recursion

Another Example

• Suppose we define Suppose we define ff((nn) for all ) for all nnNN recursively by:recursively by:– Let Let ff(0)=3(0)=3– For all For all nnNN, let , let f(nf(n+1)=2+1)=2ff((nn)+3)+3

• What are the values of the following?What are the values of the following?– ff(1)= (1)= ff(2)= (2)= ff(3)= (3)= ff(4)=(4)=9 21 45 93

Page 8: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 8

Module #16 - Recursion

Spurious examples

• It can take close attention to see whether a It can take close attention to see whether a set of equations constitute a recursive set of equations constitute a recursive definition. E.g., considerdefinition. E.g., consider

• f(0)= 0 , f(0)= 0 , f(n)= 2(f(n-2)) for n>=1f(n)= 2(f(n-2)) for n>=1

• f(0)=f(1)-1f(0)=f(1)-1f(n)= 2(f(n-1)) for n>=1f(n)= 2(f(n-1)) for n>=1

Page 9: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 9

Module #16 - Recursion

Recursive definition of Factorial

• Give an inductive (recursive) definition of Give an inductive (recursive) definition of the factorial function,the factorial function,

FF((nn) :) :≡≡ nn! :! :≡ ∏≡ ∏1≤1≤i≤ni≤n i i = = 1 122……nn..

Page 10: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 10

Module #16 - Recursion

Recursive definition of Factorial

• Give an inductive (recursive) definition of Give an inductive (recursive) definition of the factorial function,the factorial function,

FF((nn) :) :≡≡ nn! :! :≡ ∏≡ ∏1≤1≤i≤ni≤n i i = = 1 122……nn..– Base case of definition: Base case of definition: FF(1) :(1) :≡≡ 1 1– Recursive part: Recursive part: FF((nn) :) :≡≡ n n FF((nn−−1).1).

• FF(2)=2(2)=2• FF(3)=6(3)=6

Page 11: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 11

Module #16 - Recursion

More Easy Examples

• Recursive definitions exist for:Recursive definitions exist for:ii++nn ( (ii integer, integer, nn natural) using only natural) using only ss((ii) = ) = ii+1.+1.aa··nn ( (aa real, real, nn natural) using only addition natural) using only additionaann ((aa real, real, nn natural) using only multiplication natural) using only multiplication

Page 12: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 12

Module #16 - Recursion

The Fibonacci Series

• The The Fibonacci seriesFibonacci series ffnn≥0≥0 is a famous series is a famous series defined by:defined by:

ff00 : :≡ 0, ≡ 0, ff11 :≡ 1, :≡ 1, ffnn≥2≥2 :≡ :≡ ffnn−1−1 + + ffnn−2−2

Leonardo Fibonacci1170-1250

01 1

2 35 8

13

Page 13: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 13

Module #16 - Recursion

An upper bound on Fib. series

• Theorem: Theorem: ffnn < < 22nn..• Proof:Proof: By induction. By induction.Base cases:Base cases: ff00 = 0 < 2 = 0 < 200 = 1 = 1

ff11 = 1 < 2 = 1 < 211 = 2 = 2

Inductive step: Use 2Inductive step: Use 2ndnd principle of induction principle of induction (strong induction). Assume (strong induction). Assume kk<<nn, , ffkk < < 22kk. . Then Then ffnn = = ffnn−1−1 + + ffnn−2−2 is (because of IH) is (because of IH) < 2< 2nn−1−1 + 2 + 2nn−2−2 < 2 < 2nn−1−1 + 2 + 2nn−1−1 = 2. 2 = 2. 2nn−1−1 =2 =2nn. ■. ■

Note use ofbase cases ofrecursive def’n.

Implicitly for all nN

Page 14: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 14

Module #16 - Recursion

(A lower bound on Fibonacci series

• Theorem.Theorem. For all integers For all integers nn ≥ 3, ≥ 3, ffnn > > ααnn−2−2, where , where αα = = (1+5(1+51/21/2)/2 ≈ 1.61803.)/2 ≈ 1.61803.

• Proof.Proof. (Using strong induction.) (Using strong induction.)– Let Let PP((nn) = () = (ffnn > > ααnn−2−2).).– Base cases: Base cases: For For nn=3, note that=3, note that αα < 2 = < 2 = ff33. For . For nn=4, =4, αα22 = =

(1+2·5(1+2·51/21/2+5)/4 = (3+5+5)/4 = (3+51/21/2)/2 ≈ 2.61803 < 3 = )/2 ≈ 2.61803 < 3 = ff44..– Inductive step: Inductive step: For For kk≥4, assume ≥4, assume PP((jj) for 3≤) for 3≤jj≤≤kk, prove , prove

PP((kk+1). Note +1). Note αα22 = = αα+1. Thus, +1. Thus, ααkk−1−1 = ( = (αα+1)+1)ααkk−3−3 = = ααkk−2−2 + + ααkk−3−3. . By inductive hypothesis, By inductive hypothesis, ffkk−1−1

> > ααkk−3−3 and and ffkk > > ααkk−2−2. So, . So, ffkk+1+1 = = ffkk

+ + ffkk−1−1 > > ααkk−2−2 + + ααkk−3−3 = = ααkk−1−1. Thus . Thus PP((kk+1). ■+1). ■))

Page 15: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 15

Module #16 - Recursion

Recursively Defined Sets

• An infinite An infinite setset SS may be defined may be defined recursively, by giving:recursively, by giving:– A finite set of A finite set of basebase elements of elements of SS..– A rule for constructing new elements of A rule for constructing new elements of SS from from

previously-established elements.previously-established elements.– Implicitly, Implicitly, SS has no other elements than these. has no other elements than these.

• Example:Example: Let 3 Let 3SS, and let , and let xx++yySS if if xx,,yySS..What is What is SS??

Page 16: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 16

Module #16 - Recursion

• Given an alphabet Given an alphabet ΣΣ, what is , what is ΣΣ*?*? εε ΣΣ** ( (εε :≡ “”, the empty string) :≡ “”, the empty string)

ww ΣΣ* * xx ΣΣ → → wxwx ΣΣ**Bookuses λ

Page 17: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 17

Module #16 - Recursion

The Set of All Strings

• Given an alphabet Given an alphabet ΣΣ, the set , the set ΣΣ** of all strings of all strings over over ΣΣ can be recursively defined by: can be recursively defined by:

εε ΣΣ** ( (εε :≡ “”, the empty string) :≡ “”, the empty string)ww ΣΣ* * xx ΣΣ → → wxwx ΣΣ**

• Exercise:Exercise: Prove that this definition is Prove that this definition is equivalent to equivalent to

Bookuses λ

N

n

n:

Page 18: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 18

Module #16 - Recursion

Other Easy String Examples

• Give recursive definitions for:Give recursive definitions for:– The concatenation of strings The concatenation of strings ww11ww22..– The length The length ((ww) of a string ) of a string ww..– Well-formed formulae of propositional logic Well-formed formulae of propositional logic

involving involving TT, , FF, propositional variables, and , propositional variables, and operators in {¬, operators in {¬, , , , →, ↔}., →, ↔}.

– Well-formed arithmetic formulae involving Well-formed arithmetic formulae involving variables, numerals, and ops in {+, −, *}.variables, numerals, and ops in {+, −, *}.

Page 19: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 19

Module #16 - Recursion

Flashback: The language of propositional logic defined

• Atoms: p1, p2, p3, ..Atoms: p1, p2, p3, ..• Formulas:Formulas:

– All atoms are formulasAll atoms are formulas– If If is a formula then is a formula then ¬ ¬ is a formula is a formula– If If and and are formulas then ( are formulas then ( ) is a formula) is a formula

• (Recall that negation and disjunction form a (Recall that negation and disjunction form a functionally complete set of connectives)functionally complete set of connectives)

Page 20: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 20

Module #16 - Recursion

Proofs about formulas

• This was a recursive definition of the This was a recursive definition of the concept ‘formula’concept ‘formula’

• It can be used to prove theorems using an It can be used to prove theorems using an induction that follows the structure of the induction that follows the structure of the definition. E.g.,definition. E.g.,

• Theorem: “Every formula containing Theorem: “Every formula containing contains brackets around the arguments of contains brackets around the arguments of any occurrence of any occurrence of ””

Page 21: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 21

Module #16 - Recursion

Proofs about formulas

• Theorem: “Every formula containing Theorem: “Every formula containing contains brackets around the arguments of contains brackets around the arguments of any occurrence of any occurrence of ””

• Define Define complexitycomplexity of a formula as the of a formula as the number number of rule applicationsof rule applications needed to construct the needed to construct the formulaformula

• E.g., complexity(pE.g., complexity(p00)=1)=1 complexity( complexity(¬ p¬ p00) =2) =2 complexity (p complexity (p00 ¬¬pp77) =4) =4

Page 22: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 22

Module #16 - Recursion

Complexity defined recursively

• If If is an atom then is an atom then complexity( complexity()=1)=1

• If If = = then then complexity( complexity()=complexity()=complexity()+1)+1

• If If = ( = () then ) then complexity( complexity()=complexity()=complexity()+complexity()+complexity()+1)+1

Page 23: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 23

Module #16 - Recursion

Proofs about formulas

• Theorem: “Every formula Theorem: “Every formula containing containing contains brackets around its argumentscontains brackets around its arguments””

• Proof using complexity of Proof using complexity of ::• Basis step: Complexity(Basis step: Complexity()=1. This means )=1. This means

that that is a proposition letter. Hence, it is a proposition letter. Hence, it contains no contains no and the property holds for and the property holds for automatically (i.e. trivially).automatically (i.e. trivially).

Page 24: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 24

Module #16 - Recursion

Proofs about formulas

• Inductive step: IH=“property holds when Inductive step: IH=“property holds when complexity(complexity()<n”. Consider formula )<n”. Consider formula whose complexity is n. whose complexity is n. The first case:The first case:

1.1. ==, where complexity(, where complexity()<n)<nIt follows by IH that the property holds for It follows by IH that the property holds for ..Consequently, it also holds for Consequently, it also holds for ==(because (because contains the same disjunctions, contains the same disjunctions, disjuncts, and brackets as disjuncts, and brackets as ).).

Page 25: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 25

Module #16 - Recursion

Proofs about formulas

• Inductive step: IH=“property holds when Inductive step: IH=“property holds when complexity(complexity()<n”. Consider formula )<n”. Consider formula whose complexity is n. whose complexity is n. The second case:The second case:

2. 2. =(=(), where complexity(), where complexity() and ) and complexity(complexity() < n. It follows by IH that the ) < n. It follows by IH that the property holds for property holds for and and . It must then also . It must then also hold for hold for =(=(), because no brackets are ), because no brackets are removed from around arguments of removed from around arguments of . .

Page 26: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 26

Module #16 - Recursion

Proofs about formulas

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

Page 27: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 27

Module #16 - Recursion

Proofs about formulas

• Why did we use the Why did we use the second (i.e., strong)second (i.e., strong) principle of mathematical induction, principle of mathematical induction, instead of the first principle? Recallinstead of the first principle? Recall

2.2. =(=(), where complexity(), where complexity() and complexity() and complexity() < ) < n. It follows by IH that the property holds for n. It follows by IH that the property holds for and and . It must then also hold for . It must then also hold for =(=(). ).

We don’t know that We don’t know that complexity(complexity() = complexity() = complexity() = n) = n1. 1.

(In fact, complexity((In fact, complexity() + complexity() + complexity() = n) = n1, and neither is equal to 0 )1, and neither is equal to 0 )

Page 28: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 28

Module #16 - Recursion

Structural Induction

• We have seen that normal induction (1We have seen that normal induction (1stst or 2 or 2ndnd principle) applies to recursively defined objectsprinciple) applies to recursively defined objects

• But we can also use a more dedicated principle, But we can also use a more dedicated principle, called called structural inductionstructural induction : :– Basis stepBasis step: show that proposition holds for all elements : show that proposition holds for all elements

specified in basis step of the recursive definitionspecified in basis step of the recursive definition– Recursive stepRecursive step: show that IF proposition holds for each : show that IF proposition holds for each

of the elements used for constructing new elements in of the elements used for constructing new elements in the recursive step of the definition, THEN the the recursive step of the definition, THEN the proposition holds for these new elements proposition holds for these new elements

Page 29: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 29

Module #16 - Recursion

Structural induction applied to earlier example

• Theorem: “Every formula containing Theorem: “Every formula containing contains brackets around the arguments of contains brackets around the arguments of any occurrence of any occurrence of ””

• Basis stepBasis step: proposition holds for ‘basis : proposition holds for ‘basis elements’, since these are atoms.elements’, since these are atoms.

• Recursive stepRecursive step: suppose prop. holds for : suppose prop. holds for and and . Prove that it holds for . Prove that it holds for ¬ ¬ and for and for (( ). (Etc.)). (Etc.)

Page 30: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 30

Module #16 - Recursion

Structural Induction:

• Proving something about a recursively Proving something about a recursively defined object using an inductive proof defined object using an inductive proof whose structure mirrors the object’s whose structure mirrors the object’s definition.definition.

• Example problem: Example problem: Let 3Let 3SS, and let , and let xx++yySS if if xx,,yySS. (Nothing else is an element of S.) . (Nothing else is an element of S.) Prove: Prove: SS = 3,6,9,12, ... = 3,6,9,12, ... = { = {nnZZ++| (3|| (3|nn)}.)}.

Page 31: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 31

Module #16 - Recursion

Example continued• Let 3Let 3SS, and let , and let xx++yySS if if xx,,yySS. Let . Let AA = { = {nnZZ++| (3|| (3|nn)}. )}. • Theorem:Theorem: AA==SS. . Proof:Proof: We show that We show that AASS and and SSAA..

– To show To show AASS, show [, show [nnZZ++ (3| (3|nn)])]→ → nnSS..• Inductive proof.Inductive proof. Let Let PP((nn) :≡ ) :≡ nnSS. Induction over positive multiples . Induction over positive multiples

of 3. Base case: of 3. Base case: nn=3, thus 3=3, thus 3SS by def’n. of by def’n. of SS. Inductive step:. Inductive step:nnS S (Induction Hyp);(Induction Hyp); we also havewe also have 33SS, so by definition of , so by definition of SS, it , it follows that follows that nn+3+3SS. .

– To show To show SSAA: let : let nnSS, show , show nnAA..• Structural inductive proof.Structural inductive proof. Let Let PP((nn):≡):≡nnAA. Two cases: . Two cases: nn=3 (base =3 (base

case), which is in case), which is in AA, or , or nn==xx++yy (recursive step). For the recursive step, (recursive step). For the recursive step, suppose xsuppose xA and yA and yA, that is, 3|A, that is, 3|xx and 3| and 3|y. y. It follows thatIt follows that 3|(3|(xx++yy).).(For example,(For example, ifif x=3a x=3a and and y=3b y=3b thenthen x+y=3(a+b). x+y=3(a+b).)) Thus Thus xx++yy AA..

Page 32: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 32

Module #16 - Recursion

Recursive Algorithms (§3.5)

• An An algorithmalgorithm is recursive if it solves a is recursive if it solves a problem by reducing it to an instance of the problem by reducing it to an instance of the same problem with a smaller input.same problem with a smaller input.

• Example: A procedure to compute Example: A procedure to compute aann..procedureprocedure powerpower((aa≠≠0: real, 0: real, nnNN))

ifif n n = 0 = 0 then return then return 11elseelse returnreturn a a · · powerpower((aa, , nn−1)−1)

Page 33: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 33

Module #16 - Recursion

Recursive Algorithms (§3.5)

procedureprocedure powerpower((22, , 33))returnreturn 2 2 · · powerpower((22, , 33−1)−1)

returnreturn 2 2 · · powerpower((22, , 33−2)−2) returnreturn 2 2 · · powerpower((22, , 33−3)=1−3)=1

This calculates that 2This calculates that 23 3 = 2.2.2.1 = 8 = 2.2.2.1 = 8..

Page 34: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 34

Module #16 - Recursion

Recursive Algorithms (§3.5)

• This is just an algorithmic form of a This is just an algorithmic form of a recursively defined function:recursively defined function:

aa0 0 = 1 = 1 aan+1 n+1 = a.a = a.ann

Page 35: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 35

Module #16 - Recursion

Efficiency of Recursive Algorithms

• Recursive algorithms can be slowRecursive algorithms can be slow• The time complexity of a recursive The time complexity of a recursive

algorithm may depend critically on the algorithm may depend critically on the number of recursive calls it makes.number of recursive calls it makes.

Page 36: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 36

Module #16 - Recursion

Recursive Fibonacci Algorithm

procedureprocedure fibonaccifibonacci((nn NN))ifif nn=0 =0 returnreturn 0 0ifif nn=1 =1 returnreturn 1 1returnreturn fibonaccifibonacci((nn−1)+−1)+fibonaccifibonacci((nn−2)−2)

• Is this an efficient algorithm?Is this an efficient algorithm?• How many additions are performed?How many additions are performed?

Page 37: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 37

Module #16 - Recursion

Example: computing fibonacci(4)

f(4)f(4) f(3) f(2)f(3) f(2) f(2) f(2) f(1) f(1)f(1) f(1) f(0) f(0) f(1)f(1) f(0) f(0)• The problem: the same values are computed The problem: the same values are computed

again and again. (E.g., f(1) is computed again and again. (E.g., f(1) is computed three times)three times)

Page 38: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 38

Module #16 - Recursion

Analysis of Fibonacci Procedure

• Theorem:Theorem: The preceding procedure for The preceding procedure for fibonaccifibonacci((nn) ) performs performs ffnn+1+1−1 addition operations.−1 addition operations.– Proof:Proof: By strong structural induction over By strong structural induction over nn, based on the , based on the

procedure’s own recursive definition.procedure’s own recursive definition.• Base cases:Base cases: fibonaccifibonacci(0) performs 0 additions, and (0) performs 0 additions, and ff0+10+1−1 = −1 =

1 − 1 = 0. Likewise, 1 − 1 = 0. Likewise, fibonaccifibonacci(1) performs 0 additions, and (1) performs 0 additions, and ff1+11+1−1 −1 = 1−1 = 0.= 1−1 = 0.

• Inductive step: Inductive step: For For nn>1, by strong inductive hypothesis, >1, by strong inductive hypothesis, fibonaccifibonacci((nn−1) and −1) and fibonaccifibonacci((nn−2) perform −2) perform ffnn−1 and −1 and ffnn−1−1−1 −1 additions respectively. Now additions respectively. Now fibonaccifibonacci((nn) adds 1 more, that is ) adds 1 more, that is ((ffnn−1)+ (−1)+ (ffnn−1−1−1)+1 = −1)+1 = ffnn++ffnn−1−11 =(def of fib)= 1 =(def of fib)= ffnn+1+11. ■1. ■

Page 39: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 39

Module #16 - Recursion

• The latter was a sneak preview of next’s year’s The latter was a sneak preview of next’s year’s Models of Computation course:Models of Computation course:– Complexity: Complexity:

• ““How long does a given algorithm take?”How long does a given algorithm take?”[This is where induction can be used] [This is where induction can be used]

• ““What’s the fastest possible algorithm for a given problem?”What’s the fastest possible algorithm for a given problem?”

– Computability (“How do I know whether a problem can Computability (“How do I know whether a problem can be solved be solved at allat all using an algorithm?”) using an algorithm?”)

Page 40: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 40

Module #16 - Recursion

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

Page 41: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 41

Module #16 - Recursion

Rooted Trees

• Trees are covered in more depth in chapter 9.Trees are covered in more depth in chapter 9.– Briefly, a tree is a graph in which there is Briefly, a tree is a graph in which there is

exactly one undirected path between each pair exactly one undirected path between each pair of nodes.of nodes.

Page 42: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 42

Module #16 - Recursion

Rooted Trees

• Recursive definition of the set of (rooted) Recursive definition of the set of (rooted) trees:trees:– Basis stepBasis step: Any single node : Any single node rr is a tree is a tree

with root with root rr..– Recursive stepRecursive step: If : If TT11, …, , …, TTnn are disjoint trees are disjoint trees

with roots with roots rr11, …, , …, rrnn, and , and rr is a node not in any is a node not in any of the of the TTii’s, then the graph consisting of ’s, then the graph consisting of rr and and edged from edged from rr to each of to each of rr11, …, , …, rrn n is a tree with is a tree with root root rr..

Page 43: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 43

Module #16 - Recursion

Illustrating Rooted Tree Def’n.

• How rooted trees can be combined to form How rooted trees can be combined to form a new rooted tree…a new rooted tree…

T1

r1T2

r2Tn

rn

r

Page 44: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 44

Module #16 - Recursion

A simple theorem about trees

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

Page 45: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 45

Module #16 - Recursion

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

• Base caseBase case: (of course) the theorem is true in the : (of course) the theorem is true in the case of a tree consisting of just one nodecase of a tree consisting of just one node

• Induction stepInduction step: suppose the theorem holds for : suppose the theorem holds for disjoint trees disjoint trees TT11, …, , …, TTnn with roots with roots rr11, …, , …, rrn n .Then it also holds for the graph consisting of the graph consisting of rr and edged from and edged from rr to each of to each of rr11, …, , …, rrnn (see over)(see over)

Page 46: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 46

Module #16 - Recursion

proving the induction step

There is only one path from There is only one path from rrii to to rrj, j, Given IH, there is Given IH, there is only one path between any two nodes within any only one path between any two nodes within any rrii . . Consequently, there is only one path from any Consequently, there is only one path from any node in the whole tree to any other node in the tree.node in the whole tree to any other node in the tree.

T1

r1T2

r2Tn

rn

r

Page 47: University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

04/22/23 (c)2001-2003, Michael P. Frank 47

Module #16 - Recursion

There is much more ...

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