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

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
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.
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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,
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
© 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.
Data Structures and Abstractions with Java, 4e Frank Carrano
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 15 Recursion.
Chapter 8: Recursion Java Software Solutions
Recursion Chapter 10.
Recursive Definitions
Chapter 8: Recursion Java Software Solutions
CS302 - Data Structures using C++
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Java Software Solutions Foundations of Program Design Sixth Edition
Recursive Thinking.
Presentation transcript:

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

Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an Array Recursively Processing a Linked Chain The Time Efficiency of Recursive Methods  The Time Efficiency of countDown  The Time Efficiency of Computing x n Copyright ©2012 by Pearson Education, Inc. All rights reserved

Contents A Simple Solution to a Difficult Problem A Poor Solution to a Simple Problem Tail Recursion Indirect Recursion Using a Stack Instead of Recursion Copyright ©2012 by Pearson Education, Inc. All rights reserved

Objectives Decide whether given recursive method will end successfully in finite amount of time Write recursive method Estimate time efficiency of recursive method Identify tail recursion and replace it with iteration Copyright ©2012 by Pearson Education, Inc. All rights reserved

What Is Recursion? We often solve a problem by breaking it into smaller problems When the smaller problems are identical (except for size)  This is called “recursion” Repeated smaller problems  Until problem with known solution is reached Example: Counting down from 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-1 Counting down from 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-1 Counting down from 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-1 Counting down from 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved

What Is Recursion A method that calls itself is  A recursive method. The invocation is  A recursive call or  Recursive invocation Example:  Countdown Copyright ©2012 by Pearson Education, Inc. All rights reserved

Design of Recursive Solution What part of solution can contribute directly? What smaller (identical) problem has solution that …  When taken with your contribution  Provides the solution to the original problem When does process end?  What smaller but identical problem has known solution  Have you reached this problem, or base case? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Design Guidelines Method must receive input value Must contain logic that involves this input value and leads to different cases One or more cases should provide solution that does not require recursion  Base case or stopping case One or more cases must include recursive invocation of method Copyright ©2012 by Pearson Education, Inc. All rights reserved

Tracing Recursive Method Given recursive countDown method Figure 7-2 The effect of the method call countDown(3) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-3 Tracing the recursive call countDown(3) Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 7-4 The stack of activation records during the execution of the call countDown(3) Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 7-4 The stack of activation records during the execution of the call countDown(3) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Recursive Methods That Return a Value Example:  Compute the sum n  For any integer n > 0. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-5 Tracing the execution of sumOf(3) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-5 Tracing the execution of sumOf(3) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Recursively Processing an Array Consider an array of integers We seek a method to display all or part Declaration public static void displayArray (int[] array, int first, int last) Solution could be  Iterative  Recursive Copyright ©2012 by Pearson Education, Inc. All rights reserved

Recursively Processing an Array Recursive solution starting with array[first] Recursive solution starting with array[last] Copyright ©2012 by Pearson Education, Inc. All rights reserved

Dividing an Array in Half Common way to process arrays recursively  Divide array into two portions  Process each portion separately Must find element at or near middle int mid = (first + last) / 2; FIGURE 7-6 Two arrays with their middle elements within their left halves Copyright ©2012 by Pearson Education, Inc. All rights reserved

Dividing an Array in Half Recursive method to display array  Divides array into two portions Copyright ©2012 by Pearson Education, Inc. All rights reserved

Displaying a Bag Recursively When implemented with an array Copyright ©2012 by Pearson Education, Inc. All rights reserved

Recursively Processing a Linked Chain Method display, implemented recursively Copyright ©2012 by Pearson Education, Inc. All rights reserved

Displaying a Chain Backwards Display last node, then next-to-last, etc.  Recursive implementation  Easier than an iterative implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Time Efficiency of Recursive Methods Consider method countdown Recurrence relation Proof by induction shows Thus method is O(n) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Time Efficiency of Computing x n Recursive calculation for x n Can be shown Thus efficiency is O(log n) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Simple Solution to a Difficult Problem Consider Towers of Hanoi puzzle Figure 7-7 The initial configuration of the Towers of Hanoi for three disks. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Towers of Hanoi Rules 1. Move one disk at a time. Each disk you move must be a topmost disk. 2. No disk may rest on top of a disk smaller than itself. 3. You can store disks on the second pole temporarily, as long as you observe the previous two rules. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Solution Move a disk from pole 1 to pole 3 Move a disk from pole 1 to pole 2 Move a disk from pole 3 to pole 2 Move a disk from pole 1 to pole 3 Move a disk from pole 2 to pole 1 Move a disk from pole 2 to pole 3 Move a disk from pole 1 to pole 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-8 The sequence of moves for solving the Towers of Hanoi problem with three disks Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-8 The sequence of moves for solving the Towers of Hanoi problem with three disks Copyright ©2012 by Pearson Education, Inc. All rights reserved

Recursive Solution To solve for n disks …  Ask friend to solve for n – 1 disks  He in turn asks another friend to solve for n – 2  Etc.  Each one lets previous friend know when their simpler task is finished Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-9 The smaller problems in a recursive solution for four disks Copyright ©2012 by Pearson Education, Inc. All rights reserved

Recursive Algorithm Copyright ©2012 by Pearson Education, Inc. All rights reserved

Algorithm Efficiency Moves required for n disks We note and conjecture (proved by induction) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Poor Solution to a Simple Problem Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, … Suggests a recursive solution Note the two recursive calls Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 7-10 The computation of the Fibonacci number F6 using (a) recursion; (b) iteration Copyright ©2012 by Pearson Education, Inc. All rights reserved

Time Efficiency of Algorithm Looking for relationship Can be shown that Conclusion: Do not use recursive solution that repeatedly solves same problem in its recursive calls. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Tail Recursion When the last action performed by a recursive method is a recursive call Example: Repeats call with change in parameter or variable Copyright ©2012 by Pearson Education, Inc. All rights reserved

Tail Recursion Consider this simple change to make an iterative version  Replace if with while  Instead of recursive call, subtract 1 from integer Change of tail recursion to iterative often simple Copyright ©2012 by Pearson Education, Inc. All rights reserved

Indirect Recursion Consider chain of events  Method A calls Method B  Method B calls Method C  and Method C calls Method A Mutual recursion  Method A calls Method B  Method B calls Method A Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 7-11 An example of indirect recursion Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using a Stack Instead of Recursion A way of replacing recursion with iteration Consider recursive displayArray Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using a Stack Instead of Recursion We make a stack that mimics the program stack  Push objects onto stack like activation records  Shown is example record Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using a Stack Instead of Recursion Iterative version of displayArray Copyright ©2012 by Pearson Education, Inc. All rights reserved

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