This is my first course in data structures and every lecture / TA lecture , we talk about O(log(n))
. This is probably a dumb question but I'd appreciate if someone can explain to me exactly what does it mean !?
这是我在数据结构和每堂课/助教课上的第一节课,我们讨论O(log(n))这可能是一个愚蠢的问题,但如果有人能准确地向我解释这到底是什么意思,我会很感激。
5 个解决方案
#1
47
It means that the thing in question (usually running time) scales in a manner that is consistent with the logarithm of its input size.
这意味着所讨论的对象(通常是运行时间)以一种与输入大小的对数一致的方式进行扩展。
Big-O notation doesn't mean an exact equation, but rather a bound. For instance, the output of the following functions is all O(n):
大o表示法的意思不是精确方程,而是一个界。例如,以下函数的输出都是O(n):
f(x) = 3x
g(x) = 0.5x
m(x) = x + 5
Because as you increase x, their outputs all increase linearly - if there's a 6:1 ratio between f(n)
and g(n)
, there will also be approximately a 6:1 ratio between f(10*n)
and g(10*n)
and so on.
因为当x增加时,它们的输出都会线性增加,如果f(n)和g(n)之间的比值为6:1,那么f(10*n)和g(10*n)之间的比值也大约为6:1。
As for whether O(n)
or O(log n)
is better, consider: if n = 1000
, then log n = 3
(for log-base-10). Which would you rather have your algorithm take to run: 1000 seconds, or 3 seconds?
至于O(n)还是O(log n)更好,考虑一下:如果n = 1000,那么log n = 3 (log -base-10)。你希望你的算法运行时间是1000秒还是3秒?
#2
3
For the input of size n
, an algorithm of O(n)
will perform steps perportional to n
, while another algorithm of O(log(n))
will perform steps roughly log(n)
.
对于大小为n的输入,一个O(n)的算法将执行到n的步骤,而另一个O(log(n)的算法将执行大约log(n)的步骤。
Clearly log(n)
is smaller than n
hence algorithm of complexity O(log(n))
is better. Since it will be much faster.
显然log(n)小于n,因此复杂度为O(log(n))的算法更好。因为它会快得多。
#3
1
http://en.wikipedia.org/wiki/Big_oh
http://en.wikipedia.org/wiki/Big_oh
O(log n) is better.
O(log n)更好。
#4
0
Formal definition:
正式的定义:
g(x) = O(f(x)) <=> there is x0 and constant C that for every x > x0, |g(x)| <= C|f(x)|.
g(x) = O(f(x)) <=>有x0和常数C,对于每一个x > x0, |g(x)| <= C|f(x)|。
Thus, if you find algorithm A for problem P that its complexity O(f(n)), you can say that the number of steps your algorithm will do, is lower or equal asymptotically to f(n), when n is usually the input size. (n can be anything)
因此,如果你找到问题P的算法A,它的复杂度为O(f(n)),你可以说你的算法要做的步数,是f(n)的渐近或等于f(n),而n通常是输入大小。(n可以是任何东西)
For further reading:http://en.wikipedia.org/wiki/Big_O_notation.
进一步阅读:http://en.wikipedia.org/wiki/Big_O_notation。
#5
0
O(logn) means that the algorithm's running time is dependent on the logarithm of the input size. O(n) means that the algorithm's running time is dependent on the input size.
O(logn)表示算法的运行时间依赖于输入大小的对数。O(n)表示算法的运行时间取决于输入大小。
basically, O(something) is an upper bound on the algorithm's number of instructions (atomic ones). therefore, O(logn) is tighter than O(n) and is also better in terms of algorithms analysis. But all the algorithms that are O(logn) are also O(n), but not backwards...
基本上,O(某物)是算法的指令数(原子序数)的上限。因此,O(logn)比O(n)更紧,在算法分析上也更优。但是所有的O(logn)算法也都是O(n),但不是向后的。
#1
47
It means that the thing in question (usually running time) scales in a manner that is consistent with the logarithm of its input size.
这意味着所讨论的对象(通常是运行时间)以一种与输入大小的对数一致的方式进行扩展。
Big-O notation doesn't mean an exact equation, but rather a bound. For instance, the output of the following functions is all O(n):
大o表示法的意思不是精确方程,而是一个界。例如,以下函数的输出都是O(n):
f(x) = 3x
g(x) = 0.5x
m(x) = x + 5
Because as you increase x, their outputs all increase linearly - if there's a 6:1 ratio between f(n)
and g(n)
, there will also be approximately a 6:1 ratio between f(10*n)
and g(10*n)
and so on.
因为当x增加时,它们的输出都会线性增加,如果f(n)和g(n)之间的比值为6:1,那么f(10*n)和g(10*n)之间的比值也大约为6:1。
As for whether O(n)
or O(log n)
is better, consider: if n = 1000
, then log n = 3
(for log-base-10). Which would you rather have your algorithm take to run: 1000 seconds, or 3 seconds?
至于O(n)还是O(log n)更好,考虑一下:如果n = 1000,那么log n = 3 (log -base-10)。你希望你的算法运行时间是1000秒还是3秒?
#2
3
For the input of size n
, an algorithm of O(n)
will perform steps perportional to n
, while another algorithm of O(log(n))
will perform steps roughly log(n)
.
对于大小为n的输入,一个O(n)的算法将执行到n的步骤,而另一个O(log(n)的算法将执行大约log(n)的步骤。
Clearly log(n)
is smaller than n
hence algorithm of complexity O(log(n))
is better. Since it will be much faster.
显然log(n)小于n,因此复杂度为O(log(n))的算法更好。因为它会快得多。
#3
1
http://en.wikipedia.org/wiki/Big_oh
http://en.wikipedia.org/wiki/Big_oh
O(log n) is better.
O(log n)更好。
#4
0
Formal definition:
正式的定义:
g(x) = O(f(x)) <=> there is x0 and constant C that for every x > x0, |g(x)| <= C|f(x)|.
g(x) = O(f(x)) <=>有x0和常数C,对于每一个x > x0, |g(x)| <= C|f(x)|。
Thus, if you find algorithm A for problem P that its complexity O(f(n)), you can say that the number of steps your algorithm will do, is lower or equal asymptotically to f(n), when n is usually the input size. (n can be anything)
因此,如果你找到问题P的算法A,它的复杂度为O(f(n)),你可以说你的算法要做的步数,是f(n)的渐近或等于f(n),而n通常是输入大小。(n可以是任何东西)
For further reading:http://en.wikipedia.org/wiki/Big_O_notation.
进一步阅读:http://en.wikipedia.org/wiki/Big_O_notation。
#5
0
O(logn) means that the algorithm's running time is dependent on the logarithm of the input size. O(n) means that the algorithm's running time is dependent on the input size.
O(logn)表示算法的运行时间依赖于输入大小的对数。O(n)表示算法的运行时间取决于输入大小。
basically, O(something) is an upper bound on the algorithm's number of instructions (atomic ones). therefore, O(logn) is tighter than O(n) and is also better in terms of algorithms analysis. But all the algorithms that are O(logn) are also O(n), but not backwards...
基本上,O(某物)是算法的指令数(原子序数)的上限。因此,O(logn)比O(n)更紧,在算法分析上也更优。但是所有的O(logn)算法也都是O(n),但不是向后的。