Module #16: Recursion
description
Transcript of Module #16: Recursion
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
1/18
Module
#16 -
Recu
rsio
n
Bogazici UniversityBogazici UniversityDepartment of Computer EngineeringDepartment of Computer Engineering
CCmpE 220 mpE 220 Discrete Discrete MathematicsMathematics
16. Recursion 16. Recursion
Haluk BingölHaluk Bingöl
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
2/18
Module
#16 -
Recu
rsio
n
Module #16:Module #16:RecursionRecursion
Rosen 5Rosen 5thth ed., §§3.4-3.5 ed., §§3.4-3.5~34 slides, ~2 lectures~34 slides, ~2 lectures
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
3/38
Module
#16 -
Recu
rsio
n
§3.4: Recursive Definitions§3.4: Recursive Definitions
In induction, we In induction, we proveprove all members of an infinite set all members of an infinite set satisfy some predicate satisfy some predicate PP by: by:proving the truth of the predicate for larger members in proving the truth of the predicate for larger members in
terms of that of smaller members.terms of that of smaller members.
In In recursive definitionsrecursive definitions, we similarly , we similarly definedefine a function, a function, a predicate, a set, or a more complex structure a predicate, a set, or a more complex structure over an infinite domain (universe of discourse) by:over an infinite domain (universe of discourse) by:defining the function, predicate value, set membership, or defining the function, predicate value, set membership, or
structure of larger elements in terms of those of smaller structure of larger elements in terms of those of smaller ones.ones.
In In structural inductionstructural induction, we inductively prove , we inductively prove properties of recursively-defined objects in a way properties of recursively-defined objects in a way that parallels the objects’ own recursive definitions.that parallels the objects’ own recursive definitions.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
4/38
Module
#16 -
Recu
rsio
n
RecursionRecursion
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 itselfor of part of itself
This may seem circular, but it isn’t necessarily.This may seem circular, but it isn’t necessarily.
An inductive proof establishes the truth of An inductive proof establishes the truth of PP((nn+1) +1) recursivelyrecursively in terms of in terms of PP((nn).).
There are also recursive There are also recursive algorithmsalgorithms, , definitionsdefinitions, , functionsfunctions, , sequencessequences, , setssets, and , and other structures.other structures.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
5/38
Module
#16 -
Recu
rsio
n
Recursively Defined Recursively Defined FunctionsFunctionsSimplest case: Simplest case:
One way to define a function One way to define a function ff::ℕℕSS (for (for any set 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).
Ex.Ex. Define the series Define the series aan n ::≡≡ 22nn recursively: recursively:
Let Let aa0 0 ::≡≡ 1.1.
For For nn>0, let >0, let aann ::≡≡ 22aann-1-1..
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
6/38
Module
#16 -
Recu
rsio
n
Another ExampleAnother Example
Suppose we define Suppose we define ff((nn) for all ) for all nnℕℕ recursively by:recursively by:Let Let ff(0)=3(0)=3
For all For all nnℕℕ, 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
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
7/38
Module
#16 -
Recu
rsio
n
Recursive definition of Recursive definition of FactorialFactorialGive an inductive (recursive) definition Give an inductive (recursive) definition
of the factorial function,of the factorial function,FF((nn) :) :≡≡ nn! :! :≡ ∏≡ ∏1≤1≤i≤ni≤n i = i = 1 122……nn..
Base case: Base case: FF(0) :(0) :≡≡ 1 1
Recursive part: Recursive part: FF((nn) :) :≡≡ n n FF((nn−−1).1).FF(1)=1(1)=1
FF(2)=2(2)=2
FF(3)=6(3)=6
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
8/38
Module
#16 -
Recu
rsio
n
More Easy ExamplesMore Easy Examples
Write down recursive definitions for:Write down recursive definitions 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 addition
aann ((aa real, real, nn natural) using only multiplication natural) using only multiplication
∑∑0≤0≤i≤ni≤n aaii (for an arbitrary series of numbers { (for an arbitrary series of numbers {aaii})})
∏∏0≤0≤i≤ni≤n aai i (for an arbitrary series of numbers {(for an arbitrary series of numbers {aaii})})
∩∩0≤0≤i≤ni≤n S Sii (for an arbitrary series of sets { (for an arbitrary series of sets {SSii})})
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
9/38
Module
#16 -
Recu
rsio
n
The Fibonacci SeriesThe Fibonacci Series
The The Fibonacci seriesFibonacci series ffnn≥0≥0 is a famous is a famous series defined by:series 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
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
10/38
Module
#16 -
Recu
rsio
n
Inductive Proof about Fib. Inductive Proof about Fib. seriesseriesThm.Thm. ffnn < < 22nn..
ProofProof.. 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 principle of induction (strong induction). Assume induction (strong induction). Assume kk<<nn, , ffkk < < 22kk. . Then Then ffnn = = ffnn−1−1 + + ffnn−2−2 is is < 2< 2nn−1−1 + 2 + 2nn−2−2 < 2 < 2nn−1−1 + 2 + 2nn−1−1 = 2 = 2nn. . ■■
Note use ofbase cases ofrecursive def’n.
Implicitly for all nℕ
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
11/38
Module
#16 -
Recu
rsio
n
A lower bound on Fibonacci A lower bound on Fibonacci seriesseriesThm.Thm. 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. (Using strong induction.)Proof. (Using strong induction.)Let Let PP((nn) = () = (ffnn > > ααnn−2−2).).Base cases: Base cases:
For For nn=3, =3, αα33−2−2 = = α α ≈ 1.61803 < 2 = ≈ 1.61803 < 2 = ff33. . For For nn=4, =4, αα44−2−2 = = αα22 = (1+2·5 = (1+2·51/21/2+5)/4 = (3+5+5)/4 = (3+51/21/2)/2 ≈ )/2 ≈ 2.61803 < 3 = 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. +1. αα((kk+1)+1)−−22 = = ααkk−1−1 = = αα22 ααkk−−33 = ( = (αα+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, αα((kk+1)+1)−−22 = = ααkk−2−2 + + ααkk−3−3 << ffkk + + ffkk−1−1 = = ffkk+1+1. . Thus Thus PP((kk+1). ■+1). ■
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
12/38
Module
#16 -
Recu
rsio
n
LamLamé’s Theoremé’s Theorem
Thm.Thm. aa,,bbℤℤ++, , aa ≥≥bb,,the number of steps in Euclid’sthe number of steps in Euclid’salgorithm to find gcd(algorithm to find gcd(aa,,bb) is) is≤ 5≤ 5kk, , where where kk = = loglog1010 bb+1 is the+1 is thenumber of decimal digits in number of decimal digits in bb..Thus, Euclid’s algorithm is linear-Thus, Euclid’s algorithm is linear-
time in the number of digits in time in the number of digits in bb..
Proof: Proof: Uses the Fibonacci sequence!Uses the Fibonacci sequence!See next 2 slides.See next 2 slides.
Gabriel Lamé1795-1870
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
13/38
Module
#16 -
Recu
rsio
n
Proof of LamProof of Lamé’s Theoremé’s Theorem
Consider the sequence of division-Consider the sequence of division-algorithm equations used in Euclid’s algorithm equations used in Euclid’s algorithmalgorithm::rr00 = = rr11qq11 + + rr22 with 0 with 0 ≤ ≤ rr22 < < rr11
rr11 = = rr22qq22 + + rr33 with 0 with 0 ≤ ≤ rr33 < < rr22
……
rrnn−2−2 = = rrnn−1−1qqnn−1−1 + + rrnn with 0 with 0 ≤ ≤ rrnn < < rrnn−1−1
rrnn−1−1 = = rrnnqqnn + r + rnn+1+1 with with rrnn+1+1 = 0 (terminate) = 0 (terminate)
The number of divisions (iterations)The number of divisions (iterations) is is nn..
Where a = r0,b = r1, and gcd(a,b)=rn.
Continued on next slide…
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
14/38
Module
#16 -
Recu
rsio
n
LamLaméé Proof, continued Proof, continuedSince Since rr00 ≥ ≥ rr11 > > rr22 > … > > … > rrnn, ,
each quotient each quotient qqi i ≡ ≡ rrii−1−1//rrii ≥ 1 ≥ 1, i=1, .., i=1, ...., , nn-1.-1.Since Since rrnn−1−1 = = rrnnqqnn and and rrnn−1−1 > > rrnn, , qqnn ≥ 2. ≥ 2.So we have the following relations between So we have the following relations between rr and and ff::
rrnn ≥ 1 = ≥ 1 = ff22
rrnn−1−1 ≥ 2 ≥ 2rrnn ≥ 2 ≥ 2 = = ff33
rrnn−2−2 ≥ ≥ rrnn−1−1 + + rrnn ≥ ≥ ff22 + + ff33 = = ff44
……rr22 ≥ ≥ rr33 + + rr44 ≥ ≥ ffnn−1−1 + + ffnn−2−2 = = ffnn
bb = = rr11 ≥ ≥ rr22 + + rr33 ≥ ≥ ffnn + + ffnn−1−1 = = ffnn+1+1..
Thus, if Thus, if nn>2 divisions are used, then >2 divisions are used, then bb ≥ ≥ ffnn+1+1 > > ααnn−1−1. . Thus, logThus, log1010 bb > log > log1010((ααnn−1−1) = () = (nn−1)log−1)log10 10 αα ≈ ( ≈ (nn−1)0.208 > (−1)0.208 > (nn−1)/5.−1)/5.If If bb has has kk decimal digits, then log decimal digits, then log1010 bb < < kk, so , so nn−1−1 < 5< 5kk, so , so n ≤ n ≤ 55k.k.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
15/38
Module
#16 -
Recu
rsio
n
Recursively Defined SetsRecursively Defined Sets
An infinite set An infinite set SS may be defined may be defined recursively, by giving:recursively, by giving:
i)i) A small finite set of A small finite set of basebase elements of elements of SS..ii)ii) A rule for constructing new elements of A rule for constructing new elements of
SS from previously-established elements. from previously-established elements.iii)iii) Implicitly, Implicitly, SS has no other elements but has no other elements but
these.these.
Ex.Ex. Let 3 Let 3SS, and let , and let xx++yySS if if xx,,yySS..What is What is SS??
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
16/38
Module
#16 -
Recu
rsio
n
The Set of All StringsThe Set of All Strings
Def. Def. Given an alphabet Given an alphabet ΣΣ, the set , the set ΣΣ** of of all strings over all strings over ΣΣ can be recursively can be recursively defined by:defined by:
λλ ΣΣ** ( (λλ :≡ “”, the empty string) :≡ “”, the empty string)
ww ΣΣ* * xx ΣΣ → → wxwx ΣΣ**
Exercise: Prove that this definition is Exercise: Prove that this definition is equivalent to our old one: equivalent to our old one:
N
n
n:
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
17/38
Module
#16 -
Recu
rsio
n
Other Easy String ExamplesOther Easy String Examples
Give recursive definitions for:Give recursive definitions for:
• The concatenation of strings The concatenation of strings ww11··ww22..
• The length The length ((ww) of a string ) of a string ww..• Well-formed formulae of propositional Well-formed formulae of propositional
logic involving logic involving TT, , FF, propositional , propositional variables, and operators in {¬, variables, and operators in {¬, , , , →, ↔}., →, ↔}.
• Well-formed arithmetic formulae involving Well-formed arithmetic formulae involving variables, numerals, and opvariables, numerals, and operationserations in {+, in {+, −, *, ↑}.−, *, ↑}.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
18/38
Module
#16 -
Recu
rsio
n
Rooted TreesRooted Trees
Trees will be covered in more depth in chapter Trees will be covered in more depth in chapter 9.9.
Briefly, a Briefly, a treetree is a graph in which there is exactly one is a graph in which there is exactly one undirected path between each pair of nodes.undirected path between each pair of nodes.
An An undirected graphundirected graph can be represented as a set of can be represented as a set of unordered pairs (called unordered pairs (called edgesedges) of objects called ) of objects called nodesnodes..
Definition of the set of Definition of the set of rooted treesrooted trees::i)i) Any single node Any single node rr is a rooted tree. is a rooted tree.ii)ii) If If TT11, …, , …, TTnn are disjoint rooted trees with are disjoint rooted trees with
respective roots respective roots rr11, …, , …, rrnn, and , and rr is a node not in is a node not in any of the any of the TTii’s, then another rooted tree is ’s, then another rooted tree is {{{{rr,,rr11},…,{},…,{rr,,rrnn}}}}TT11……TTnn..
iii)iii) That is all.That is all.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
19/38
Module
#16 -
Recu
rsio
n
Illustrating Rooted Tree Illustrating Rooted Tree Def’n.Def’n.How rooted trees can be combined to How rooted trees can be combined to
form a new rooted tree…form a new rooted tree…
T1T1
r1
T2T2
r2
TnTnrn
r
…Draw some examples…
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
20/38
Module
#16 -
Recu
rsio
n
Extended Binary TreesExtended Binary Trees
A special case of rooted trees.A special case of rooted trees.Def. EDef. Extended binary treextended binary trees (s (EBTEBT))::
i)i) The empty set The empty set is an is an EBTEBT..
ii)ii) If If TT11,,TT22 are disjoint EBTs, are disjoint EBTs, then then ee11ee22 TT11TT22 is an EBT, is an EBT, where where ee11 = = if if TT11 = = , and , and ee11 = {( = {(rr,,rr11)} if )} if TT11≠≠ and and has root has root rr11, and , and similarly for similarly for ee22..
iii)iii) That is all.That is all.Draw some examples…
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
21/38
Module
#16 -
Recu
rsio
n
Full Binary TreesFull Binary Trees
A special case of extended binary trees.A special case of extended binary trees.Def. Def. Recursive definition of Recursive definition of full binary treefull binary treess ((FBTFBT))::
i) i) A single node A single node rr is a is a FBTFBT..Note this is different from the EBT base case.Note this is different from the EBT base case.
ii) ii) If If TT11,,TT22 are disjoint FBTs, are disjoint FBTs, then then ee11ee22TT11TT22, , where where ee11 = = if if TT11 = = , and , and ee11 = {( = {(rr,,rr11)} if )} if TT11≠≠ and and has root has root rr11, and , and similarly for similarly for ee22..
Note this is the same as the EBT recursive case!Note this is the same as the EBT recursive case!Can simplify it to “If Can simplify it to “If TT11,,TT22 are disjoint FBTs with are disjoint FBTs with
roots roots rr11 and and rr22, then {(, then {(rr, , rr11),(),(rr,,rr22)})} TT11TT22 is an is an FBT.”FBT.”
i) That is all.i) That is all.Draw some examples…
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
22/38
Module
#16 -
Recu
rsio
n
Structural InductionStructural Induction
Proving something about a recursively Proving something about a recursively defined object using an inductive defined object using an inductive proof whose structure mirrors the proof whose structure mirrors the object’s definition.object’s definition.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
23/38
Module
#16 -
Recu
rsio
n
ExampleExampleThm. Thm. Let 3Let 3SS, and let , and let xx++yySS if if xx,,yySS,, and that is all and that is all. .
Let Let AA = { = {nnℤℤ++| (3|| (3|nn)}. )}. Than Than AA==SS. .
ProofProof.. We show that We show that AASS and and SSAA..To show To show AASS, show [, show [nnℤℤ++ (3| (3|nn)])]→ → nnSS..
Inductive proof.Inductive proof. Let Let PP((nn) :≡ ) :≡ nnSS. Induction over . Induction over positive multiples of 3. positive multiples of 3. Base caseBase case.. nn=3, thus 3=3, thus 3SS by by def’n. of def’n. of SS. . Inductive stepInductive step.. Given Given PP((nn), prove ), prove PP((nn+3). By inductive +3). By inductive hyp., hyp., nnSS, and 3, and 3SS, so by def’n of , so by def’n of SS, , 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. .
BaBase casese case.. 3 3SS . Since 3|3, . Since 3|3, 33A.A. RRecursive stepecursive step.. x,y x,y SS,, nn==xx++yy SS and and x,y x,y A A . Since 3|. Since 3|xx and 3|and 3|yy, we have 3|(, we have 3|(xx++yy), thus ), thus xx++yy == n n AA..
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
24/38
Module
#16 -
Recu
rsio
n
stopstop
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
25/38
Module
#16 -
Recu
rsio
n
Recursive Algorithms (Recursive Algorithms (§3.5)§3.5)
Recursive definitions can be used to Recursive definitions can be used to describe describe algorithmsalgorithms as well as as well as functions and sets.functions and sets.
Ex.Ex. A procedure to compute A procedure to compute aann..procedureprocedure powerpower((aa≠≠0: real, 0: real, nnℕℕ))
ifif n n = 0 = 0 then return then return 11elseelse returnreturn a a · · powerpower((aa, , nn−1)−1)
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
26/38
Module
#16 -
Recu
rsio
n
Efficiency of Recursive Efficiency of Recursive AlgorithmsAlgorithmsThe 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.
Ex.Ex. Modular exponentiationModular exponentiation to a power to a power nn can take log(can take log(nn) time if done right, but ) time if done right, but linear time if done slightly differently.linear time if done slightly differently.Task: Compute Task: Compute bbnn modmod mm, where, where
mm≥2, ≥2, nn≥0, and 1≤≥0, and 1≤bb<<mm..
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
27/38
Module
#16 -
Recu
rsio
n
Modular Exponentiation Alg. Modular Exponentiation Alg. #1#1Uses the fact that Uses the fact that bbnn = = b·bb·bnn−1−1 and that and that
x·yx·y mod mod m = x·m = x·((yy modmod mm) ) mod mod mm..(Prove the latter theorem at home.)(Prove the latter theorem at home.)
procedure procedure mpowermpower((bb≥1≥1,,nn≥0≥0,,m>b m>b ℕℕ)){Returns {Returns bbnn mod mod mm.}.}ifif nn=0 =0
then return then return 1 1 elseelse return return ((bb·mpower·mpower((bb,,nn−1,−1,mm)) )) modmod mm
Note this algorithm takes Note this algorithm takes ΘΘ((nn) steps!) steps!
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
28/38
Module
#16 -
Recu
rsio
n
Modular Exponentiation Alg. Modular Exponentiation Alg. #2#2Uses the fact that Uses the fact that bb22kk = = bbkk·2·2 = ( = (bbkk))22..
procedureprocedure mpowermpower((bb,,nn,,mm) ) {same signature}{same signature}
if if nn=0 =0 then return then return 11else ifelse if 2| 2|nn
thenthen returnreturn mpowermpower((bb,,nn/2,/2,mm))22 modmod mm
else returnelse return ( (mpowermpower((bb,,nn−1,−1,mm)·)·bb) ) modmod mm
What is its time complexity?What is its time complexity?Θ(log n) steps
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
29/38
Module
#16 -
Recu
rsio
n
A Slight VariationA Slight Variation
Nearly identical but takes Nearly identical but takes ΘΘ((nn) time ) time instead!instead!
procedureprocedure mpowermpower((bb,,nn,,mm) ) {same signature}{same signature}
if if nn=0 =0 then return then return 11else ifelse if 2| 2|nn
thenthen returnreturn ( (mpowermpower((bb,,nn/2,/2,mm)·)· mpowermpower((bb,,nn/2,/2,mm)) )) modmod mm
else returnelse return ( (mpowermpower((bb,,nn−1,−1,mm)·)·bb) ) modmod mm
The number of recursive calls made is critical!
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
30/38
Module
#16 -
Recu
rsio
n
Recursive Euclid’s AlgorithmRecursive Euclid’s Algorithm
procedureprocedure gcdgcd((aa,,bbNN))ifif aa = 0 = 0
thenthen return return bbelse returnelse return gcdgcd((bb modmod aa, , aa))
Note recursive algorithms are often Note recursive algorithms are often simpler to code than iterative ones… simpler to code than iterative ones…
However, they can consume more However, they can consume more stack space, if your compiler is not stack space, if your compiler is not smart enough.smart enough.
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
31/38
Module
#16 -
Recu
rsio
n
Recursive Linear SearchRecursive Linear Search
Note there is no real advantage to using Note there is no real advantage to using recursion here, rather than just looping recursion here, rather than just looping forfor loc loc := := ii to to j…j…
procedureprocedure searchsearch((aa: series; : series; ii, , jj: integer;: integer;xx: item to be found): item to be found)
{Find {Find xx in series in series aa at a location at a location ≥i≥i and and <<jj}}
if if aaii = = xx then then returnreturn i i {At the right item? Return it!}{At the right item? Return it!}
ifif ii = = jj then then returnreturn 0 0 {No locations in range? Failure!}{No locations in range? Failure!}
returnreturn searchsearch((ii+1, +1, jj, , xx) ) {Try rest of range}{Try rest of range}
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
32/38
Module
#16 -
Recu
rsio
n
Recursive Binary SearchRecursive Binary Search
procedureprocedure binarySearchbinarySearch((aa, , xx, , ii, , jj) ) {same sig}{same sig}{Find location of {Find location of xx in in aa, , ≥≥ii and < and <jj}} mm := := ((ii + + jj)/2)/2 {Go to halfway point.}{Go to halfway point.}
if if xx = = aamm then then returnreturn mm {Did we luck out?}{Did we luck out?}
ifif xx<<aamm ii<<mm {If it’s to the left,}{If it’s to the left,} then then returnreturn binarySearchbinarySearch((aa,,xx,,ii,,mm−1)−1)
{Check that ½}{Check that ½}
else ifelse if aamm<<xx mm<<jj {If it’s to right,}{If it’s to right,}
then then return return binarySearchbinarySearch((aa,,xx,,mm+1,+1,jj)) {Check that ½}{Check that ½}
elseelse return 0return 0 {No more items, failure.}{No more items, failure.}
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
33/38
Module
#16 -
Recu
rsio
n
Recursive Fibonacci Recursive Fibonacci AlgorithmAlgorithmprocedureprocedure fibonaccifibonacci((nn NN))ifif nn=0 =0
then then returnreturn 0 0ifif nn=1 =1
then then returnreturn 1 1returnreturn fibonaccifibonacci((nn−1)+−1)+fibonaccifibonacci((nn−2)−2)
Is this an efficient algorithm?Is this an efficient algorithm?Is it polynomial-time in Is it polynomial-time in nn??
How many additions are performed?How many additions are performed?
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
34/38
Module
#16 -
Recu
rsio
n
Analysis of Fibonacci Analysis of Fibonacci ProcedureProcedureThm.Thm. The preceding procedure for The preceding procedure for fibonaccifibonacci((nn) )
performs performs ffnn+1+1−1 addition operations.−1 addition operations.
ProofProof.. By strong structural induction over By strong structural induction over nn, , based on the procedure’s own recursive based on the procedure’s own recursive definition.definition.
Base cases:Base cases: fibonaccifibonacci(0) performs 0 additions, and (0) performs 0 additions, and ff0+10+1−1 = −1 = ff11 − 1 = 1 − 1 = 0. Likewise, − 1 = 1 − 1 = 0. Likewise, fibonaccifibonacci(1) performs (1) performs 0 additions, and 0 additions, and ff1+11+1−1 = −1 = ff22−1 = 1−1 = 0.−1 = 1−1 = 0.
Inductive step: Inductive step: For For nn>1, by strong inductive >1, by strong inductive hypothesis, hypothesis, fibonaccifibonacci((nn−1) and −1) and fibonaccifibonacci((nn−2) do −2) do ffnn−1 and −1 and ffnn−1−1−1 additions respectively, and −1 additions respectively, and fibonaccifibonacci((nn) adds 1 more, for a total of ) adds 1 more, for a total of ffnn−1+ −1+ ffnn−1−1−1+1 = −1+1 = ffnn++ffnn−1−1+1 = +1 = ffnn+1+1+1. ■+1. ■
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
35/38
Module
#16 -
Recu
rsio
n
A more efficient algorithmA more efficient algorithm
procedureprocedure findFibfindFib((aa,,bb,,mm,,nn)){Given {Given aa==ffmm−1−1, , bb==ffmm, and , and mm≤≤nn, return , return ffnn}}ifif mm==nn
then then returnreturn bbreturnreturn findFibfindFib((bb, , a+ba+b, , mm+1, +1, nn))
procedureprocedure fastFibfastFib((nn) ) {Find {Find ffnn in in ΘΘ((nn) steps.}) steps.}ifif nn=0=0
then then return 0return 0returnreturn findFibfindFib(0,1,1,(0,1,1,nn))
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
36/38
Module
#16 -
Recu
rsio
n
Recursive Merge SortRecursive Merge Sort
procedure procedure sortsort((LL = = 11,…, ,…, nn))ifif nn>1 >1
thenthen { {mm := := nn/2/2 {this is rough ½-way point}{this is rough ½-way point}
LL := := mergemerge((sortsort((11,…, ,…, mm),), sortsort((m+m+11,…, ,…, nn))))
}}returnreturn L L
The merge (next slide) takes The merge (next slide) takes ΘΘ((nn) steps, ) steps, and merge-sort takes and merge-sort takes ΘΘ((nn log log nn).).
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
37/38
Module
#16 -
Recu
rsio
n
Recursive Merge MethodRecursive Merge Method
procedureprocedure mergemerge((AA,,BB: sorted lists): sorted lists){Given two sorted lists {Given two sorted lists AA=(=(aa11,…,,…,aa||AA||), ), BB=(=(bb11,…,,…,bb||BB||), return a sorted list of ), return a sorted list of all.}all.}ifif AA = () = ()
then then returnreturn B B {If {If AA is empty, it’s is empty, it’s BB.}.}ifif BB = () = ()
then then returnreturn A A {If {If BB is empty, it’s is empty, it’s AA.}.}
ifif aa11<<bb11 then then return return ((aa11,, mergemerge((((aa22,…,…aa||AA||),), BB))))else else returnreturn ( (bb11,, mergemerge((AA,(,(bb22,…,,…,bb||BB||))))))
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
38/38
Module
#16 -
Recu
rsio
n
Merge RoutineMerge Routineprocedureprocedure mergemerge((AA, , BB: sorted lists): sorted lists)
LL = empty list = empty listii:=0, :=0, jj:=0, :=0, kk:=0:=0whilewhile ii<|<|AA| | jj<|<|BB| {|| {|AA| is length of | is length of AA}} ifif ii=|=|AA| | thenthen {{
LLkk := := BBjj; ; jj := := jj + 1 + 1
} } else if else if jj=|=|BB| | thenthen {{LLkk := := AAii; ; ii := := i i + 1+ 1
} } else if else if AAii < B < Bjj then then {{LLkk := := AAii; ; ii := := i i + 1+ 1
} } elseelse {{
LLkk := := BBjj; ; jj := := jj + 1 + 1
}}kk := := kk+1+1
returnreturn LLTakes Θ(|A|+|B|) time
Base
d o
n R
ose
n, D
iscr
ete
Math
em
ati
cs &
Its
A
pp
licati
ons,
5e
Pre
pare
d b
y (
c)2
00
1-2
00
4 M
ichael P. Fra
nk
Mod
ified
by (
c) 2
00
4-2
00
5 H
alu
k B
ing
öl
39/38
Module
#16 -
Recu
rsio
n
ReferencesReferences
• RosenRosenDiscrete Mathematics and its Discrete Mathematics and its Applications, 5eApplications, 5eMc GrawHill, 2003Mc GrawHill, 2003