Questions tagged [complexity-theory]

Computational complexity theory is a branch of the theory of computation in theoretical computer science and mathematics that focuses on classifying computational problems according to their inherent difficulty. Particularly common in programming is *amortized analysis* for time or space

Given a problem and a computational model, the complexity of the problem with respect to the model is a function of the input size. Complexity functions may measure different quantities, such as time or space:

  • The time complexity measures how much time is required by the model to solve the problem, given an input.
  • The space complexity measure how much memory space is required by the model to solve the problem, given an input.

As a measure, the complexity can be defined as a function:

c = f(n)

where n is the input size.

Usually, the complexity is classified with the analytical form of f(n). For example, if f(n) = n, the complexity is linear with respect to n; if f(n) = n2, the complexity is quadratic; and, if f(n) = 2n, the complexity is exponential.

3530 questions
75
votes
1 answer

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
73
votes
6 answers

Time complexity of unshift() vs. push() in Javascript

I know what is the difference between unshift() and push() methods in JavaScript, but I'm wondering what is the difference in time complexity? I suppose for push() method is O(1) because you're just adding an item to the end of array, but I'm not…
dperitch
  • 1,729
  • 2
  • 15
  • 22
73
votes
6 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
  • 18,509
  • 49
  • 147
  • 268
68
votes
1 answer

Why SortedSet.GetViewBetween isn't O(log N)?

In .NET 4.0+, a class SortedSet has a method called GetViewBetween(l, r), which returns an interface view on a tree part containing all the values between the two specified. Given that SortedSet is implemented as a red-black tree, I naturally…
Skiminok
  • 2,521
  • 1
  • 21
  • 29
68
votes
8 answers

General rules for simplifying SQL statements

I'm looking for some "inference rules" (similar to set operation rules or logic rules) which I can use to reduce a SQL query in complexity or size. Does there exist something like that? Any papers, any tools? Any equivalencies that you found on…
MicSim
  • 25,056
  • 15
  • 84
  • 124
67
votes
7 answers

Explain the proof by Vinay Deolalikar that P != NP

Recently there has been a paper floating around by Vinay Deolalikar at HP Labs which claims to have proved that P != NP. Could someone explain how this proof works for us less mathematically inclined people?
Nixuz
  • 3,265
  • 3
  • 35
  • 43
64
votes
7 answers

Is list::size() really O(n)?

Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to some sources, this is in fact implementation dependent as the standard doesn't say what the complexity has to be. The comment in this blog entry…
foraidt
  • 5,241
  • 4
  • 50
  • 78
63
votes
7 answers

Sorting algorithms for data of known statistical distribution?

It just occurred to me, if you know something about the distribution (in the statistical sense) of the data to sort, the performance of a sorting algorithm might benefit if you take that information into account. So my question is, are there any…
static_rtti
  • 46,349
  • 44
  • 123
  • 180
62
votes
6 answers

Can an O(n) algorithm ever exceed O(n^2) in terms of computation time?

Assume I have two algorithms: for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { //do something in constant time } } This is naturally O(n^2). Suppose I also have: for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { …
Brian
  • 6,390
  • 14
  • 52
  • 70
60
votes
5 answers

es6 Map and Set complexity, v8 implementation

Is it a fair assumption that in v8 implementation retrieval / lookup is O(1)? (I know that the standard doesn't guarantee that)
Uri
  • 20,857
  • 6
  • 38
  • 61
59
votes
4 answers

Python dictionary keys. "In" complexity

Quick question to mainly satisfy my curiosity on the topic. I am writing some large python programs with an SQlite database backend and will be dealing with a large number of records in the future, so I need to optimize as much as I can. For a few…
tknickman
  • 3,440
  • 2
  • 31
  • 43
57
votes
7 answers

Differences between time complexity and space complexity?

I have seen that in most cases the time complexity is related to the space complexity and vice versa. For example in an array traversal: for i=1 to length(v) print (v[i]) endfor Here it is easy to see that the algorithm complexity in terms of…
Little
  • 2,841
  • 6
  • 33
  • 57
56
votes
9 answers

Hashtable in C++?

I usually use C++ stdlib map whenever I need to store some data associated with a specific type of value (a key value - e.g. a string or other object). The stdlib map implementation is based on trees which provides better performance (O(log n)) than…
Marcos Bento
  • 1,910
  • 4
  • 21
  • 19
56
votes
6 answers

Intuitive explanation for why QuickSort is n log n?

Is anybody able to give a 'plain english' intuitive, yet formal, explanation of what makes QuickSort n log n? From my understanding it has to make a pass over n items, and it does this log n times...Im not sure how to put it into words why it does…
Jim_CS
  • 3,724
  • 13
  • 41
  • 80
55
votes
3 answers

What's the Time Complexity of Average Regex algorithms?

I'm not new to using regular expressions, and I understand the basic theory they're based on--finite state machines. I'm not so good at algorithmic analysis though and don't understand how a regex compares to say, a basic linear search. I'm…
avgvstvs
  • 5,556
  • 6
  • 38
  • 69