1 / 35

And now for something completely different . . .

And now for something completely different . . . . Part 12 Python 3 Introducing Recursion . Recursion. 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm. Recursion. 12.1 Introduction to Recursion

vic
Download Presentation

And now for something completely different . . .

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. And now for something completely different . . . AHD c 2009

  2. Part 12 Python 3 Introducing Recursion AHD c 2009

  3. Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009

  4. Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009

  5. Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself. AHD c 2009

  6. A recursive solution to a programming problem uses a function which calls itself AHD c 2009

  7. Recursion Recursion is a very important concept in computer science, and is always studied along with sorting and searching data structures. AHD c 2009

  8. Recursion versus Repetition It's important to realize that most problems which have a recursive solution can also be solved by repetition. For example, the binary search algorithm can be implemented using either repetition or recursion. AHD c 2009

  9. Recursion versus Repetition There are times when repetition is the method of choice. . . AHD c 2009

  10. Recursion . . . but there are some problems which can only be solved elegantly by way of recursion. AHD c 2009

  11. Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009

  12. Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself which can either be solved explicitly or can be solved recursively. . . AHD c 2009

  13. In recursion, the base case is the version of the problem that can be solved explicitly, the problem that we know the answer to. . . AHD c 2009

  14. Consider the problem of solving the factorial of any positive integer. The factorial of a number n is written as n! and is calculated as follows: For any number n, n! = n * n-1 * n-2 * n-3 ... * 1 AHD c 2009

  15. Example: Calculation of 5! 5! = 5 * 4 * 3 * 2 * 1 = 120 Example: Calculation of 4! 4! = 4 * 3 * 2 * 1 = 24 AHD c 2009

  16. Example: Calculation of 3! 3! = 3 * 2 * 1 = 6 Example: Calculation of 2! 2! = 2 * 1 = 2 Example: Calculation of 1! 1! = 1 * 1 = 1 AHD c 2009

  17. Note that 5! is the same as 5 * 4! Note that 4! is the same as 4 * 3! Note that 3! is the same as 3 * 2! Which means that any factorial can be expressed in terms of a smaller version of itself. AHD c 2009

  18. A recursive function def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) 12-01.py See all programs at: http://www.annedawson.net/Python3Programs.txt AHD c 2009

  19. Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009

  20. Binary Search Binary search is a more specialized algorithm than a sequential search as it takes advantage of the fact that the data has been sorted. . . AHD c 2009

  21. Binary Search The underlying idea of binary search is to divide the sorted data into two halves and to examine the data at the point of the split. AHD c 2009

  22. 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 AHD c 2009

  23. Binary Search Since the data is sorted, we can easily ignore one half or the other depending on where the value we're looking for lies in comparison to the value at the split. This makes for a much more efficient search than linear search. AHD c 2009

  24. Binary Search Algorithms A binary search algorithm involves breaking down the problem into a smaller version of itself, and hence is an ideal candidate for a recursive solution. . . AHD c 2009

  25. Binary Search Binary search involves binary decisions, decisions with two choices. At each step in the process you can eliminate half of the data you're searching. . . AHD c 2009

  26. Binary Search If you have an list of 16 numbers (N = 16), you can find any one number in at most, 4 steps: 16 -> 8 -> 4 -> 2 -> 1 Log216 = 4 AHD c 2009

  27. Searching for number 7 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 33? AHD c 2009

  28. Searching for number 7 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 10? AHD c 2009

  29. Searching for number 7 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 7? 7 found! AHD c 2009

  30. The recursive algorithm binarySearch(a, value, left, right) if right < left return not found mid = floor((left+right)/2) if a[mid] = value return mid else if value < a[mid] binarySearch(a, value, left, mid-1) else if value > a[mid] binarySearch(a, value, mid+1, right) AHD c 2009

  31. The iterative (repetitive) algorithm binarySearch(a, value, left, right) while left <= right mid = floor((left+right)/2) if a[mid] = value return mid else if value < a[mid] right = mid-1 else if value > a[mid] left = mid+1 return not found AHD c 2009

  32. AHD c 2009

  33. This presentation uses the following program file: 12-01.py See all programs at: http://www.annedawson.net/Python3Programs.txt AHD c 2009

  34. End of Python3_Recursion.ppt AHD c 2009

  35. Last updated: Wednesday 2nd December 2009, 6:39 PT, AHD AHD c 2009

More Related