Recursive Definitions

Similar documents
Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

APCS-AB: Java. Recursion in Java December 12, week14 1

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

UNIT 5A Recursion: Basics. Recursion

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Data Structures And Algorithms

Algorithmic Methods Tricks of the Trade


Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSC-140 Assignment 4

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

UNIT 5A Recursion: Basics. Recursion

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

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

Recursion. Chapter 2. Objectives. Upon completion you will be able to:

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

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

Recursion. Chapter 5

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Recursion. Chapter 7

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Reduction & Recursion Overview

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

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

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

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

Chapter 10: Recursive Problem Solving

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

recursive algorithms 1

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Lecture 24 Tao Wang 1

Recursion. notes Chapter 8

Reading 8 : Recursion

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

Recursion. Jordi Cortadella Department of Computer Science

Recursion Introduction OBJECTIVES

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursion Chapter 3.5

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Algorithm. Building blocks of algorithm

Overview. What is recursion? When one function calls itself directly or indirectly.

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

q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

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

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

STUDENT LESSON A9 Recursion

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

Summary. csci 210: Data Structures Recursion. Recursive algorithms. Recursion. Topics READING:

csci 210: Data Structures Recursion

CmpSci 187: Programming with Data Structures Spring 2015

EE 368. Weeks 4 (Notes)

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

Lecture 10: Recursion vs Iteration

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Recursion Chapter 4 Self-Reference. Recursive Definitions Inductive Proofs Implementing Recursion

CSE 214 Computer Science II Recursion

CS 112 Introduction to Programming

CSC 273 Data Structures

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

Chapter 15: Recursion

Programming II (CS300)

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Programming II (CS300)

cs Java: lecture #6

CS 310 Advanced Data Structures and Algorithms

Solving problems by recursion

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

More Complicated Recursion CMPSC 122

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

Programming II (CS300)

Decision-Making and Repetition

Chapter 5: Recursion. Objectives

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

Lecture 6 CS2110 Spring 2013 RECURSION

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

Lecture P6: Recursion

Lecture 6: Recursion RECURSION

Department of Computer Science Yale University Office: 314 Watson Closely related to mathematical induction.

Recursive Algorithms II

Fundamentals of Programming Session 13

Recursion -- Introduction

Transcription:

Recursion

Objectives Explain the underlying concepts of recursion Examine recursive methods and unravel their processing steps Explain when recursion should and should not be used Demonstrate the use of recursion to solve problems 8-2

Recursive Definitions Recursion: defining something in terms of itself Recursive definition Uses the word or concept being defined in the definition itself Includes a base case that is defined directly, without self-reference 8-3

Recursive Definitions Example: define a group of people Iterative definition: a group is 2 people, or 3 people, or 4 people, or Recursive definition: a group is: 2 people or, a group is: a group plus one more person The concept of a group is used to define itself! The base case is a group is 2 people 8-4

Exercise Give an iterative and a recursive definition of a sequence of characters e.g. CS 1027 Iterative definition: a sequence of characters is? Recursive definition: a sequence of characters is? 8-5

Recursive Definitions Example: consider the following list of numbers: 24, 88, 40, 37 It can be defined recursively: list of numbers:is a number or a number comma list of numbers i.e. It is defined to be a single number, or a number followed by a comma followed by a list of numbers 8-6

Tracing a Recursive Definition To determine whether the sequence 24, 88, 40, 37 is a list of numbers, apply the recursive portion of the definition: 24 is a number and, is a comma, so 24, 88, 40, 37 is a list of numbers if and only if 88, 40, 37 is a list of numbers Apply the same part of the definition to the sequence 88, 40, 37 Eventually, we ll need to apply the base case of the definition 8-7

list?? 24, 88, 40, 37 number comma list?? 24, 88, 40, 37 number comma list?? 88, 40, 37 General portion of definition has been applied here number comma list?? 40, 37 Base case from the definition has been applied here number 37 Yes: 24, 88, 40, 37 is a list 8-8

Recursive Definitions A recursive definition consists of two parts: The base case: this defines the simplest case or starting point The recursive part: this is the general case, that describes all the other cases in terms of smaller versions of itself Why is a base case needed? A definition without a non-recursive part causes infinite recursion 8-9

