Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think...

62
Fall 2007 CS 225 1 Recursion Chapter 7
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think...

Page 1: 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.

Fall 2007 CS 225 1

Recursion

Chapter 7

Page 2: 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.

Fall 2007 CS 225 2

Chapter Objectives• To understand how to think recursively• To learn how to trace a recursive method• To learn how to write recursive algorithms

and methods for searching arrays• To learn about recursive data structures and

recursive methods for a LinkedList class• To understand how to use recursion to

– solve the Towers of Hanoi problem– to process two-dimensional images– to solve search problems such as finding a path

through a maze using backtracking

Page 3: 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.

Fall 2007 CS 225 3

Recursive Thinking

• Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways– Recursion provides a different way of

thinking about a problem

• Recursion splits a problem into one or more simpler versions of itself

Page 4: 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.

Fall 2007 CS 225 4

Recursive Thinking

If there is one figure in the nestprocess the figure

elseprocess the outer figureprocess the nest inside the outer figure

Page 5: 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.

Fall 2007 CS 225 5

When to Use a Recursive Algorithm

• There must be at least one case (the base case), for a small value of n, that can be solved directly

• A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Page 6: 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.

Fall 2007 CS 225 6

Designing a Recursive Algorithm

• Recognize the base case and provide a solution to it

• Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case

• Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 7: 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.

Fall 2007 CS 225 7

String Length Algorithm

if the string is empty

the length is 0

else

length = 1 plus length of substring that excludes the first character

Page 8: 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.

Fall 2007 CS 225 8

Proof by induction

• Prove the theorem is true for the base case

• Show that if the theorem is assumed true for n, then it must be true for n+1

• See the slides on induction

Page 9: 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.

Fall 2007 CS 225 9

Proving that a Recursive Method is Correct

• Recursive proof is similar to induction– Verify the base case is recognized and

solved correctly– Verify that each recursive case makes

progress towards the base case– Verify that if all smaller problems are

solved correctly, then the original problem is also solved correctly

Page 10: 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.

Fall 2007 CS 225 10

Tracing a Recursive Method

Page 11: 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.

Fall 2007 CS 225 11

Recursive Definitions of Mathematical Formulas

• Mathematicians often use recursive definitions of formulas that lead very naturally to recursive algorithms

• Examples include:– Factorial– Powers– Greatest common divisor

• If a recursive function never reaches its base case, a stack overflow error occurs

Page 12: 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.

Fall 2007 CS 225 12

Recursive Factorial Method

• Recursive definitionn! = 1 if n=0 n * (n - 1)! if n>0

• Methodpublic static int factorial( int n) { if (n==0) return 1; else return n * factorial( n-1);}

Page 13: 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.

Fall 2007 CS 225 13

Recursive Algorithm for xn

• Algorithmif n is 0 result is 1else result is x * xn-1

• Methodpublic static double power( double x, int n) { if (n==0) return 1; else return x * power( x, n-1);

Page 14: 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.

Fall 2007 CS 225 14

Greatest Common Divisor• Euclid's algorithm (assumes m>=n)

gcd(m, n) = n if n is a divisor of m

gcd(m, n) = gcd( n, m % n) otherwise

• Methodpublic static double gcd( int m, int n) {

if (m%n==0)

return n;

else if (m<n);

return gcd( n, m);

else

return gcd( n, m%n);

}

Page 15: 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.

Fall 2007 CS 225 15

Recursion Versus Iteration• There are similarities between recursion and iteration• In iteration, a loop repetition condition determines

whether to repeat the loop body or exit from the loop• In recursion, the condition usually tests for a base

case • You can always write an iterative solution to a

problem that is solvable by recursion• Recursive code may be simpler than an iterative

algorithm and thus easier to write, read, and debug• Iteration is usually more efficient

Page 16: 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.

Fall 2007 CS 225 16

Iterative Factorial Method

public static factorialIter(

int n) {

int result = 1;

for k=1; k<=n; k++)

result *= n;

return result;

}

Page 17: 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.

Fall 2007 CS 225 17

Efficiency of Recursion

