Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.

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

COSC 2006 Data Structures I Recursion III
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.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 2 Recursion:
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
© 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.
Ch. 3: Recursion. Recursive Solutions Recursion –An extremely powerful problem-solving technique –Breaks a problem into smaller identical problems –An.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
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.
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-
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
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.
Chapter 14: Recursion Starting Out with C++ Early Objects
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.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
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.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COSC 2006 Data Structures I Recursion II
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 9: Algorithm Efficiency and Sorting Data Abstraction &
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
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.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
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 CENG 707.
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Algorithm Efficiency and Sorting
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Chapter 3 Recursion: The Mirrors

© 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving technique –Breaks a problem into smaller identical problems –An alternative to iteration An iterative solution involves loops

© 2004 Pearson Addison-Wesley. All rights reserved 3-3 Recursive Solutions Sequential 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

© 2004 Pearson Addison-Wesley. All rights reserved 3-4 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

© 2004 Pearson Addison-Wesley. All rights reserved 3-5 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?

© 2004 Pearson Addison-Wesley. All rights reserved 3-6 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

© 2004 Pearson Addison-Wesley. All rights reserved 3-7 A Recursive Valued Method: The Factorial of n A recursive definition of factorial(n) factorial(n) = 1if n = 0 n * factorial(n-1)if n > 0 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)

© 2004 Pearson Addison-Wesley. All rights reserved 3-8 A Recursive void 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

© 2004 Pearson Addison-Wesley. All rights reserved 3-9 A Recursive void Method: Writing a String Backward Figure 3.6 A recursive solution

© 2004 Pearson Addison-Wesley. All rights reserved 3-10 A Recursive void Method: Writing a String Backward A recursive solution, second try wrtiteBackward2(s ) wrtiteBackward2(s minus first character)

© 2004 Pearson Addison-Wesley. All rights reserved 3-11 Multiplying Rabbits (The Fibonacci Sequence) “Facts” about rabbits –Rabbits never die –A rabbit reaches sexual maturity exactly two months after birth, that is, at the beginning of its third month of life –Rabbits are always born in male-female pairs At the beginning of every month, each sexually mature male-female pair gives birth to exactly one male-female pair

© 2004 Pearson Addison-Wesley. All rights reserved 3-12 Multiplying Rabbits (The Fibonacci Sequence) Problem –How many pairs of rabbits are alive in month n? Recurrence relation rabbit(n) = rabbit(n-1) + rabbit(n-2)

© 2004 Pearson Addison-Wesley. All rights reserved 3-13 Multiplying Rabbits (The Fibonacci Sequence) Figure 3.10 Recursive solution to the rabbit problem

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

© 2004 Pearson Addison-Wesley. All rights reserved 3-15 Searching an Array: Finding the Largest Item in an Array A recursive solution if (anArray has only one item) { maxArray(anArray) is the item in anArray } else if (anArray has more than one item) { maxArray(anArray) is the maximum of maxArray(left half of anArray) and maxArray(right half of anArray) } // end if

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

© 2004 Pearson Addison-Wesley. All rights reserved 3-17 Binary Search A high-level binary search if (anArray is of size 1) { Determine if anArray’s item is equal to value } else { Find the midpoint of anArray Determine which half of anArray contains value if (value is in the first half of anArray) { binarySearch (first half of anArray, value) } else { binarySearch(second half of anArray, value) } // end if

© 2004 Pearson Addison-Wesley. All rights reserved 3-18 Binary Search Implementation issues: –How will you pass “half of anArray ” to the recursive calls to binarySearch ? –How do you determine which half of the array contains value? –What should the base case(s) be? –How will binarySearch indicate the result of the search?

© 2004 Pearson Addison-Wesley. All rights reserved 3-19 Finding the k th Smallest Item in an Array The recursive solution proceeds by: 1.Selecting a pivot item in the array 2.Cleverly arranging, or partitioning, the items in the array about this pivot item 3.Recursively applying the strategy to one of the partitions

© 2004 Pearson Addison-Wesley. All rights reserved 3-20 Finding the k th Smallest Item in an Array Figure 3.18 A partition about a pivot

© 2004 Pearson Addison-Wesley. All rights reserved 3-21 Finding the k th Smallest Item in an Array Let: kSmall(k, anArray, first, last) = k th smallest item in anArray[first..last] Solution: –After select pivot p and partition array into S 1 and S 2 : kSmall(k, anArray, first, last) kSmall(k, anArray, first, pivotIndex-1) if k < pivotIndex – first + 1 = pif k = pivotIndex – first + 1 kSmall(k-(pivotIndex-first+1), anArray, pivotIndex+1, last) if k > pivotIndex – first + 1

© 2004 Pearson Addison-Wesley. All rights reserved 3-22 Organizing Data: The Towers of Hanoi Figure 3.19a and b a) The initial state; b) move n - 1 disks from A to C

© 2004 Pearson Addison-Wesley. All rights reserved 3-23 The Towers of Hanoi Figure 3.19c and d c) move one disk from A to B; d) move n - 1 disks from C to B

© 2004 Pearson Addison-Wesley. All rights reserved 3-24 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, spare, destination) solveTowers(1, source, destination, spare) solveTowers(count-1, spare, destination, source) } //end if

© 2004 Pearson Addison-Wesley. All rights reserved 3-25 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

© 2004 Pearson Addison-Wesley. All rights reserved 3-26 Recursion and Efficiency Consider maxArray(), binarySearch(), and kSmall() –They look similar – but do they have the same efficiency? –If not, which is the most efficient and which one is the least? Consider Tower of Hanoi –How (in)efficient is it? –Can we improve the efficiency?