-4

I'm having trouble understanding Big-O Notation. Here is an algorithm I wrote, it is supposed to be an alternative of (C++) Stack's size() function, and I need to determine its running time with the assumption that there are n elements in the stack when it is invoked.

Algorithm size():
    Input: none
    Output: A constant value of the size of an n-element stack.
Let V be a vector of n type objects.
Let S be the name of the stack that is being operated on by this function.
K ← 0                       
V
while !empty() 
    V.push_back(top())      //Keep track of elements in V
    S.pop()             //Remove element from stack
    K ← K + 1           //Count the size of the stack
return K                //Return the size of the stack  
for i ← K – 1, i > 0, i-- do            
    S.push(V[i])            //Retain initial contents of stack

Please correct me where I'm wrong:

  • In the line K ← 0, I think that is an O(1) operation.

  • Creating the vector V is also an O(1) operation.

  • the while loop is an O(n) operation since it runs until it empties a stack containing n contents.

  • Pushing values back into V is an O(n) operation.

  • Popping contents off the stack S is an O(n) operation.

  • Returning K is an O(1) operation.

  • The for loop is an O(n) operation.

  • Pushing contents back into S is an O(n) operation.

user207421
  • 305,947
  • 44
  • 307
  • 483
user11892
  • 103
  • 5
  • 4
    math.stackexchange.com or cs.stackexchange.com would be better places for questions about Big-Oh notation. – Barmar Jan 29 '15 at 02:59
  • Unless you have to copy the entire vector or stack, pushing and popping elements should be O(1). – Barmar Jan 29 '15 at 03:01
  • 1
    The `for` loop will never be executed, because it's after the `return` statement. – Barmar Jan 29 '15 at 03:03
  • Looks to me like you have the algorithm a bit wrong, but the individual complexities pretty much correct. Usually you'd summarize the overall complexity as the "worst" of the component complexities, O(n) in your case. – Jerry Coffin Jan 29 '15 at 03:04
  • @JerryCoffin besides the return statement thing that Barmar pointed out, what is wrong about the algorithm? – user11892 Jan 29 '15 at 03:06
  • I'd have to write/run code to be *sure* but I believe you also have an off-by-one error on copying the data back from V to S (you go from k-1 down to 1, but should go from K-1 down to 0). – Jerry Coffin Jan 29 '15 at 03:12
  • Shall we assume the unbound `empty()` and `top()` are actually from `S` ? – WhozCraig Jan 29 '15 at 03:39
  • *"The for loop is an O(n) operation. Pushing contents back into S is an O(n) operation."* - this is imprecise. The loop iterates O(n) times invoking the single-element push with is O(1) each time it runs. Collectively, the whole loop & push thing is O(n). – Tony Delroy Jan 29 '15 at 03:48

1 Answers1

1

The push and pop operations are O(1). The for loop that contains the push is O(n) and the push is O(1).

MIT Always puts out good stuff to study http://web.mit.edu/16.070/www/lecture/big_o.pdf.

J Blaz
  • 783
  • 1
  • 6
  • 26