Download presentation
Presentation is loading. Please wait.
Published byDoris Gibbs Modified over 6 years ago
1
CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….
2
Slides are at http://www.comp.nus.edu.sg/~yanhwa/
HELLO! Slides are at
3
PE 2 Saturday T_T
4
Lab 4 Frog 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
Lab 4 Frog
6
for (i = 0; i < size; i++) { scanf("%s", mod[i]); }
Lab 4 Array of strings? char mod[][MAX_LEN] for (i = 0; i < size; i++) { scanf("%s", mod[i]); }
7
Use anything! (kidding)
Lab 4 Elevator Use a structure! Use array of strings! Use arrays! Use anything! (kidding)
8
Divide and conquer paradigm
Lecture Summary Recursion 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 is like (strong) maths induction.
Lecture Summary Recursion 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
Lecture Summary Recursion 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.
11
Recursion May not be efficient (esp due to duplicate computation)
Lecture Summary Recursion 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) -> = 6 f(3) -> 2 * 3 + f(2) -> = 12 f(4) -> 2 * 4 + f(3) -> = 20 f(5) -> 2 * 5 + f(4) -> = 30
13
Tutorial 1c What does the function compute? Trace the function?
14
Summing digits in an integer
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
What is the smaller problem?
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
Combine qn 2 and qn 3 together
Tutorial 4
17
What should we do at each call?
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
What should I assume is already done for me?
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.