0

I just started a course on Asymptotic Analysis and in one of our assignments I am supposed to add functionality to a function without changing the complexity. The complexity is log(N). The homework guideline asks me specifically to change the runtime by a 'constant'. Would making it 3Log(N) be considered changing it by a constant?

Bart
  • 19,692
  • 7
  • 68
  • 77
devjeetroy
  • 1,855
  • 6
  • 26
  • 43
  • Did you forget the exact definition of `O(log N)` ? Go back to the definition to understand that the answer is trivially *yes* – Basile Starynkevitch Apr 27 '12 at 05:07
  • Can you show how did you end up getting `3log(n)`? – noMAD Apr 27 '12 at 05:08
  • I believe that adding something, like (constant + log(N)) would be considered adding a constant factor, whereas 3LogN would be multiplying. Although I know by definition that O(3LogN) = o(LogN), I feel my instructor meant the additive form. The thing is I did not want to take any chances with my assignment so I wanted to clarify with more knowledgeable folks. – devjeetroy Apr 27 '12 at 05:16
  • I think you need to look up what "factor" means. – Carl Norum Apr 27 '12 at 05:21

1 Answers1

4

Yes, more specifically, this would be changing it by a multiplicative constant. You could also change it by an additive constant like log(N)+5.

trutheality
  • 23,114
  • 6
  • 54
  • 68
  • Yes, this thing is changing an algorithm by a multiple of something like `3` is highly unlikely unless you do something like run a nested loop just 3 times which again might not be the optimal solution. – noMAD Apr 27 '12 at 05:10
  • The original function involved adding an element to a binary search tree. To complete my assignment, I had to modify the code so as to traverse the tree not once, but twice. Would that be 2LogN? – devjeetroy Apr 27 '12 at 05:18
  • @noMAD It's not uncommon to need to do something in a constant number passes over a data structure, or to add a constant number of operations inside the iteration. – trutheality Apr 27 '12 at 05:20
  • @ZachSchnider: You are traversing your tree once at a time no matter how many times you do it. So, it would still end up being `O(log n)` or in the case of traversing the entire tree it should be `O(n)`. – noMAD Apr 27 '12 at 05:21
  • 1
    @ZachSchnider Yes. What noMAD is alluding to is probably the fact that `O(2 log n)` is still `= O(log n)`. – trutheality Apr 27 '12 at 05:21
  • @trutheality: How can parsing a tree twice cost you `O(2Log n)`? Its the Asymptotic time analysis. – noMAD Apr 27 '12 at 05:22
  • @noMAD, as far as asymptotic time analysis is concerned, passing it **once** is `O(9001 log n)` because the constant doesn't matter. Passing it twice can take `2 log n` operations, which is `O( log n ) = O( 2 log n ) = O( 9001 log n )`. – trutheality Apr 27 '12 at 05:25
  • What about `O(log n)` and `O(log n^2)`. By properties of logarithms isn't `2 log n` equal to `log n^2`? I'm not sure what I'm saying even makes sense. – nikhil Jan 28 '13 at 08:11
  • 1
    @nikhil That's right: `O( log(n^2) ) = O(2 log n) = O(log n)`. But don't confuse `log(n^2)` with `(log n)^2`, because those don't have the same asymptotic complexity. – trutheality Jan 28 '13 at 17:01