Discussion We can get information from our recursive definition by starting at the base case, for example: 2 people form a group (base case) So, 2 + 1 or 3 people form a group So, 3 + 1 or 4 people form a group etc. We can also get information by ending at the base case, for example: Do 4 people form a group? 8-10

More Recursive Definitions Mathematical formulas are often expressed recursively Example: the formula for factorial for any positive integer n, n! (n factorial) is defined to be the product of all integers between 1 and n inclusive Express this definition recursively 1! = 1 (the base case) n! = n * (n-1)! for n>=2 Now determine the value of 4! 8-11

Discussion Recursion is an alternative to iteration, and can be a very powerful problemsolving technique What is iteration? repetition, as in a loop What is recursion? defining something in terms of a smaller or simpler version of itself (why smaller/simpler? ) 8-12

Recursive Programming Recursion is a programming technique in which a method can call itself to solve a problem A method in Java that invokes itself is called a recursive method, and must contain code for The base case The recursive part 8-13

Example of Recursive Programming Consider the problem of computing the sum of all the numbers between 1 and n inclusive e.g. if n is 5, the sum is 1 + 2 + 3 + 4 + 5 How can this problem be expressed recursively? Hint: the above sum is the same as 5 + 4 + 3 + 2 + 1 8-14

Recursive Definition of Sum of 1 to n n Σk = 1 k = n + Σ n-1 k = 1 k for n >1 This reads as: the sum of 1 to n = n + the sum of 1 to n-1 What is the base case? the sum of 1 to 1 = 1 8-15

Trace Recursive Definition of Sum of 1 to n n Σ k = 1 k = n + Σ n-1 k = 1 n-2 +Σ k = n + (n-1) k = 1 k n-3 +Σ = n + (n-1) + (n-2) k = 1 k = n + (n-1) + (n-2) + + 3 + 2 + 1 8-16

A Recursive Method for Sum public static int sum (int n) { int result; if (n == 1) result = 1; else result = n + sum (n-1); return result; } 8-17

How Recursion Works What happens when any method is called? A call frame or activation record is set up That call frame is pushed onto the runtime stack or execution stack What happens when a recursive method calls itself? It s actually just like calling any other method! An activation record is set up That activation record ame is pushed onto the execution stack 8-18

How Recursion Works Note: For a recursive method, how many copies of the code are there? Just one! (like any other method) When does the recursive method stop calling itself? When the base case is reached What happens then? That invocation of the method completes, its activation record is popped off the execution stack, and control returns to the method that invoked it 8-19

How Recursion Works But which method invoked it? the previous invocation of the recursive method This method now completes, its activation record is popped off the execution stack, and control returns to the method that invoked it And so on until we get back to the first invocation of the recursive method 8-20

Tracing int k = sum(4); Call is made from main( ). Bottom activation record on the execution stack is for the main program; all others are for calls to sum( ). The stack is redrawn at each call to sum( ), and just before each return. Main program call returns to the OS; all others return to the addition in n + sum(n-1). 2 n? res 3 n? res 3 n? res 4 n? res 4 n? res 4 n? res? k? k? k? k 8-21

Base case occurs here; no new recursive call Value returned by previous call is added to n to yield this call s return value 1 n? res 1 n 1 res 2 n? res 2 n? res 2 n 3 res 3 n? res 3 n? res 3 n? res 3 n 6 res 4 n? res 4 n? res 4 n? res 4 n? res? k? k? k? k 8-22

Value returned by previous call is added to n to yield this call s return value 4 n 10 res Result of int k = sum(4); has been stored? k 10 k 8-23

Discussion: Recursion vs. Iteration Just because we can use recursion to solve a problem, doesn't mean we should! Would you use iteration or recursion to compute the sum of 1 to n? Why? 8-24

Exercise: Factorial Method Write an iterative method to compute the factorial of a positive integer. Write a recursive method to compute the factorial of a positive integer. Which do you think is faster, the recursive or the iterative version of the factorial method? 8-25

