recursion - tom reboldtomrebold.com/csis10b/lectures/08l/lec8.pdf · tail recursion •consider...
TRANSCRIPT
![Page 1: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/1.jpg)
Recursion
Chapter 7
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 2: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/2.jpg)
Contents
• What Is Recursion?
• Tracing a Recursive Method
• Recursive Methods That Return a Value
• Recursively Processing an Array
• Recursively Processing a Linked Chain
• The Time Efficiency of Recursive Methods
The Time Efficiency of countDown
The Time Efficiency of Computing xn
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 3: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/3.jpg)
Contents
• A Simple Solution to a Difficult Problem
• A Poor Solution to a Simple Problem
• Tail Recursion
• Indirect Recursion
• Using a Stack Instead of Recursion
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 4: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/4.jpg)
Objectives
• Decide whether given recursive method
will end successfully in finite amount of
time
• Write recursive method
• Estimate time efficiency of recursive
method
• Identify tail recursion and replace it with
iteration
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 5: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/5.jpg)
What Is Recursion?
• We often solve a problem by breaking it
into smaller problems
• When the smaller problems are identical
(except for size)
This is called “recursion”
• Repeated smaller problems
Until problem with known solution is reached
• Example: Counting down from 10
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 6: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/6.jpg)
Figure 7-1 Counting down from 10
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 7: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/7.jpg)
Figure 7-1 Counting down from 10
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 8: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/8.jpg)
Figure 7-1 Counting down from 10
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 9: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/9.jpg)
What Is Recursion
• A method that calls itself is
A recursive method.
• The invocation is
A recursive call or
Recursive invocation
• Example:
Countdown
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 10: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/10.jpg)
Design of Recursive Solution
• What part of solution can contribute directly?
• What smaller (identical) problem has solution
that …
When taken with your contribution
Provides the solution to the original problem
• When does process end?
What smaller but identical problem has known
solution
Have you reached this problem, or base case? Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 11: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/11.jpg)
Design Guidelines
• Method must receive input value
• Must contain logic that involves this input
value and leads to different cases
• One or more cases should provide
solution that does not require recursion
Base case or stopping case
• One or more cases must include recursive
invocation of method
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 12: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/12.jpg)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 13: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/13.jpg)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 14: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/14.jpg)
Tracing Recursive Method
• Given recursive countDown method
• Figure 7-2 The effect of the method call countDown(3)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 15: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/15.jpg)
Figure 7-3 Tracing the recursive call countDown(3)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 16: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/16.jpg)
FIGURE 7-4 The stack of activation records during the execution of the call countDown(3)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 17: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/17.jpg)
FIGURE 7-4 The stack of activation records during the execution of the call countDown(3)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 18: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/18.jpg)
![Page 19: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/19.jpg)
![Page 20: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/20.jpg)
Recursive Methods
That Return a Value • Example:
Compute the sum 1 + 2 + . . . + n
For any integer n > 0.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 21: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/21.jpg)
Figure 7-5 Tracing the execution of sumOf(3)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 22: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/22.jpg)
Figure 7-5 Tracing the execution of sumOf(3)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 23: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/23.jpg)
![Page 24: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/24.jpg)
![Page 25: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/25.jpg)
Recursively Processing an
Array • Consider an array of integers
• We seek a method to display all or part
• Declaration public static void displayArray
(int[] array, int first, int last)
• Solution could be
Iterative
Recursive
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 26: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/26.jpg)
Recursively Processing an Array
• Recursive solution starting with array[first]
• Recursive solution starting with array[last]
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 27: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/27.jpg)
Dividing an Array in Half
• Common way to process arrays
recursively
Divide array into two portions
Process each portion separately
• Must find element at or near middle int mid = (first + last) / 2;
FIGURE 7-6 Two arrays with their middle elements within their left halves
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 28: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/28.jpg)
Dividing an Array in Half
• Recursive method to display array
Divides array into two portions
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 29: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/29.jpg)
Binary Search of a Sorted Array
• Algorithm for binary search
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 30: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/30.jpg)
Figure 18-6 A recursive binary search of a sorted array that
(a) finds its target;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 31: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/31.jpg)
Figure 18-6 A recursive binary search of a sorted array that
(b) does not find its target
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 32: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/32.jpg)
Efficiency of a
Binary Search of an Array
• Given n elements to be searched
• Number of recursive calls is of order
log2 n
• How many compares to search for an
item in an array of 1 million items? Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 33: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/33.jpg)
Java Class Library: The Method binarySearch
• Class Arrays contains versions of static
method
Note specification
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 34: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/34.jpg)
Merge Sort
• Divide array into two halves
Sort the two halves
Merge them into one sorted array
• Uses strategy of “divide and conquer”
Divide problem up into two or more distinct,
smaller tasks
• Good application for recursion
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 35: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/35.jpg)
Figure 9-1 Merging two sorted arrays into one sorted array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 36: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/36.jpg)
Figure 9-2 The major steps in a merge sort
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 37: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/37.jpg)
Merge Sort Algorithm
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 38: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/38.jpg)
Algorithm to Merge
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 39: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/39.jpg)
Figure 9-3 The effect of the recursive calls and
the merges during a merge sort
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 40: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/40.jpg)
Time Efficiency of Recursive
Methods • Consider method countdown
• Recurrence relation
• Proof by induction shows
• Thus method is O(n)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 41: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/41.jpg)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 42: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/42.jpg)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 43: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/43.jpg)
Efficiency of Merge Sort
• For n = 2k entries
In general k levels of recursive calls are made
• Each merge requires at most 3n – 1
comparisons
• Calls to merge do at most 3n – 22
operations
• Can be shown that efficiency is O(n log n)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 44: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/44.jpg)
Simple Solution to a Difficult
Problem • Consider Towers of Hanoi puzzle
Figure 7-7 The initial configuration of the Towers of Hanoi for three disks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 45: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/45.jpg)
Towers of Hanoi
• Rules
1. Move one disk at a time. Each disk you
move must be a topmost disk.
2. No disk may rest on top of a disk smaller
than itself.
3. You can store disks on the second pole
temporarily, as long as you observe the
previous two rules.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 46: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/46.jpg)
Solution • Move a disk from pole 1 to pole 3
• Move a disk from pole 1 to pole 2
• Move a disk from pole 3 to pole 2
• Move a disk from pole 1 to pole 3
• Move a disk from pole 2 to pole 1
• Move a disk from pole 2 to pole 3
• Move a disk from pole 1 to pole 3
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 47: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/47.jpg)
Figure 7-8 The sequence of moves for solving the Towers of
Hanoi problem with three disks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 48: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/48.jpg)
Figure 7-8 The sequence of moves for solving the Towers of
Hanoi problem with three disks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 49: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/49.jpg)
![Page 50: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/50.jpg)
![Page 51: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/51.jpg)
Recursive Solution
• To solve for n disks …
Ask friend to solve for n – 1 disks
He in turn asks another friend to solve for n –
2
Etc.
Each one lets previous friend know when their
simpler task is finished
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 52: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/52.jpg)
Figure 7-9 The smaller problems in a recursive solution
for four disks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 53: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/53.jpg)
Recursive Algorithm, VER1
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 54: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/54.jpg)
Recursive Algorithm, VER2
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 55: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/55.jpg)
![Page 56: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/56.jpg)
![Page 57: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/57.jpg)
Algorithm Efficiency
• Moves required for n disks
• We note and conjecture
(proved by induction)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 58: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/58.jpg)
Poor Solution to a Simple
Problem • Fibonacci sequence
1, 1, 2, 3, 5, 8, 13, …
• Suggests a recursive solution Note the two recursive calls
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 59: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/59.jpg)
FIGURE 7-10 The computation of the Fibonacci number F6
using (a) recursion; (b) iteration
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 60: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/60.jpg)
Time Efficiency of Algorithm
• Looking for relationship
• Can be shown that
• Conclusion: Do not use recursive solution
that repeatedly solves same problem in its
recursive calls.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 61: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/61.jpg)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 62: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/62.jpg)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 63: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/63.jpg)
Tail Recursion
• When the last action performed by a
recursive method is a recursive call
• Example:
• Repeats call with change in parameter or
variable Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 64: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/64.jpg)
Tail Recursion
• Consider this simple change to make an
iterative version
Replace if with while
Instead of recursive call, subtract 1 from integer
• Change of tail recursion to iterative often
simple Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 65: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/65.jpg)
Indirect Recursion
• Consider chain of events
Method A calls Method B
Method B calls Method C
and Method C calls Method A
• Mutual recursion
Method A calls Method B
Method B calls Method A
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 66: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/66.jpg)
Figure 7-11 An example of indirect recursion
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 67: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/67.jpg)
Using a Stack Instead of
Recursion • A way of replacing recursion with iteration
• Consider recursive displayArray
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 68: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/68.jpg)
Using a Stack Instead of
Recursion • We make a stack that mimics the program
stack
Push objects onto stack like activation records
Shown is example record
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 69: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/69.jpg)
Using a Stack Instead of
Recursion • Iterative version of displayArray
Copyright ©2012 by Pearson Education, Inc. All rights reserved
![Page 70: Recursion - Tom Reboldtomrebold.com/csis10b/lectures/08L/lec8.pdf · Tail Recursion •Consider this simple change to make an iterative version](https://reader035.vdocuments.mx/reader035/viewer/2022071003/5fc05eaaea50a11c0327448d/html5/thumbnails/70.jpg)
Recursion Chapter 7
Copyright ©2012 by Pearson Education, Inc. All rights reserved