Reputation: 6957
Time complexity:
O(log(N)+M)
withN
being the number of elements in the sorted set andM
the number of elements returned.
This is from Redis docs. I understand the concept of Big O but I have my doubts about what role does log()
play in this context. I read about log
and answers in SO, e.g it can have different bases.
Could someone explain this time complexity with few examples?
For example: N = 1000, M = 10
and N = 1,000,000, M = 1000
Upvotes: 0
Views: 989
Reputation: 2154
Let me dissect this thing part by part...
O(log n): logarithmic complexity
To understand this complexity class you first need to develop an intuition around the log
function.
You have:
ln
: natural logarithm, or logarithm base e
log
: binary logarithm, or logarithm base 2. Unless another base is specifiedlog*
: also called the iterated logarithmComplexity classes frequently use the binary logarithm, or logarithm base 2 rather than the rest. But it's still important to make a distinction between those.
The log
function is defined by "the power to which the number 2 must be raised to obtain the value n". However that definition can be a bit hard to reason about for people like you and me.
You can rather understand it in a more informal / less correct way as: "the number of times you can divide by number by 2".
8 / 2 = 4
-> 4 / 2 = 2
-> 2 / 2 = 1
.log(8) = 3
Wrapping things up: O(log n)
means that if the input has size n
, the time used by the function will be proportional to log n
.
O(log n)
is much faster than O(n)
.
O(n): Linear complexity
This means that if the input has size n
, the time used by the function will be proportional to n
. Meaning, it has a 1:1 proportionality.
Different variables
Now, what to do when you have different variable names...
In this case, N and M speak of different variables. Some examples
M x N
(e.g: M rows and N columns) matrix can take O(M * N)
time.V
number of vertices and E
number of edges can have its complexity defined as a function of V
and E
, e.g: O(V + E)
or O(V * E)
or whatever.Finally, going back to your question:
What does O(log(N) + M)
mean in this particular context?
Means that it will be the sum of:
O(log N)
in terms of the elements of the setO(N)
in terms of the elements being returned (the query results)Upvotes: 4
Reputation: 372814
Remember that big-O notation talks about long-term growth rates, rather than the actual numerical number of steps required by some algorithm. If a function's runtime is 100log N + 200M or 3log N + 150M, in both cases the runtime is O(log N + M), so just knowing the big-O notation won't let you predict the runtime on a given N and M a priori.
What you can do is use your knowledge of the fact that the runtime is O(log N + M) to extrapolate the runtime given some data points. For example, if you know the runtime when N = 10,000,000 and M = 1,000 is 1s, you can predict that the runtime when N = 10,000,000 and M = 2,000 will probably be something like 2s because the runtime scales linearly as a function of M. The more data points you have to work with, the better your prediction will be. You can also see that the runtime is far less sensitive to changes in N than in M, since for log N to increase by a factor of two you'd need to effectively square the value of N.
Upvotes: 2