CSC212 Data Structure - Section FG Lecture 14 Reasoning about Recursion Instructor: Zhigang Zhu Department of Computer Science City College of New York.

Slides:



Advertisements
Similar presentations
Reasoning About Code; Hoare Logic, continued
Advertisements

Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion … just in case you didn’t love loops enough …
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
ICS103 Programming in C Lecture 11: Recursive Functions
Copyright © 2003 Pearson Education, Inc. Slide 1.
Describing Syntax and Semantics
C++ for Engineers and Scientists Third Edition
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Induction and recursion
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CS101 Computer Programming I Chapter 4 Extra Examples.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC211 Data Structures Lecture 14 Reasoning about Recursion Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
1 Interactive Computer Theorem Proving CS294-9 October 19, 2006 Adam Chlipala UC Berkeley Lecture 9: Beyond Primitive Recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion DRILL: Please take out your notes on Recursion
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
CIS Principles of Programming
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Announcements Final Exam on August 17th Wednesday at 16:00.
Applied Algorithms (Lecture 17) Recursion Fall-23
CSc212AB Data Structures Lecture 17
Programming application CC213
Programming Languages 2nd edition Tucker and Noonan
Recursion This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials.
Announcements Final Exam on August 19th Saturday at 16:00.
Announcements Final Exam on December 25th Monday at 16:00.
ICS103 Programming in C Lecture 11: Recursive Functions
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

CSC212 Data Structure - Section FG Lecture 14 Reasoning about Recursion Instructor: Zhigang Zhu Department of Computer Science City College of New York

Outline of This Lecture p Recursive Thinking: General Form p recursive calls and stopping cases p Infinite Recursion p runs forever p One Level Recursion p guarantees to have no infinite recursion p How to Ensure No Infinite Recursion p if a function has multi level recursion p Inductive Reasoning about Correctness p using mathematical induction principle

Recursive Thinking: General Form p Recursive Calls p Suppose a problem has one or more cases in which some of the subtasks are simpler versions of the original problem. These subtasks can be solved by recursive calls p Stopping Cases /Base Cases p A function that makes recursive calls must have one or more cases in which the entire computation is fulfilled without recursion. These cases are called stopping cases or base cases

Infinite Recursion p In all our examples, the series of recursive calls eventually reached a stopping case, i.e. a call that did not involve further recursion p If every recursive call produce another recursive call, then the recursion is an infinite recursion that will, in theory, run forever. p Can you write one?

Example: power (x,n) = x n p Rules: p power(3.0,2) = = 9.0 p power(4.0, 3) = = 64.0 p power(x, 0) = x 0 = 1 if x != 0 p x -n = 1/ x n where x<>0, n > 0 p power(3.0, -2) = = 1/3.0 2 = 1/9 p 0 n p = 0 if n > 0 p invalid if n<=0 (and x == 0)

ipower(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 x n

ipower(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 x n double product =1; for (int i = 1; i<=n; ++i) product *= x; return product;

power(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 x n

One Level Recursion p First general technique for reasoning about recursion: p Suppose that every case is either a stopping case or it makes a recursive call that is a stopping case. Then the deepest recursive call is only one level deep, and no infinite recursion occurs.

Multi-Level Recursion p In general recursive calls don’t stop a just one level deep – a recursive call does not need to reach a stopping case immediately. p In the last lecture, we have showed two examples with multiple level recursions  As an example to show that there is no infinite recursion, we are going to re-write the power function – use a new function name pow

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 x n change this into a recursive call based on the observation x n =x x n-1 if n>0

pow (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 x n All of the cases: x n x n =0 <0 underfined =0 =0 underfined =0 > 0 0 !=0 < 0 1/x -n !=0 = 0 1 !=0 > 0 x*x n-1

How to ensure NO Infinite Recursion p when the recursive calls go beyond one level deep  You can ensure that a stopping case is eventually reached by defining a numeric quantity called variant expression - without really tracing through the execution p This quantity must associate each legal recursive call to a single number, which changes for each call and eventually satisfies the condition to go to the stopping case

Variant Expression for pow p The variant expression is abs(n)+1 when n is negative and p the variant expression is n when n is positive p A sequence of recursion call p pow(2.0, -3) has a variant expression abs(n)+1, which is 4; it makes a recursive call of pow(2.0, 3)

Variant Expression for pow p The variant expression is abs(n)+1 when n is negative and p the variant expression is n when n is positive p A sequence of recursion call p 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)

Variant Expression for pow p The variant expression is abs(n)+1 when n is negative and p the variant expression is n when n is positive p A sequence of recursion call p 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)

Variant Expression for pow p The variant expression is abs(n)+1 when n is negative and p the variant expression is n when n is positive p A sequence of recursion call p 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)

Variant Expression for pow p The variant expression is abs(n)+1 when n is negative and p the variant expression is n when n is positive p A sequence of recursion call p 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.

Ensuring NO Infinite Recursion p It is enough to find a variant expression and a threshold with the following properties (p446):  Between one call of the function and any succeeding recursive call of that function, the value of the variant expression decreases by at least some fixed amount. p What is that fixed amount of pow(x,n)?  If the function is called and the value of the variant expression is less than or equal to the threshold, then the function terminates without making any recursive call p What is the threshold of pow(x,n) p Is this general enough?

Reasoning about the Correctness p First show NO infinite recursion then show the following two conditions are also valid: p Whenever the function makes no recursive calls, show that it meets its pre/post-condition contract (BASE STEP) p Whenever the function is called, by assuming all the recursive calls it makes meet their pre-post condition contracts, show that the original call will also meet its pre/post contract (INDUCTION STEP)

pow (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 x n All of the cases: x n x n =0 <0 underfined =0 =0 underfined =0 > 0 0 !=0 < 0 1/x -n !=0 = 0 1 !=0 > 0 x*x n-1

Summary of Reason about Recursion p First check the function always terminates (not infinite recursion) p next make sure that the stopping cases work correctly p finally, for each recursive case, pretending that you know the recursive calls will work correctly, use this to show that the recursive case works correctly

Reading, Exercises and Assignment p Reading p Section 9.3 p Self-Test Exercises p p Assignment 5 online p four recursive functions p due on Wednesday, November 12, 2014 p Exam 2 – November 10 (Monday) p Come to class on Wed (Nov 5) for reviews and assignment/quiz discussions