Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

Starting Out with Java: From Control Structures through Objects
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Recursion. Binary search example postponed to end of lecture.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 19 Recursion.
Chapter 19 Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Starting Out with C++, 3 rd Edition Chapter 19 Recursion.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion Powerful Tool
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Topic 6 Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 15 Recursion.
Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion UW CSE 160 Winter 2017
Module 1-10: Recursion.
Chapter 19: Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion

Chapter 14 Starting Out with C++: Early Objects 5/e slide 2 © 2006 Pearson Education. All Rights Reserved Topics 14.1 Introduction to Recursion 14.2 The Recursive Factorial Function 14.3 The Recursive gcd Function 14.4 Solving Recursively Defined Problems 14.5 A Recursive Binary Search Function 14.6 The QuickSort Algorithm 14.7 Exhaustive and Enumeration Algorithms 14.8 Recursion Versus Iteration

Chapter 14 Starting Out with C++: Early Objects 5/e slide 3 © 2006 Pearson Education. All Rights Reserved 14.1 Introduction to Recursion A recursive function is a function that calls itself. Recursive functions can be useful in solving problems that can be broken down into smaller or simpler subproblems of the same type. A base case should eventually be reached, at which time the breaking down (recursion) will stop.

Chapter 14 Starting Out with C++: Early Objects 5/e slide 4 © 2006 Pearson Education. All Rights Reserved Recursive Functions Consider a function for solving the count-down problem from some number num down to 0 : –The base case is when num is already 0 : the problem is solved and we “blast off!” –If num is greater than 0, we count off num and then recursively count down from num-1

