1 / 28

Mastering Recursion: A Comprehensive Guide to Recursive Algorithms

Explore the fundamental programming technique of recursion to tackle complex problems. Learn the ins and outs of recursion, recursive definitions, base cases, and more. Dive into recursive programming with Java and understand when to implement recursion effectively. Make informed decisions on leveraging recursion for elegant solutions.

pdibella
Download Presentation

Mastering Recursion: A Comprehensive Guide to Recursive Algorithms

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. CSE 501NFall ‘0912: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost

  2. Lecture Outline • Lab 4 questions • Recursion • Recursive Algorithms

  3. Recursion • Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems • We will look at • thinking & programming in a recursive manner • the correct use of recursion • recursion examples

  4. Recursive Definitions • A recursive definition is one which uses the word or concept being defined in the definition itself • A recursive computation solves a problem by using the solution of the same problem with simpler values • For recursion to terminate, there must be special cases for the simplest inputs • These are often called “base cases” • When defining an English word, a recursive definition is often not helpful but in other situations, a recursive definition can be an appropriate way to express a concept

  5. Recursion • Two key requirements for recursion success: • Every recursive call must simplify the computation in some way • There must be special cases to handle the simplest computations directly • Base cases

  6. Recursive Definitions • Consider the following list of numbers: 24, 88, 40, 37 • Such a list can be defined recursively as follows: • That is, a LIST is defined to be a single number, or a number followed by a comma followed by a LIST • The concept of a LIST is used to define itself A LIST is a: number or a: number comma LIST

  7. Recursive Definitions • The recursive part of the LIST definition is used several times, terminating with the non-recursive part: • number comma LIST • 24 , 88, 40, 37 • number comma LIST • 88 , 40, 37 • number comma LIST • 40 , 37 • number • 37

  8. Infinite Recursion • All recursive definitions have to have a non-recursive part • (The base case) • If they didn't, there would be no way to terminate the recursive path • Such a definition would cause infinite recursion • This problem is similar to an infinite loop, but the non-terminating "loop" is part of the definition itself

  9. Recursive Definitions • N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive • This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)! • A factorial is defined in terms of another factorial • Eventually, the base case of 1! is reached

  10. 120 24 6 2 Recursive Definitions 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1

  11. Recursive Programming • A method in Java can invoke itself • Called a recursive method • The code of a recursive method must be structured to handle both the base case and the recursive case • Each call to the method sets up a new execution environment, with new parameters and local variables • As with any method call, when the method completes, control returns to the method that invoked it (which may be an earlier invocation of itself)

  12. Recursive Programming • Consider the problem of computing the sum of all the numbers between 1 and any positive integer N • This problem can be recursively defined as:

  13. Recursive Programming // This method returns the sum of 1 to num public intsummation (int num) { int result; if (num == 1) result = 1; else result = num + summation(n-1); return result; }

  14. result = 6 sum(3) result = 3 sum sum(2) result = 1 sum sum(1) sum Recursive Programming main

  15. Recursive Programming • Note that just because we can use recursion to solve a problem, doesn't mean we should • For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is easier to understand • However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version • You must carefully decide whether recursion is the correct technique for any problem

  16. Indirect Recursion • A method invoking itself is considered to be direct recursion • A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again • For example, method m1 could invoke m2, which invokes m3, which in turn invokes m1 again • This is called indirect recursion, and requires all the same care as direct recursion • It is often more difficult to trace and debug

  17. m1 m2 m3 m1 m2 m3 m1 m2 m3 Indirect Recursion

  18. Detecting Palindromes • How can we determine whether a String is a palindrome or not? • Iterative solution? • Recursive solution?

  19. Detecting Palindromes String text = “able was I ere I saw elba”; public boolean isPalindrome (String text, int start, int end) { if (start == end) return true; else if (text.getCharAt(start) == text.getCharAt(end)) { return isPalindrome(start++, end--); } else { return false; } } isPalindrome(text, start, end) returns whether the substring of text from start to end is a palindrome

  20. Pascal’s Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 int computePascalTriangleValue (int col, int row) { // … } [ Example on Board ]

  21. Tiled Pictures • Consider the task of repeatedly displaying a set of images in a mosaic • Three quadrants contain individual images • Upper-left quadrant repeats pattern • The base case is reached when the area for the images shrinks to a certain size

  22. Tiled Pictures

  23. Tiled Pictures private final int MIN = 20; public void drawPictures(int size, Graphics page) { page.drawImage(everest, 0, size/2, size/2, size/2); page.drawImage(goat, size/2, 0, size/2, size/2); page.drawImage(world, size/2, size/2, size/2, size/2); if (size > MIN) { drawPictures(size/2, page); } }

  24. Fractals • A fractal is a geometric shape made up of the same pattern repeated in different sizes and orientations • The Koch Snowflake is a particular fractal that begins with an equilateral triangle • To get a higher order of the fractal, the sides of the triangle are replaced with angled line segments

  25. < x5, y5> < x5, y5> < x4, y4> < x3, y3> < x2, y2> < x1, y1> < x1, y1> Koch Snowflakes Becomes

  26. Koch Snowflakes

  27. Koch Snowflakes

  28. Conclusion • Questions? • Midterm in class on Tuesday • Email me questions if you have them • I will be in lab now

More Related