我已经通过谷歌和Stack Overflow搜索,但无处我能够找到一个清楚和直接的解释如何计算时间复杂度。

我已经知道了什么?

比如下面这样简单的代码:

char h = 'y'; // This will be executed 1 time
int abc = 0; // This will be executed 1 time

比如下面这样的循环:

for (int i = 0; i < N; i++) {
    Console.Write('Hello, World!!');
}

int i = 0;这将只执行一次。

时间实际上是计算到i=0,而不是声明。

i < N;这将被执行N+1次 i++将执行N次

因此,这个循环所需的操作数为{1+(N+1)+N} = 2N+2。(但这仍然可能是错误的,因为我对自己的理解没有信心。)

好的,这些基本的小计算我知道,但在大多数情况下,我看到的时间复杂度是O(N) O(N²)O(log N) O(N !)等等。


当前回答

如何计算算法的时间复杂度

你把它将执行的机器指令数量作为输入大小的函数相加,然后将表达式简化为最大(当N非常大时)项,并可以包括任何简化的常数因子。

例如,让我们看看如何简化2N + 2个机器指令,将其描述为O(N)。

为什么要去掉两个2 ?

我们感兴趣的是N变大时算法的性能。

考虑2N和2这两项。

当N变大时,这两项的相对影响是什么?假设N是一百万。

第一项是200万,第二项只有2。

因此,对于大N,我们只保留最大的项。

现在我们从2N + 2到2N。

传统上,我们只对常数因素下的性能感兴趣。

这意味着当N很大时,我们并不关心性能差异是否有常数倍数。2N的单位一开始就没有明确定义。所以我们可以乘或除一个常数因子得到最简单的表达式。

所以2N变成了N。

其他回答

对于有数学头脑的人来说:主定理是研究复杂性时另一个有用的东西。

O(n)是大O符号,用于表示算法的时间复杂度。当你把一个算法的执行次数加起来时,你会得到一个像2N+2这样的结果。在这个表达式中,N是主导项(N的值增加或减少对表达式影响最大的项)。O(N)是时间复杂度N是主要项。

例子

For i = 1 to n;
  j = 0;
while(j <= n);
  j = j + 1;

这里内层循环的总执行次数为n+1,外层循环的总执行次数为n(n+1)/2,因此整个算法的总执行次数为n+1 + n(n+1/2) = (n2 + 3n)/2。 这里n²是主导项所以这个算法的时间复杂度是O(n2)

其他答案集中在大o符号和实际例子上。我想从理论的角度来回答这个问题。下面的解释必然缺乏细节;Michael Sipser的《计算理论导论》是学习计算复杂性理论的一个很好的来源。

图灵机

研究计算问题的最广泛的模型是图灵机。图灵机有一个一维的纸带,上面有符号,用作存储设备。它有一个磁带头,用来从磁带中读写。它有一个决定机器行为的转换表,这是一个在机器创建时决定的固定硬件组件。图灵机在离散时间步长下工作,如下所示:

它能读出胶带下面的符号。 根据符号及其内部状态(只能取有限个值)的不同,它从转换表中读取三个值s、σ和X,其中s是内部状态,σ是符号,X是左或右。

它的内部状态变成了s。

它将读取的符号更改为σ。

它将磁带头按照X的方向移动一步。

图灵机是强大的计算模型。它们能做你的数字计算机能做的一切。它们是在现代数字计算机出现之前由理论计算机科学之父和数学家艾伦·图灵提出的。

时间复杂度

It is hard to define the time complexity of a single problem like "Does white have a winning strategy in chess?" because there is a machine which runs for a single step giving the correct answer: Either the machine which says directly 'No' or directly 'Yes'. To make it work we instead define the time complexity of a family of problems L each of which has a size, usually the length of the problem description. Then we take a Turing machine M which correctly solves every problem in that family. When M is given a problem of this family of size n, it solves it in finitely many steps. Let us call f(n) the longest possible time it takes M to solve problems of size n. Then we say that the time complexity of L is O(f(n)), which means that there is a Turing machine which will solve an instance of it of size n in at most C.f(n) time where C is a constant independent of n.

它不是依赖于机器吗?数字计算机能做得更快吗?