• Recursive methods often have slower execution times when compared to their iterative counterparts

• The overhead for loop repetition is smaller than the overhead for a method call and return

• If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method– The reduction in efficiency does not outweigh the

advantage of readable code that is easy to debug

Page 18: 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.

Fall 2007 CS 225 18

Recursive Fibonacci Method

• Recursive definitionfibn = 1 for n = 1,2

fibn-1 + fibn-2 for n>2

• Methodpublic static int fibonacci( int n) { if (n<=2) return 1; else return fibonacci(n-1) + fibonacci(n-2);

}

Page 19: 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.

Fall 2007 CS 225 19

Efficiency of Recursion: Exponential Fibonacci

Inefficient

Page 20: 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.

Fall 2007 CS 225 20

An O(n) Recursive Fibonacci Method

• To avoid excessive computation, remember the previous two values.

• Start the computationpublic static int fibonacciStart( int n) { return fibo( 1, 0, n); }

• Recursive method private static int fibo( int current, int previous, int n) if (n==1) return current; else return fibo( current + previous, current, n-1);

}

Page 21: 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.

Fall 2007 CS 225 21

Efficiency of Recursion: O(n) Fibonacci

Efficient

Page 22: 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.

Fall 2007 CS 225 22

Recursive Array Search

• Searching an array can be accomplished using recursion

• Simplest way to search is a linear search– Examine one element at a time starting with the first element

and ending with the last

• Base case for recursive search is an empty array– Result is negative one

• Another base case would be when the array element being examined matches the target

• Recursive step is to search the rest of the array, excluding the element just examined

Page 23: 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.

Fall 2007 CS 225 23

Algorithm for Recursive Linear Array Search

Page 24: 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.

Fall 2007 CS 225 24

Implementation of Recursive Linear Search

Page 25: 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.

Fall 2007 CS 225 25

Design of Binary Search Algorithm

• Binary search can be performed only on an array that has been sorted

• Stop cases– The array is empty– Element being examined matches the target

• Checks the middle element for a match with the target

• Throw away the half of the array that the target cannot lie within

Page 26: 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.

Fall 2007 CS 225 26

Binary Search Algorithm

Page 27: 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.

Fall 2007 CS 225 27

Trace of a Binary Search

Page 28: 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.

Fall 2007 CS 225 28

Implementation of Binary Search

Page 29: 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.

Fall 2007 CS 225 29

Implementation of Binary Search

Page 30: 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.

Fall 2007 CS 225 30

Efficiency of Binary Search and the Comparable Interface

• At each recursive call we eliminate half the array elements from consideration– O(log2n)

• Classes that implement the Comparable interface must define a compareTo method that enables its objects to be compared in a standard way– CompareTo allows one to define the ordering of

elements for their own classes

Page 31: 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.

Fall 2007 CS 225 31

Method Arrays.binarySearch

• Java API class Arrays contains a binarySearch method– Can be called with sorted arrays of primitive types

or with sorted arrays of objects– If the objects in the array are not mutually

comparable or if the array is not sorted, the results are undefined

– If there are multiple copies of the target value in the array, there is no guarantee which one will be found

– Throws ClassCastException if the target is not comparable to the array elements

Page 32: 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.

Fall 2007 CS 225 32

Recursive Data Structures

• Computer scientists often encounter data structures that are defined recursively– Trees (Chapter 8) are defined recursively

• Linked list can be described as a recursive data structure

• Recursive methods provide a very natural mechanism for processing recursive data structures

• The first language developed for artificial intelligence research was a recursive language called LISP

Page 33: 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.

Fall 2007 CS 225 33

Recursive Definition of a Linked List

• A non-empty linked list is a collection of nodes such that each node references another linked list consisting of the nodes that follow it in the list

• The last node references an empty list• A linked list is empty, or it contains a

node, called the list head, that stores data and a reference to a linked list

Page 34: 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.

Fall 2007 CS 225 34

Recursive Size Method

Page 35: 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.

Fall 2007 CS 225 35

Recursive toString Method

Page 36: 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.

Fall 2007 CS 225 36

Recursive Replace Method

Page 37: 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.

