Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. 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

CSC 205 Programming II Lecture 10 Towers of Hanoi.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
COMPSCI 105 S Principles of Computer Science Recursion 1.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
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.
Recursion. Binary search example postponed to end of lecture.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
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.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Recursion 1. CHAPTER TWO Recursion: A recursive function is a function that calls itself. Two functions A and B are mutually recursive, if A calls B and.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Data Structures and Abstractions with Java, 4e Frank Carrano
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 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Programming for Engineers in Python Autumn Lecture 8: Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion.
Recursion CENG 707.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion CENG 707.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion: The Mirrors
Recursion: The Mirrors
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion Chapter 10.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion: The Mirrors
Recursion Data Structures.
Recursion Chapter 18.
Recursion: The Mirrors
Recursion: The Mirrors
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved

Recursive Solutions Recursion An extremely powerful problem-solving technique Breaks a problem in smaller identical problems An alternative to iteration An iterative solution involves loops All the recursive solutions have an iterative implementation © 2011 Pearson Addison-Wesley. All rights reserved

Recursive Solutions Sequential search Binary search Starts at the beginning of the collection Looks at every item in the collection in order until the item being searched for is found Binary search Repeatedly halves the collection and determines which half could contain the item Uses a divide and conquer strategy © 2011 Pearson Addison-Wesley. All rights reserved

Recursive Solutions Facts about a recursive solution A recursive method calls itself Each recursive call solves an identical, but smaller, problem A test for the base case enables the recursive calls to stop Base case: a known case in a recursive definition Eventually, one of the smaller problems must be the base case © 2011 Pearson Addison-Wesley. All rights reserved

Recursive Solutions Four questions for construction recursive solutions How can you define the problem in terms of a smaller problem of the same type? How does each recursive call diminish the size of the problem? What instance of the problem can serve as the base case? As the problem size diminishes, will you reach this base case? © 2011 Pearson Addison-Wesley. All rights reserved

What happens in Memory ? © 2011 Pearson Addison-Wesley. All rights reserved

A Recursive Valued Method: The Factorial of n Problem Compute the factorial of an integer n An iterative definition of factorial(n) factorial(n) = n * (n-1) * (n-2) * … * 1 for any integer n > 0 factorial(0) = 1 © 2011 Pearson Addison-Wesley. All rights reserved

A Recursive Valued Method: The Factorial of n The pattern is : factorial(n) = n*factorial (n-1) A recurrence relation A mathematical formula that generates the terms in a sequence from previous terms Example factorial(n) = n * [(n-1) * (n-2) * … * 1] = n * factorial(n-1) A recursive definition of factorial(n) factorial(n) = 1 if n = 0 n * factorial(n-1) if n > 0 © 2011 Pearson Addison-Wesley. All rights reserved

Recursive Factorial © 2011 Pearson Addison-Wesley. All rights reserved

StackOverflow Error ! *data in this case is function calls If the base condition is wrong, it results in a stackOverflow. © 2011 Pearson Addison-Wesley. All rights reserved

A Recursive Method: Writing a String Backward Problem Given a string of characters, write it in reverse order Recursive solution Each recursive step of the solution diminishes by 1 the length of the string to be written backward Base case Write the empty string backward © 2011 Pearson Addison-Wesley. All rights reserved

Reverse Recursive function © 2011 Pearson Addison-Wesley. All rights reserved

The Fibonacci Sequence The Fibonacci sequence is, by definition, the integer sequence in which every number after the first two is the sum of the two preceding numbers 0 1 1 2 3 5 8 13 …… The iterative solution to Fibonacci sequence: © 2011 Pearson Addison-Wesley. All rights reserved

The Fibonacci Sequence Base cases fib(2), fib(1) Recursive definition fib(n) = 1 if n is 1 or 2 fib(n-1) + fib(n-2) if n > 2 Fibonacci sequence The series of numbers fib(1), fib(2), fib(3), and so on © 2011 Pearson Addison-Wesley. All rights reserved

Fibonnacci Recursive function © 2011 Pearson Addison-Wesley. All rights reserved

Finding the Largest Item in an Array Figure 3-13 Recursive solution to the largest-item problem © 2011 Pearson Addison-Wesley. All rights reserved

Finding the Largest Item in an Array A recursive solution © 2011 Pearson Addison-Wesley. All rights reserved

Sequential Vs Binary Search Sequential Search Binary Search © 2011 Pearson Addison-Wesley. All rights reserved

Binary Search Recursive © 2011 Pearson Addison-Wesley. All rights reserved

Organizing Data: The Towers of Hanoi The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3) No disk may be placed on top of a smaller disk. © 2011 Pearson Addison-Wesley. All rights reserved

Tower of Hanoi Solution for n=3 © 2011 Pearson Addison-Wesley. All rights reserved

Tower of Hanoi Solution for n= 4 Pattern: Shift n-1 disk from A to B (using C as auxilary) Shift last disk from A to C Shift n-1 disk from B to C (using A as auxilary) © 2011 Pearson Addison-Wesley. All rights reserved

The Towers of Hanoi Pseudocode solution solveTowers(count, source, destination, spare) if (count is 1) { Move a disk directly from source to destination } else { solveTowers(count-1, source, aux, destination) solveTowers(1, source, destination, aux) solveTowers(count-1, aux, destination, source) } //end if © 2011 Pearson Addison-Wesley. All rights reserved

Recursion and Efficiency Some recursive solutions are so inefficient that they should not be used Factors that contribute to the inefficiency of some recursive solutions Overhead associated with method calls Inherent inefficiency of some recursive algorithms © 2011 Pearson Addison-Wesley. All rights reserved

Summary Recursion solves a problem by solving a smaller problem of the same type Four questions to keep in mind when constructing a recursive solution How can you define the problem in terms of a smaller problem of the same type? How does each recursive call diminish the size of the problem? What instance of the problem can serve as the base case? As the problem size diminishes, will you reach this base case? © 2011 Pearson Addison-Wesley. All rights reserved

Summary A recursive call’s postcondition can be assumed to be true if its precondition is true The box trace can be used to trace the actions of a recursive method Recursion can be used to solve problems whose iterative solutions are difficult to conceptualize © 2011 Pearson Addison-Wesley. All rights reserved

Summary Some recursive solutions are much less efficient than a corresponding iterative solution due to their inherently inefficient algorithms and the overhead of method calls If you can easily, clearly, and efficiently solve a problem by using iteration, you should do so © 2011 Pearson Addison-Wesley. All rights reserved