Transciber’s notes:
- ⟺ is “if and only if”
- ⟹ is “implies”
- ∀ is “for all”
- ∃ is “exists”
Big-Oh–Part I
CMPT 225
Slide 1
- Recall: if
f,g
are functions f:N->N
,g:N->N
- f is
O(g)
means there are constants n0,c>0
s.t. for every n>n0,f(n)≤c.g(n)
- that is for all but finitely many “small” values of n: f(n)≤cg(n)
- or f grows no faster than g (asymtotically)
- we typically write f(n)=O(g(n))
Consider f(n)
,g(n)
:
Claim: f(n)=O(g(n))
Why:
- When f(n)>g(n) for all n?
- But f(n)<2g(n) for almost all values of
n
.
- choose n0 to “exclude” the real values of
n
.
- Now: f(n)<2g(n) for all n>n0
- So: f(n)=O(g(n))
Also:
(A graph a cannot transcribe because the notation doesn’t make any sense. All future graphs have complexity on the y axis and the value of n [input to the function] as the x asxis.)
Claim: f(n) is not O(g(n))
However large we choose c,n0, there will be some k (larger than n0) s.d.: n>k⟹f(n)>cg(n)
Consider O(1)
(i.e. g(n)=1)
f(n)=O(1) if ∃n0,c>0 s.t. ∀n>n0 f(n)≤c×1
(Another graph that makes no Goddamn sense whatsoever.)
- for every n>n0,f(n)<c <– >–>
- so, f(n) grwos no faster than a constant
- so f(n) is asymptotically bounded by a constant
The constant does not matter
Fact: f(n)=O(1)⟺f(n)=O(1027)⟺f(n)=O(10271)
Suppose: f(n)=O(1027) *
Claim: f(n) is also O(10271)
* means ∃n0,c>0 s.t. n>n0⟹f(n)≤c×1027
Want to show: ∃n0,c1>0 s.t. n>n0⟹f(n)≤c1×10271
Choose c1 big enough that c110271≤c×1027
e.g: c1=c×1054
Then:
∀n>n0,f(n)≤c×1027≤c×10−27−1054≤c1−10−27
(54-27=27)
So: f(n)=O(10−27)
Asymtotic Notation (e.g. Big-Oh)
- Is not “about” algorithm
- Is a tool for describing (growth of) functions
- It is useful for describing functions related to algorithms + data structures, e.g.:
- minimum or maximum time taken
- minimum or maximum space needed
- etc.
- We use it so often for worst-case time for an algorithm that we often leave implciit a statement like “let T(n) be the max time taken by algorithm A as an input of size as most n.” This statement is essential.
Ex. Complexity of Palindrome Checking
- using a stack & queue
- Algorithm:
- Insert all tokens into a stack & a queue
- Repeat pop one token; dequeue one token. if different report ‘no’.
- size of input = number of symbols or token
- each token is (all together O(1)):
- pushed on the stack, O(1)
- enqueued on the queue, O(1)
- popped off the stack, O(1)
- dequeued from the queue, O(1)
- compared to one other token, O(1)
- n tokens⟹n×O(1) time in total
- So: T(n)=n×O(1)=O(n)
What does n×O(1)=O(n) mean?
It means: f(n)=O(1)⟺n×f(n)=O(n)
To see if it is true:
f(n)=O(1)⟺∃c>0 s.t. f(n)<c,for any n∈Z⟺∃c>0 s.t. n×f(n)<c×n,for any n∈N⟺n×f(n)=O(n)
End