Download presentation
Presentation is loading. Please wait.
Published bySamantha Francis Modified over 8 years ago
1
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP 112 2014T1 #8
2
COMP 112 2: 2 © D.Streader Overview What is recursion? How dose iteration relate to recursion? Why use recursion? 1.Make some problems are easier to solve 2.Makes some problems quicker to solve Recursion will become more important in Java 8 Ideally you should be able to decide if recursion is appropriate or not for the problems that you have to solve. Functions over infinite domains frequently need recursion.
3
COMP 112 2: 3 © D.Streader Towers of Hanoi (run demo) Goal is to move discs to the right hand place Rules: 1.Move only one disc at a time 2.Do not place a disc on top of a smaller disc Try to design an algorithm that will work no mater how many discs there are but with out recursion (using iteration)
4
COMP 112 2: 4 © D.Streader Recursion – what is it. A recursive method is a method that calls itself. Even with out recursion Java is functional complete. That is you do not need recursion to solve any problem. But thinking recursively can greatly help. Java maintains a call stack – see BlueJ debugging Calling method Move adds move to the call stack If method Move calls itself a new call to move is added to the stack before the initial call is removed
5
COMP 112 2: 5 © D.Streader A classic problem solving metaphor is Divide and conquer The size of the TofH problem is n the number of discs. A solution for TofH-n can be constructed assuming a solution for TofH-(n-1). Move 3 L discs to M Move 1 L disc to R Move 3 M discs to R MLR n = 4 Divide and conquer
6
COMP 112 2: 6 © D.Streader A constructive appraoch Divide and conquer decomposes a large problem into smaller problems Alternative appraoch is to use solution to smaller problems to build solutions to larger problems Solution with 2 discs - Move 1 disc L2M and 1 disc L2R and 1 disc M2R Solution with 3 discs - Move 2 disc L2M and 1 disc L2R and 2 disc M2R Solution with 4 discs - Move 3 disc L2M and 1 disc L2R and 3 disc M2R Solution with n discs - Move n-1 disc L2M and 1 disc L2R and n-1 disc M2R
7
COMP 112 2: 7 © D.Streader Towers of Hanoi This problem can be solved in three lines of recursive code. Plus a lot of boiler plate. Note the only method that actually moves any disc is moveonedisc(); The method move() simply controls what disc to move.
8
COMP 112 2: 8 © D.Streader Other problems Island mapped by black cells in a 2D array. Flood fill the island by changing the island green with out changing anything other than the island. 1.Solve recursively 2.Implement without recursion Parsing
9
COMP 112 2: 9 © D.Streader Divide and conquer for Speed Find an element in a sorted list of size n (Problem size n) Look at each element in turn until you find the element. Algorithm complexity O(n) order n Alternatively you can divide the list into two parts and search one of the shorter lists. Which depends on the value at the split. Next you do this recursively until there is one element. Algorithm complexity O(log(n)) order log(n) What is this log(n)? Which is faster for any given problem size n?
10
COMP 112 2: 10 © D.Streader Log(n) for large n (data sizes) Even large data sizes cannot be split in 2 a lot of times! You can only slit in 2 data with 1,099,511,627,776 elements 40 times Algorithms of order log(n) complexity become very very much faster than algorithms of order n complexity. m2^m 12 24 38 416 532 664 7128 8256 9512 101024 15 32,768 20 1,048,576 30 1 1,073,741,824 40 1,099,511,627,776 log nn
11
COMP 112 2: 11 © D.Streader Sort Bubble sort is of complexity O(n 2 ) Using divide and conquer design a faster algorithm!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.