.121 15-20 Flashcards
(103 cards)
What is big omega notation?
lower bound - T(n) ≥ M×f(n) for all n ≥ n₀
grows no slower than f(n) - ≥
find simplest so also big Ω of anything that grows slower
What is big theta notation?
tight bound - M1×f(n) ≤ T(n) ≤ M2×f(n) for all n ≥ n₀
combo of other 2 - grows no slower + no faster than f(n) - ==
How do you prove big theta?
prove big O and big omega
e.g. T(n) ∈ O(n) AND T(n) ∈ Ω(n) → we can say T(n) ∈ Θ(n)
How do you prove big omega?
replace anything that isn’t the dominant term with a 0 then add up to find M
find n0 by getting equal to the n term and dividing to get n alone
e.g. given T(n) = 3n + 4, f(n) = n
3n ≥ 3n (equal), n ≥ 0
3n + 4 ≥ 3n + 0 when n ≥ 0
M = 3 (given the 3n) and n0 = 0 therefore T(n) ∊ Ω(n)
How do you prove big O?
get n0 by collecting non-dominant terms + dividing them to get n alone
get M by multiplying non-dominant terms by the dominant term and adding them up
e.g. given T(n) = 3n + 4, f(n) = n
T(n) = 3n + 4 ≤ 3n + 4n so T(n) = 3n + 4 ≤ 7n
4 ≤ 4n → divide by 4 → 1 ≤ n
M=7 and n0=1 → T(n) ≤ 7n for all n ≥ 1 therefore T(n) ∊ O(n)
What is a recursive algorithm?
an algorithm which calls itself w/ smaller input values
obtains the result for the current input by applying simple operations to the returned smaller input
How do you find the recurrence relation for recursive algorithms?
find constant time complexity (no recursion) and write T(1) = T(n) of the one operation
(probs constant so can just write 1)
then do T(n) for prior constant + recursive call
How can we evaluate recursive time complexity?
- back substitution
- recursion tree
- master theorem
How does back substitution work?
replace recurrent relation w/ other equivalent options - e.g. T(n-1) + 1 = T(n-2) + 2 or T(n/2) + 1 = T(n/4) + 2
then find pattern of substitution - e.g. T(n-k) + k or T(n/2^k) + k
apply this pattern to T(1) to reach final theta notation
- e.g. if k = n -1 and T(n) = T(n-k) + k = T(1) + n -1 = 1 + n -1 = n therefore T(n) = n thus Θ(n)
What is sorting data useful for?
ranking data, more efficient searching (in general) + binary searching
How does a selection sort work?
find smallest value in input + extract it into new array
repeat till all items are in new array
How does an in-place selection sort work?
find smallest value in input + swap w/ 1st value of array
repeat till index is at end of array
(change 1st to next index as each item is added)
What does an in-place sort mean?
sorts the items within the array that was inputted instead of creating a new array to help save memory
O(1) space complexity!!
What do we need to analyse for sorts?
correctness + time complexity
What is the time complexity of a selection sort?
O(n^2) in all cases
How does an insertion sort work?
move elements from input to new array in list order
once added check if the element is > than items alr in list + move accordingly
How does an in-place insertion sort work?
compare values in the list order + swap accordingly
e.g. 1st > 2nd ? 2nd > 3rd? etc.