Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0. Chapter 2: Recursion: The Mirrors.

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.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
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.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
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.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
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.
© 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.
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-
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
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.
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.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
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 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 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
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.
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.
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.
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.
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.
CSC 205 Programming II Lecture 8 Recursion.
Recursion CENG 707.
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

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 Recursive Solutions Recursion is an extremely powerful problem-solving technique –Breaks problem into smaller identical problems –An alternative to iteration, which involves loops A binary search is recursive –Repeatedly halves the data collection and searches the one half that could contain the item –Uses a divide and conquer strategy –Example : to search a word in a dictionary A base case is a special case whose solution you know

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Facts about a recursive solution 1.A recursive function calls itself  Means function searchDictionary will can function searchDictionary that makes it recursive 2.Each recursive call solves an identical, but smaller, problem  Each call to the searchDictionary function will cut the search area 3.The solution to at least one smaller problem— the base case—is known  Such as searching a one page is the base case 4.Eventually, one of the smaller problems must be the base case; reaching the base case enables the recursive calls to stop

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Four questions for constructing 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?

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive Valued Function: The Factorial of n Problem –Compute the factorial of an integer n –Usually written as n! -factorial(3) or 3! = 3 * 2 * 1 = 6 An iterative definition of factorial(n) factorial(n) = n * (n – 1) * (n – 2) * … * 1 for any integer n > 0 factorial(0) = 1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive Valued Function: 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) -Similarly; -factorial (3) = 3 * factorial (2) -factorial (2) = 2 * factorial (1) -factorial (1) = 1 * factorial (0) -factorial (0) = 1 – the base case

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver return 2*fact(1) 2* ? Figure 2.2 fact(3) System.out.println(fact(3));? return 3*fact(2) 3*? return 1*fact(0) 1* ? return

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive Valued Function: The Factorial of n 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)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive Valued Function: The Factorial of n Box trace –A systematic way to trace the actions of a recursive function –Each box roughly corresponds to an activation record –Contains a function’s local environment at the time of and as a result of the call to the function Figure 2-3 A box

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive Valued Function: The Factorial of n Figure 2-3 A box

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive Valued Function: The Factorial of n A function’s local environment includes: –The function’s local variables –A copy of the actual value arguments –A return address in the calling routine –The value of the function itself

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive void Function: 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 Execution of writeBackward can be traced using the box trace Temporary cout statements can be used to debug a recursive method

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive void Function: Writing a String Backward Execution of writeBackward can be traced using the box trace Temporary cout statements can be used to debug a recursive method

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver A Recursive void Function: Writing a String Backward Figure 2-6 A recursive solution writeBackward(“cat”,3) tac

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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) Figure 2-10 Recursive solution to the rabbit problem

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Multiplying Rabbits (The Fibonacci Sequence) Figure 2-10 Recursive solution to the rabbit problem

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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; that is, 1, 1, 2, 3, 5, 8, 13, 21, 34, … Thus, rabbit(6) = 8 pairs –Month1= 1 pair, the original pair –Month2= 1 pair, not sexually mature –Month3= 2 pairs, original pair has given birth –Month4= 3 pairs, the original has given birth again, pair on month 3 not mature yet –Month5= 5 pairs, all rabbit alive in month 3 (2 pairs) has matured. Add the offspring to the pair of month 4 (3 pairs) so total 5 pairs –Month6 = 8 pairs, 3 newborn pairs from the pairs alive in month 4 plus 5 pairs alive in month 5

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Organizing a Parade Problem –How many ways can you organize a parade of length n? –The parade will consist of bands and floats in a single line –One band cannot be placed immediately after another

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Organizing a Parade Let: –P(n) be the number of ways to organize a parade of length n –F(n) be the number of parades of length n that end with a float –B(n) be the number of parades of length n that end with a band Then –P(n) = F(n) + B(n)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Organizing a Parade Number of acceptable parades of length n that end with a float –F(n) = P(n – 1) Number of acceptable parades of length n that end with a band –B(n) = F(n – 1) Number of acceptable parades of length n –P(n) = P(n – 1) + P(n – 2)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Organizing a Parade Base cases P(1) = 2(The parades of length 1 are float and band.) P(2) = 3(The parades of length 2 are float- float, band- float, and float-band.)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Organizing a Parade Solution P(1) = 2 P(2) = 3 P(n) = P(n – 1) + P(n – 2) for n > 2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Mr. Spock’s Dilemma (Choosing k out of n Things) Problem –How many different choices are possible for exploring k planets out of n planets in a solar system? Let c(n, k) be the number of groups of k planets chosen from n In terms of Planet X: c(n, k) = the number of groups of k planets that include Planet X + the number of groups of k planets that do not include Planet X

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Mr. Spock’s Dilemma (Choosing k out of n Things) Let c(n, k) be the number of groups of k planets chosen from n In terms of Planet X: c(n, k) = the number of groups of k planets that include Planet X + the number of groups of k planets that do not include Planet X

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Mr. Spock’s Dilemma (Choosing k out of n Things) The number of ways to choose k out of n things is the sum of –The number of ways to choose k – 1 out of n – 1 things and the number of ways to choose k out of n – 1 things –c(n, k) = c(n – 1, k – 1) + c(n – 1, k)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Mr. Spock’s Dilemma (Choosing k out of n Things) Base cases –There is one group of everything c(k, k) = 1 –There is one group of nothing c(n, 0) = 1 –Although k cannot exceed n here, we want our solution to be general c(n, k ) = 0 if k > n

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Mr. Spock’s Dilemma (Choosing k out of n Things) Recursive solution c(n, k) = 1 if k = 0 1 if k = n 0 if k > n c(n – 1, k – 1) + c(n – 1, k) if 0 < k < n

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Mr. Spock’s Dilemma (Choosing k out of n Things) Figure 2-12 The recursive calls that c(4, 2) generates

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Searching an Array: Finding the Largest Item in an Array Figure 2-13 Recursive solution to the largest-item problem

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Binary Search A high-level binary search binarySearch(in anArray:ArrayType, in value:ItemType) 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) }

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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?

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver int binarySearch(const int anArray[], int first, int last, int value) { int index; if (first > last) index = -1; // value not in original array else { // Invariant: If value is in anArray, // anArray[first] <= value <= anArray[last] int mid = (first + last)/2; if (value == anArray[mid]) index = mid; // value found at anArray[mid] else if (value < anArray[mid]) // point X index = binarySearch(anArray, first, mid-1, value); else // point Y index = binarySearch(anArray, mid+1, last, value); } // end if return index; } // end binarySearch 34

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Finding the k th Smallest Item in an Array The recursive solution proceeds by: –Selecting a pivot item in the array –Cleverly arranging, or partitioning, the items in the array about this pivot item –Recursively applying the strategy to one of the partitions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Finding the k th Smallest Item in an Array Figure 2-18 A partition about a pivot

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Finding the k th Smallest Item in an Array Let: kSmall(k, anArray, first, last) = k th smallest item in anArray[first..last]

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Finding the k th Smallest Item in an Array Solution: kSmall(k, anArray, first, last) = kSmall(k,anArray,first,pivotIndex-1) if k < pivotIndex – first + 1 = p if k = pivotIndex – first + 1 = kSmall(k-(pivotIndex-first+1), anArray, pivotIndex+1, last) if k > pivotIndex – first + 1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Organizing Data: The Towers of Hanoi Figure 2-19a and b (a) The initial state; (b) move n - 1 disks from A to C

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver The Towers of Hanoi Figure 2-19c and d (c) move one disk from A to B; (d) move n - 1 disks from C to B

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 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 function calls –Inherent inefficiency of some recursive algorithms –Do not use a recursive solution if it is inefficient and there is a clear, efficient iterative solution

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursion and Efficiency Do not use a recursive solution if it is inefficient and there is a clear, efficient iterative solution

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Summary Recursion solves a problem by solving a smaller problem of the same type Four questions: –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(s) of the problem can serve as the base case? –As the problem size diminishes, will you reach a base case?

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Summary To construct a recursive solution, assume a recursive call’s postcondition is 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Summary Some recursive solutions are much less efficient than a corresponding iterative solution due to their inherently inefficient algorithms and the overhead of function calls If you can easily, clearly, and efficiently solve a problem by using iteration, you should do so