cs221 algorithms and data structures recursion and
play

CS221: Algorithms and Data Structures Recursion and Iteration Alan - PowerPoint PPT Presentation

CS221: Algorithms and Data Structures Recursion and Iteration Alan J. Hu (Borrowing many slides from Steve Wolfman) 1 Learning Goals By the end of this unit, you will be able to Describe the relationship between recursion/iteration


  1. Recursion is handled the same way! n=3 n=4

  2. Recursion is handled the same way! n=3 n=4

  3. Recursion is handled the same way! n=2 n=3 n=4

  4. Recursion is handled the same way! n=2 n=3 n=4

  5. Recursion is handled the same way! return 1 n=3 n=4

  6. Recursion is handled the same way! n=3, result=1+… n=4

  7. Recursion is handled the same way! n=1 n=3, result=1+… n=4

  8. Recursion is handled the same way! return 1 n=3, result=1+… n=4

  9. Recursion is handled the same way! n=3, result=1+1 n=4

  10. Recursion is handled the same way! return 2 n=4

  11. Recursion is handled the same way! n=4, result=2+…

  12. Recursion is handled the same way! n=4, result=2+…

  13. Recursion is handled the same way! n=2 n=4, result=2+…

  14. Recursion is handled the same way! n=2 n=4, result=2+…

  15. Recursion is handled the same way! return 1 n=4, result=2+…

  16. Recursion is handled the same way! n=4, result=2+1

  17. Recursion is handled the same way! return 3

  18. Recursion is handled the same way! As I said before, do NOT try to think about recursion this way!

  19. Recursion is handled the same way! As I said before, do NOT try to think about recursion this way! However, by seeing what the computer does, we can see what takes time and space: Each call takes time. We will try to avoid wasted calls. The max depth of the call stack is the max space, because each activation record takes O(1) space.

  20. Aside: Activation Records and Computer Security • Have you heard about “buffer overrun” attacks? • Suppose, when talking to your buddy, he manages to make you forget what you were in the middle of doing before his call? • Suppose a function messes up the return address in the call stack?

  21. Aside: Computer Security n=2 n=4, result=2+…

  22. Aside: Computer Security Evil attacker code: install backdoor install rootkit n=2 install Sony DRM software … n=4, result=2+…

  23. Aside: Computer Security Evil attacker code: install backdoor install rootkit n=2 install Sony DRM software … n=4, result=2+…

  24. Aside: Computer Security Evil attacker code: install backdoor install rootkit return 1 install Sony DRM software … n=4, result=2+…

  25. Aside: Computer Security Evil attacker code: install backdoor install rootkit install Sony DRM software … n=4, result=2+…

  26. Limits of the Call Stack int fib(int n) { if (n == 1) return 1; else if (n == 2) return 1; else return fib(n-1) + fib(n-2); } cout << fib(0) << endl; What will happen? a. Returns 1 immediately. b. Runs forever (infinite recursion) c. Stops running when n “wraps around” to positive values. d. Bombs when the computer runs out of stack space. e. None of these. 70

  27. Function Calls in Daily Life • How do you handle interruptions in daily life? – You’re at home, working on CPSC221 project. – You stop to look up something in the book. – Your roommate/spouse/partner/parent/etc. asks for your help moving some stuff. – Your buddy calls. – The doorbell rings.

  28. Tail Calls in Daily Life • How do you handle interruptions in daily life? – You’re at home, working on CPSC221 project. – You stop to look up something in the book. – Your roommate/spouse/partner/parent/etc. asks for your help moving some stuff. – Your buddy calls. – The doorbell rings. • If new task happens just as you finish previous task, there’s no need for new activation record. • These are called tail calls.

  29. Why Tail Calls Matter • Since a tail call doesn’t need to generate a new activation record on the stack, a good compiler won’t make the computer do that. • Therefore, a tail call doesn’t increase depth of call stack. • Therefore, the program uses less space if you can set it up to use a tail call.

  30. Managing the Call Stack: Tail Recursion void endlesslyGreet() { cout << "Hello, world!" << endl; endlesslyGreet(); } This is clearly infinite recursion. The call stack will get as deep as it can get and then bomb, right? But... why? What work is the call stack doing? There’s nothing to remember on the stack! Try compiling it with at least –O2 optimization and running. 74 It won’t give a stack overflow!

  31. Tail Recursion A function is “tail recursive” if for every recursive call in the function, that call is the absolute last thing the function needs to do before returning. In that case, why bother pushing a new stack frame? There’s nothing to remember. Just re-use the old frame. That’s what most compilers will do. 75

  32. Tail Recursion A function is “tail recursive” if for every recursive call in the function, that call is the absolute last thing the function needs to do before returning. In that case, why bother pushing a new stack frame? There’s nothing to remember. Just re-use the old frame. That’s what most compilers will do. Note: KW textbook is WRONG on definition of tail recursion! They say it’s based on the last line, and the example they give is NOT tail recursive! 76

  33. Tail Recursive? int fib(int n) { if (n <= 2) return 1; else return fib(n-1) + fib(n-2); } Tail recursive? a. Yes. b. No. c. Not enough information. 77

  34. Tail Recursive? int factorial (int n) { if (n == 0) return 1; else return n * factorial(n – 1); } Tail recursive? a. Yes. b. No. c. Not enough information. 78

  35. Tail Recursive? int fact(int n) { return fact_acc(n, 1); } int fact_acc (int n, int acc) { if (n == 0) return acc; else return fact_acc(n – 1, acc * n); } Tail recursive? a. Yes. b. No. c. Not enough information. 79

  36. Mythbusters: Recursion vs. Iteration Which one can do more? Recursion or iteration? 80

  37. MythBusters: Simulating a Loop with Recursion int i = 0 recDoFoo(0, n) while (i < n) doFoo(i) Where recDoFoo is: i++ void recDoFoo(int i, int n) { if (i < n) { doFoo(i) recDoFoo(i + 1, n) } } Anything we can do with iteration, we can do with recursion. 81

  38. Mythbusters: Recursion vs. Iteration Which one can do more? Recursion or iteration? So, since iteration is just a special case of recursion (when it’s tail recursive), recursion can do more. But… 82

  39. Mythbusters: Recursion vs. Iteration Which one can do more? Recursion or iteration? So, since iteration is just a special case of recursion (when it’s tail recursive), recursion can do more. But… If you have a stack (or can implement one somehow), iteration with a stack can do anything recursion can! 83

  40. Mythbusters: Recursion vs. Iteration Which one can do more? Recursion or iteration? So, since iteration is just a special case of recursion (when it’s tail recursive), recursion can do more. But… If you have a stack (or can implement one somehow), iteration with a stack can do anything recursion can! – (Aside: If you are developing a new computational paradigm, e.g., with DNA, being able to simulate a 84 stack is a key building block.)

  41. Mythbusters: Recursion vs. Iteration Which one can do more? Recursion or iteration? So, since iteration is just a special case of recursion (when it’s tail recursive), recursion can do more. But… If you have a stack (or can implement one somehow), iteration with a stack can do anything recursion can! – This can be a little tricky. – Better to let the computer do it for you! 85

  42. Simulating Recursion with a Stack • What does a recursive call do? – Saves current values of local variables and where execution is in the code. – Assigns parameters their passed in value. – Starts executing at start of function again. • What does a return do? – Goes back to most recent call. – Restores most recent values of variables. – Gives return value back to caller. • We can do on a stack what the computer does for us on the system stack…

  43. Simulating Recursion with a Stack • Cut the function at each call or return, into little pieces of code. Give each piece a name. • Create a variable pc, which will hold the name of the piece of code to run. • Put all the pieces in a big loop. At the top of the loop, choose which piece to run based on pc. • At each recursive call, push local variables, push name of code to run after return, push arguments, set pc to Start. • At Start, pop function arguments. • At other labels, pop return value, pop local variables. • At return, pop “return address” into pc, push return value. This is not something we expect you to do in full generality in CPSC 221.

  44. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n Anything we can do with recursion, push(Middle);push(n-1);pc=Start; continue; we can do with iteration w/ a stack. } } else { //pc==Middle result=pop(); oldn=pop(); result=oldn*result; pc=pop(); push(result); } 88 } // result is on top of stack

  45. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n push(Middle);push(n-1);pc=Start; continue; } } else { //pc==Middle result=pop(); oldn=pop(); result=oldn*result; 2 pc=pop(); push(result); } 89 Done } // result is on top of stack

  46. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n push(Middle);push(n-1);pc=Start; continue; } } else { //pc==Middle n=2 result=pop(); oldn=pop(); result=oldn*result; pc=pop(); push(result); } 90 Done } // result is on top of stack

  47. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n push(Middle);push(n-1);pc=Start; continue; } } else { //pc==Middle n=2 result=pop(); oldn=pop(); result=oldn*result; pc=pop(); push(result); } 91 Done } // result is on top of stack

  48. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n n=2 push(Middle);push(n-1);pc=Start; continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 92 Done } // result is on top of stack

  49. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n n=2 push(Middle);push(n-1);pc=Start; continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 93 Done } // result is on top of stack

  50. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n n=2 push(Middle);push(n-1);pc=Start; continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 94 Done } // result is on top of stack

  51. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n n=1 push(Middle);push(n-1);pc=Start; continue; } } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 95 Done } // result is on top of stack

  52. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } push(n); //save old n n=1 push(Middle);push(n-1);pc=Start; continue; } } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 96 Done } // result is on top of stack

  53. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } 0 push(n); //save old n n=1 push(Middle);push(n-1);pc=Start; Middle continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 97 Done } // result is on top of stack

  54. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } 0 push(n); //save old n n=1 push(Middle);push(n-1);pc=Start; Middle continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 98 Done } // result is on top of stack

  55. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } n=0 push(n); //save old n push(Middle);push(n-1);pc=Start; Middle continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 99 Done } // result is on top of stack

  56. Simulating Recursion with a Stack push(Done); push(n); pc=Start; while (1) { int factorial (int n) { if (pc==Done) break; if (n == 0) return 1; if (pc==Start) { else n=pop(); if (n == 0) { return n * pc=pop(); push 1; continue; factorial(n – 1); } else { } n=0, pc=Middle push(n); //save old n push(Middle);push(n-1);pc=Start; 1 continue; } 1 } else { //pc==Middle result=pop(); oldn=pop(); Middle result=oldn*result; 2 pc=pop(); push(result); } 100 Done } // result is on top of stack

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend