1 / 7

Recursion

Recursion. Definition: A method that calls itself. Examples public int factorial(int n) { if (n==1) return 1; return n * factorial(n-1); } public void hello(int n) { if (n>=0) { System.out.println("Hello"); hello(n-1); } }.

ethibault
Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion • Definition: A method that calls itself. • Examples public int factorial(int n) { if (n==1) return 1;return n * factorial(n-1); } public void hello(int n) { if (n>=0) { System.out.println("Hello"); hello(n-1); } }

  2. Java creates an activation record for every method callDefinition: An activation record is a block of memory on the callstack containing parameter values, local variables, the return address, and return value. The activation record exists only while the method executes The activation record is released when the method returns A stack is a last-in first-out list A list is an ordered collection of elements. Example call: int i = do(9, -1); System.out.println(i); Declaration: public int do(int n,int k) { int num = 5;return n + k + num; } Why does it work? Activation Record n: 9 k: -1 num: 5valueToReturn: 13 Return addr: Addr of the println() call.

  3. Another Example • Simple Example: See ../demos/SumByRecursion.java • Notes: • The then branch of the 'if' statement is the non-recursive part (the basis case) • The else branch contains the recursive call • A stack overflow occurs if the basis case never executes • Question: • What happens if startIndex is negative?

  4. Designing a recursive method • There are two requirements • Define the basis case • Define the relationship between the problem and one or more smaller problems of the same kind • When is recursion beneficial? • Recursive algorithms are ALWAYS slower than their non-recursive counterparts. Why? • Recursive algorithms can often reduce a problem to just a few statements. • Non-recursive algorithms can be sometimes more difficult to program and maintain.

  5. Some more examples • Fibonacci sequence: 1, 1, 2, 3, 5, 8, … • Return the value of a particular index in the sequence. (1st number in the sequence is index 0) • Basis case: n<2; Recursive step: return f(n-2)+f(n-1) • Greatest common denominator • Basis case: y%x = 0; Recursive step: return gcd(y%x,x) • See ../demos/GCD.java • Palindrome Tester • Basis case: length <=1 or first character <> last character • Recursive step: pal(s.substring(1,s.length()-2)) • Print a string (Question: How would you reverse a string?) • Basis case: length=1; Recursive step: print char 0 and call with remainder(i.e., take the action on the way back down the call stack) Do try these at home … 

  6. Towers of Hanoi • A disk is represented by its size 0 == smallest • A tower is represented by a char: A, B, or C • src == tower holding the disks to be moved • dest == tower to which to move the disks • spare == tower to use as a temporary holding place • https://www.cs.cmu.edu/~cburch/survey/recurse/hanoiimpl.html FUNCTION recursiveMove(disk, src, dest, spare): IF disk == 0, THEN: move disk from srcto dest ELSE: recursiveMove(disk - 1, src, spare, dest) move disk from srcto dest recursiveMove(disk - 1, spare, dest, src) END IF

  7. Graphics and Fractals • Definition: A recursive geometric pattern • Examples: • Drawing a tree visually • Sierpinski's triangles • Koch's Snowflake - https://en.wikipedia.org/wiki/Koch_snowflake See demos/KochFlakeLeaf.java • Mandelbrot set – uses complex numbers • Our lab4: Recursively generate an image

More Related