Complexity Flashcards
1
Q
Big O
A
- O(g(n)) is the set of functions are “upper-bounded” by g(n)
- If f(n) is a function that describes the running time of the program (w.r.t. input)
- Then f(n) is in O(g(n)) if g(n) dominates f(n) for some c after n0
- Basically, f(n) will always be <= c*g(n)
2
Q
Big Omega
A
- Ω(g(n)) is the set of functions are “lower-bounded” by g(n)
- If f(n) is a function that describes the running time of the program (w.r.t. input)
- Then f(n) is in Ω(g(n)) if f(n) dominates g(n) for some c after n0
- Basically, f(n) will always be >= c*g(n)
3
Q
Little O
A
- More restrictive version of big O
- o(g(n)) is the set of functions that are dominated by g(n) for all constants c after some n0
- f(n) and g(n) can’t have the same dominant term (can’t both be squares or cubes, etc)
4
Q
Little Omega [ω]
A
- More restrictive version of big Ω
- ω(g(n)) is the set of functions that are dominate g(n) for all constants c after some n0
- f(n) and g(n) can’t have the same dominant term (can’t both be squares or cubes, etc)
5
Q
Theta
A
- The set of functions for which g(n) is both an upper and lower bound
- Θ(g(n)) is the set of functions that are in both O(g(n)) and Ω(g(n))
- Provides a tight bound
- c1*g(n) <= f(n) <= c2*g(n)
- f(n) is sandwiched between g(n) on both sides (albeit with different constants c1 and c2)
- Many functions do not have a simple Θ
6
Q
Big O
Formal Definition
A
f(n) is in the set O(g(n)) if and only if:
There exist positive constants c & N0 s.t. for all n > N0,
f(n) <= c*g(n)
7
Q
Asymptotic
A
- Basically, when n is really large
8
Q
Best Case/Worst Case
A
- Describes how running time changes based on the type of input
- A program has a different best case and worst case if running time differs depending on input
- Ex: an algorithm might have a constant running time when input is sorted (best case), but a quadratic running time when input is reversed (worst case)
- Best/worst/average case each have their own Big O and Big Omega
9
Q
Upper Bound/Lower Bound
A
- An upper bound of f(n) is any function g(n) that is >= f(n) asymptotically
- A function f(n) has multiple (infinite) upper bounds
- Ex:
- n2/2 is an upper bound of n2
- n3 is also an upper bound of n2
- Lower bound is basically the reverse
- Any function g(n) <= f(n)
10
Q
Space Complexity
A
- The maximum amount of space required at any given time for an algorithm to complete its task
- Uses the same notation (Big O, Big Omega, etc) as time complexity
- Includes:
- The input
- Any auxiliary space
- Space created in the stack (in recursive calls)
- But, in general, when discussing space complexity, we only care about aux space and stack space
11
Q
Amortized Analysis
A
- An alternative approach to calculating the worst-case running time
- Used when your algorithm has more than one operation, some are cheap and some are expensive
- Ex: “append” consists of both populating a cell and, occationally, resizing an array
- Provides an upper bound on a sequence of operations
- Unlike average case, can’t get a higher running time (on a sequence) by being “unluckly”
- Three approaches
- aggregate method
- accounting method
- potential method
12
Q
Average Case
A
- The performance of an algorithm based on what input is “most likely” to look like
- Based on probability of likely inputs
- Often requires statistical analysis
- Has its own upper bound/lower bound
13
Q
Averaging Method
A
- The most common type of amortized analysis
- Basically, add up the cost of n operations, and divide by n
14
Q
Logarithms and Complexity
A
For an input size of n
- log2(n) is the number of times you can divide n by 2 before n becomes a fraction (while n is >= 1)
- Number of “iterations/steps” to process all elements
- Ex: input of size 32 can be processed with 5 steps
15
Q
Recursion and Complexity
A
- Time complexity is number of recursive calls (size of recursion tree) * complexity of each recursive calls
- Ex: fibonacci has O(2n) recursive calls, each is constant
- Ex: mergesort as O(n) recursive calls, each of which is (on average) O(log2(n))
- Space complexity is max height of stack (max height of tree) * aux space in each frame + maximum amount of space allocated by any individual call