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
2
votes
5 answers

Relation of time complexity and space complexity

Can an algorithm having a time complexity of O(n) have a space complexity of O(n2) or more than that?
noddy
  • 1,010
  • 4
  • 14
  • 27
2
votes
2 answers

Fastest way to remove first n elements from MutableList

I am programming in Kotlin and have a MutableList from which I would like to remove the first n elements from that specific list instance. This means that functions like MutableList.drop(n) are out of the question. One solution would of course be to…
Filip Östermark
  • 311
  • 2
  • 15
2
votes
1 answer

Why is the time complexity of my algorithm to calculate the factorial of a number O(n^2) instead of the expected O(n)?

Very perplexed with this one. I have three implementations of an algorithm to calculate the factorial of a number. I calculated the average runtimes of each for input size up to 2500 and plotted them. From the visual inspection it seems that they…
beyond_sci
  • 25
  • 4
2
votes
1 answer

Why is this (presumably more efficient) dynamic algorithm being outperformed by the naive recursive version?

I have the following problem as homework: Write a O(N^2) algorithm to determine whether the string can be broken into a list of words. You can start by writing an exponential algorithm and then using dynamic programming to improve the runtime…
2
votes
0 answers

Polynomial time algorithm for finding clique of size Ω(logn)

I have a homework question asking for a polynomial algorithm to find a clique of size Ω(logn). My current implementation is as follows: Divide the graph into n^logn microsets (subgraphs) of size logn and store them as adjacency matrices For each…
Clayton C.
  • 863
  • 1
  • 8
  • 17
2
votes
1 answer

search in list efficiency way (nested lis)

I have a nested list with 514000 list. I have to find duplicated list and store their position. I wrote a code, but with bad efficiency way.514000*514000 Do you have a goold solution with good efficiency way ? my nested…
PersianGulf
  • 2,845
  • 6
  • 47
  • 67
2
votes
2 answers

Big O time complexity of LeetCode 567

Below is my solution of LeetCode 567. https://leetcode.com/problems/permutation-in-string/ What would the Big o time complexity of code below be? Should it not be O(n*m) because of if (pattern_map == str_map): in code? Edit: n = pattern_map, m =…
Sxc-Dev
  • 123
  • 1
  • 7
2
votes
0 answers

Big O and Big Omega denotes Worst Case and Best case Respectively. Is it True?

I am just searching on google about Difference between Big Oh, Big Omega and Big Theta and found the first google search the geeksforgeeks article which says Big oh (O) – Worst case, Big Omega (Ω) – Best case and Big Theta (Θ) – Average case.…
2
votes
2 answers

Big O notation - python function

What is the Big O notation of this function (as a function of n = len(lst)): def foo(lst): jump = 1 total = 0 while jump < len(lst): for i in range(0, len(lst), jump): total += i jump = jump * 2 return…
2
votes
1 answer

Understanding time complexity, big-O notation

Edit- this question is from a former test in my course, the official answer is O(n) I need help understanding why does the run-time complexity in the following code is O(n) and not O(n*log(n)) def fun(n): total = 0 while n > 5: n = n //…
MadaBit
  • 145
  • 7
2
votes
0 answers

Are successive for loops over the same iterable slower than a single loop?

I often see, and have an inclination to write code where only 1 pass is made over a for loop, if it can be helped. I think there is a part of me (and of the people whose code I read) that feels it might be more efficient. But is it actually? Often,…
David Lalo
  • 155
  • 7
2
votes
1 answer

How to analyse space complexity of algorithm for CountNonDivisible?

I'm trying to analyse the worst-case space complexity of this algorithm for solving Codility's CountNonDivisible problem. The problem statement: You’re given an array A consisting of N integers. For each number A[i] such that 0 ≤ i < N, we want to…
2
votes
1 answer

Understanding Example 16 printing the powers of 2 from Big O notation - Cracking the Coding Interview

Had the author missed to compute I/O calls? The following function prints the powers of 2 from 1 through n (inclusive). For example, if n is 4, it would print 1,2, and 4. What is its runtime? int powersOf2(int n) { if (n < 1) { return 0; }…
Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18
2
votes
1 answer

How to to remove a double/nested for-loop? String to float transformation in python

I have the following polygon of a geographic area that I fetch via a request in CAP/XML format from an API The raw data looks like this: 22.3243,113.8659 22.3333,113.8691 22.4288,113.8691 22.4316,113.8742 22.4724,113.9478 22.5101,113.9951…
2
votes
1 answer

Can std::sort be specialized to use Bubble Sort for int* using as-if rule?

std::sort has complexity requirements, which is O(n log n). Besides common QuickSort or IntroSort, other algorithms would fit it. Sure Bubble Sort with O(n2) does not fit. However, if we take a specialization for pointers to int without explicit…
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
1 2 3
99
100