1 / 86

Advanced Recursion Design by Contract Data Abstraction

Lecture 7. Advanced Recursion Design by Contract Data Abstraction. Advanced Recursion. Recursive Function Example: Factorial. Problem: calculate n! (n factorial) n! = 1 if n = 0 n! = 1 * 2 * 3 *...* n if n > 0 Recursively: if n = 0 , then n! = 1

vilmos
Télécharger la présentation

Advanced Recursion Design by Contract Data Abstraction

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. Lecture 7 Advanced RecursionDesign by ContractData Abstraction

  2. Advanced Recursion

  3. Recursive Function Example: Factorial Problem: calculate n! (n factorial) n! = 1 if n = 0 n! = 1 * 2 * 3 *...* n if n > 0 Recursively: if n = 0, then n! = 1 if n > 0, then n! = n * (n-1)!

  4. Solving Recursive Problems • See recursive solutions as two sections: • Current • Rest N! = N * (N-1)! 7! = 7 * 6! 7! = 7 * (6 * 5 * 4 * 3 * 2 * 1 * 1)

  5. Factorial Function function Factorial returnsa Num (n isoftype in Num) // Calculates n factorial, n! // Precondition: n is a non-negative // integer if (n = 0) then Factorial returns 1 else Factorial returns n * Factorial(n-1) endif endfunction //Factorial

  6. if (0 = 0) then Fact returns 1 else Fact returns 1 endif endfunction //Fact if (1 = 0) then Fact returns 1 else Fact returns 1 * Fact(0) endif endfunction //Fact if (2 = 0) then Fact returns 1 else Fact returns 2 * Fact(1) endif endfunction //Fact if (3 = 0) then Fact returns 1 else Fact returns 3 * Fact(2) endif endfunction //Fact algorithm Test ans <- Fact(3) endalgorithm

  7. 1. Actual parameters stored on the stack 5. Return value and release stack frame 3. Create a new Stack Frame 4. Unfinished Business 2. Recursive call to Fact Tracing Details function Fact returnsa Num (2) if (2 = 0) then Fact returns 1 else Fact returns 2 * Fact(1) endif endfunction //Fact

  8. Activation Stack for Factorial Call the function: answer <- Fact(5) Main Algorithm: Unfinished: answer <- Fact (5)

  9. Activation Stack for Factorial Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  10. Activation Stack for Factorial Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  11. Activation Stack for Factorial Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  12. Activation Stack for Factorial Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  13. Activation Stack for Factorial Fact. 5th: N=1, Unfinished: 1*Fact(0) Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  14. Activation Stack for Factorial Fact. 6th: N=0, Finished: returns 1 Fact. 5th: N=1, Unfinished: 1*Fact(0) Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  15. Activation Stack for Factorial Fact. 5th: N=1, Finished: returns 1*1 Fact. 4th: N=2, Unfinished: 2*Fact(1) Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  16. Activation Stack for Factorial Fact. 4th: N=2, Finished: returns 2*1 Fact. 3rd: N=3, Unfinished: 3*Fact(2) Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  17. Activation Stack for Factorial Fact. 3rd: N=3, Finished: returns 3*2 Fact. 2nd: N=4, Unfinished: 4*Fact(3) Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  18. Activation Stack for Factorial Fact. 2nd: N=4, Finished: returns 4*6 Fact. 1st: N=5, Unfinished: 5*Fact(4) Main Algorithm: Unfinished: answer <- Fact (5)

  19. Activation Stack for Factorial Fact. 1st: N=5, Finished: returns 5*24 Main Algorithm: Unfinished: answer <- Fact (5)

  20. Activation Stack for Factorial Main Algorithm: Finished: answer <- 120

  21. LB Exponentiation baseexponent e.g. 53 Could be written as a function Power(base, exp)

  22. LB Can we write it recursively? be = b * b(e-1) What’s the limiting case? When e = 0 we have b0 which always equals? 1

  23. Another Recursive Function function Power returnsa Num (base, exp isoftype in Num) // Computes the value of BaseExp // Pre: exp is a non-negative integer if (exp = 0) then Power returns 1 else Power returns base * Power(base, exp-1) endif endfunction //Power

  24. Power base = 3 exp = 0 Finished:1 1 Power base = 3 exp = 1 3 *Power(3,0) 3 Power base = 3 exp = 2 3 *Power(3,1) 9 Power base = 3 exp = 3 3 *Power(3,2) 27 Power base = 3 exp = 4 3 *Power(3,3) 81 Algo: total <- Power(3,4) Function Power returnsa Num (base, exp isoftype in Num) //Computes the value of BaseExp //Preconditions: exp is a non-negative integer if(exp = 0 ) then Power returns 1 else Power returns (base * Power(base, exp – 1)) endif endfunction //Power Activations Stack Example 1 3 9 27 Total <- 81

  25. LB Bunnies? • The Time: 13th Century • The Place: Italy • The Man: Fibonacci • The Problem: We start with a pair of newborn rabbits. At the end of the 2nd month, and each month thereafter the female gives birth to a new pair of rabbits: one male and one female. The babies mature at the same rate as the parents and begin to produce offspring on the same schedule. So how many rabbits do we have at the end of one year?

  26. = 1 pair bunnies (m/f) LB

  27. A More Complex Recursive Function Fibonacci Number Sequence if n = 1, then Fib(n) =1 if n = 2, then Fib(n) = 1 if n > 2, then Fib(n) = Fib(n-2) + Fib(n-1) Numbers in the series: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

  28. Fibonacci Sequence Function function Fib returnsa Num (n iot in Num) // Calculates the nth Fibonacci number // Precondition: N is a positive integer if ((n = 1) OR (n = 2)) then Fib returns 1 else Fib returnsFib(n-2) + Fib(n-1) endif endfunction //Fibonacci

  29. Tracing with Multiple Recursive Calls Main Algorithm: answer <- Fib(5)

  30. Tracing with Multiple Recursive Calls Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)

  31. Tracing with Multiple Recursive Calls Fib(3): Fib returns Fib(1) + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)

  32. Tracing with Multiple Recursive Calls Fib(1): Fib returns 1 Fib(3): Fib returns Fib(1) + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)

  33. Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)

  34. Tracing with Multiple Recursive Calls Fib(2): Fib returns 1 Fib(3): Fib returns 1 + Fib(2) Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)

  35. Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + 1 Fib(5): Fib returns Fib(3) + Fib(4) Main Algorithm: answer <- Fib(5)

  36. Tracing with Multiple Recursive Calls Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  37. Tracing with Multiple Recursive Calls Fib(4): Fib returns Fib(2) + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  38. Tracing with Multiple Recursive Calls Fib(2): Fib returns 1 Fib(4): Fib returns Fib(2) + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  39. Tracing with Multiple Recursive Calls Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  40. Tracing with Multiple Recursive Calls Fib(3): Fib returns Fib(1) + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  41. Tracing with Multiple Recursive Calls Fib(1): Fib returns 1 Fib(3): Fib returns Fib(1) + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  42. Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  43. Tracing with Multiple Recursive Calls Fib(2): Fib returns 1 Fib(3): Fib returns 1 + Fib(2) Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  44. Tracing with Multiple Recursive Calls Fib(3): Fib returns 1 + 1 Fib(4): Fib returns 1 + Fib(3) Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  45. Tracing with Multiple Recursive Calls Fib(4): Fib returns 1 + 2 Fib(5): Fib returns 2 + Fib(4) Main Algorithm: answer <- Fib(5)

  46. Tracing with Multiple Recursive Calls Fib(5): Fib returns 2 + 3 Main Algorithm: answer <- Fib(5)

  47. Tracing with Multiple Recursive Calls Main Algorithm: answer <- 5

  48. Mutual Recursion Recursion doesn’t always occur because a routine calls itself... Mutual Recursion occurs when two routines call each other. A B A B

  49. Mutual Recursion Example Problem: Determine whether a number, N, is odd or even. • If N is equal to 0, then n is even • N is odd if N-1 is even

  50. Example Implementation function Odd returnsa Boolean (n iot in Num) if (n = 0) then Odd returns FALSE else Odd returns Even (n - 1) endif endfunction //Odd function Even returnsa Boolean (n iot in Num) if (n = 0) then Even returns TRUE else Even returns Odd (n - 1) endif endfunction //Even

More Related