1 / 20

By: Lokman Chan

Recursive Algorithm. --------. By: Lokman Chan. Recursion. Definition: A function that is define in terms of itself. Goal: Reduce the solution to a problem with a particular set of input to the same problem with smaller input values.

Download Presentation

By: Lokman Chan

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. Recursive Algorithm -------- By: Lokman Chan

  2. Recursion Definition: Afunction that is define in terms of itself. Goal: Reduce the solution to a problem with a particular set of input to the same problem with smaller input values. Simplify a complex problem to several sub problems Examples: Looking up a word from a dictionary (non mathematical) Solutions to “Towers of Hanol” problem (mathematical) Merge Sort, reversing a list, binary search, etc… --------

  3. Lookup a phone number in phone book Since the phone numbers are listed in alphabetical, we can actually apply the recursive strategy to find a number Consider  Search: middle page = (first page + last page)/2 Open the phone book to middle page; If (name is on middle page) then done; //this is the base case else if (name is alphabetically before middle page) last page = middle page //redefine search area to front half Search //recursive call with reduced number of pages else //name must be after middle page first page = middle page //redefine search area to back half Search //recursive call with reduced number of pages

  4. Towers of Hanoi The recursive algorithm is based on the observation that moving a tower of height h from pole A to pole B is equivalent to moving a tower of height h-1 to pole C, them moving the last disk from pole A to pole B, and finally moving the the tower from pole C to B. So the problem of moving a tower of height h, has been reduced to the one of moving a tower of height h-1.

  5. The idea behind… Objective: Move tower of 3 disks from peg a to peg b, with the help of peg c. Rules: Only one disk can be moved at a time. A larger disk can never be placed on top of a smaller disk. Assume the disk1 is smallest, disk2 is larger and disk3 is largest 1 2 3 _______ _______ _______ A B C 2 3 1 _______ _______ _______ A B C 

  6. Moresteps … 3 1 2 _______ _______ _______ A B C 1 3 2 _______ _______ _______ A B C 

  7. Steps… 1 3 2 _______ _______ _______ A B C 1 3 2 _______ _______ _______ A B C 

  8. Finally 2 1 3 _____ _____ _____ A B C 1 2 3 _______ _______ ______ A B C  It turns out to be a recurrence relationship and can be computed using recursive algorithm For better simulations, please go to http://www.cut-the-knot.org/recurrence/hanoi.shtml

  9. Merge Sort Merge sort can be done nicely with recursive algorithm but can also be implemented without using recursion. (For demonstration purpose, we will discuss only recursive merge sort) Merge sort is a sorting algorithm using divide and conquer algorithm.

  10. Divide and Conquer General Strategy: 1) Split the problem into subproblems 2) Solve subproblems 3) Combine the results

  11. http://www.cs.nott.ac.uk/~nza/G5BADS01/slides4.pdf

  12. http://www.cs.nott.ac.uk/~nza/G5BADS01/slides4.pdf

  13. Recursive Merge Sort • Split the array into two halves • Call merge on each half • Merge sorted Halves .

  14. Time Complexity Levels of recursion: log2N At each level: merging N items Time complexity: O(N log2 N) Space complexity: O(N)

  15. Basic foundation of Recursive Algo… • Base case(s): There must be at least one base which can be solved without recursion • A recursive call should always making progress, heading towards the base case. (Simplify the problem) • Don’t duplicate work by solving the same instance of a problem in separate recursive calls (don’t use recursion instead of a simple loop) 4) Design rule: Assume that all the recursive calls work

  16. Advantages of recursive algorithms The recursive algorithm gives a more understandable, code The actaul code can be expressed with a few lines of code when defined properly

  17. Disadvantages of Recursive Algo… Not all mathematically recursive functions can be efficiently implemented by Java’s simulation of recursion Infinite recursion can lead to stack overflow if the recursive code run (loops) forever

  18. THE END

More Related