2

Can an algorithm having a time complexity of O(n) have a space complexity of O(n2) or more than that?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
noddy
  • 1,010
  • 4
  • 14
  • 27

5 Answers5

6

The space complexity cannot be more than the time complexity because writing X units of space takes Omega(X) time.

Irit Katriel
  • 3,534
  • 1
  • 16
  • 18
  • what do we actually include in space complexity ?Are the spaces used for intermediate results also included in space complexity? – noddy Aug 22 '11 at 17:32
  • Yep - include any and all space. –  Aug 22 '11 at 17:34
  • @noddy - If an algorithm will not run without space for intermediate results, then that counts as part of the space complexity of the algorithm. – Ted Hopp Aug 22 '11 at 17:34
  • How much space your algorithm needs in order to run. That means the total amount of space you need to allocate at once (so if you allocate 2x bytes, free x bytes and then allocate 2x bytes again, you only need 3x bytes of space). – Irit Katriel Aug 22 '11 at 17:36
  • @IritKatriel can you provide a reliable source for that statement? – JulianWgs Sep 15 '20 at 20:20
4

have a look at the DSPACE and DTIME groups, which indicate what algorithm can be done in which time/space complexity, and the relationship between groups.

all algorithms that use O(n) time are in the group DTIME(n).
all algorithms that use O(n^2) space, are in the group DSPACE(n^2).
since DTIME(n) <= NTIME(n) <= DSPACE(n) < DSPACE(n^2), so every algorithm that is O(n) time, is also O(n^2) space.

amit
  • 175,853
  • 27
  • 231
  • 333
  • 2
    well i would better ask that given that an algorithm has time complexity O(n),can we comment anything about the lower and upper bounds of its space complexity? – noddy Aug 22 '11 at 17:41
  • 1
    @noddy: if an algorithm is O(n) time, it is bound to be O(n) space, according to the theorem about complexity groups relationship. the lower bound is O(1), for instance: searching an element in unsorted list is O(n) time and O(1) space. – amit Aug 22 '11 at 17:44
  • @amit- Are you sure you want to use NTIME here? Most of the time people are talking about algorithms they assume deterministic algorithms, so DTIME might be a better complexity class to consider. – templatetypedef Aug 22 '11 at 18:47
  • @templatedtypedef you are right, I'm going to edit my answer, I meant DTIME. thanks for the comment! – amit Aug 22 '11 at 18:58
  • Can you explain again if an algorithm is O(n) time, why its the lower space bound is O(1) and upper space bound is O(n)? – Gqqnbig Jan 17 '15 at 20:52
  • 1
    @LoveRight I gave a mathematical proof using the known DSPACE and DTIME sets. The intuition behind it is you cannot write to the memory without consuming time, so for every "unit" of time consumed you can write at most X units of space. This means if your algorithm runs `O(n)` time, you can write at most `O(n*X)` units of space, but since `X` is constant, `O(n*X)=O(n)`. Hope that's clear. – amit Jan 17 '15 at 21:59
2

Since all O(n) functions are trivially O(n2) (see, e.g., Wikipedia on Big O notation), the answer is "yes."

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Big-O notation deals in upper bounds, technically speaking an algorithm is O( g(n) ) for any and all g(n) that grow asympoticaly faster than f(n), so if an algorithm is O(n) it must be O(n^2) and O(n^99).

Little-o notation deals in tonight upper bounds, i.e the least fastest growing set of functions which grow faster than f(n). Therefore its not valid to say f(n) is o(n^2) iff it is o(n).

Edit (to answer comment):

If given an algorithm A and being told reliably that A is O(n^2) then there is the possibility that A is O(n) (or whatever) but you would have to analyse A to find out yourself. Conversely, if reliably told A was o(n^2) it cannot be O(n).

  • well i understand the thing about triviality of O(n) also being O(square of n) so instead i should ask the other way round...can it be less then O(n)?leave the trivialities please... – noddy Aug 22 '11 at 17:38
0

To answer the question you probably meant to ask: generally, the accounting is such that allocating a given amount of memory takes a proportional amount of time. Why? well, practically speaking, something needs to initialize the memory before you use it.

Alternately, if you assume that all your memory comes pre-initialized, then this will not be the case after your procedure writes all over it; something would still need to clean up the memory afterwards...

There are actually a variety of processor models used in algorithm analysis; if you wanted, you could specify a model that says "pre-zeroed memory is free, and you don't have to clean up after yourself", which would yield a different metric for algorithms that use memory sparsely. However, in practice memory allocation and garbage collection are not free, so this metric would have limited practical relevance.

comingstorm
  • 25,557
  • 3
  • 43
  • 67