Example: Fibonacci Numbers Fibonacci numbers are those of the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, Define them recursively: fib(1) = 1 fib(2) = 1 fib(n) = fib(n 1) + fib(n 2) for n > 2 This sequence is also known as the solution to the Multiplying Rabbits Problem 8-26

A Recursive Method for Fibonacci Numbers // precondition (assumption) : n > = 1 public static int rfib (int n) { if ((n == 1) (n == 2)) return 1; else return rfib(n 1) + rfib(n 2); } 8-27

An Iterative Method for Fibonacci Numbers public static int ifib(int n) { if ((n == 1) (n == 2)) return 1; else { int prev = 1, current = 1, next; for (int i = 3; i <= n; i ++) { next = prev + current; prev = current; current = next; } return next; } 8-28

Discussion Which solution looks simpler, the recursive or the iterative? Which one is (much) faster? Why? Note: recursive and iterative code for Fibonacci are both online - try running them both, and time them! 8-29

Evaluating fib(6) a fib(6) 8 Letters: Give order of calls Numbers: Return values fib(5) b + k fib(4) 5 3 e fib(4) c + h fib(3) 3 2 fib(3) + g fib(2) d 2 fib(2)+fib(1) f 1 fib(2)+fib(1) i 1 1 j l 1 fib(3) + fib(2) o 2 fib(2)+fib(1) m 1 n 1 1 1 8-30

Useful Recursive Solutions Quicksort for sorting a set of values Backtracking problems in Artificial Intelligence Formal language definitions such as Backus- Naur Form (BNF) <ident> ::= <letter> <ident><letter> <ident><digit> etc. Evaluating algebraic expressions in postfix form (how did we do this earlier?) etc. 8-31

Recursive Solutions For some problems, recursive solutions are simpler and more elegant than iterative solutions Classic example: Towers of Hanoi Puzzle invented in the 1880 s by a mathematician named Edouard Lucas Based on a legend for which there are many versions, but they all involve monks or priests moving 64 gold disks from one place to another. When their task is completed, the world will end 8-32

The Towers of Hanoi The Towers of Hanoi puzzle is made up of Three vertical pegs Several disks that slide onto the pegs The disks are of varying size, initially placed on one peg with the largest disk on the bottom and increasingly smaller disks on top 8-33

The Towers of Hanoi Puzzle 8-34

The Towers of Hanoi Goal: move all of the disks from one peg to another following these rules: Only one disk can be moved at a time A disk cannot be placed on top of a smaller disk All disks must be on some peg (except for the one in transit) 8-35

Towers of Hanoi Solution: 4 disks Goal: Move the disks from peg A to peg C A B C A B C A B C A B C 8-36

A B C A B C A B C A B C 8-37

A B C A B C A B C A B C 8-38

A B C A B C A B C A B C 8-39

Towers of Hanoi Recursive Solution To move a stack of n disks from the original peg to the destination peg: move the topmost n-1 disks from the original peg to the extra peg move the largest disk from the original peg to the destination peg move the n-1 disks from the extra peg to the destination peg The base case occurs when moving just the smallest disk (that is, when solving the 1-disk problem) 8-40

Towers of Hanoi Recursive Solution Note that the number of moves increases exponentially as the number of disks increases! So, how long will it take for the monks to move those 64 disks? The recursive solution is simple and elegant to express (and program); an iterative solution to this problem is much more complex See SolveTowers.java, TowersOfHanoi.java 8-41

Analyzing Recursive Algorithms Analyzing a loop: determine the number of operations in each iteration of the loop and add it over the number of times the loop is executed Recursive analysis is similar: determine the number of operations in the method body and add it over the number of times the recursive call is made) 8-42

Analyzing Recursive Algorithms Example: Towers of Hanoi Size of the problem? The number of disks n Operations per call? A constant number Except for the base case, each recursive call results in calling itself twice more So, to solve a problem of n disks, we make 2 n -1 disk moves Therefore the algorithm is O(2 n ), which is called exponential complexity 8-43

Exercise What is the time complexity of: 1. the recursive factorial method? 2. the iterative factorial method? 3. the recursive Fibonacci method? 4. the iterative Fibonacci method? 8-44