Recursion Rosen 5 th ed., §3.4-3.5. Recursion Sometimes, defining an object explicitly might be...

Post on 19-Dec-2015

215 views 1 download

Transcript of Recursion Rosen 5 th ed., §3.4-3.5. Recursion Sometimes, defining an object explicitly might be...

Recursion

Rosen 5Rosen 5thth ed., §3.4-3.5 ed., §3.4-3.5

Recursion

• SometimesSometimes, defining , defining an object explicitly might be an object explicitly might be difficult.difficult.

• RecursionRecursion is a process of defining an object in is a process of defining an object in terms of terms of itselfitself (or of part of itself). (or of part of itself).

• Recursion can be used to define: Recursion can be used to define: – FunctionsFunctions– SequencesSequences– SetsSets– ProblemsProblems– AAlgorithmslgorithms

Recursion vs Induction

• Using recursion, we Using recursion, we definedefine an object (e.g., a an object (e.g., a function, a predicate or a set) over an function, a predicate or a set) over an infinite number of elements by infinite number of elements by defining defining large size objects in terms of smaller size large size objects in terms of smaller size ones.ones.

• In induction, we In induction, we proveprove all members of an all members of an infinite set have some predicate infinite set have some predicate PP by by proving the truth for large size objects in proving the truth for large size objects in terms of smaller size onesterms of smaller size ones..

Recursively Defined Functions

• One way to define a function One way to define a function ff::NNSS (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).

• E.g.E.g.: 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..

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

Use Recursive Definition to Prove Various Properties

• Theorem: Theorem: ffnn < < 22nn..• Proof:Proof: By induction. By induction.

Base cases:Base cases: ff00 = 0 < 2 = 0 < 200 = 1 = 1ff11 = 1 < 2 = 1 < 211 = 2 = 2

Inductive step: Use Inductive step: Use strongstrong induction. induction.

Assume 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 nN

Recursively Defined Sets

• An infinite set An infinite set SS may be defined may be defined recursively, by giving:recursively, by giving:– A small finite set of A small 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 but these. has no other elements but these.

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

Recursive Problems and Algorithms (§3.5)

• Sometimes, the best way to solve a problem Sometimes, the best way to solve a problem is by solving a is by solving a smaller versionsmaller version of the exact of the exact same problem first.same problem first.

• Recursion is a technique that solves a Recursion is a technique that solves a problem by solving a problem by solving a smaller problemsmaller problem of of the same type.the same type.

• There are many problems whose solution can There are many problems whose solution can be defined recursively:be defined recursively:

ExampleExample: : n factorialn factorial

Problems defined recursively

1 if n = 0n!= (recursive solution)

(n-1)!*n if n > 0

1 if n = 0n!= (closed form solution)

1*2*3*…*(n-1)*n if n > 0

Coding the factorial function

• Recursive implementationRecursive implementation

