3

I'm trying to understand better the idea of O(n), so I'm wonder about this:

If we know that a>=b so O(a+b)=O(a)?
I know that O(a)+O(a)=O(2a)=O(a), but I'm wondering if it's true for something that it's smaller then a, I mean - if O(a+b)=O(a).

I think that it's true because a+b=O(2a), but I'd like to know if I'm wrong...

(P.S. it will be true if a and b are constants?)

Thank you!

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
Yoar
  • 183
  • 8

1 Answers1

6

You're totally correct in simplifying O(a+b) = O(a) as per this case.

It's so because

 a>=b (given)
 O(a+b) <= O(a+a) = O(2a) = O(a) // as clearly mentioned by you.

Example :-

Let's assume

a = n; b = log(n).

Then,you can see

O(a+b) = O(n+log(n)) = O(n) = O(a).
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Thanks! But if they are constants? (a and b). It the same answer? – Yoar Nov 27 '14 at 19:59
  • 1
    For constants, it won't matter and will resolve to O(1) as it is standard notation for constant time-complexity. Like `O(2+3) = O(2) = O(1)` as there's nothing like O(2), O(2+3),etc. The notation for constant complexity is O(1) always. – Am_I_Helpful Nov 27 '14 at 20:02
  • Yes, I know but if it's like a number of vertices at tree - I mean - If I have two trees with `a` and `b` vertices, and I want to do something at with those trees at `O(a)`, so `O(a+b)=O(a)`? – Yoar Nov 27 '14 at 20:07
  • 1
    a and b will be measured as a common parameter serving as the number of vertices of tree(commonly put under the term n), and hence,then also your complexity would be O(a)--->though it is more better and proper to write O(a+b) there or rather O(n) where n is the parameter representing no. of vertices of the tree rather than O(a). Computer Scientists prefer the latter way,i.e, `O(a+b)` representation. – Am_I_Helpful Nov 27 '14 at 20:12
  • I meant that there is two trees - one with `a` vertices and one with `b` vertices... So it still be `O(a+b)=O(a)`? Thank you! – Yoar Nov 27 '14 at 20:15
  • 1
    No,then it'd be properly O(a+b) as these are not functions and hence represent the vertices of different trees,whatever their logical relationship be(talking about >,<,=). It is different from the case which you asked without quoting the exact question! here,the main thing(in simplification of big-O) is the governing function. – Am_I_Helpful Nov 27 '14 at 20:20
  • I'll try to explain myself better: I have two trees - One with `a` vertices and one with `b` vertices - I want to do something and merge them (for example) , but I'm trying to do this at `O(b)`, so if I have an algorithm that can make it at `O(a+b)` so I can say that I succeed? because `a>=b`, and I know that `O(a+a)=O(a)` - Thank you so much!! – Yoar Nov 27 '14 at 20:26
  • 1
    Keep in mind that although it would theoretically simplify to O(a) (or O(1) in the second example), sometimes the simplification is not trivial. If a is 10000000 and b is 10000000-1, ab will be a LOT larger than just a, thus possibly messing with your assumption of the average runtime. – Ricky Mutschlechner Nov 27 '14 at 20:27
  • 1
    @RickyMutschlechner, Thank you, but I'm not talking about `ab`.. it's about `O(a+b)`, I'm right at my assumption of the average runtime? – Yoar Nov 27 '14 at 20:30
  • 1
    @Yoar- See,first of all, your assumption that O(a) and O(b) are distinct is incorrect. As I already mentioned that both are same parameter type,i.e.,a vertex---so your trees become a function of vertex,i.e., O(n). So, O(a)=O(n) and similarly,O(b)= O(n) and hence,merging trees means O(n). But,even,if you say that you try to consider them as two different functions,then the output will be O(a+b) as a and b are independent to each other and hence,a can't be compared to be. I hope you got it now! – Am_I_Helpful Nov 27 '14 at 20:31
  • I'm understand it!! Thank you so much!! :-) – Yoar Nov 27 '14 at 20:48
  • 1
    On the matter of constants, to define a constant in terms of a variable `a` we would note that it is proportional to `a⁰ = 1`, hence O(1). – Jon Hanna Nov 28 '14 at 19:14