Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Introduction to Computing Science and Programming I.

Similar presentations


Presentation on theme: "Recursion Introduction to Computing Science and Programming I."— Presentation transcript:

1 Recursion Introduction to Computing Science and Programming I

2 Recursion Remember that the factorial function, x!, is defined as x * (x-1) * (x-2)…*2*1 Remember that the factorial function, x!, is defined as x * (x-1) * (x-2)…*2*1 Here’s a solution to this using a for loop Here’s a solution to this using a for loop def factorial(x): ans = 1 ans = 1 for i in range(x): for i in range(x): ans = ans * (i+1) ans = ans * (i+1) return ans return ans

3 Recursion There’s another way you can define the factorial function There’s another way you can define the factorial function x! = x * (x-1)! x! = x * (x-1)! = x * (x-1) * (x-2)! = x * (x-1) * (x-2)! = x * (x-1) * (x-2) *… * 1 * 0! = x * (x-1) * (x-2) *… * 1 * 0! 0! = 1 by definition 0! = 1 by definition In this way the function can be defined in terms of itself. In code you can call a function from within itself, this is recursion. In this way the function can be defined in terms of itself. In code you can call a function from within itself, this is recursion.

4 Recursion Recursion takes place when a function calls itself. The basic goal is to find a simpler version of the same function. Recursion takes place when a function calls itself. The basic goal is to find a simpler version of the same function. Another factorial function definition Another factorial function definition def factorial(x): def factorial(x): if (x==0): if (x==0): return 1 return 1 else: else: return x * factorial(x-1) return x * factorial(x-1)

5 Recursion What happens when we call factorial(3) What happens when we call factorial(3) factorial(3) makes a call to factorial(2) factorial(3) makes a call to factorial(2) factorial(2) makes a call to factorial(1) factorial(2) makes a call to factorial(1) factorial(1) calls factorial(0) factorial(1) calls factorial(0) factorial(0) returns 1 factorial(0) returns 1 factorial(1) completes and returns 1 factorial(1) completes and returns 1 Now factorial(2) can complete and returns 2 Now factorial(2) can complete and returns 2 And factorial(3) can complete and returns 6 And factorial(3) can complete and returns 6

6 Recursion The factorial function illustrates the two parts the every recursive function needs to have. The factorial function illustrates the two parts the every recursive function needs to have. 1. The recursion 1. The recursion def factorial(x): def factorial(x): if (x==0): if (x==0): return 1 return 1 else: else: return x * factorial(x-1) return x * factorial(x-1) The recursion is where the function calls itself where it needs the answer to a simpler (smaller) version of the problem. factorial(x) needs the answer to factorial(x-1) to complete The recursion is where the function calls itself where it needs the answer to a simpler (smaller) version of the problem. factorial(x) needs the answer to factorial(x-1) to complete

7 Recursion 1. The base case 1. The base case def factorial(x): def factorial(x): if (x==0): if (x==0): return 1 return 1 else: else: return x * factorial(x-1) return x * factorial(x-1) The base case gives a point where the problem doesn’t need to be broken down into a smaller version. The base case gives a point where the problem doesn’t need to be broken down into a smaller version. What would happen without a base case? What would happen without a base case? def factorial(x): def factorial(x): return x * factorial(x-1) return x * factorial(x-1)

8 Recursion How to set up a recursive function. How to set up a recursive function. Find a simpler subproblem Find a simpler subproblem This is the recursion. Each recursive call should bring the problem closer to the base case. This is the recursion. Each recursive call should bring the problem closer to the base case. Find a base case that doesn’t require a recursive call. Find a base case that doesn’t require a recursive call. Make sure you don’t make a recursive call before you check the base case. Make sure you don’t make a recursive call before you check the base case. def factorial(x): ans = x * factorial(x-1) if x==0: if x==0: ans = 1 ans = 1 return ans return ans

9 Recursion Recursion can be used to reverse the order of items in a list. Recursion can be used to reverse the order of items in a list. The subproblem isn’t as obvious as it was with the factorial example. The subproblem isn’t as obvious as it was with the factorial example. If you reverse the tail of a list (all but the first element) and then append the head (first element) you’ve reversed the list. If you reverse the tail of a list (all but the first element) and then append the head (first element) you’ve reversed the list. [1, 2, 3, 4] ---> [4, 3, 2, 1] [1, 2, 3, 4] ---> [4, 3, 2, 1] [2, 3, 4] ---> [4, 3, 2] [2, 3, 4] ---> [4, 3, 2]

10 Recursion So we have our recursion. We’ll repeatedly call reverse for the tail of the list. So we have our recursion. We’ll repeatedly call reverse for the tail of the list. At what point do we reach a base case? At what point do we reach a base case? A list that is empty or has one element is the reverse of itself, so this would be a good spot to end the recursion. A list that is empty or has one element is the reverse of itself, so this would be a good spot to end the recursion.

11 Recursion Reverse List function Reverse List function def reverseList(x): if len(x) <= 1: if len(x) <= 1: return x return x ans = reverseList(x[1:]) ans = reverseList(x[1:]) ans = ans.append(x[0]) ans = ans.append(x[0]) return ans return ans

12 Recursion Triangle of numbers Triangle of numbers We want to print a triangle of numbers with a given number of rows. We want to print a triangle of numbers with a given number of rows. if rows = 5 if rows = 5112123123412345

13 Recursion If we want to print a triangle with 5 rows, we first need to print out the first four rows. This will be our recursive case. If we want to print a triangle with 5 rows, we first need to print out the first four rows. This will be our recursive case. Once we get down to printing out a single row we don’t need to recurse any more so this will be our base case. Once we get down to printing out a single row we don’t need to recurse any more so this will be our base case.

14 Recursion def numTriangle(rows): if rows == 0: if rows == 0: return return else: else: numTriangle(rows-1) numTriangle(rows-1) line = “” line = “” for i in range(rows): for i in range(rows): line = line + str(i+1) line = line + str(i+1) print line print line

15 Recursion Remember that the sorting algorithms we looked at had running time n 2 while the best algorithms have running time n log n Remember that the sorting algorithms we looked at had running time n 2 while the best algorithms have running time n log n One of these n log n algorithms is merge sort. Merge sort uses the idea of splitting the problem into smaller pieces and recursion to get this advantage. One of these n log n algorithms is merge sort. Merge sort uses the idea of splitting the problem into smaller pieces and recursion to get this advantage.

16 Recursion The basic idea behind merge sort The basic idea behind merge sort Split the list into two halves Split the list into two halves Recursively sort each half Recursively sort each half Merge th two sorted halves Merge th two sorted halves [3, 6, 9, 4, 1, 7, 2, 8] [3, 6, 9, 4, 1, 7, 2, 8] [3, 6, 9, 4] [1, 7, 2, 8] [3, 6, 9, 4] [1, 7, 2, 8] [3, 6] [9, 4] [1, 7] [2, 8] [3, 6] [9, 4] [1, 7] [2, 8] [3] [6] [9] [4] [1] [7] [2] [8] [3] [6] [9] [4] [1] [7] [2] [8] We reached the base case, so now start merging. We reached the base case, so now start merging. [3, 6] [4, 9] [1, 7] [2, 8] [3, 6] [4, 9] [1, 7] [2, 8] [3, 4, 6, 9] [1, 2, 7, 8] [3, 4, 6, 9] [1, 2, 7, 8] [1, 2, 3, 4, 6, 7, 8, 9] [1, 2, 3, 4, 6, 7, 8, 9]

17

18

19


Download ppt "Recursion Introduction to Computing Science and Programming I."

Similar presentations


Ads by Google