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
138
votes
27 answers

A Regex that will never be matched by anything

This might sound like a stupid question, but I had a long talk with some of my fellow developers and it sounded like a fun thing to think of. So; what's your thought - what does a Regex look like, that will never be matched by any string,…
F.P
  • 15,760
  • 32
  • 114
  • 184
127
votes
5 answers

What guarantees are there on the run-time complexity (Big-O) of LINQ methods?

I've recently started using LINQ quite a bit, and I haven't really seen any mention of run-time complexity for any of the LINQ methods. Obviously, there are many factors at play here, so let's restrict the discussion to the plain IEnumerable…
tzaman
  • 42,181
  • 9
  • 84
  • 108
117
votes
8 answers

B-Tree vs Hash Table

In MySQL, an index type is a b-tree, and access an element in a b-tree is in logarithmic amortized time O(log(n)). On the other hand, accessing an element in a hash table is in O(1). Why is a hash table not used instead of a b-tree in order to…
JohnJohnGa
  • 14,494
  • 15
  • 58
  • 83
109
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
  • 328,018
  • 92
  • 813
  • 992
108
votes
8 answers

Big-oh vs big-theta

Possible Duplicate: What is the difference between Θ(n) and O(n)? It seems to me like when people talk about algorithm complexity informally, they talk about big-oh. But in formal situations, I often see big-theta with the occasional big-oh thrown…
Boris Yeltz
  • 2,201
  • 5
  • 20
  • 20
104
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
  • 1,918
  • 5
  • 16
  • 23
100
votes
13 answers

What's the fastest algorithm for sorting a linked list?

I'm curious if O(n log n) is the best a linked list can do.
Dirk
  • 6,445
  • 13
  • 47
  • 72
98
votes
4 answers

Why is the knapsack problem pseudo-polynomial?

I know that Knapsack is NP-complete while it can be solved by DP. They say that the DP solution is pseudo-polynomial, since it is exponential in the "length of input" (i.e. the numbers of bits required to encode the input). Unfortunately I did not…
94
votes
3 answers

Time complexity of python set operations?

What is the the time complexity of each of python's set operations in Big O notation? I am using Python's set type for an operation on a large number of items. I want to know how each operation's performance will be affected by the size of the set.…
Stephen Emslie
  • 8,829
  • 6
  • 28
  • 27
89
votes
5 answers

.NET Console Application Exit Event

In .NET, is there a method, such as an event, for detecting when a Console Application is exiting? I need to clean up some threads and COM objects. I am running a message loop, without a form, from the console application. A DCOM component that I…
user79755
  • 2,363
  • 4
  • 28
  • 36
83
votes
3 answers

What is O(log* N)?

What is O(log* N) and how is it different from O(log N)?
Timmy
  • 11,360
  • 18
  • 66
  • 104
81
votes
7 answers

How to understand the knapsack problem is NP-complete?

We know that the knapsack problem can be solved in O(nW) complexity by dynamic programming. But we say this is a NP-complete problem. I feel it is hard to understand here. (n is the number of items. W is the maximum volume.)
cnhk
  • 1,125
  • 2
  • 11
  • 13
79
votes
7 answers

O(N log N) Complexity - Similar to linear?

So I think I'm going to get buried for asking such a trivial question but I'm a little confused about something. I have implemented quicksort in Java and C and I was doing some basic comparissons. The graph came out as two straight lines, with the C…
gav
  • 28,323
  • 22
  • 60
  • 89
77
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
  • 13,132
  • 45
  • 92
  • 134
75
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,001
  • 1
  • 10
  • 14