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
5102
votes
42 answers

What is a plain English explanation of "Big O" notation?

I'd prefer as little formal definition as possible and simple mathematics.
1203
votes
11 answers

What are the differences between NP, NP-Complete and NP-Hard?

What are the differences between NP, NP-Complete and NP-Hard? I am aware of many resources all over the web. I'd like to read your explanations, and the reason is they might be different from what's out there, or there is something that I'm not…
DarthVader
  • 46,241
  • 67
  • 190
  • 289
957
votes
9 answers

How to find time complexity of an algorithm

The Question How to find time complexity of an algorithm? What have I done before posting a question on SO ? I have gone through this, this and many other links But no where I was able to find a clear and straight forward explanation for how to…
Yasser Shaikh
  • 44,064
  • 44
  • 190
  • 271
918
votes
23 answers

Big O, how do you calculate/approximate it?

Most people with a degree in CS will certainly know what Big O stands for. It helps us to measure how well an algorithm scales. But I'm curious, how do you calculate or approximate the complexity of your algorithms?
sven
  • 17,326
  • 10
  • 48
  • 62
622
votes
16 answers

How can building a heap be O(n) time complexity?

Can someone help explain how can building a heap be O(n) complexity? Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (the remainder are leaves, and can't violate the heap property). So, this means the complexity…
GBa
  • 14,931
  • 15
  • 47
  • 67
444
votes
8 answers

Constant Amortized Time

What is meant by "Constant Amortized Time" when talking about time complexity of an algorithm?
VarunGupta
  • 5,367
  • 5
  • 25
  • 31
354
votes
11 answers

Computational complexity of Fibonacci Sequence

I understand Big-O notation, but I don't know how to calculate it for many functions. In particular, I've been trying to figure out the computational complexity of the naive version of the Fibonacci sequence: int Fibonacci(int n) { if (n <= 1) …
Juliet
  • 76,873
  • 44
  • 191
  • 224
347
votes
32 answers

Are there any O(1/n) algorithms?

Are there any O(1/n) algorithms? Or anything else which is less than O(1)?
Shalmanese
  • 5,096
  • 9
  • 27
  • 41
328
votes
5 answers

Cost of len() function

What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary)
Imran
  • 76,055
  • 23
  • 93
  • 124
313
votes
6 answers

Determining complexity for recursive functions (Big O notation)

I have a Computer Science Midterm tomorrow and I need help determining the complexity of these recursive functions. I know how to solve simple cases, but I am still trying to learn how to solve these harder cases. These were just a few of the…
Michael_19
  • 3,749
  • 6
  • 22
  • 19
249
votes
6 answers

What's "P=NP?", and why is it such a famous question?

The question of whether P=NP is perhaps the most famous in all of Computer Science. What does it mean? And why is it so interesting? Oh, and for extra credit, please post a proof of the statement's truth or falsehood. :)
raldi
  • 19,496
  • 29
  • 73
  • 85
236
votes
9 answers

Is log(n!) = Θ(n·log(n))?

I am to show that log(n!) = Θ(n·log(n)). A hint was given that I should show the upper bound with nn and show the lower bound with (n/2)(n/2). This does not seem all that intuitive to me. Why would that be the case? I can definitely see how to…
Mark
  • 5,623
  • 12
  • 38
  • 51
188
votes
35 answers

How to find the lowest common ancestor of two nodes in any binary tree?

The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as - struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something…
Siddhant
  • 2,495
  • 4
  • 19
  • 22
179
votes
5 answers

Are 2^n and n*2^n in the same time complexity?

Resources I've found on time complexity are unclear about when it is okay to ignore terms in a time complexity equation, specifically with non-polynomial examples. It's clear to me that given something of the form n2 + n + 1, the last two terms are…
matty-d
  • 2,533
  • 3
  • 15
  • 21
150
votes
6 answers

HashMap get/put complexity

We are used to saying that HashMap get/put operations are O(1). However it depends on the hash implementation. The default object hash is actually the internal address in the JVM heap. Are we sure it is good enough to claim that the get/put are O(1)…
Michael
  • 37,415
  • 63
  • 167
  • 303
1
2 3
99 100