Data Structures and Abstractions with Java, 4e Frank Carrano

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

CSC 205 Programming II Lecture 10 Towers of Hanoi.
Starting Out with Java: From Control Structures through Objects
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
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.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
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++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
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.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion Chapter 10.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursion Chapter 11.
Recursion Data Structures.
CSC 143 Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Presentation transcript:

Data Structures and Abstractions with Java, 4e Frank Carrano Recursion Chapter 7 Data Structures and Abstractions with Java, 4e Frank Carrano © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

What Is Recursion? Consider hiring a contractor to build He hires a subcontractor for a portion of the job That subcontractor hires a sub-subcontractor to do a smaller portion of job The last sub-sub- … subcontractor finishes Each one finishes and reports “done” up the line © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Example: The Countdown FIGURE 7-1 Counting down from 10 © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Example: The Countdown FIGURE 7-1 Counting down from 10 © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Example: The Countdown FIGURE 7-1 Counting down from 10 © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Example: The Countdown Recursive Java method to do countdown. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Definition Recursion is a problem-solving process Breaks a problem into identical but smaller problems. A method that calls itself is a recursive method. The invocation is a recursive call or recursive invocation. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Design Guidelines Method must be given an input value Method definition must contain logic that involves this input, leads to different cases One or more cases should provide solution that does not require recursion Else infinite recursion One or more cases must include a recursive invocation © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Programming Tip Iterative method contains a loop Recursive method calls itself Some recursive methods contain a loop and call themselves If the recursive method with loop uses while, make sure you did not mean to use an if statement © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tracing a Recursive Method FIGURE 7-2 The effect of the method call countDown(3) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tracing a Recursive Method FIGURE 7-4 The stack of activation records during the execution of the call countDown(3) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tracing a Recursive Method FIGURE 7-4 The stack of activation records during the execution of the call countDown(3) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Stack of Activation Records Each call to a method generates an activation record Recursive method uses more memory than an iterative method Each recursive call generates an activation record If recursive call generates too many activation records, could cause stack overflow © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursive Methods That Return a Value Recursive method to calculate © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tracing a Recursive Method FIGURE 7-5 Tracing the execution of sumOf(3) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tracing a Recursive Method FIGURE 7-5 Tracing the execution of sumOf(3) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing an Array Given definition of a recursive method to display array. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing an Array Starting with array[first] © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing an Array Starting with array[last] © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing an Array FIGURE 7-6 Two arrays with their middle elements within their left halves © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing an Array Consider first + (last – first) / 2 Why? Processing array from middle. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Displaying a Bag Recursive method that is part of an implementation of an ADT often is private. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing a Linked Chain Display data in first node and recursively display data in rest of chain. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Recursively Processing a Linked Chain Displaying a chain backwards. Traversing chain of linked nodes in reverse order easier when done recursively. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Time Efficiency of Recursive Methods Using proof by induction, we conclude method is O(n). © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Time Efficiency of Computing xn Efficiency of algorithm is O(log n) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Simple Solution to a Difficult Problem FIGURE 7-7 The initial configuration of the Towers of Hanoi for three disks. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Simple Solution to a Difficult Problem Rules: Move one disk at a time. Each disk moved must be topmost disk. No disk may rest on top of a disk smaller than itself. You can store disks on the second pole temporarily, as long as you observe the previous two rules. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Solutions FIGURE 7-8 The sequence of moves for solving the Towers of Hanoi problem with three disks © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Solutions FIGURE 7-8 The sequence of moves for solving the Towers of Hanoi problem with three disks © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

FIGURE 7-9 The smaller problems in a recursive solution for four disks Solutions FIGURE 7-9 The smaller problems in a recursive solution for four disks © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Solutions Recursive algorithm to solve any number of disks. Note: for n disks, solution will be 2n – 1 moves © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Poor Solution to a Simple Problem Algorithm to generate Fibonacci numbers. Why is this inefficient? © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Poor Solution to a Simple Problem FIGURE 7-10 The computation of the Fibonacci number F6 using (a) recursion … Fn = Ω(an) © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Poor Solution to a Simple Problem FIGURE 7-10 The computation of the Fibonacci number F6 using (b) iteration. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tail Recursion When the last action performed by a recursive method is a recursive call. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Tail Recursion In a tail-recursive method, the last action is a recursive call This call performs a repetition that can be done by using iteration. Converting a tail-recursive method to an iterative one is usually a straightforward process. © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Indirect Recursion Example Difficult to understand and trace Method A calls Method B Method B calls Method C Method C calls Method A Difficult to understand and trace But does happen occasionally © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Indirect Recursion Consider evaluation of validity of an algebraic expression Algebraic expression is either a term or two terms separated by a + or – operator Term is either a factor or two factors separated by a * or / operator Factor is either a variable or an algebraic expression enclosed in parentheses Variable is a single letter © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

FIGURE 7-11 An example of indirect recursion © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Using a Stack Instead of Recursion An example of converting a recursive method to an iterative one © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Using a Stack Instead of Recursion An iterative displayArray to maintain its own stack © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

Using a Stack Instead of Recursion An iterative displayArray to maintain its own stack © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.

End Chapter 7 © 2015 Pearson Education, Inc., Upper Saddle River, NJ.  All rights reserved.