Fall 2007 CS 225 37

Recursive Add Method

Page 38: 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.

Fall 2007 CS 225 38

Recursive Remove Method

Page 39: 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.

Fall 2007 CS 225 39

Recursive Remove Method (continued)

Page 40: 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.

Fall 2007 CS 225 40

Problem Solving with Recursion

• Will look at two problems– Towers of Hanoi– Counting cells in a blob

Page 41: 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.

Fall 2007 CS 225 41

Towers of Hanoi

Page 42: 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.

Fall 2007 CS 225 42

Towers of Hanoi (continued)

Page 43: 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.

Fall 2007 CS 225 43

Algorithm for Towers of Hanoi

Page 44: 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.

Fall 2007 CS 225 44

Algorithm for Towers of Hanoi (continued)

Page 45: 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.

Fall 2007 CS 225 45

Algorithm for Towers of Hanoi (continued)

Page 46: 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.

Fall 2007 CS 225 46

Recursive Algorithm for Towers of Hanoi

Page 47: 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.

Fall 2007 CS 225 47

Implementation of Recursive Towers of Hanoi

Page 48: 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.

Fall 2007 CS 225 48

Counting Cells in a Blob

• Consider how we might process an image that is presented as a two-dimensional array of color values

• Information in the image may come from– X-Ray– MRI– Satellite imagery– Etc.

• Goal is to determine the size of any area in the image that is considered abnormal because of its color values

Page 49: 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.

Fall 2007 CS 225 49

Classes for Counting Cells in a Blob

Page 50: 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.

Fall 2007 CS 225 50

Algorithm for Counting Cells in a Blob

if the cell is outside the grid result is 0if color is not abnormal result is 0else

set color to marker color result = 1 + number of cells in blob that includes a neighbor

Page 51: 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.

Fall 2007 CS 225 51

Implementation

Page 52: 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.

Fall 2007 CS 225 52

Implementation

Page 53: 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.

Fall 2007 CS 225 53

Counting Cells in a Blob

Page 54: 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.

Fall 2007 CS 225 54

Backtracking

• Backtracking is an approach to using trial and error in a search for a solution– Backtracking is a systematic approach to trying

alternative paths and eliminating them if they don’t work

• For example: finding a path through a maze– Start by walking down a path as far as you can go

• Eventually, you will reach your destination or you won’t be able to go any farther

• If you can’t go any farther, you will need to retrace your steps

• You need a way to keep track of what you've already tried

Page 55: 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.

Fall 2007 CS 225 55

Backtracking

• Never try the exact same path more than once, and you will eventually find a solution path if one exists

• Problems that are solved by backtracking can be described as a set of choices made by some method

• Recursion allows us to implement backtracking in a relatively straightforward manner– Each activation frame is used to remember the

choice that was made at that particular decision point

• A program that plays chess may involve some kind of backtracking algorithm

Page 56: 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.

Fall 2007 CS 225 56

Backtracking

Page 57: 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.

Fall 2007 CS 225 57

Recursive Algorithm for Finding Maze Path

Page 58: 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.

Fall 2007 CS 225 58

Maze Implementation

Page 59: 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.

Fall 2007 CS 225 59

Maze Implementation

Page 60: 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.

Fall 2007 CS 225 60

Maze Implementation

Page 61: 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.

Fall 2007 CS 225 61

Chapter Review

• A recursive method has a standard form• To prove that a recursive algorithm is correct, you

must– Verify that the base case is recognized and solved

correctly– Verify that each recursive case makes progress toward

the base case– Verify that if all smaller problems are solved correctly,

then the original problem must also be solved correctly

• The run-time stack uses activation frames to keep track of argument values and return points during recursive method calls

Page 62: 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.

Fall 2007 CS 225 62

Chapter Review

• Mathematical Sequences and formulas that are defined recursively can be implemented naturally as recursive methods

• Recursive data structures are data structures that have a component that is the same data structure

• Towers of Hanoi and counting cells in a blob can both be solved with recursion

• Backtracking is a technique that enables you to write programs that can be used to explore different alternative paths in a search for a solution