Download presentation
Presentation is loading. Please wait.
1
Big-O & Recursion
2
Recursion What is the Big-O of each?
3
Recursion What is the Big-O of each? Need Number of recursive calls
Work done at each recursive call
4
Recursion What calls get made?
5
Recursion What calls get made?
16, 8, 4, 2, , 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
6
Recursion Number of recursive calls
16, 8, 4, 2, , 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 O(logn) O(n)
7
Recursion BigO Assume print is O(1) All work is O(1)
= logn calls * O(1) work per call = O(logn)
8
Recursion BigO All work is O(1) = n calls * O(1) work per call = O(n)
9
Recursion BigO Work is is O(1) + O(n) = O(n) O(n) recursive calls
= O(n) calls * O(n) work per call = O(n2) foo(array, size) if(size == 0) return for(int i = 0; i < size; i++) cout << array[i] foo(array, size - 1) Recursive calls for size = 10: 10, 9, 8, 7, … , 0 O(n)
10
Proofs
11
Recurrence Relationships
Recurrence Relationship: Recursively defined math function: Used to: Formally establish recursive BigO Tackle complex recursion (multiple calls)
12
Recursion What is the Big-O? T(n) = T(n - 1) + 1 T(0) = 1
13
Recursion What is the Big-O? T(n) = T(n - 1) + 1 Level 1
= … = T(n - k) + k Level 1 Level 2 Level 3 Level k
14
Recursion What is the Big-O? Know: T(n) = T(n - k) + k T(0) = 1
So solve n – k = 0 … k = n
15
Recursion What is the Big-O? Given: T(n) = T(n - k) + k k = n
Plug in k: 𝑇 𝑛 =𝑇 𝑛 −𝑛 + 𝑛 𝑇 𝑛 =𝑇 0 + 𝑛 𝑇 𝑛 =1 + 𝑛 𝑇 𝑛 ≈ 𝑛
16
Recursion What is the Big-O? T(n) = T(n/2) + 1 T(1) = 1
17
Recursion What is the Big-O? T(n) = T(n/2) + 1 Level 1
= … = T(n/2k) + k Level 1 Level 2 Level 3 Level k
18
Recursion What is the Big-O? T(n) = T(n/2k) + k Level k
So what is k? How many levels are there? Level k
19
Recursion What is the Big-O? T(n) = T(n/2k) + k Know T(1) = 1
Need to solve n/2k = 1
20
Recursion What is the Big-O? T(n) = T(n/2k) + k Know T(1) = 1
Need to solve n/2k = 1 n = 2k log2(n) = k
21
Recursion What is the Big-O? Given: T(n) = T(n/2k) + k k = log2(n)
Plug in k: 𝑇 𝑛 =𝑇 𝑛 2 𝑙𝑜𝑔 2 𝑛 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 =𝑇 𝑛 𝑛 + 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 =𝑇 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 =1+ 𝑙𝑜𝑔 2 𝑛 ≈ 𝑙𝑜𝑔 2 𝑛
22
Common Recurrences Big O's to recognize: Our focus is intuitive
Math in 231/232 & Analysis of Algorithms
23
Derivations if you want them…
T(n) = 2T(n/2) + cn + c T(n) = 2T(n/2) + n + 1 = 2(2T(n/4) + (n/2) + 1) + n + 1 = 4T(n/4) + 2n = 4T(n/4) + 2n = 4(2T(n/8) + (n/4) + 1) + 2n = 8T(n/8) + 3n + 3 = … = 2kT(n/2k) + kn + k Level 1 Level 2 Level 3 Level k
24
Derivations if you want them…
T(n) = 2T(n/2) + cn + c Level k T(n) = 2k T(n/2k) + kn + k T(1) = 1, solve 1 = n/2k k = log2n Substitute: 𝑇 𝑛 = 2 𝑙𝑜𝑔 2 𝑛 𝑇 𝑛 2 𝑙𝑜𝑔 2 𝑛 𝑙𝑜𝑔 2 𝑛 𝑛+ 𝑙𝑜𝑔 2 𝑛 =𝑛𝑇 𝑛 𝑛 + 𝑛 𝑙𝑜𝑔 2 𝑛+ 𝑙𝑜𝑔 2 𝑛 =𝑛𝑇 1 + 𝑛 𝑙𝑜𝑔 2 𝑛+ 𝑙𝑜𝑔 2 𝑛 =𝑛+ 𝑛 𝑙𝑜𝑔 2 𝑛+ 𝑙𝑜𝑔 2 𝑛 ≈𝑛 𝑙𝑜𝑔 2 𝑛
25
Derivations if you want them…
T(n) = 2T(n - 1) + c T(n) = 2T(n - 1) + 1 = 2(2T(n - 2) + 1) + 1 = 4T(n - 2) = 4T(n - 2) = 4(2T(n - 3) + 1) = 8T(n - 3) + 7 = … = 2kT(n - k) + (2k – 1) Level 1 Level 2 Level 3 Level k
26
Derivations if you want them…
T(n) = 2T(n - 1) + c Level k T(n) = 2kT(n - k) + (2k – 1) T(1) = 1, solve 1 = n - k k = n - 1 Substitute: 𝑇 𝑛 = 2 𝑛−1 𝑇 𝑛 −(𝑛 −1) +( 2 𝑛−1 −1) = 2 𝑛−1 𝑇 𝑛−1 −1 = 2 𝑛− 𝑛−1 −1 =2 2 𝑛−1 −1 ≈ 2 𝑛−1 ≈ 2 𝑛
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.