1 / 24

CS1010 Discussion Group 11

CS1010 Discussion Group 11. Week 11 – Recursion recursion recursion…. HELLO!. S lides are at http ://www.comp.nus.edu.sg/~yanhwa /. PE 2. Saturday T_T. Frog. Lab 4.

lpeterson
Download Presentation

CS1010 Discussion Group 11

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. CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….

  2. HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

  3. PE 2 Saturday T_T

  4. Frog Lab 4 • Computation function jump should not printf(“Invalid move!”). It should return -1 to main() if jump is invalid and let main do the printing • How to reduce repeated code and combine left facing frog and right facing frog?

  5. Frog Lab 4

  6. Array of strings? Lab 4 • char mod[][MAX_LEN] • for (i = 0; i < size; i++) { • scanf("%s", mod[i]); • }

  7. Elevator Lab 4 • Use a structure! • Use array of strings! • Use arrays! • Use anything! (kidding)

  8. Recursion Lecture Summary • Divide and conquer paradigm • At first you will learn by tracing: Winding and Unwinding. Experience the magic. Assume nothing. • Later, you will learn to accept that the magic can only be fully understood if you assume that it works and you only need to care about the base case and how it combines correct answer(s) from previous calls to get the a new correct answer for this call!

  9. Recursion Lecture Summary • Recursion is like (strong) maths induction. Understand why the base case is as such. Assume that fib(k) is correct for k < n Thus we assume fib(n-1) and fib(n-2) is correct. Now that we got two correct answers for the smaller problems, how do we combine them to solve the larger problem of fib(n)? • Recurrence relation: • fn = fn-1 + fn-2 n≥ 2 • f0 = 0 • f1 = 1

  10. Recursion Lecture Summary • Do you understand how recursion makes use of the function call stack for it to work? • Stack overflow • Very important to make sure your base case(s) can be reached eventually and not calling itself forever. https://www.youtube.com/watch?v=PORo1ut9kMs

  11. Recursion Lecture Summary • May not be efficient (esp due to duplicate computation) • Function call overhead (calling the function, storing the activation record)

  12. Tutorial 1a Trace from bottom-up for such questions to reduce chance of mistakes. f(0) -> 0 f(1) -> 2 * 1 + f(0) -> 2 f(2) -> 2 * 2 + f(1) -> 2 + 4 = 6 f(3) -> 2 * 3 + f(2) -> 6 + 6 = 12 f(4) -> 2 * 4 + f(3) -> 12 + 8 = 20 f(5) -> 2 * 5 + f(4) -> 20 + 10 = 30

  13. Tutorial 1c What does the function compute? Trace the function?

  14. Tutorial 2 Summing digits in an integer What should be the base case? n < 10 return n (only have one digit left) At each call we return the “last digit” And combine with the sum of the rest of the digits

  15. Tutorial 3 What is the smaller problem? At this call, I see the last element and compare it with m. I decide whether to return the last element or m. What is m? m is the largest element for the rest of the array Thus it finds the maximum value of the array.

  16. Tutorial 4 Combine qn 2 and qn 3 together

  17. Tutorial 5 North east paths What should we do at each call? We should decide which direction to go. When do we have the base case? When do we only have one path left?

  18. Tutorial 5

  19. Tutorial 6 Auxiliary function

  20. Tutorial 7

  21. Tutorial 7

  22. Tutorial 8 What is the base case? What should I assume is already done for me? What should I do at this step? How to combine the answers from prev step with this step? At each step I need to fill the row and column!

  23. Tutorial 9 I will compare the mid value every time with the value we want to find How to know what is the mid and know where I am searching? I have “arrows” variables that store the indexes of the “start” and “end” So I need extra information… = auxiliary function Since for binary search we assume the array to be already sorted, if the value we want to find is larger than mid we only need to search at the part of the array mid+ 1 to end and can ignore the rest. = our recursive call Same for if value is smaller than mid

  24. Tutorial 9

More Related