csc211 data structures lecture 14 reasoning about recursion instructor: prof. xiaoyan li department...

28
CSC211 Data Structures Lecture 14 Lecture 14 Reasoning about Reasoning about Recursion Recursion Instructor: Prof. Xiaoyan Instructor: Prof. Xiaoyan Li Li Department of Computer Department of Computer Science Science Mount Holyoke College Mount Holyoke College

Upload: andrew-greer

Post on 21-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

CSC211 Data Structures CSC211 Data Structures

Lecture 14Lecture 14Reasoning about RecursionReasoning about Recursion

Instructor: Prof. Xiaoyan LiInstructor: Prof. Xiaoyan LiDepartment of Computer Science Department of Computer Science

Mount Holyoke CollegeMount Holyoke College

Page 2: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Outline of This LectureOutline of This Lecture

Recursive Thinking: General FormRecursive Thinking: General Form recursive calls and stopping casesrecursive calls and stopping cases

Infinite Recursion Infinite Recursion runs forever runs forever

One Level Recursion One Level Recursion guarantees to have no infinite recursionguarantees to have no infinite recursion

How to Ensure No Infinite RecursionHow to Ensure No Infinite Recursion if a function has multi level recursionif a function has multi level recursion

Inductive Reasoning about CorrectnessInductive Reasoning about Correctness using mathematical induction principleusing mathematical induction principle

Page 3: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Recursive Thinking: General FormRecursive Thinking: General Form

Recursive Calls Recursive Calls Suppose a problem has one or more cases in which Suppose a problem has one or more cases in which

some of the subtasks are simpler versions of the some of the subtasks are simpler versions of the original problem. These subtasks can be solved by original problem. These subtasks can be solved by recursive callsrecursive calls

Stopping Cases /Base CasesStopping Cases /Base Cases A function that makes recursive calls must have one or A function that makes recursive calls must have one or

more cases in which the entire computation is fulfilled more cases in which the entire computation is fulfilled without recursion. These cases are called stopping without recursion. These cases are called stopping cases or base casescases or base cases

Page 4: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Infinite Recursion Infinite Recursion

In all our examples, the series of recursive In all our examples, the series of recursive calls eventually reached a calls eventually reached a stopping casestopping case, i.e. , i.e. a call that did not involve further recursion a call that did not involve further recursion

If every recursive call produce another If every recursive call produce another recursive call, then the recursion is an recursive call, then the recursion is an infinite recursioninfinite recursion that will, in theory, run that will, in theory, run forever.forever.

Can you write one? Can you write one?

Page 5: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Example: power (x,n) = xnExample: power (x,n) = xn

Rules:Rules: power(3.0,2) = 3.0power(3.0,2) = 3.022 = 9.0 = 9.0 power(4.0, 3) = 4.0power(4.0, 3) = 4.033 = 64.0 = 64.0 power(x, 0) = xpower(x, 0) = x0 0 = 1 if x != 0= 1 if x != 0 xx-n-n = 1/ x = 1/ xn n where x<>0, n > 0 where x<>0, n > 0

power(3.0, -2) = 3.0power(3.0, -2) = 3.0-2-2 = 1/3.0 = 1/3.022= 1/9= 1/9 00n n

= 0 if n > 0= 0 if n > 0 invalid if n<=0 (and x == 0) invalid if n<=0 (and x == 0)

Page 6: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

ipower(x, n): Infinite Recursionipower(x, n): Infinite Recursion

double ipower(double x, int n)// Library facilities used: cassert { if (x == 0) assert(n > 0); //precondition

