Questions tagged [big-o]

The Big-O notation is used to represent asymptotic upper bounds. It describes relevant time or space complexity of algorithms. Big-O analysis provides a coarse and simplified estimate of a problem difficulty.

The Big-O notation is used to represent asymptotic upper-bounds. It allows a person to see if a problem will take years or seconds to compute on a modern computer.

In computer science, it is most commonly used when talking about the time complexity of algorithms, but can also refer to the storage required.

For example, a linear search on an unsorted array of size N, is O(N). If we put the elements first in a hash table, the space used is O(N) (Theta(N) to be more precise), but the search time is O(1) in the average case.

It should be noted that Big-O only represents an upper bound for a function. Therefore an O(N) function will also be O(NlogN), O(N²), O(N!), etc. In many cases Big-O is used imprecisely and Big-Theta should be used instead.

If a complexity is given by a recurrence relation, an analysis can often be carried out via the Master Theorem.

Properties

  • Summation
    O(f(n)) + O(g(n)) -> O(max(f(n), g(n)))
    For example: O(n^2) + O(n) = O(n^2)

  • Multiplication by a positive constant
    O(c * f(n)) -> O(f(n))
    For example: O(1000 * n^2) = O(n^2)

  • Multiplication
    O(f(n)) * O(g(n)) -> O(f(n) * g(n))
    For example: O(n^2) * O(n) = O(n^2 * n) = O(n^3)

  • Transitivity
    f(n) = O(g(n)) and g(n) = O(h(n)) then f(n) = O(h(n))

Groups of Big-O

Complexity Sample algorithms
O(N!) Get all permutations of N items
O(2^N) Iterating over all subsets of N items
O(N^3) Calculating all triplets from N items
O(N^2) Enumerating all pairs from N items, insert sort
O(NLog(N)) Quick sort, merge sort
O(N) Getting min, max, average, iterating over N items
O(Log(N)) Binary search
O(1) Getting an item by the index in the array

More info

6779 questions
126
votes
2 answers

What is pseudopolynomial time? How does it differ from polynomial time?

What is pseudopolynomial time? How does it differ from polynomial time? Some algorithms that run in pseudopolynomial time have runtimes like O(nW) (for the 0/1 Knapsack Problem) or O(√n) (for trial division); why doesn't that count as polynomial…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
125
votes
2 answers

Big O of JavaScript arrays

Arrays in JavaScript are very easy to modify by adding and removing items. It somewhat masks the fact that most languages arrays are fixed-size, and require complex operations to resize. It seems that JavaScript makes it easy to write poorly…
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
122
votes
8 answers

Understanding Time complexity calculation for Dijkstra Algorithm

As per my understanding, I have calculated time complexity of Dijkstra Algorithm as big-O notation using adjacency list given below. It didn't come out as it was supposed to and that led me to understand it step by step. Each vertex can be…
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
122
votes
2 answers

What would cause an algorithm to have O(log log n) complexity?

This earlier question addresses some of the factors that might cause an algorithm to have O(log n) complexity. What would cause an algorithm to have time complexity O(log log n)?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
119
votes
11 answers

Time complexity of Euclid's Algorithm

I am having difficulty deciding what the time complexity of Euclid's greatest common denominator algorithm is. This algorithm in pseudo-code is: function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a It…
Donald T
  • 10,234
  • 17
  • 63
  • 91
118
votes
8 answers

What is the difference between lower bound and tight bound?

With the reference of this answer, what is Theta (tight bound)? Omega is lower bound, quite understood, the minimum time an algorithm may take. And we know Big-O is for upper bound, means the maximum time an algorithm may take. But I have no idea…
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
117
votes
15 answers

Is this technically an O(1) algorithm for "Hello World"?

Would this be classified as an O(1) algorithm for "Hello, World!" ?? public class Hello1 { public static void Main() { DateTime TwentyYearsLater = new DateTime(2035,01,01); while ( DateTime.Now < TwentyYearsLater ) { …
Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35
116
votes
7 answers

Is Big O(logn) log base e?

For binary search tree type of data structures, I see the Big O notation is typically noted as O(logn). With a lowercase 'l' in log, does this imply log base e (n) as described by the natural logarithm? Sorry for the simple question but I've…
BuckFilledPlatypus
  • 2,108
  • 5
  • 18
  • 23
113
votes
6 answers

What would cause an algorithm to have O(log n) complexity?

My knowledge of big-O is limited, and when log terms show up in the equation it throws me off even more. Can someone maybe explain to me in simple terms what a O(log n) algorithm is? Where does the logarithm come from? This specifically came up…
user1189352
  • 3,628
  • 12
  • 50
  • 90
92
votes
16 answers

Example of O(n!)?

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I'm asking about time complexity.
Derek Long
  • 1,169
  • 1
  • 11
  • 15
88
votes
7 answers

Difference between O(n) and O(log(n)) - which is better and what exactly is O(log(n))?

This is my first course in data structures and every lecture / TA lecture , we talk about O(log(n)) . This is probably a dumb question but I'd appreciate if someone can explain to me exactly what does it mean !?
JAN
  • 21,236
  • 66
  • 181
  • 318
87
votes
4 answers

What is the complexity of regular expression?

What is the complexity with respect to the string length that takes to perform a regular expression comparison on a string?
Ahmad Farid
  • 14,398
  • 45
  • 96
  • 136
84
votes
11 answers

Implement a queue in which push_rear(), pop_front() and get_min() are all constant time operations

I came across this question: Implement a queue in which push_rear(), pop_front() and get_min() are all constant time operations. I initially thought of using a min-heap data structure which has O(1) complexity for a get_min(). But push_rear() and…
bits
  • 8,110
  • 8
  • 46
  • 55
82
votes
9 answers

Why is accessing an element of a dictionary by key O(1) even though the hash function may not be O(1)?

I see how you can access your collection by key. However, the hash function itself has a lot of operations behind the scenes, doesn't it? Assuming you have a nice hash function which is very efficient, it still may take many operations. Can this be…
monogate
  • 1,419
  • 1
  • 11
  • 16
81
votes
2 answers

multiset, map and hash map complexity

I would like to know the complexity in Big O notation of the STL multiset, map and hash map classes when: inserting entries accessing entries retrieving entries comparing entries
Harry