Recursion: The Mirrors

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

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.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion Gordon College CPS212
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.
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.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
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++
© 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.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
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 2: Recursion: The Mirrors.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
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.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
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.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Recursion Powerful Tool
Recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion CENG 707.
Chapter 19: Recursion.
Recursion CENG 707.
Recursion Topic 5.
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion Chapter 12.
Chapter 15 Recursion.
Programming with Recursion
Announcements Final Exam on August 17th Wednesday at 16:00.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion: The Mirrors
Recursion Chapter 11.
Chapter 14: Recursion Starting Out with C++ Early Objects
Announcements Final Exam on August 19th Saturday at 16:00.
Recursion Chapter 18.
Recursion: The Mirrors
Chapter 17 Recursion.
Recursion: The Mirrors
Chapter 18 Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Recursion: The Mirrors Chapter 2

Recursive Solutions Recursion breaks problem into smaller identical problems An alternative to iteration A recursive solution

Recursive Solutions A recursive function calls itself Each recursive call solves an identical, but smaller, problem Test for base case enables recursive calls to stop Eventually, one of smaller problems must be the base case Recursion and looping have the same computational power

Recursive Solutions EX: Factorial Fact( n) = 1, if n = 0 = n * Fact(n-1), if n > 0 This is an example of a recurrence relation Questions for constructing recursive solutions How to define the problem in terms of a smaller problem of same type? How does each recursive call diminish the size of the problem? What instance of problem can serve as base case? As problem size diminishes, will you reach base case?

A Recursive Valued Function: The Factorial of n An iterative solution A recursive solution Note: Do not use recursion if a problem has a simple, efficient iterative solution

A Recursive Valued Function: The Factorial of n Box Trace

The Box Trace Label each recursive call Represent each call to function by a new box Draw arrow from box that makes call to newly created box After you create new box executing body of function On exiting function, cross off current box and follow its arrow back

The Box Trace The beginning of the box trace A box

The Box Trace Box trace of fact(3)

The Box Trace Box trace of fact(3)

The Box Trace Box trace of fact(3)

Writing an Array’s Entries in Backward Order? The function writeArrayBackward

The Binary Search of a Sorted Array – algorithm? Consider details before implementing algorithm: How to pass half of anArray to recursive calls of binarySearch ? How to determine which half of array contains target? What should base case(s) be? How will binarySearch indicate result of search?

The Binary Search Box trace of binarySearch with anArray = <1, 5, 9, 12, 15, 21, 29, 31>: (a) a successful search for 9;

The Binary Search Box trace of binarySearch with anArray = <1, 5, 9, 12, 15, 21, 29, 31>: (b) an unsuccessful search for 6

Binary Search Code void recBinarySearch (ArrayType a, int first, int last, ElementType item, bool & found, int & loc) /*------------------------------------------------------- Pre: Elements of a are in ascending order; item has the same type as the array elements Post: found = true and loc = position of item if search is successful; otherwise, found is false. -----------------------------------------------------------*/ { if (first > last) // anchor 1 -- empty sublist found = false; else // inductive case { loc = (first + last) / 2; if (item < a[loc]) // the first half recBinarySearch( a, first, loc-1, found loc); else if (item > a[loc]) // the second half recBinarySearch (a, loc + 1, last, found, loc); else found = true; // anchor 2 -- found item } }

A Bad Use of Recursion (Multiplying Rabbits) Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34 f1 = 1, f2 = 1 … fn = fn -2 + fn -1 A recursive function unsigned long Fib (unsigned n) /* Pre: n > 0 Post : if n > 0, returns nth fibonacci number, else returns error code */ { if (n <= 2) return 1; return Fib (n – 1) + Fib (n – 2); }

A Bad Use of Recursion Why is this inefficient? Note the block trace

Iterative Fibonacci /* Pre: n > 0 Post : if n > 0, returns nth fibonacci number, else returns error code */ unsigned long fib( unsigned int n ) { unsigned long k, f1, f2; if ( n < 2 ) return n; else { f1 = f2 = 1; for(k = 2; k <n; k++) { f = f1 + f2; f2 = f1; f1 = f; } return f;

Palindrome Checker – activity #include <iostream> #include <string> using namespace std; bool palChecker(const string &s, int first, int last); int main() { string palindrome = "able was I ere I saw elba"; //string palindrome = ""; //string palindrome = "foobar"; palChecker(palindrome, 0, palindrome.length() -1) ? cout<< " Is a palindrome. \n" : cout<< " Is not a palindrome. \n”; } bool palChecker(const string &s, int first, int last){ /* Precondition: none Postcondition : returns true iff s = sR */ if(first > last) return true; if(s[first] != s[last]) return false; return palChecker(s, first + 1, last - 1);}

The Towers of Hanoi The problem statement Solution Task Move disks from left peg to right peg When disk moved, must be placed on a peg Only one disk (top disk on a peg) moved at a time Larger disk may never be placed on a smaller disk The problem statement Beginning with n disks on pole A and zero disks on poles B and C, solve towers(n, A, B, C) . Solution With all disks on A, solve towers(n – 1, A, C, B) With the largest disk on pole A and all others on pole C, solve towers(n – 1, A, B, C) With the largest disk on pole B and all the other disks on pole C, solve towers(n – 1, C, B, A)

The Towers of Hanoi Hand trace the recursive solution with 3 and 5 disks, use http://haubergs.com/hanoi in Safari. Examine solveTowers.cpp – Chapter 2, section 2.5 on web site Move top disk from pole A to pole C Move top disk from pole A to pole B Move top disk from pole C to pole B Move top disk from pole B to pole A Move top disk from pole B to pole C

The Towers of Hanoi The order of recursive calls that results from solveTowers(3, A, B, C)

Choosing k Out of n Things-skip A Combinatorial method This example of a recursive solution comes from the field of Combinatorics Problem: A D.J. plays 10 songs each hour. There are 40 different songs. How many different one hour sets are possible? Or in general given 40 different things, how many sets of size 10 can be chosen Or using the standard notation – n choose k

N choose K From 5 choose 2 Consider a simpler problem, from 5 letterschoose 2, ABCDE All the sets with A (from 4 choose 1): AB AC AD AE Those without A (from 4 choose 2, no A): BC BD BE CD CE DE Or, in general, we get

N choose K Rewritten as a function named c that has two parameters n and k: We're getting closer but where is the base case?

N choose K base cases cont. The base cases for n choose k First, n must be at least as big as k: We cannot choose a 10 song set from 9 songs When n == k, there is only one possible 10 song set from 10 songs To be meaningful, k must be at least 1 We're not interested in sets with 0 songs When k is 1, there are n ways to choose If we only play 1 song set, and we have 10 songs to choose from, we get n, or 10, possible sets

N choose K Finally, here is the recursive definition of n choose k The recursive definition of n choose k summarizes all of these points: What is c(5, 2)? ____________ What is c(4, 1)? ____________ What is c(4, 2)? ____________ What is c(6, 3)? ____________

Choosing k Out of n Things Function for recursive solution.

Choosing k Out of n Things The recursive calls that g (4, 2) generates

Recursion and Efficiency Factors that contribute to inefficiency Overhead associated with function calls Some recursive algorithms inherently inefficient Keep in mind Recursion can clarify complex solutions … but … Clear, efficient iterative solution may be better

End Chapter 2