Presentation is loading. Please wait.

Presentation is loading. Please wait.

And now for something completely different . . .

Similar presentations


Presentation on theme: "And now for something completely different . . ."— Presentation transcript:

1 And now for something completely different . . .
10-Apr-19 AHD c 2009

2 Part 12 Python 3 Introducing Recursion
10-Apr-19 AHD c 2009

3 Recursion 12.1 Introduction to Recursion
A practical example of recursion A recursive binary search algorithm 10-Apr-19 AHD c 2009

4 Recursion 12.1 Introduction to Recursion
A practical example of recursion A recursive binary search algorithm 10-Apr-19 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. 10-Apr-19 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. 10-Apr-19 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. 10-Apr-19 AHD c 2009

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

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

11 Recursion 12.1 Introduction to Recursion
A practical example of recursion A recursive binary search algorithm 10-Apr-19 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. . . 10-Apr-19 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. . . 10-Apr-19 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 * 1 10-Apr-19 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 10-Apr-19 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 10-Apr-19 AHD c 2009

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

18 A recursive function 12-01.py def factorial(n): if n == 0: return 1
else: return n * factorial(n-1) 12-01.py See all programs at: 10-Apr-19 AHD c 2009

19 Recursion 12.1 Introduction to Recursion
A practical example of recursion A recursive binary search algorithm 10-Apr-19 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. . . 10-Apr-19 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. 10-Apr-19 AHD c 2009

22 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 10-Apr-19 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. 10-Apr-19 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. . . 10-Apr-19 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. . . 10-Apr-19 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 10-Apr-19 AHD c 2009

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

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

29 Searching for number 7 is 7 <= 7? 7 found! 2 7 9 10 11 15 21 33 35
41 53 75 82 88 91 94 is 7 <= 7? 7 found! 10-Apr-19 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) 10-Apr-19 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 10-Apr-19 AHD c 2009

32 10-Apr-19 AHD c 2009

33 This presentation uses the following program file:
12-01.py See all programs at: 10-Apr-19 AHD c 2009

34 End of Python3_Recursion.ppt
10-Apr-19 AHD c 2009

35 Last updated: Wednesday 2nd December 2009, 6:39 PT, AHD
10-Apr-19 AHD c 2009


Download ppt "And now for something completely different . . ."

Similar presentations


Ads by Google