简体   繁体   English

比较O(2 / n)和O(1)的时间复杂度

[英]compare Time complexity of O(2/n) and O(1)

If there are 2 functions with time complexity of O(2/n) and O(100). 如果有2个函数的时间复杂度分别为O(2 / n)和O(100)。 which function has lesser execution time ?. 哪个函数执行时间更短? Is there any real function with time complexity of 2/n ?. 是否存在时间复杂度为2 / n的实函数? (found this in some algorithm question paper) (在一些算法试卷中找到了这一点)

Firstly, in the O notation (as well as Theta and Omega), you can dismiss any constants, because the definition already includes the part "for some constant k ". 首先,在O表示法(以及Theta和Omega)中,您可以忽略任何常量,因为定义中已经包含了“对于某些常量k ”部分。

So, basically, O(100) is equivalent to O(1), while O(2/n) is equivalent to O(1/n). 因此,基本上,O(100)等于O(1),而O(2 / n)等于O(1 / n)。 Which has faster execution time -- depends on the n . 执行时间更快-取决于n If I presume that 100 and 2/n are directly used to calculate execution time, than the execution time is: 如果我假设直接使用1002 / n计算执行时间,则执行时间为:

  • 100 (units of time) for O(100) in all cases 所有情况下 O(100) 均为 100 (时间单位)
  • more than 100 (units of time) for O(2/n) for n < 0.02 对于n <0.02的 O(2 / n)大于100 (时间单位)
  • less than 100 (units of time) for O(2/n) for n >= 0.02 对于n> = 0.02的 O(2 / n)小于100 (时间单位)

Now, I hope this question is purely theoretical, because in reality there is no algorithm with O(1/n) complexity -- it would mean that it takes less time (and that's note time per amount of data, that's just time) the more data it needs to process. 现在,我希望这个问题纯粹是理论上的,因为实际上并没有O(1 / n)复杂度的算法-这意味着它会花费更少的时间 (这是每数据量的记录时间,仅是时间)需要处理的更多数据。 I hope this is clear, that there is no algorithm that could take 0 time for infinite amount of data. 我希望这一点很清楚,没有算法可以花费0时间处理无限量的数据。

The other complexity, O(100) is an algorithm that takes the same steps no matter what the input data is, and thus always has constant time of execution (actually, it only has to be bounded by a constant, it can run faster that that sometimes). 另一个复杂度O(100)是一种算法,无论输入数据是什么,它都执行相同的步骤,因此始终具有恒定的执行时间(实际上,它仅需以一个常数为界,它可以以更快的速度运行)有时)。 An example would be a program that reads an input from a file of integers, and then returns the sum of the first 100 numbers in that file or of all the numbers if there isn't a 100 numbers present. 一个示例是从整数文件中读取输入的程序,然后返回该文件中前100个数字的总和;如果不存在100个数字,则返回所有数字的总和。 Since it always reads at most a 100 numbers (the rest can be ignored) and sums them, it is bounded by a constant number of steps. 由于它始终最多读取100个数字(其余数字可以忽略)并对其求和,因此它受恒定数量的步长限制。

Surely it depends on the value of N. The O(100) is basically fixed time, and may run faster or slower than the O(2/N) depending on what n is. 当然,它取决于N的值。O(100)基本上是固定时间,并且取决于n是什么,其运行速度可能比O(2 / N)快或慢。

I can't think of an O(2/N) algorithm off hand, something that gets faster with more data... sounds a bit weird. 我想不出O(2 / N)算法,随着更多数据的出现而变得更快……听起来有点怪异。

An O(2/n) algorithm that does anything is practically impossible. O(2/n)算法什么也不做,实际上是不可能的。

An algorithm is a finite sequence of steps that produces a result. 算法是产生结果的有限步骤序列。 Since a "step" on a computer takes a certain amount of time (for example at least one CPU cycle), the only way an algorithm can have O(2/n) time is if it takes zero time for sufficiently large n . 由于计算机上的“步”需要一定的时间量(例如至少一个CPU周期),因此算法可以具有O(2/n)时间的唯一方法是,如果n足够大,则需要时间。 Hence it does nothing. 因此,它什么也不做。

Leaving aside algorithms and time complexity: an O(2/n) function is "less than" constant, in the sense that a O(2/n) function necessarily tends to 0 as n tends to infinity, whereas an O(1) function doesn't necessarily do that. 抛开算法和时间复杂度: O(2/n)函数“小于”常量,从某种意义上来说, O(2/n)函数必然趋于0,因为n趋于无穷大,而O(1)函数不一定要这样做。

A remark on the text of the question from this paper: any function that is O(100) is also O(1) , and any function that is O(2/n) is also O(1/n) . 关于本文问题的注释:任何O(100)函数也是O(1) ,任何O(2/n)函数也是O(1/n) It doesn't really make much sense to write big-O with unnecessary constants in there, but since it's an examination perhaps it's there to potentially confuse you. 在里面写有不必要的常量的big-O并没有多大意义,但由于这是一种检查,因此可能会使您感到困惑。

I am not familiar with any algorithm that has O(2/n) running time and I doubt one can exist, but let's look at the mathematical qustion. 我对运行时间为O(2/n)任何算法都不熟悉,我怀疑一个算法是否可以存在,但让我们看一下数学问题。

The mathematical question should be: (1) is O(2/n) a subset of O(1) 1 (2) Is O(1) a subset of O(2/n) ? 数学问题应该是:(1) O(2/n)O(1) 1的子集吗(2) O(1)O(2/n)的子集吗?

  1. yes. 是。 Let f(n) be a function in O(2/n) - that means that there are constants c,N such that for each n > N : c*f(n) < 2/n . f(n)O(2/n)一个函数-这意味着存在常数c,N,对于每个n > Nc*f(n) < 2/n There are also constants c2,N2 such that c*2/n < 1 for each n > N2 , and thus min{c1,c2} * f(n) < 1 for each n > max{N1,N2} . 还有常数c2,N2 ,对于每个n > N2c*2/n < 1 ,因此对于每个n > max{N1,N2} min{c1,c2} * f(n) < 1 so O(2/n) is subset of O(1) 所以O(2/n)O(1)子集

  2. No. Since lim(2/n) = 0 at infinity, for each c,N , there is n>N such that 2/n < 1*c and thus f(n) = 2/n is not in O(1) , while it is in O(2/n) 否。由于lim(2/n) = 0在无穷大处为lim(2/n) = 0 ,对于每个c,N ,存在n>N使得2/n < 1*c ,因此f(n) = 2/n不在O(1) ,当它在O(2/n)

Conclusion: a function that is O(2/n) is also O(1) - but not the other way around. 结论: O(2/n)函数也是O(1) -但反之则不然。
It means - each function in O(2/n) scales "smaller" then 1. 这意味着O(2/n)每个函数的比例都“小”,然后为1。


(1) It is identical to O(100) , since O(1) = O(100) (1)与O(100)相同,因为O(1) = O(100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM