Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing algorithm complexity

Please, help me to compare complexity of two algorithms.

  1. O(N+1000) + O(M*log(M))
  2. O(N*5) + O(2000)

N = 100000 M = 100

I can't understand, what should I do with O(...)? Can I leave it? And just do...

(N+1000) + (M*log(M)) = 101200
(N*5) + 2000 = 502000

Is it right?

Thank you

UPDATED

I have task and I have two probable solutions for it. First solution's algorithm complexity O(N) + O(M log(M)), see http://code.google.com/p/redis/wiki/ZunionstoreCommand ; the second solution consists of two algorithms with complexities O(N) http://code.google.com/p/redis/wiki/SunionCommand and O(N*M) http://code.google.com/p/redis/wiki/SinterCommand. I thought that I can replace N and M with real world values to compare speed of both solutions.

like image 347
Kirzilla Avatar asked Sep 05 '26 12:09

Kirzilla


2 Answers

Big-O notation does not tell you how fast a specific algorithm is for a specific data set - it tells you about the asymptotic performance. In Big-O notation you can ignore constants and constant coefficients:

  1. O(N + M*log(M))
  2. O(N)

Now you can see that the second algorithm has better asymptotic performance.

like image 87
Mark Byers Avatar answered Sep 08 '26 02:09

Mark Byers


When comparing complexity in a general case, you ignore constant values.

O(N+1000 + M*log(M))

becomes

O(N + M*log(M))

while

O(N*5 + 2000)

becomes

O(N)

Now, if you know FOR SURE those are the values you'll have for M and N, you can do the math and be more precise, but you'll also have to know how long each operation takes -- big-O notation is used for scaling, not so much for how an algorithm performs in a particular case. If your data is that static, you may as well just run both algorithms and see which returns faster.

Edit: as somebody else pointed out, proper notation isn't the sum of two big-Os, but rather the big-O of the sum.

like image 38
PeterL Avatar answered Sep 08 '26 02:09

PeterL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!