Download presentation
Presentation is loading. Please wait.
1
Recursion- Fibonacci mpack
2
Recursion Base case Recurrence Time Complexity/ Space Complexity
3
According to Wikipedia…
Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem A common computer programming tactic is to divide a problem into sub-problems of the same type as the original, solve those sub-problems, and combine the results. This is often referred to as the divide-and-conquer method; when combined with a lookup table that stores the results of solving sub-problems (to avoid solving them repeatedly and incurring extra computation time), it can be referred to as dynamic programming or memoization. BlaBlaBla
4
Base cases A recursive function definition has one or more base cases, meaning input(s) for which the function produces a result trivially (without recurring), Because the base case breaks the chain of recursion, it is sometimes also called the "terminating case". there is not an obvious base case implied by the input data; for these one may add a parameter(such as the number of terms to be added, in our series example) to provide a 'stopping criterion' that establishes the base case.
5
Recursive cases one or more recursive cases, meaning input(s) for which the program recurs (calls itself). The job of the recursive cases can be seen as breaking down complex inputs into simpler ones.
6
In my words, Recursion is.. DC
DIVIDE: the big problems into smaller problems until it reaches to the base case COMPUTE: from the base case to the goal(the biggest problem) This is why we need base case <- to stop division and start computation smaller problems(recursive cases) <- which covers all the cases
7
Recursion 1. base case 2. recurrence
8
(1) TWO Base Cases We have two base cases!
9
(2) Recursive Cases – HOW to DIVIDE into simpler/smaller problems
We have two recursive cases!
10
(3) The order of how to operate recursion
Start!
11
(3) The order of how to operate recursion
Input!
12
(3) The order of how to operate recursion
Compare with base cases
13
(3) The order of how to operate recursion
Divide the bigger one to smaller one
14
(3) The order of how to operate recursion
Divide the bigger one to smaller one Divide the bigger one to smaller one
15
(3) The order of how to operate recursion
Divide the bigger one to smaller one Divide the bigger one to smaller one Divide the bigger one to smaller one
16
(3) The order of how to operate recursion
Divide the bigger one to smaller one Divide the bigger one to smaller one Divide the bigger one to smaller one .
17
(3) The order of how to operate recursion
When it reaches to base cases!!! Divide the bigger one to smaller one Divide the bigger one to smaller one Divide the bigger one to smaller one .
18
(3) The order of how to operate recursion
When it reaches to base cases!!! Start COMPUTING!!! Divide the bigger one to smaller one Divide the bigger one to smaller one Divide the bigger one to smaller one .
19
(3) The order of how to operate recursion
When it reaches to base cases!!! Start COMPUTING!!! Divide the bigger one to smaller one Divide the bigger one to smaller one Divide the bigger one to smaller one COMPUTE .
20
(3) The order of how to operate recursion
Divide the bigger one to smaller one Divide the bigger one to smaller one COMPUTE
21
(3) The order of how to operate recursion
Divide the bigger one to smaller one COMPUTE
22
(3) The order of how to operate recursion
Finish COMPUTING!, so pass the result
23
(3) The order of how to operate recursion
Get the RESULT
24
Time complexity In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the string representing the input.
25
(4) Time Complexity O(n) O(1) O(n-1) + O(n-2)
26
(4) Time Complexity O(n) = O(1) + O(n-1) + O(n-2)
29
Quick Sort 1. stop condition 2. pick pivot “value” 3. sort
4. recursive part [Divide and Conquer]
30
1. Stop condition
31
2. pick pivot “value”
32
3. sort
33
4. recursive part
34
EXAMPLE
35
(1) First
36
(2) Left of First(smaller part) = Second
37
(3) Left of Second(smaller part)
38
(4) Right of Second(bigger part)
39
(5) Right of First(smaller part)
40
Go Back to original length
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.