Yes! Some problems can be solved faster by other models of computation, for example two tape Turing machines solve some problems faster than those with a single tape. This is why theoreticians prefer to use robust complexity classes such as NL, P, NP, PSPACE, EXPTIME, etc. For example, P is the class of decision problems whose time complexity is O(p(n)) where p is a polynomial. The class P do not change even if you add ten thousand tapes to your Turing machine, or use other types of theoretical models such as random access machines.

理论与实践的差异

通常假设整数加法的时间复杂度为O(1)。这种假设在实践中是有意义的,因为计算机使用固定数量的位来存储许多应用程序的数字。理论上没有理由假设这样的事情,所以加法的时间复杂度是O(k)其中k是表示整数所需的比特数。

寻找一类问题的时间复杂度

显示问题时间复杂度的最直接方法是构造一个图灵机,它能在O(f(n))时间内解决问题。为复杂问题创建图灵机并非易事;人们需要熟悉它们。图灵机的转换表很少给出,它是高层次的描述。随着人们对机器的熟悉,就更容易看到机器需要多长时间才能停下来。


说明一个问题不是O(f(n))时间复杂度是另一回事……尽管有一些像时间层次定理这样的结果,但这里还有很多悬而未决的问题。例如,NP中的问题是否在P中,即是否可在多项式时间内解决,是数学千年大奖中的七个问题之一,解决者将获得100万美元的奖励。

如何计算算法的时间复杂度

你把它将执行的机器指令数量作为输入大小的函数相加,然后将表达式简化为最大(当N非常大时)项,并可以包括任何简化的常数因子。

例如,让我们看看如何简化2N + 2个机器指令,将其描述为O(N)。

为什么要去掉两个2 ?

我们感兴趣的是N变大时算法的性能。

考虑2N和2这两项。

当N变大时,这两项的相对影响是什么?假设N是一百万。

第一项是200万,第二项只有2。

因此,对于大N,我们只保留最大的项。

现在我们从2N + 2到2N。

传统上,我们只对常数因素下的性能感兴趣。

这意味着当N很大时,我们并不关心性能差异是否有常数倍数。2N的单位一开始就没有明确定义。所以我们可以乘或除一个常数因子得到最简单的表达式。

所以2N变成了N。

循环的几个例子。

O(n) time complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. For example following functions have O(n) time complexity. // Here c is a positive integer constant for (int i = 1; i <= n; i += c) { // some O(1) expressions } for (int i = n; i > 0; i -= c) { // some O(1) expressions } O(nc) time complexity of nested loops is equal to the number of times the innermost statement is executed. For example, the following sample loops have O(n2) time complexity for (int i = 1; i <=n; i += c) { for (int j = 1; j <=n; j += c) { // some O(1) expressions } } for (int i = n; i > 0; i += c) { for (int j = i+1; j <=n; j += c) { // some O(1) expressions } For example, selection sort and insertion sort have O(n2) time complexity. O(log n) time complexity of a loop is considered as O(log n) if the loop variables is divided / multiplied by a constant amount. for (int i = 1; i <=n; i *= c) { // some O(1) expressions } for (int i = n; i > 0; i /= c) { // some O(1) expressions } For example, [binary search][3] has _O(log&nbsp;n)_ time complexity. O(log log n) time complexity of a loop is considered as O(log log n) if the loop variables is reduced / increased exponentially by a constant amount. // Here c is a constant greater than 1 for (int i = 2; i <=n; i = pow(i, c)) { // some O(1) expressions } //Here fun is sqrt or cuberoot or any other constant root for (int i = n; i > 0; i = fun(i)) { // some O(1) expressions }


时间复杂度分析的一个例子

int fun(int n)
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < n; j += i)
        {
            // Some O(1) task
        }
    }
}

分析:

For i = 1, the inner loop is executed n times.
For i = 2, the inner loop is executed approximately n/2 times.
For i = 3, the inner loop is executed approximately n/3 times.
For i = 4, the inner loop is executed approximately n/4 times.
…………………………………………………….
For i = n, the inner loop is executed approximately n/n times.

因此上述算法的总时间复杂度为(n + n/2 + n/3 +…+ n/n),即为n *(1/1 + 1/2 + 1/3 +…+ 1/n)

级数(1/1 + 1/2 + 1/3 +…+ 1/n)的重要之处在于大约是O(log n),所以上面代码的时间复杂度是O(n·log n)。


引用:

1 2 3