350 likes | 505 Views
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
E N D
Part 12 Python 3 Introducing Recursion AHD c 2009
Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009
Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009
Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself. AHD c 2009
A recursive solution to a programming problem uses a function which calls itself AHD c 2009
Recursion Recursion is a very important concept in computer science, and is always studied along with sorting and searching data structures. AHD c 2009
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
Recursion versus Repetition There are times when repetition is the method of choice. . . AHD c 2009
Recursion . . . but there are some problems which can only be solved elegantly by way of recursion. AHD c 2009
Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009
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
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
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
Example: Calculation of 5! 5! = 5 * 4 * 3 * 2 * 1 = 120 Example: Calculation of 4! 4! = 4 * 3 * 2 * 1 = 24 AHD c 2009
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
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
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
Recursion 12.1 Introduction to Recursion 12.2 A practical example of recursion 12.3 A recursive binary search algorithm AHD c 2009
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
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
2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 AHD c 2009
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
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
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
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
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
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
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
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
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
This presentation uses the following program file: 12-01.py See all programs at: http://www.annedawson.net/Python3Programs.txt AHD c 2009
End of Python3_Recursion.ppt AHD c 2009
Last updated: Wednesday 2nd December 2009, 6:39 PT, AHD AHD c 2009