Questions tagged [time-complexity]

The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n^3 + 3n, the asymptotic time complexity is O(n^3).

Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Thus the amount of time taken and the number of elementary operations performed by the algorithm differ by at most a constant factor.

Since an algorithm may take a different amount of time even on inputs of the same size, the most commonly used measure of time complexity, the worst-case time complexity of an algorithm, denoted as T(n), is the maximum amount of time taken on any input of size n. Time complexities are classified by the nature of the function T(n).

For instance, an algorithm with T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2^n) is said to be an exponential time algorithm.

Useful links:

Related tags:

10064 questions
47
votes
11 answers

Algorithm for ordering data so that neighbor elements are as identical as possible

I have a (potentially large) list data of 3-tuples of small non-negative integers, like data = [ (1, 0, 5), (2, 4, 2), (3, 2, 1), (4, 3, 4), (3, 3, 1), (1, 2, 2), (4, 0, 3), (0, 3, 5), (1, 5, 1), (1, 5,…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
47
votes
1 answer

Performance of Array.push vs Array.unshift

I was reading about runtime complexities of array operations and learned that... the ECMAScript specification does not mandate a specific runtime complexity, so it depends on the specific implementation / JavaScript engine / runtime behavior [1]…
le_m
  • 19,302
  • 9
  • 64
  • 74
46
votes
2 answers

How to calculate time complexity of backtracking algorithm?

How to calculate time complexity for these backtracking algorithms and do they have same time complexity? If different how? Kindly explain in detail and thanks for the help. 1. Hamiltonian cycle: bool hamCycleUtil(bool graph[V][V], int…
da3m0n
  • 473
  • 1
  • 4
  • 7
46
votes
1 answer

Trie complexity and searching

What is the complexity of creating a trie of a list of words and what is complexity of searching other set of word in that trie? Should I use trie for string searching, when i have hashtable?
Varun
  • 1,159
  • 1
  • 14
  • 19
45
votes
15 answers

LRU cache in Java with Generics and O(1) operations

This is a question that comes up a lot in job interviews. The idea is to define a data structure instead of using Java's built in LinkedHashMap. An LRU cache deletes the least recently used entry to insert a new one. So, given the following…
liarspocker
  • 2,434
  • 3
  • 17
  • 26
44
votes
3 answers

Time complexity of string slice

What's the time complexity of slicing a Python string? Given that Python strings are immutable, I can imagine slicing them being either O(1) or O(n) depending upon how slicing is implemented. I need to write a function that iterates over all…
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
44
votes
1 answer

What is the Big O analysis of this algorithm?

I'm working on a data structures course and I'm not sure how to proceed w/ this Big O analysis: sum = 0; for(i = 1; i < n; i++) for(j = 1; j < i*i; j++) if(j % i == 0) for(k = 0; k < j; k++) sum++; My…
user2986376
  • 451
  • 3
  • 5
44
votes
1 answer

Find all possible substring in fastest way

For String A = "abcd" then answer should be {a,ab,abc,abcd,b,bc,bcd,c,cd,d} To find all the substring I have used following method for (int i = 0; i < A.length(); i++) { for (int j = i+1; j <= A.length(); j++) { …
user2228588
  • 443
  • 1
  • 4
  • 7
43
votes
5 answers

Explanation of runtimes of BFS and DFS

Why are the running times of BFS and DFS O(V+E), especially when there is a node that has a directed edge to a node that can be reached from the vertex, like in this example in the following…
43
votes
4 answers

HashSet look-up complexity?

A look-up operation OR contains for single can be O(n) in worst-case right ? So, for n elements look up in hashSet will be O(n^2)?
phoenix
  • 3,531
  • 9
  • 30
  • 31
43
votes
7 answers

How to improve performance of this code?

Thanks to some help from people here, I was able to get my code for Tasmanian camels puzzle working. However, it is horribly slow (I think. I'm not sure because this is my first program in Python). The example run in the bottom of the code takes a…
nakiya
  • 14,063
  • 21
  • 79
  • 118
42
votes
8 answers

Example of Big O of 2^n

So I can picture what an algorithm is that has a complexity of n^c, just the number of nested for loops. for (var i = 0; i < dataset.len; i++ { for (var j = 0; j < dataset.len; j++) { //do stuff with i and j } } Log is something…
dlkulp
  • 2,204
  • 5
  • 32
  • 46
42
votes
4 answers

Time complexity of delete[] operator

What is the Time Complexity of the delete[] operator? I mean how is it implemented - does it iterate over all the elements in the array and calls destructor for every element? Does this operator do the same for primitive types (int, etc.) and user…
rooster
  • 665
  • 1
  • 6
  • 12
42
votes
2 answers

How efficient/fast is Python's 'in'? (Time Complexity wise)

In Python, what is the efficiency of the in keyword, such as in: a = [1, 2, 3] if 4 in a: ...
John
  • 4,596
  • 11
  • 37
  • 43
41
votes
7 answers

C++ string::find complexity

Why the c++'s implemented string::find() doesn't use the KMP algorithm (and doesn't run in O(N + M)) and runs in O(N * M)? Is that corrected in C++0x? If the complexity of current find is not O(N * M), what is that? so what algorithm is implemented…
Farzam
  • 1,272
  • 3
  • 14
  • 25