Big O Flashcards
Definition of Big O
a way to measure how a computer algorithm scale as the amount of data involved increases
Does it depend on speed?
No, it depends on how the algorithm scales
what do u calculate?
the input the has the greatest effect on the algorithm as it scales
O(1)
an algorithm that will execute at the same amount of time regardless the input
O(N)
an algorithm that it’s time will grow in direct proportion to the amount of data
O(N^2)
the amount of time will be proportional to the square of the amount of data
O(LOG N)
the amount of time will decrease by 50% with each iteration
Algorithm complexity
is the comparison of two algorithms at an idea level and not at a low lever , not concerning ourselves with language or platform or hardware or CPU because an algorithm running on assembly will be much faster than on python
Complexity Analysis
allows us to measure how fast a program runs when it performs a computational task and also allows us to to explain how a program behaves depending on the input
Worst Case Analysis
we analyze algorithms considering the worst case meaning we calculate the most unlucky input to output the highest number of instructions
Asymptotic behavior
is dropping all constant factors and only keeping the largest growing term
What are the names of time complexity O(1) , O(N) , O(N^2) , O(LOG N)
Constant-time algorithm , Linear , Quadratic , Logarithmic
how to link between theta and O for explanation
any program that is theta of a is O of b when b is worse than a
A Θ( n ) algorithm is O( n ) A Θ( n ) algorithm is O( n2 ) A Θ( n2 ) algorithm is O( n3 ) A Θ( n ) algorithm is O( 1 ) A O( 1 ) algorithm is Θ( 1 ) A O( n ) algorithm is Θ( 1 )
We know that this is true as our original program was Θ( n ). We can achieve O( n ) without altering our program at all.
As n2 is worse than n, this is true.
As n3 is worse than n2, this is true.
As 1 is not worse than n, this is false. If a program takes n instructions asymptotically (a linear number of instructions), we can’t make it worse and have it take only 1 instruction asymptotically (a constant number of instructions).
This is true as the two complexities are the same.
This may or may not be true depending on the algorithm. In the general case it’s false. If an algorithm is Θ( 1 ), then it certainly is O( n ). But if it’s O( n ) then it may not be Θ( 1 ). For example, a Θ( n ) algorithm is O( n ) but not Θ( 1 ).
What alternative names to we call theta or O
theta is a tight bound while O is an upper bound