Download presentation
Presentation is loading. Please wait.
1
ITEC200 – Week07 Recursion
2
www.ics.mq.edu.au/ppdp 2 Learning Objectives – Week07 Recursion (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
3
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
4
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)); }
5
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
6
www.ics.mq.edu.au/ppdp 6 Tracing a Recursive Method
7
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
8
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
9
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 )
10
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
11
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
12
www.ics.mq.edu.au/ppdp 12 An Exponential Recursive Fibonacci Method Inefficient!
13
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
14
www.ics.mq.edu.au/ppdp 14 Algorithm for Recursive Linear Array Search
15
www.ics.mq.edu.au/ppdp 15 Algorithm for Recursive Binary Array Search
16
www.ics.mq.edu.au/ppdp 16 Algorithm for Recursive Binary Array Search (continued)
17
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
18
www.ics.mq.edu.au/ppdp 18 Problem Solving with Recursion - Three Case Studies Towers of Hanoi Counting cells in a blobFinding a path through a maze
19
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
20
www.ics.mq.edu.au/ppdp 20 Towers of Hanoi (continued)
21
www.ics.mq.edu.au/ppdp 21 Algorithm for Towers of Hanoi
22
www.ics.mq.edu.au/ppdp 22 Algorithm for Towers of Hanoi (continued)
23
www.ics.mq.edu.au/ppdp 23 Algorithm for Towers of Hanoi (continued)
24
www.ics.mq.edu.au/ppdp 24 Recursive Algorithm for Towers of Hanoi
25
www.ics.mq.edu.au/ppdp 25 Implementation of Recursive Towers of Hanoi
26
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.
27
www.ics.mq.edu.au/ppdp 27 Count-cells Implementation
28
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
29
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
30
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.