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

What is the meaning of O( polylog(n) )? In particular, how is polylog(n) defined?

Brief: When academic (computer science) papers say "O(polylog(n))", what do they mean? I'm not confused by the "Big-Oh" notation, which I'm very familiar with, but rather by the function polylog(n). They're not talking about the complex analysis…
Managu
  • 8,369
  • 2
  • 27
  • 36
54
votes
7 answers

Why is the Big-O complexity of this algorithm O(n^2)?

I know the big-O complexity of this algorithm is O(n^2), but I cannot understand why. int sum = 0; int i = 1; j = n * n; while (i++ < j--) sum++; Even though we set j = n * n at the beginning, we increment i and decrement j during each…
53
votes
8 answers

why does accessing an element in an array take constant time?

Lets say I have an array as: int a[]={4,5,7,10,2,3,6} when I access an element, such as a[3], what does actually happen behind the scenes? Why do many algorithm books (such as the Cormen book...) say that it takes a constant time? (I'm just a noob…
user188276
52
votes
7 answers

Time complexity of nested for-loop

I need to calculate the time complexity of the following code: for (i = 1; i <= n; i++) { for(j = 1; j <= i; j++) { // Some code } } Is it O(n^2)?
yyy
  • 763
  • 1
  • 6
  • 11
52
votes
8 answers

Which is better: O(n log n) or O(n^2)

Okay so I have this project I have to do, but I just don't understand it. The thing is, I have 2 algorithms. O(n^2) and O(n*log2n). Anyway, I find out in the project info that if n<100, then O(n^2) is more efficient, but if n>=100, then O(n*log2n)…
user3579272
  • 523
  • 1
  • 5
  • 6
50
votes
2 answers

Finding Big O of the Harmonic Series

Prove that 1 + 1/2 + 1/3 + ... + 1/n is O(log n). Assume n = 2^k I put the series into the summation, but I have no idea how to tackle this problem. Any help is appreciated
user2092408
  • 503
  • 1
  • 4
  • 4
48
votes
13 answers

What's Up with O(1)?

I have been noticing some very strange usage of O(1) in discussion of algorithms involving hashing and types of search, often in the context of using a dictionary type provided by the language system, or using dictionary or hash-array types used…
orcmid
  • 2,550
  • 18
  • 20
48
votes
6 answers

Time complexity of accessing a Python dict

I am writing a simple Python program. My program seems to suffer from linear access to dictionaries, its run-time grows exponentially even though the algorithm is quadratic. I use a dictionary to memoize values. That seems to be a bottleneck. …
x10
  • 3,522
  • 1
  • 21
  • 32
47
votes
30 answers

Are games the most complex / impressive applications?

I was thinking today about what could be the most complex / impressive application ever written. So I started thinking of what I am comfortable with and use everyday, databases. Then I went into the field of the unknown (to most of us I guess), the…
Luca Matteis
  • 28,287
  • 19
  • 109
  • 164
47
votes
4 answers

Example of a factorial time algorithm O( n! )

I'm studying time complexity in school and our main focus seems to be on polynomial time O(n^c) algorithms and quasi-linear time O(nlog(n)) algorithms with the occasional exponential time O(c^n) algorithm as an example of run-time perspective.…
recursion.ninja
  • 4,968
  • 7
  • 42
  • 76
45
votes
8 answers

How is the implementation of LinkedHashMap different from HashMap?

If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap? What are all the extra overhead LinkedHashMap has when compared to HashMap in Java?
Passionate programmer
  • 5,328
  • 10
  • 34
  • 43
44
votes
5 answers

What is the runtime complexity of python list functions?

I was writing a python function that looked something like this def foo(some_list): for i in range(0, len(some_list)): bar(some_list[i], i) so that it was called with x = [0, 1, 2, 3, ... ] foo(x) I had assumed that index access of lists…
Marquis Wang
  • 10,222
  • 4
  • 28
  • 24
43
votes
3 answers

What is O(1) space complexity?

I am having a hard time understanding what is O(1) space complexity. I understand that it means that the space required by the algorithm does not grow with the input or the size of the data on which we are using the algorithm. But what does it…
coder123
  • 601
  • 1
  • 6
  • 16
42
votes
1 answer

Different decision tree algorithms with comparison of complexity or performance

I am doing research on data mining and more precisely, decision trees. I would like to know if there are multiple algorithms to build a decision trees (or just one?), and which is better, based on criteria such as Performance Complexity Errors in…
39
votes
18 answers

What is the best way to get the minimum or maximum value from an Array of numbers?

Let's say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2] What is the best way to find the minimum or maximum value in that Array? Right now, to get the maximum, I am looping through the Array, and resetting a variable to the value if it is…