Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

1 Dynamic Memory continued: Memory Leaks

Recursion. CSCI 112: Programming in C

Programming II (CS300)

Programming II (CS300)

CS 101: Computer Programming and Utilization

Lecture Transcript While and Do While Statements in C++

CS 161 Intro to CS I. Finish Pointers/Start Recursion

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

STUDENT LESSON A9 Recursion

Computer Programming

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Recursive Definitions

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

CS 310 Advanced Data Structures and Algorithms

Chapter 17 - Notes Recursion

CSE 230 Intermediate Programming in C and C++ Recursion

Search: Advanced Topics and Conclusion

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

recursive algorithms 1

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

EK131 E5 Introduction to Engineering

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

CSE 230 Computer Science II (Data Structure) Introduction

CSC212. Data Structure. Lecture 12 Reasoning about Recursion. Instructor: George Wolberg Department of Computer Science City College of New York

NCS 301 DATA STRUCTURE USING C

Programming II (CS300)

Koch snowflake. Fractal Fern

Chapter 15: Recursion

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

CS103L SPRING 2017 UNIT 8: RECURSION

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

EC 413 Computer Organization

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Software Development. Modular Design and Algorithm Analysis

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Lecture 24 Tao Wang 1

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Search: Advanced Topics and Conclusion

AP Programming - Chapter 17 Lecture page 1 of 5

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Computer Science II Lecture 1 Introduction and Background

Reasoning about programs

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

1.7 Recursion. Department of CSE

PDF created with pdffactory Pro trial version Recursion

CSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

COP 4020 Fall 2005 Presentation Joshua Burkholder 2005 NOV 27. Recursion. What, Why, How, When, and What If?

PREPARING FOR PRELIM 1

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Multithreaded Algorithms Part 1. Dept. of Computer Science & Eng University of Moratuwa

= otherwise W-4 W-1. Overview. CSE 142 Computer Programming I. Overview. Factorial Function

Lecture Notes: Hoare Logic

Lecture Notes on Memory Layout

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Algorithms and Programming

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

6.001 Notes: Section 4.1

CSC 1052 Algorithms & Data Structures II: Recursion

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recursive Functions Part B Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Use of simple functions in programs Contract-centric view of programming with functions Flow of control in function call and return Activation records and call stack Parameter passing by value and reference Recursive functions 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Designing recursive functions Termination and recursive changing of parameters Recursion vs iteration 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Acknowledgment Some examples in this lecture are from An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 All such examples indicated in slides with the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Recall: Encoding Example We want to store quiz 1 and quiz 2 marks of CS101 students in an encoded form Encoding strategy: encode(m, n) = 2 m x 3 n Assume all marks are integers in {1, 2, 10} Observe: encode(m, n) = encode(m, n-1) x 3, if m, n > 1 = encode(m-1, 1) x 2, if m > 1, n=1 = 2 x 3 = 6, if m=1, n=1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

A Recursive Solution #include <iostream> using namespace std; int newenc(int q1marks,int q2marks); int main() { } for ( ) { cipher = newenc(q1marks, q2marks); } return 0; Are we really sure that every call to newenc that satisfies precondition eventually terminates? // PRECONDITION: int newenc(int q1marks, int q2marks) { switch(q2marks) { case 1: if (q1marks == 1) {return 6;} else {return 2*newEnc(q1Marks 1, 1); } break; default: } } // POSTCONDITION: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Caveats Using Recursive Functions Must specify how to terminate the recursion Otherwise, recursion (calling a function from itself) can go on forever Changing parameters in an Must ensure that in any invocation, the recursion changes parameters in an orderly orderly way way such to that ensure recursion eventually terminates termination encode(m, n) = encode(m, n-1) x 3, if m, n > 1 = encode(m-1, 1) x 2, if m > 1, n=1 = 2 x 3 = 6, if m=1, n=1 Termination case Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(4, 3) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(4, 2) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(4, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(3, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(2, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 encode(1, 1) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14 m

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Well-founded ordering of parameters with a least element Move monotonically along order towards least element Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15 m

Caveats Using Recursive Functions encode(m, n) = 2 m x 3 n can also be thought as Changing parameters in this way doesn t ensure termination encode(m, n) = encode(m, n+1)/3, if m, n > 1 = encode(m+1, 1)/2, if m > 1, n = 1 = 2 x 3 = 6, if m = 1, n = 1 Termination case Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Caveats Using Recursive Functions Think of all possible valuations of parameters as ordered with a fixed end (termination case) Recursion must change values of parameters so that we move along this order monotonically towards fixed end Termination Case 5 4 3 2 n 1 1 2 3 4 5 6 7 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17 m

A Second Example of Recursion Given n, compute factorial(n) = 1 x 2 x x n // PRECONDITION: integer n >= 0 int factorial(int n) { if ( n == 0 ) {return 1;} // factorial(0) = 1 Termination case else { return (n * factorial(n-1)); // Reduce parameter monotonically // to 0, and use recursion } } // POSTCONDITION: return value = factorial(n) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

A Third Example [Sec 10.3 of AGRBook] Virahanka numbers: V 0 = V 1 = 1, and V n = V n-1 + V n-2 for n >= 2 Also known as Fibonacci numbers (although Virahanka studied these in the context of counting specific types of poetic meters before Fibonacci!) // PRECONDITION: integer n >= 0 int Virahanka(int n) { if ((n == 0) (n == 1)) { return 1; } else { return ( Virahanka(n-1) + Virahanka(n-2) ); } } // POSTCONDITION: return value = V n Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Watch Number of Recursive Calls Virahanka(4) Virahanka(3) Virahanka(2) Virahanka(2) Virahanka(1) Virahanka(1) Virahanka(0) Virahanka(1) Virahanka(0) Number of calls required to compute Virahanka(n) grows exponentially with n Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

Is There A Better Way? An iterative solution is much better here int Virahanka(int n) { int count, result; int prevvn = 1, prevprevvn = 1; if ((n == 0) (n == 1)) { return 1; } else { for(count = 2; count <= n; count++) { result = prevvn + prevprevvn; prevprevvn = prevvn; prevvn = result; } return result; } } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21

Recursion vs Iteration Recursive formulation usually clean, intuitive and succinct Need to worry about recursion termination (well-founded ordering of parameter values) Need to worry about number of recursive calls Iterative formulation may be less clean or intuitive (not always!) Need to worry about loop invariants, loop variants and termination Can be very efficient if formulated correctly Best practice: Judicious mix of iteration and recursion Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

Summary Recursive functions Termination and ordering of parameter values Recursion: Monotonically move towards termination case Recursion vs iteration Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23