ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can:...

Preview:

Citation preview

ITEC200 – Week07

Recursion

www.ics.mq.edu.au/ppdp 2

Learning Objectives – Week07Recursion (Ch 07)

Students can:• Design recursive algorithms to solve simple problems involving

strings or mathematical calculations• Construct methods that implement recursive algorithms• Provide traces of recursive methods and describe the flow of

recursive program execution using activation frames • Analyse recursive algorithms and methods for searching arrays• Describe approaches to creating recursive data structures and

make augmentations to them• Analyse recursive approaches to problem solving (Towers of

Hanoi problem, counting blobs, finding a pathway through a maze) and make augmentations to them

www.ics.mq.edu.au/ppdp 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

www.ics.mq.edu.au/ppdp 4

String Length Algorithm

Task: Design a recursive approach for finding the length of a String

public static int length(String str) { if (str == null || str.equals("")) return 0; else return 1 + length(str.substring(1));}

www.ics.mq.edu.au/ppdp 5

Steps to Design 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)

• 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

www.ics.mq.edu.au/ppdp 6

Tracing a Recursive Method

www.ics.mq.edu.au/ppdp 7

Proving that a Recursive Method is Correct

• 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

• 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

www.ics.mq.edu.au/ppdp 8

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

www.ics.mq.edu.au/ppdp 9

Recursive Factorial Method

Task: Design a recursive approach for calculating n!( Note: n! = n*(n-1)*(n-2)*…*3*2*1, so 5! = 5*4*3*2*1 = 120 )

www.ics.mq.edu.au/ppdp 10

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

www.ics.mq.edu.au/ppdp 11

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 usually code it as a recursive method– The reduction in efficiency needs to be weighed up against the

advantage of readable code that is easy to debug

www.ics.mq.edu.au/ppdp 12

An Exponential Recursive Fibonacci Method

Inefficient!

www.ics.mq.edu.au/ppdp 13

Recursive Array Search

• Searching an array can be accomplished using recursion

• Simplest way to search is using a linear search• Most efficient way to search is using a binary

search

www.ics.mq.edu.au/ppdp 14

Algorithm for Recursive Linear Array Search

www.ics.mq.edu.au/ppdp 15

Algorithm for Recursive Binary Array Search

www.ics.mq.edu.au/ppdp 16

Algorithm for Recursive Binary Array Search (continued)

www.ics.mq.edu.au/ppdp 17

Recursive Data Structures

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

• Computer scientists often encounter data structures that are defined recursively– Linked list can be described as a recursive data structure

– Trees (Chapter 8) are defined recursively

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

www.ics.mq.edu.au/ppdp 18

Problem Solving with Recursion - Three Case Studies

Towers of Hanoi

Counting cells in a blob Finding a path through a maze

www.ics.mq.edu.au/ppdp 19

Towers of Hanoi

•Task: write a program that provides a description of how to move a tower of disks from one pole to another pole, subject to the following constraints:

- Only the top disk on a peg can be moved to another peg

- A larger disk cannot be placed on top of a smaller disk

www.ics.mq.edu.au/ppdp 20

Towers of Hanoi (continued)

www.ics.mq.edu.au/ppdp 21

Algorithm for Towers of Hanoi

www.ics.mq.edu.au/ppdp 22

Algorithm for Towers of Hanoi (continued)

www.ics.mq.edu.au/ppdp 23

Algorithm for Towers of Hanoi (continued)

www.ics.mq.edu.au/ppdp 24

Recursive Algorithm for Towers of Hanoi

www.ics.mq.edu.au/ppdp 25

Implementation of Recursive Towers of Hanoi

www.ics.mq.edu.au/ppdp 26

Counting Cells in a Blob

Task: calculate the number of adjoining cells of the same colour amongst a collection of cells.

www.ics.mq.edu.au/ppdp 27

Count-cells Implementation

www.ics.mq.edu.au/ppdp 28

Finding a pathway through a maze

Task: provide a recursive solution for finding a pathway through a maze

www.ics.mq.edu.au/ppdp 29

Where to from here…

• Work through Chapter 7 of the Koffman & Wolfgang Text

• Conceptual Questions and Practical Exercises• Submit all preliminary work• Be prompt for your online class

www.ics.mq.edu.au/ppdp 30

Acknowledgements

These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 7 PowerPoint presentation

by Elliot B. Koffman and Paul A. T. Wolfgang

Recommended