Chapter 14 Starting Out with C++: Early Objects 5/e slide 5 © 2006 Pearson Education. All Rights Reserved Recursive Functions A recursive function for counting down to 0: void countDown(int num) { if (num == 0) cout << "Blastoff!"; else { cout << num << "…"; countDown(num-1); // recursive } // call }

Chapter 14 Starting Out with C++: Early Objects 5/e slide 6 © 2006 Pearson Education. All Rights Reserved What Happens When Called? If a program contains a line like countDown(2); 1.countDown(2) generates the output 2..., then it calls countDown(1) 2.countDown(1) generates the output 1..., then it calls countDown(0) 3.countDown(0) generates the output Blastoff!, then returns to countDown(1) 4.countDown(1) returns to countDown(2) 5.countDown(2) returns to the calling function

Chapter 14 Starting Out with C++: Early Objects 5/e slide 7 © 2006 Pearson Education. All Rights Reserved What Happens When Called? third call to countDown num is 0 countDown(1); countDown(0); // no // recursive // call second call to countDown num is 1 first call to countDown num is 2 OUTPUT: Blastoff!

Chapter 14 Starting Out with C++: Early Objects 5/e slide 8 © 2006 Pearson Education. All Rights Reserved Stopping the Recursion A recursive function should include a test for the base cases In the sample program, the test is: if (num == 0)

Chapter 14 Starting Out with C++: Early Objects 5/e slide 9 © 2006 Pearson Education. All Rights Reserved Stopping the Recursion void countDown(int num) { if (num == 0) // test cout << "Blastoff!"; else { cout << num << "...\n"; countDown(num-1); // recursive } // call }

Chapter 14 Starting Out with C++: Early Objects 5/e slide 10 © 2006 Pearson Education. All Rights Reserved Stopping the Recursion With each recursive call, the parameter controlling the recursion should move closer to the base case Eventually, the parameter reaches the base case and the chain of recursive calls terminates

Chapter 14 Starting Out with C++: Early Objects 5/e slide 11 © 2006 Pearson Education. All Rights Reserved Stopping the Recursion void countDown(int num) { if (num == 0) // base case cout << "Blastoff!"; else { cout << num << "...\n"; countDown(num-1); } Value passed to recursive call is closer to base case of num == 0.

Chapter 14 Starting Out with C++: Early Objects 5/e slide 12 © 2006 Pearson Education. All Rights Reserved What Happens When Called? Each time a recursive function is called, a new copy of the function runs, with new instances of parameters and local variables being created As each copy finishes executing, it returns to the copy of the function that called it When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function

Chapter 14 Starting Out with C++: Early Objects 5/e slide 13 © 2006 Pearson Education. All Rights Reserved Types of Recursion Direct recursion –a function calls itself Indirect recursion –function A calls function B, and function B calls function A. Or, –function A calls function B, which calls …, which calls function A

Chapter 14 Starting Out with C++: Early Objects 5/e slide 14 © 2006 Pearson Education. All Rights Reserved 14.2 The Recursive Factorial Function The factorial of a nonnegative integer n is the product of all positive integers less or equal to n Factorial of n is denoted by n! The factorial of 0 is 1 0 ! = 1 n ! = n x (n-1) x … x 2 x 1 if n > 0

Chapter 14 Starting Out with C++: Early Objects 5/e slide 15 © 2006 Pearson Education. All Rights Reserved Recursive Factorial Function Factorial of n can be expressed in terms of the factorial of n-1 0 ! = 1 n ! = n x (n-1) ! Recursive function int factorial(int n) { if (n == 0) return 1; else return n *factorial(n-1); }

Chapter 14 Starting Out with C++: Early Objects 5/e slide 16 © 2006 Pearson Education. All Rights Reserved 14.3 The Recursive gcd Function Greatest common divisor (gcd) of two integers x and y is the largest number that divides both x and y The Greek mathematician Euclid discovered that –If y divides x, then gcd( x, y ) is just y –Otherwise, the gcd( x, y ) is the gcd of y and the remainder of dividing x by y

Chapter 14 Starting Out with C++: Early Objects 5/e slide 17 © 2006 Pearson Education. All Rights Reserved The Recursive gcd Function int gcd(int x, int y) { if (x % y == 0) //base case return y; else return gcd(y, x % y); }

Chapter 14 Starting Out with C++: Early Objects 5/e slide 18 © 2006 Pearson Education. All Rights Reserved 14.4 Solving Recursively Defined Problems The natural definition of some problems leads to a recursive solution Example: Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21,... After the starting 0, 1, each term is the sum of the two preceding terms Recursive solution: fib(n) = fib(n – 1) + fib(n – 2); Base cases: n == 0, n == 1

Chapter 14 Starting Out with C++: Early Objects 5/e slide 19 © 2006 Pearson Education. All Rights Reserved Recursive Fibonacci Function int fib(int n) { if (n <= 0) // base case return 0; else if (n == 1) // base case return 1; else return fib(n – 1) + fib(n – 2); }

Chapter 14 Starting Out with C++: Early Objects 5/e slide 20 © 2006 Pearson Education. All Rights Reserved 14.5 A Recursive Binary Search Function Assume an array a sorted in ascending order, and an item X We want to write a function that searches for X within the array a, returning the index of X if it is found, and returning -1 if X is not in the array

Chapter 14 Starting Out with C++: Early Objects 5/e slide 21 © 2006 Pearson Education. All Rights Reserved Recursive Binary Search A recursive strategy for searching a portion of the array from lo to hi is to set m to position of middle of portion of array: mlo hi

Chapter 14 Starting Out with C++: Early Objects 5/e slide 22 © 2006 Pearson Education. All Rights Reserved Recursive Binary Search If a[m] == X, we found X, so return m If a[m] > X, recursively search a[lo..m-1] If a[m] < X, recursively search a[m+1..hi] mlo hi

Chapter 14 Starting Out with C++: Early Objects 5/e slide 23 © 2006 Pearson Education. All Rights Reserved Recursive Binary Search int bSearch(int a[],int lo,int hi,int X) { int m = (lo + hi) /2; if(lo > hi) return -1; // base if(a[m] == X) return m; // base if(a[m] > X) return bsearch(a,lo,m-1,X); else return bsearch(a,m+1,hi,X); }

Chapter 14 Starting Out with C++: Early Objects 5/e slide 24 © 2006 Pearson Education. All Rights Reserved 14.6 The QuickSort Algorithm Recursive algorithm that can sort an array Determines an element to use as pivot value: pivot sublist 1 sublist 2

Chapter 14 Starting Out with C++: Early Objects 5/e slide 25 © 2006 Pearson Education. All Rights Reserved The QuickSort Algorithm Once pivot value is determined, values are shifted so that elements in sublist1 are = pivot Algorithm then recursively sorts sublist1 and sublist2 Base case: sublist has size <=1 pivot value sublist 1 sublist 2

Chapter 14 Starting Out with C++: Early Objects 5/e slide 26 © 2006 Pearson Education. All Rights Reserved 14.7 Exhaustive and Enumeration Algorithms Enumeration algorithm: generate all possible combinations Example: all possible ways to make change for a certain amount of money Exhaustive algorithm: search a set of combinations to find an optimal one Example: change for a certain amount of money that uses the fewest coins

Chapter 14 Starting Out with C++: Early Objects 5/e slide 27 © 2006 Pearson Education. All Rights Reserved 14.8 Recursion vs. Iteration Benefits (+), disadvantages(-) for recursion: +Natural formulation of solution to certain problems +Results in shorter, simpler functions –May not execute very efficiently Benefits (+), disadvantages(-) for iteration: +Executes more efficiently than recursion –May not be as natural as recursion for some problems

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion