module #14: recursion rosen 5 th ed., §§3.4-3.5 in this class, we will study recursion, one of the...

23
Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 ass, we will study recursion, one of the most impor r science. t class, we talk about mathematical induction: ) is true, P(n) is also true rsion is a little bit more advanced concept than in

Upload: domenic-hampton

Post on 13-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Module #14:Recursion

Rosen 5th ed., §§3.4-3.5

In this class, we will study recursion, one of the most important topicsin computer science.In the last class, we talk about mathematical induction: when P(n-1) is true, P(n) is also true

Now recursion is a little bit more advanced concept than induction

Page 2: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Why Recursion?

• Sometimes it is difficult to define an object explicitly

• However, it may be easy to define this object in terms of itself

• This process is called recursion

An object can be any mathematical concept, a function, a set, a sequence, even an algorithm.

An object is explained or described using something about itself or some part of itself.

Page 3: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursive Definitions• In induction, we prove all members of an infinite

set have some property P by proving the truth for larger members in terms of that of smaller members.

• In recursive definitions, we similarly define a function, a predicate or a set over an infinite number of elements by defining the function or predicate value or set-membership of larger elements in terms of that of smaller ones.

This slide shows similarity and difference between mathematicalinduction and recursion

The truth for larger member can be derived from the truth for smaller memberIn recursion, that relationship is generalized for any mathematical object

In mathematical induction we talk about some properties of a sequence

Page 4: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursion

• Recursion is a general term for the practice of defining an object in terms of itself (or of part of itself).

• An inductive proof establishes the truth of P(n+1) recursively in terms of P(n).

• There are also recursive algorithms, definitions, functions, sequences, and sets.

Page 5: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursively Defined Functions

• Simplest case: One way to define a function f:NS (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 recursively:

– Let a0 :≡ 1.

– For n>0, let an :≡ 2an-1.

Recurrence relation in Ch. 6

Page 6: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Another Example

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

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

Page 7: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursive definition of Factorial

• Give an inductive definition of the factorial function F(n) :≡ n! :≡ 23…n.– Base case: F(0) :≡ 1– Recursive part: F(n) :≡ n F(n-1).

• F(1)=1• F(2)=2• F(3)=6

Page 8: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

The Fibonacci Series

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

f0 :≡ 0, f1 :≡ 1, fn≥2 :≡ fn−1 + fn−2

01 1

2 35 8

13

Page 9: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Inductive Proof about Fib. series

• Theorem: fn < 2n.

• Proof: By induction.

Base cases: f0 = 0 < 20 = 1f1 = 1 < 21 = 2

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. ■

Note use ofbase cases ofrecursive def’n.

Implicitly for all nN

Page 10: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursively Defined Sets

• 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.

• Example: Let 3S, and let x+yS if x,yS.What is S?

Page 11: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

The Set of All Strings

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

ε Σ* (ε :≡ “”, the empty string)

w Σ* x Σ → wx Σ*

textbookuses λ

instead of ε

Page 12: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursive Algorithms (§3.5)

• Recursive definitions can be used to describe algorithms as well as functions and sets.

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

• Example: A procedure to compute an.

procedure power(a≠0: real, nN)

if n = 0 then return 1else return a · power(a, n−1)

Page 13: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Efficiency of Recursive Algorithms

• The time complexity of a recursive algorithm may depend critically on the number of recursive calls it makes.

• Example: Modular exponentiation to a power n can take log(n) time if done right, but linear time if done slightly differently.– Task: Compute bn mod m, where

m≥2, n≥0, and 1≤b<m.

Page 14: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Modular Exponentiation Alg. #1

Uses the fact that bn = b·bn−1 and that x·y mod m = x·(y mod m) mod m.(Prove the latter theorem at home.)

procedure mpower(b≥1,n≥0,m>b N){Returns bn mod m.}if n=0 then return 1 elsereturn (b·mpower(b,n−1,m)) mod m

Note this algorithm takes Θ(n) steps!

Page 15: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Modular Exponentiation Alg. #2

• Uses the fact that b2k = bk·2 = (bk)2.• x·y mod m = (x mod m)·(y mod m) mod m

procedure mpower(b,n,m) {same signature}

if n=0 then return 1else if 2|n then

return mpower(b,n/2,m)2 mod melse return (mpower(b,n−1,m)·b) mod m

What is its time complexity?

Θ(log n) steps

Page 16: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

A Slight Variation

Nearly identical but takes Θ(n) time instead!

procedure mpower(b,n,m) {same signature}

if n=0 then return 1else if 2|n then

return (mpower(b,n/2,m)· mpower(b,n/2,m)) mod melse return (mpower(b,n−1,m)·b) mod m

The number of recursive calls made is critical.

Page 17: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Recursive Euclid’s Algorithm

procedure gcd(a,bN)if a = 0 then return belse return gcd(b mod a, a)

• Note recursive algorithms are often simpler to code than iterative ones…

• However, they can consume more stack space, if your compiler is not smart enough.

Page 18: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Merge Sort

Strategy: Recursively split the list in half and merge the two returned segments.

Page 19: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

12 272516 2915145

12 16 2725 145 2915

12 2915145272516A =

12 16 25 27 5 14 15 29

12 16 25 27 5 14 15 29

12 16 25 27 5 14 15 29

5 12 14 15 16 25 27 29splitmerge

Page 20: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

12 2915145272516A =

B =

if (A[first] < A[last])

B[next] = A[first];

first++;

else

B[next] = A[last];

last++;

next++;

first last

next

5 12 14 15 16 25 27 29

Merge Process

Page 21: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

12 272516 2915145

12 16 2725 145 2915

12 16 25 27 5 14 15 29

12 2915145272516A =

Levels = ?log(n)

Work = ?n

Total Complexity =

n*log(n)

Number of compares at each level during merge

Page 22: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Merge Sort

procedure sort(L = 1,…, n)if n>1 then

m := n/2 {this is rough ½-way point}L := merge(sort(1,…, m),

sort(m+1,…, n))return L

• The merge takes Θ(n) steps, and merge-sort takes Θ(n log n).

Page 23: Module #14: Recursion Rosen 5 th ed., §§3.4-3.5 In this class, we will study recursion, one of the most important topics in computer science. In the last

Merge Routine

procedure merge(A, B: sorted lists)L = empty listi:=0, j:=0, k:=0 (i:first, j:last, k:next in slide 20)while i<|A| v j<|B| {|A| is length of A} if i=|A| then Lk := Bj; j := j + 1

else if j=|B| then Lk := Ai; i := i + 1else if Ai < Bj then Lk := Ai; i := i + 1else Lk := Bj; j := j + 1k := k+1

return LTakes Θ(|A|+|B|) time