int Factorial(int n)int Factorial(int n){{ if (n==0) if (n==0) // base case// base case return 1;return 1; elseelse return n * Factorial(n-1);return n * Factorial(n-1);}}

Another example: n choose k (combinations)

• Given Given nn things, how many different sets of things, how many different sets of size size kk can be chosen? can be chosen?

nn n-1 n-1 n-1 n-1 == ++ , 1 < k < n , 1 < k < n (recursive solution)(recursive solution)kk k k k-1 k-1

nn n! n! == , 1 < k < n , 1 < k < n (closed-form (closed-form solution)solution)kk k!(n-k)! k!(n-k)!

with base cases:with base cases:

nn n n = n (k = 1), = n (k = 1), = 1 (k = n)= 1 (k = n)

11 n n

int Combinations(int n, int k)int Combinations(int n, int k)

{{

if(k == 1) if(k == 1) // base case 1// base case 1

return n;return n;

else if (n == k) else if (n == k) // base case 2// base case 2

return 1;return 1;

elseelse

return(Combinations(n-1, k) + Combinations(n-1, k-1));return(Combinations(n-1, k) + Combinations(n-1, k-1));

}}

n choose k (combinations)

n choose k (combinations)

How do I write a recursive function?

• Determine the Determine the size factorsize factor• Determine the Determine the base case(s)base case(s)

(the one(s) for which you know the answer)(the one(s) for which you know the answer)

• Determine the Determine the general case(sgeneral case(s) ) (the one(s) where the problem is expressed as a (the one(s) where the problem is expressed as a smaller version of itself)smaller version of itself)

• Verify the algorithm Verify the algorithm (use the "Three-Question-Method")(use the "Three-Question-Method")

Three-Question Verification Method

1.1. The Base-Case Question:The Base-Case Question:Is there a non-recursive way out of the function, Is there a non-recursive way out of the function, and does the routine work correctly for this and does the routine work correctly for this "base" case? "base" case? 

2.2. The Smaller-Caller Question:The Smaller-Caller Question:Does each recursive call to the function involve a Does each recursive call to the function involve a smaller case of the original problem, leading smaller case of the original problem, leading inescapably to the base case? inescapably to the base case? 

3.3. The General-Case Question:The General-Case Question:Assuming that the recursive call(s) work Assuming that the recursive call(s) work correctly, does the whole function work correctly, does the whole function work correctly?correctly?

Compute an recursively

• Some algorithms are intuitively iterative, Some algorithms are intuitively iterative, however, they could be written recursively however, they could be written recursively too! too!

procedureprocedure powerpower((a≠a≠0: real, 0: real, nnNN))

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

Recursive Linear Search

• Another exampleAnother example

procedureprocedure searchsearch((i, j, xi, j, x))

ifif =x=x then then

location:= i else if i=j then

location:=0

elseelse searchsearch((i+1, j, xi+1, j, x))

ia

....i j

• What is the What is the size factorsize factor??The number of elements in (The number of elements in (info[first] ... info[last]info[first] ... info[last]))

• What is the What is the base case(s)base case(s)?? (1) If (1) If first > lastfirst > last, return , return falsefalse (2) If (2) If item==info[midPoint]item==info[midPoint], return , return truetrue

• What is the What is the general casegeneral case??if if item < info[midPoint] item < info[midPoint] search the first halfsearch the first halfif if item > info[midPoint]item > info[midPoint], , search the second halfsearch the second half

Recursive binary search

bool bool BinarySearchBinarySearch(A[], item, first, last)(A[], item, first, last){{ int midPoint;int midPoint;  

if(first > last) if(first > last) // base case 1// base case 1 return false;return false; else {else { midPoint = (first + last)/2;midPoint = (first + last)/2; if(item < A[midPoint])if(item < A[midPoint]) return BinarySearch(A, item, first, midPoint-1);return BinarySearch(A, item, first, midPoint-1); else if (item == A[midPoint]) { else if (item == A[midPoint]) { // base case 2// base case 2 item = A[midPoint];item = A[midPoint]; return true;return true; }} elseelse return BinarySearch(A, item, midPoint+1, last);return BinarySearch(A, item, midPoint+1, last); }}}}

Recursive binary search

Divide and Conquer

MergeSort

MergeSort (cont’d)

MergeSort (cont’d)

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

Efficiency of Recursive Algorithms

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

• What happens when a function gets called?What happens when a function gets called?

int f(int x)int f(int x){{

int y;int y;  

if(x==0)if(x==0) return 1;return 1; else {else { y = 2 * f(x-1);y = 2 * f(x-1); return y+1;return y+1; }}

}}

=f(3)

=f(2)

=f(1)

2*f(2)

2*f(1)

2*f(1)

=f(0)

Recursion vs. Iteration• Iteration can be used in place of recursionIteration can be used in place of recursion

– An iterative algorithm uses a An iterative algorithm uses a looping constructlooping construct– A recursive algorithm uses a A recursive algorithm uses a branching structurebranching structure

• Recursive solutions are often less efficient, in Recursive solutions are often less efficient, in terms of both terms of both timetime and and spacespace, than iterative , than iterative solutionssolutions

• Recursion can simplify the solution of a problem, Recursion can simplify the solution of a problem, often resulting in often resulting in shortershorter, more easily understood , more easily understood source codesource code

Recursion can be very inefficient is some cases

+=

=

=

=

=

=

+ +

+ + + +

++ + + + +

+

+

+

+

+ + +

+ + + + + + +

+ +

+

++++++++3

3

C om b (3 , 1 )

2

C om b (2 , 1 )

1

C om b (2 , 2 )

C om b (3 , 2 )

C om b (4 ,2 )

2

C om b (2 , 1 )

1

C om b (2 , 2 )

C om b (3 , 2 )

1

1

C om b (3 , 3 )

C om b (4 , 3 )

C om b (5 , 3 )

2

C om b (2 , 1 )

1

C om b (2 , 2 )

C om b (3 , 2 )

1

1

C om b (3 , 3 )

C om b (4 , 3 )

1

1

1

C om b (4 , 4 )

C om b (5 , 4 )

C om b (6 ,4 )

15

Deciding whether to use a recursive solution

• When the When the depth depth of recursive calls is of recursive calls is relatively "shallow"relatively "shallow"

• The recursive version does about the The recursive version does about the same same amount of workamount of work as the non-recursive as the non-recursive versionversion

• The recursive version is The recursive version is shorter and simplershorter and simpler than the than the nonrecursive solutionnonrecursive solution