recursion
play

Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 15 Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements: Prelim 1 Graded and released Mean : 81 out of 104 (78%) Can pick up your exam in homework


  1. CS 1110: Introduction to Computing Using Python Lecture 15 Recursion [Andersen, Gries, Lee, Marschner, Van Loan, White]

  2. Announcements: Prelim 1 • Graded and released • Mean : 81 out of 104 (78%) • Can pick up your exam in homework handback room  Need Cornell ID  Suggest printing your netid on paper • Do not discuss exam with people taking makeups. • Regrade requests : we will send email to you 10/13/16 Recursion 2

  3. Announcements: Assignment 3 • Released. • Due : Thursday, March 30 th , 11:59pm • Recommendation: follow milestone deadlines. • You MUST acknowledge help from others  We run software analyzers to detect similar programs  Have had some academic integrity violations so far • Not a recursion assignment! 10/13/16 Recursion 3

  4. Announcement: Lab 8 • Out. • Not a recursion lab! 10/13/16 Recursion 4

  5. Recursion • Recursive Definition : A definition that is defined in terms of itself 10/13/16 Recursion 5

  6. A Mathematical Example: Factorial • Non-recursive definition: n! = n × n-1 × … × 2 × 1 = n (n-1 × … × 2 × 1) • Recursive definition: for n ≥ 0 n! = n (n-1)! Recursive case 0! = 1 Base case What happens if there is no base case? 10/13/16 Recursion 6

  7. Recursion • Recursive Definition : A definition that is defined in terms of itself • Recursive Function : A function that calls itself (directly or indirectly) 10/13/16 Recursion 7

  8. Recursive Call Frames factorial 1 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" if n == 0: 1 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 10/13/16 Recursion 8

  9. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" if n == 0: 1 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 10/13/16 Recursion 9

  10. Recursion factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" if n == 0: 1 return 1 2 Now what? return n*factorial(n-1) 3 Each call is a new frame. Call: factorial(3) 10/13/16 Recursion 10

  11. What happens next? A: B: CORRECT factorial factorial 1, 3 1, 3, 1  n n 3 2 3 factorial 1 D: n 2 factorial 1, 3, 1  n 3 2 C: ERASE FRAME factorial 1, 3 factorial factorial 1 1 n 3 n n 3 2 10/13/16 Recursion 11

  12. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1 if n == 0: 1 n 2 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 10/13/16 Recursion 12

  13. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 return n*factorial(n-1) 3 Call: factorial(3) 10/13/16 Recursion 13

  14. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1 return n*factorial(n-1) n 1 3 Call: factorial(3) 10/13/16 Recursion 14

  15. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 Call: factorial(3) 10/13/16 Recursion 15

  16. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1 Call: factorial(3) n 0 10/13/16 Recursion 16

  17. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1, 2 Call: factorial(3) n 0 10/13/16 Recursion 17

  18. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 18

  19. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 3 factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 19

  20. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 1 3 RETURN factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 20

  21. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 1 3 RETURN factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 21

  22. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 1 3 RETURN factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 22

  23. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 1 3 RETURN factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 23

  24. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 6 RETURN Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 1 3 RETURN factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 24

  25. Recursive Call Frames factorial 1, 3 def factorial(n): """Returns: factorial of n. n 3 6 RETURN Pre: n ≥ 0 an int""" factorial 1, 3 if n == 0: 1 n 2 2 RETURN return 1 2 factorial 1, 3 return n*factorial(n-1) n 1 1 3 RETURN factorial 1, 2 Call: factorial(3) n 0 1 RETURN 10/13/16 Recursion 25

  26. Example: Fibonnaci Sequence • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ... a 0 a 1 a 2 a 3 a 4 a 5 a 6  Get the next number by adding previous two  What is a 8 ? A: a 8 = 21 B: a 8 = 29 C: a 8 = 34 D: None of these. 10/13/16 Recursion 26

  27. Example: Fibonnaci Sequence • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ... a 0 a 1 a 2 a 3 a 4 a 5 a 6  Get the next number by adding previous two  What is a 8 ? A: a 8 = 21 B: a 8 = 29 correct C: a 8 = 34 D: None of these. 10/13/16 Recursion 27

  28. Example: Fibonnaci Sequence • Sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ... a 0 a 1 a 2 a 3 a 4 a 5 a 6  Get the next number by adding previous two  What is a 8 ? • Recursive definition:  a n = a n -1 + a n -2 Recursive Case  a 0 = 1 Base Case  a 1 = 1 (another) Base Case Why did we need two base cases this time? 10/13/16 Recursion 28

  29. Fibonacci as a Recursive Function def fibonacci(n): """Returns: Fibonacci no. a n Precondition: n ≥ 0 an int""" if n <= 1: Base case(s) return 1 return (fibonacci(n-1)+ Recursive case fibonacci(n-2)) Handles both base cases in one conditional. 10/13/16 Recursion 29

  30. Fibonacci as a Recursive Function def fibonacci(n): """Returns: Fibonacci no. a n Precondition: n ≥ 0 an int""" fibonacci 3 5 n if n <= 1: return 1 fibonacci 1 fibonacci 1 return (fibonacci(n-1)+ 4 3 n n fibonacci(n-2)) 10/13/16 Recursion 30

  31. Recursion vs Iteration • Recursion is provably equivalent to iteration  Iteration includes for-loop and while-loop (later)  Anything can do in one, can do in the other • But some things are easier with recursion  And some things are easier with iteration • Will not teach you when to choose recursion • We just want you to understand the technique 10/13/16 Recursion 31

  32. Recursion is best for Divide and Conquer Goal : Solve problem P on a piece of data data 10/13/16 Recursion 32

  33. Recursion is best for Divide and Conquer Goal : Solve problem P on a piece of data data Idea : Split data into two parts and solve problem data 1 data 2 Solve Problem P Solve Problem P 10/13/16 Recursion 33

  34. Recursion is best for Divide and Conquer Goal : Solve problem P on a piece of data data Idea : Split data into two parts and solve problem data 1 data 2 Solve Problem P Solve Problem P Combine Answer! 10/13/16 Recursion 34

Recommend


More recommend