if (n >= 0) { return ipower(x,n); // postcondition 1 } else { return 1/ipower(x, -n); // postcondition 2 }}

Computes powers of the form xn

Page 7: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

ipower(x, n): Infinite Recursionipower(x, n): Infinite Recursion

double ipower(double x, int n)// Library facilities used: cassert { if (x == 0) assert(n > 0); //precondition

if (n >= 0) { return ipower(x,n); // need to be developed into a stopping case, how? } else { return 1/ipower(x, -n); // recursive call }}

Computes powers of the form xn

Page 8: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

ipower(x, n): Infinite Recursionipower(x, n): Infinite Recursion

double ipower(double x, int n)// Library facilities used: cassert { if (x == 0) assert(n > 0); //precondition

if (n >= 0) { return ipower(x,n); // need to be developed into a stopping case } else { return 1/ipower(x, -n); // recursive call }}

Computes powers of the form xn

double product =1;for (int i = 1; i<=n; ++i)

product *= x;return product;

Page 9: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

power(x, n): One Level Recursionpower(x, n): One Level Recursion

double power(double x, int n)// Library facilities used: cassert { double product; // The product of x with itself n times int count; if (x == 0) assert(n > 0); if (n >= 0) // stopping case { product = 1; for (count = 1; count <= n; count++) product = product * x; return product; } else // recursive call return 1/power(x, -n);}

Computes powers of the form xn

Page 10: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

One Level RecursionOne Level Recursion

First general technique for reasoning about First general technique for reasoning about recursion:recursion: Suppose that every case is either a stopping Suppose that every case is either a stopping

case or it makes a recursive call that is a case or it makes a recursive call that is a stopping case. Then the deepest recursive call is stopping case. Then the deepest recursive call is only one level deep, and no infinite recursion only one level deep, and no infinite recursion occurs.occurs.

Page 11: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Multi-Level RecursionMulti-Level Recursion

In general recursive calls don’t stop a just one In general recursive calls don’t stop a just one level deep – a recursive call does not need to reach level deep – a recursive call does not need to reach a stopping case immediately.a stopping case immediately.

In the last lecture, we have showed two examples In the last lecture, we have showed two examples with multiple level recursionswith multiple level recursions

As an example to show that there is no infinite As an example to show that there is no infinite recursion, we are going to re-write the recursion, we are going to re-write the powerpower function – use a new function name function – use a new function name powpow

Page 12: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

power(x, n) => pow(x,n)power(x, n) => pow(x,n)

double power(double x, int n)// Library facilities used: cassert { double product; // The product of x with itself n times int count; if (x == 0) assert(n > 0); if (n >= 0) // stopping case { product = 1; for (count = 1; count <= n; count++) product = product * x; return product; } else // recursive call return 1/power(x, -n);}

Computes powers of the form xn

change this into a recursive call based on the observation

xn=x xn-1 if n>0

Page 13: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

pow (x, n): Alternate Implementationpow (x, n): Alternate Implementation

double pow(double x, int n)// Library facilities used: cassert{ if (x == 0) { // x is zero, and n should be positive assert(n > 0); return 0; } else if (n == 0) return 1; else if (n > 0) return x * pow(x, n-1); else // x is nonzero, and n is negative return 1/pow(x, -n);}

Computes powers of the form xn

Page 14: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

pow (x, n): Alternate Implementationpow (x, n): Alternate Implementation

double pow(double x, int n)// Library facilities used: cassert{ if (x == 0) { // x is zero, and n should be positive assert(n > 0); return 0; } else if (n == 0) return 1; else if (n > 0) return x * pow(x, n-1); else // x is nonzero, and n is negative return 1/pow(x, -n);}

Computes powers of the form xn

All of the cases:

x n xn

=0 <0 underfined

=0 =0 underfined

=0 > 0 0

!=0 < 0 1/x-n

!=0 = 0 1

!=0 > 0 x*xn-1

Page 15: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

How to ensure NO Infinite Recursion How to ensure NO Infinite Recursion

when the recursive calls go beyond one level deepwhen the recursive calls go beyond one level deep You can ensure that a stopping case is eventually You can ensure that a stopping case is eventually

reached by defining a numeric quantity called reached by defining a numeric quantity called variant expressionvariant expression - without really tracing - without really tracing through the executionthrough the execution

This quantity must associate each legal recursive This quantity must associate each legal recursive call to a single number, which changes for each call to a single number, which changes for each call and eventually satisfies the condition to go to call and eventually satisfies the condition to go to the stopping casethe stopping case

Page 16: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Variant Expression for powVariant Expression for pow

The variant expression is abs(n)+1 when n is The variant expression is abs(n)+1 when n is negative andnegative and

the variant expression is n when n is positivethe variant expression is n when n is positive A sequence of recursion callA sequence of recursion call

pow(2.0, -3) has a variant expression abs(n)+1, pow(2.0, -3) has a variant expression abs(n)+1, which is 4; it makes a recursive call of pow(2.0, 3)which is 4; it makes a recursive call of pow(2.0, 3)

Page 17: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Variant Expression for powVariant Expression for pow

The variant expression is abs(n)+1 when n is The variant expression is abs(n)+1 when n is negative andnegative and

the variant expression is n when n is positivethe variant expression is n when n is positive A sequence of recursion callA sequence of recursion call

pow(2.0, 3) has a variant expression n, pow(2.0, 3) has a variant expression n,

which is 3; it makes a recursive call of pow(2.0, 2)which is 3; it makes a recursive call of pow(2.0, 2)

Page 18: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Variant Expression for powVariant Expression for pow

The variant expression is abs(n)+1 when n is The variant expression is abs(n)+1 when n is negative andnegative and

the variant expression is n when n is positivethe variant expression is n when n is positive A sequence of recursion callA sequence of recursion call

pow(2.0, 2) has a variant expression n, pow(2.0, 2) has a variant expression n,

which is 2; it makes a recursive call of pow(2.0, 1)which is 2; it makes a recursive call of pow(2.0, 1)

Page 19: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Variant Expression for powVariant Expression for pow

The variant expression is abs(n)+1 when n is The variant expression is abs(n)+1 when n is negative andnegative and

the variant expression is n when n is positivethe variant expression is n when n is positive A sequence of recursion callA sequence of recursion call

pow(2.0, 1) has a variant expression n, pow(2.0, 1) has a variant expression n,

which is 1; it makes a recursive call of pow(2.0, 0)which is 1; it makes a recursive call of pow(2.0, 0)

Page 20: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Variant Expression for powVariant Expression for pow

The variant expression is abs(n)+1 when n is The variant expression is abs(n)+1 when n is negative andnegative and

the variant expression is n when n is positivethe variant expression is n when n is positive A sequence of recursion callA sequence of recursion call

pow(2.0, 0) has a variant expression n, pow(2.0, 0) has a variant expression n,

which is 0; this is the stopping case.which is 0; this is the stopping case.

Page 21: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Ensuring NO Infinite RecursionEnsuring NO Infinite Recursion

It is enough to find a variant expression and a It is enough to find a variant expression and a threshold with the following properties (p446):threshold with the following properties (p446): Between one call of the function and any succeeding Between one call of the function and any succeeding

recursive call of that function, the value of the variant recursive call of that function, the value of the variant expression decreases by at least some expression decreases by at least some fixedfixed amount. amount.

What is that fixed amount of pow(x,n)?What is that fixed amount of pow(x,n)? If the function is called and the value of the variant If the function is called and the value of the variant

expression is less than or equal to the expression is less than or equal to the thresholdthreshold, then , then the function terminates without making any recursive the function terminates without making any recursive callcall

What is the threshold of pow(x,n) What is the threshold of pow(x,n)

Page 22: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Questions?Questions?

Can the following variant expression guarantees no infinite Can the following variant expression guarantees no infinite recursion if the threshold is 0?recursion if the threshold is 0?1/n? where n = 1, 2, 3, ...1/n? where n = 1, 2, 3, ...

How about the followingHow about the followingint growing_up(int n1, n2)int growing_up(int n1, n2){{ if (n1 == n2) return 0;if (n1 == n2) return 0;else return 1+growing_up(n1+1, n2);else return 1+growing_up(n1+1, n2);}}

Page 23: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Reasoning about the CorrectnessReasoning about the Correctness

First show NO infinite recursion First show NO infinite recursion thenthen show the show the following two conditions are also valid:following two conditions are also valid: Whenever the function makes no recursive calls, show Whenever the function makes no recursive calls, show

that it meets its pre/post-condition contract (BASE that it meets its pre/post-condition contract (BASE STEP)STEP)

Whenever the function is called, by assuming all the Whenever the function is called, by assuming all the recursive calls it makes meet their pre-post condition recursive calls it makes meet their pre-post condition contracts, show that the original call will also meet its contracts, show that the original call will also meet its pre/post contract (INDUCTION STEP)pre/post contract (INDUCTION STEP)

Page 24: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

pow (x, n): Alternate Implementationpow (x, n): Alternate Implementation

double pow(double x, int n)// Library facilities used: cassert{ if (x == 0) { // x is zero, and n should be positive assert(n > 0); return 0; } else if (n == 0) return 1; else if (n > 0) return x * pow(x, n-1); else // x is nonzero, and n is negative return 1/pow(x, -n);}

Computes powers of the form xn

All of the cases:

x n xn

=0 <0 underfined

=0 =0 underfined

=0 > 0 0

!=0 < 0 1/x-n

!=0 = 0 1

!=0 > 0 x*xn-1

Page 25: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Summary of Reason about RecursionSummary of Reason about Recursion

First check the function always terminates First check the function always terminates (not infinite recursion)(not infinite recursion)

next make sure that the stopping cases work next make sure that the stopping cases work correctlycorrectly

finally, for each recursive case, pretending finally, for each recursive case, pretending that you know the recursive calls will work that you know the recursive calls will work correctly, use this to show that the recursive correctly, use this to show that the recursive case works correctlycase works correctly

Page 26: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Opinions on RecursionOpinions on Recursion

I like recursion I like recursion It makes programming design easier for some It makes programming design easier for some

problems.problems.

I hate recursion I hate recursion It is unnecessary. I can always find an alternative way It is unnecessary. I can always find an alternative way

to do it.to do it.

I don’t care I don’t care I am going to find a job that I can make more money. I am going to find a job that I can make more money.

Why does recursion matter?Why does recursion matter?

Page 27: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Reading, Exercises and AssignmentReading, Exercises and Assignment

Reading Reading Section 9.3Section 9.3

Self-Test ExercisesSelf-Test Exercises 13-1713-17

Assignment 5 onlineAssignment 5 online four recursive functionsfour recursive functions due Monday, November 12, 2007due Monday, November 12, 2007

Exam 2 – November 07 (Wednesday)Exam 2 – November 07 (Wednesday) Come to class on Monday for exam reviews and Come to class on Monday for exam reviews and

discussion on testing and debuggingdiscussion on testing and debugging

Page 28: CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

Presentation & Discussion on Testing and DebuggingPresentation & Discussion on Testing and Debugging

EveryoneEveryone prepares a two-slide Powerpoint presentation. prepares a two-slide Powerpoint presentation. The common mistakes in your codesThe common mistakes in your codes How did you discover and fix themHow did you discover and fix them The debugging tool you use and howThe debugging tool you use and how How to write a program with less bugs at the first placeHow to write a program with less bugs at the first place ……

Send me your slides Send me your slides by midnight on Saturdayby midnight on Saturday..

This presentation contribute to your class participation This presentation contribute to your class participation creditscredits