Download presentation
Presentation is loading. Please wait.
1
Recursion: The Mirrors
Chapter 2
2
Chapter 2 -- Recursion: The Mirrors
The goal of this chapter is to ensure that you have a basic understanding of recursion. This chapter assumes that you have had little or no previous introduction to recursion. If, however, you have already studied recursion, you can review this chapter as necessary. Chapter 5 continues the formal discussion of recursion by examining more-difficult problems. CS 308 Chapter 2 -- Recursion: The Mirrors
3
Chapter 2 -- Recursion: The Mirrors
Recursive Solutions Def: Iteration. Recursion. Two parts to a recursive function Base Case (stopping condition) Recursive Call CS 308 Chapter 2 -- Recursion: The Mirrors
4
Chapter 2 -- Recursion: The Mirrors
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 the base case? CS 308 Chapter 2 -- Recursion: The Mirrors
5
Chapter 2 -- Recursion: The Mirrors
A Recursive Valued Function: The Factorial of n Definition fact(n) = n * fact(n-1) This is called a recurrence relation What are the two parts of a Recursive Function? Base Case Recursive Call CS 308 Chapter 2 -- Recursion: The Mirrors
6
Chapter 2 -- Recursion: The Mirrors
Code: int fact(int n) { if (n==0) return 1; else return n * fact(n-1); } CS 308 Chapter 2 -- Recursion: The Mirrors
7
Chapter 2 -- Recursion: The Mirrors
Trace of fact(3); CS 308 Chapter 2 -- Recursion: The Mirrors
8
Chapter 2 -- Recursion: The Mirrors
A Recursive void Function: Writing a String Backward Given a string of characters, write it in reverse order. How are you going to do it? CS 308 Chapter 2 -- Recursion: The Mirrors
9
Chapter 2 -- Recursion: The Mirrors
What are the two parts? Base Case Recursive Call CS 308 Chapter 2 -- Recursion: The Mirrors
10
Chapter 2 -- Recursion: The Mirrors
Counting Things The next three problems require you to count certain events or combinations of events or things. They are good examples of problems with more than one base case. They also provide good examples of tremendously inefficient recursive solutions. CS 308 Chapter 2 -- Recursion: The Mirrors
11
Chapter 2 -- Recursion: The Mirrors
Multiplying Rabbits (The Fibonacci Sequence) Rules: Rabbits never die Rabbits are born in pairs Pairs give birth every month (starting with month 3) Sequence Month pair Month pair Month pairs (M1 + new pair) Month pairs (M1 + M3 pair + new pair) Month pairs (M1 & M3 both give birth + M4) Month pairs (M1, M3, & M4 give birth + ...) CS 308 Chapter 2 -- Recursion: The Mirrors
12
Chapter 2 -- Recursion: The Mirrors
To compute rabbit(n) how would you use rabbit(n-1) rabbit(n) = number alive last month + number born rabbit(n) = rabbit(n-1) + number born But, only those alive 2 months can give birth That is --- rabbit(n-2) CS 308 Chapter 2 -- Recursion: The Mirrors
13
Chapter 2 -- Recursion: The Mirrors
So, the recursive call is: rabbit(n) = rabbit(n-1) + rabbit(n-2) But what about the stopping condition? CS 308 Chapter 2 -- Recursion: The Mirrors
14
Chapter 2 -- Recursion: The Mirrors
Code: int rabbit(int n) { if(n<=2) return 1; else return rabbit(n-1) + rabbit(n-2); } CS 308 Chapter 2 -- Recursion: The Mirrors
15
Chapter 2 -- Recursion: The Mirrors
Number of calls generated by Rabbit(7) CS 308 Chapter 2 -- Recursion: The Mirrors
16
Chapter 2 -- Recursion: The Mirrors
Organizing a Parade Rules no bands back to back (they try to outplay each other) Equations P(n) = number of parades of length n F(n) = # of parades of length n ending with a float B(n) = # of parades of length n ending with a band Therefore P(n) = F(n) + B(n) CS 308 Chapter 2 -- Recursion: The Mirrors
17
Chapter 2 -- Recursion: The Mirrors
Solving F(n) Just add a float to all the parades of length n-1 F(n) = P(n-1) B(n) The only way a parade can end with a band is if a float just came before it. B(n) = F(n-1) Therefore....B(n) = P(n-2) Therefore ... P(n) = F(n) + B(n) P(n) = P(n-1) + P(n-2) Base Cases P(1) = 2 (float or band) P(2) = 3 (float-float, float-band, band-float) CS 308 Chapter 2 -- Recursion: The Mirrors
18
Chapter 2 -- Recursion: The Mirrors
Choosing k out of n Things Recursive Call c(n,k) = c(n-1,k-1)+c(n-1,k) Base Case c(k,k)=1; c(n,0)=1 also ... c(n,k) = 0 if k>n CS 308 Chapter 2 -- Recursion: The Mirrors
19
Chapter 2 -- Recursion: The Mirrors
Code: int c(int n, int k) { if((k==0) || (k==n)) return 1; else if (k>n) return 0; else return c(n-1,k-1) + c(n-1,k) } CS 308 Chapter 2 -- Recursion: The Mirrors
20
Chapter 2 -- Recursion: The Mirrors
The recursive calls that c(4,2) generates CS 308 Chapter 2 -- Recursion: The Mirrors
21
Chapter 2 -- Recursion: The Mirrors
Searching an Array Searching is an important task that occurs frequently. This section develops a binary search and examines other searching problems that have recursive solutions. CS 308 Chapter 2 -- Recursion: The Mirrors
22
Chapter 2 -- Recursion: The Mirrors
Finding the Largest Item in an Array CS 308 Chapter 2 -- Recursion: The Mirrors
23
Chapter 2 -- Recursion: The Mirrors
Recursive calls that maxArray(<1,6,8,3>) generates CS 308 Chapter 2 -- Recursion: The Mirrors
24
Chapter 2 -- Recursion: The Mirrors
Binary Search How do you pass half an array? 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? CS 308 Chapter 2 -- Recursion: The Mirrors
25
Chapter 2 -- Recursion: The Mirrors
Successful search Array <1,5,9,12,15,21,29,31> Search for 9 CS 308 Chapter 2 -- Recursion: The Mirrors
26
Chapter 2 -- Recursion: The Mirrors
Unsuccessful search Array <1,5,9,12,15,21,29,31> Search for 6 CS 308 Chapter 2 -- Recursion: The Mirrors
27
Chapter 2 -- Recursion: The Mirrors
Organizing Data The Towers of Hanoi Goal: move all disks from A to B Rules: Can move only 1 disk at a time Can’t place larger disk on top of smaller disk CS 308 Chapter 2 -- Recursion: The Mirrors
28
Recursion and Efficiency
Two factors contribute to the inefficiency of some recursive solutions: The overhead associated with function calls The inherent inefficiency of some recursive algorithms CS 308 Chapter 2 -- Recursion: The Mirrors
29
Chapter 2 -- Recursion: The Mirrors
CS 308 Chapter 2 -- Recursion: The Mirrors
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.