Time Complexity Flashcards
Understanding the running time of algorithms and their order of growth
Time Complexity in increasing order
O(1) < O(log n) < O(n) < O(n log n) < O(2^n) < O(n!)
Time complexity of NP complete problems
Factorial or Exponential
Exponential time vs Factorial time
Exponential time < Factorial time
Examples of exponential time
2^n or x^n where x is any value. As n increases the time taken increases exponentially
Examples of quadratic time
n^2, n^2 + x*n + c etc
Quasilinear time
n * log n
Polynomial time
n ^ c. eg. n^2, n^10 etc
Log logarithmic time
log log n
Which is greater 0.001 * 1.5^n or 1000 * n^3?
0.001 * 1.5^n»_space;> 1000 * n^3. Exponential cannot be beaten by a polynomial
What is amortized time?
Amortized time is the way to express the time complexity when an algorithm has the very bad time complexity only once in a while besides the time complexity that happens most of time.
Example ArrayList insert expands to double and saves for later operations.
What is asymptotic analysis?
In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don’t measure the actual running time). We calculate, how does the time (or space) taken by an algorithm increases with the input size. Order of growth of algorithm with respect to input size
In asymptotic analysis, 1000 * n log n vs 2 * n log n?
Both are same for asymptotic analysis which is O(n log n). We ignore constants.
Let’s say Selection sort takes T(n) = n^2 to run. If sorting a deck of cards took 2 hours, how long will it take to sort 10 decks of unsorted cards?
First T(10*n) / T(n) = (10*n)^2 / n * 2 = 100. 100 times more time will be required, so 2 * 100 = 200 hours.
Time complexity of Power Set?
Exponential. O(2^n). Consider example of fragrances that can be created with set of flowers. With every new flower, the number of fragrances doubles. So it is exponential.
What is power set?
A power set is a set of all possible subsets of the set. So for a set A = {1, 2, 3}, the power set will be PowerSet(A) = {{}, {1}, {2}, {1,2}, {3}, {1, 3}, {2, 3}, {1, 2, 3}}