原文:http://www.cnblogs.com/gaochundong/p/complexity_of_algorithms.html
为何要进行算法分析?html
预测算法所需的资源
计算时间(CPU 消耗)
内存空间(RAM 消耗)
通讯时间(带宽消耗)
预测算法的运行时间
在给定输入规模时,所执行的基本操做数量。
或者称为算法复杂度(Algorithm Complexity)
如何衡量算法复杂度?c++
内存(Memory)
时间(Time)
指令的数量(Number of Steps)
特定操做的数量
磁盘访问数量
网络包数量
渐进复杂度(Asymptotic Complexity)
算法的运行时间与什么相关?算法
取决于输入的数据。(例如:若是数据已是排好序的,时间消耗可能会减小。)
取决于输入数据的规模。(例如:6 和 6 * 109)
取决于运行时间的上限。(由于运行时间的上限是对使用者的承诺。)
算法分析的种类:数组
最坏状况(Worst Case):任意输入规模的最大运行时间。(Usually)
平均状况(Average Case):任意输入规模的期待运行时间。(Sometimes)
最佳状况(Best Case):一般最佳状况不会出现。(Bogus)
例如,在一个长度为 n 的列表中顺序搜索指定的值,则网络
最坏状况:n 次比较
平均状况:n/2 次比较
最佳状况:1 次比较
而实际中,咱们通常仅考量算法在最坏状况下的运行状况,也就是对于规模为 n 的任何输入,算法的最长运行时间。这样作的理由是:数据结构
一个算法的最坏状况运行时间是在任何输入下运行时间的一个上界(Upper Bound)。
对于某些算法,最坏状况出现的较为频繁。
大致上看,平均状况一般与最坏状况同样差。
算法分析要保持大局观(Big Idea),其基本思路:dom
忽略掉那些依赖于机器的常量。
关注运行时间的增加趋势。
好比:T(n) = 73n3 + 29n3 + 8888 的趋势就至关于 T(n) = Θ(n3)。函数
渐近记号(Asymptotic Notation)一般有 O、 Θ 和 Ω 记号法。Θ 记号渐进地给出了一个函数的上界和下界,当只有渐近上界时使用 O 记号,当只有渐近下界时使用 Ω 记号。尽管技术上 Θ 记号较为准确,但一般仍然使用 O 记号表示。优化
使用 O 记号法(Big O Notation)表示最坏运行状况的上界。例如,ui
线性复杂度 O(n) 表示每一个元素都要被处理一次。
平方复杂度 O(n2) 表示每一个元素都要被处理 n 次。
Notation Intuition Informal Definition
f is bounded above by g asymptotically
Two definitions :
Number theory:
f is not dominated by g asymptotically
Complexity theory:
f is bounded below by g asymptotically
f is bounded both above and below by g asymptotically
例如:
T(n) = O(n3) 等同于 T(n) ∈ O(n3)
T(n) = Θ(n3) 等同于 T(n) ∈ Θ(n3).
至关于:
T(n) 的渐近增加不快于 n3。
T(n) 的渐近增加与 n3 同样快。
复杂度 标记符号 描述
常量(Constant)
O(1)
操做的数量为常数,与输入的数据的规模无关。
n = 1,000,000 -> 1-2 operations
对数(Logarithmic)
O(log2 n)
操做的数量与输入数据的规模 n 的比例是 log2 (n)。
n = 1,000,000 -> 30 operations
线性(Linear) O(n)
操做的数量与输入数据的规模 n 成正比。
n = 10,000 -> 5000 operations
平方(Quadratic) O(n2)
操做的数量与输入数据的规模 n 的比例为二次平方。
n = 500 -> 250,000 operations
立方(Cubic) O(n3)
操做的数量与输入数据的规模 n 的比例为三次方。
n = 200 -> 8,000,000 operations
指数(Exponential)
O(2n)
O(kn)
O(n!)
指数级的操做,快速的增加。
n = 20 -> 1048576 operations
注1:快速的数学回忆,logab = y 其实就是 ay = b。因此,log24 = 2,由于 22 = 4。一样 log28 = 3,由于 23 = 8。咱们说,log2n 的增加速度要慢于 n,由于当 n = 8 时,log2n = 3。
注2:一般将以 10 为底的对数叫作经常使用对数。为了简便,N 的经常使用对数 log10 N 简写作 lg N,例如 log10 5 记作 lg 5。
注3:一般将以无理数 e 为底的对数叫作天然对数。为了方便,N 的天然对数 loge N 简写作 ln N,例如 loge 3 记作 ln 3。
注4:在算法导论中,采用记号 lg n = log2 n ,也就是以 2 为底的对数。改变一个对数的底只是把对数的值改变了一个常数倍,因此当不在乎这些常数因子时,咱们将常常采用 "lg n"记号,就像使用 O 记号同样。计算机工做者经常认为对数的底取 2 最天然,由于不少算法和数据结构都涉及到对问题进行二分。
而一般时间复杂度与运行时间有一些常见的比例关系:
复杂度 10 20 50 100 1000 10000 100000
O(1)
<1s
<1s
<1s
<1s
<1s
<1s
<1s
O(log2(n))
<1s
<1s
<1s
<1s
<1s
<1s
<1s
O(n)
<1s
<1s
<1s
<1s
<1s
<1s
<1s
O(n*log2(n))
<1s
<1s
<1s
<1s
<1s
<1s
<1s
O(n2)
<1s
<1s
<1s
<1s
<1s
2s
3-4 min
O(n3)
<1s
<1s
<1s
<1s
20s
5 hours
231 days
O(2n)
<1s
<1s
260 days
hangs
hangs
hangs
hangs
O(n!)
<1s
hangs
hangs
hangs
hangs
hangs
hangs
O(nn)
3-4 min
hangs
hangs
hangs
hangs
hangs
hangs
计算代码块的渐进运行时间的方法有以下步骤:
肯定决定算法运行时间的组成步骤。
找到执行该步骤的代码,标记为 1。
查看标记为 1 的代码的下一行代码。若是下一行代码是一个循环,则将标记 1 修改成 1 倍于循环的次数 1 n。若是包含多个嵌套的循环,则将继续计算倍数,例如 1 n * m。
找到标记到的最大的值,就是运行时间的最大值,即算法复杂度描述的上界。
示例代码(1):
复制代码
1 decimal Factorial(int n)
2 {
3 if (n == 0)
4 return 1;
5 else
6 return n * Factorial(n - 1);
7 }
复制代码
阶乘(factorial),给定规模 n,算法基本步骤执行的数量为 n,因此算法复杂度为 O(n)。
示例代码(2):
复制代码
1 int FindMaxElement(int[] array)
2 {
3 int max = array[0];
4 for (int i = 0; i < array.Length; i++)
5 {
6 if (array[i] > max)
7 {
8 max = array[i];
9 }
10 }
11 return max;
12 }
复制代码
这里,n 为数组 array 的大小,则最坏状况下须要比较 n 次以获得最大值,因此算法复杂度为 O(n)。
示例代码(3):
复制代码
1 long FindInversions(int[] array)
2 {
3 long inversions = 0;
4 for (int i = 0; i < array.Length; i++)
5 for (int j = i + 1; j < array.Length; j++)
6 if (array[i] > array[j])
7 inversions++;
8 return inversions;
9 }
复制代码
这里,n 为数组 array 的大小,则基本步骤的执行数量约为 n*(n-1)/2,因此算法复杂度为 O(n2)。
示例代码(4):
复制代码
1 long SumMN(int n, int m)
2 {
3 long sum = 0;
4 for (int x = 0; x < n; x++)
5 for (int y = 0; y < m; y++)
6 sum += x * y;
7 return sum;
8 }
复制代码
给定规模 n 和 m,则基本步骤的执行数量为 n*m,因此算法复杂度为 O(n2)。
示例代码(5):
复制代码
1 decimal Sum3(int n)
2 {
3 decimal sum = 0;
4 for (int a = 0; a < n; a++)
5 for (int b = 0; b < n; b++)
6 for (int c = 0; c < n; c++)
7 sum += a b c;
8 return sum;
9 }
复制代码
这里,给定规模 n,则基本步骤的执行数量约为 nnn ,因此算法复杂度为 O(n3)。
示例代码(6):
复制代码
1 decimal Calculation(int n)
2 {
3 decimal result = 0;
4 for (int i = 0; i < (1 << n); i++)
5 result += i;
6 return result;
7 }
复制代码
这里,给定规模 n,则基本步骤的执行数量为 2n,因此算法复杂度为 O(2n)。
示例代码(7):
斐波那契数列:
Fib(0) = 0
Fib(1) = 1
Fib(n) = Fib(n-1) + Fib(n-2)
F() = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
复制代码
1 int Fibonacci(int n)
2 {
3 if (n <= 1)
4 return n;
5 else
6 return Fibonacci(n - 1) + Fibonacci(n - 2);
7 }
复制代码
这里,给定规模 n,计算 Fib(n) 所需的时间为计算 Fib(n-1) 的时间和计算 Fib(n-2) 的时间的和。
T(n<=1) = O(1)
T(n) = T(n-1) + T(n-2) + O(1)
fib(5) / \ fib(4) fib(3) / \ / \ fib(3) fib(2) fib(2) fib(1) / \ / \ / \
经过使用递归树的结构描述可知算法复杂度为 O(2n)。
示例代码(8):
复制代码
1 int Fibonacci(int n)
2 {
3 if (n <= 1)
4 return n;
5 else
6 {
7 int[] f = new int[n + 1];
8 f[0] = 0;
9 f[1] = 1;
10
11 for (int i = 2; i <= n; i++)
12 {
13 f[i] = f[i - 1] + f[i - 2];
14 }
15
16 return f[n];
17 }
18 }
复制代码
一样是斐波那契数列,咱们使用数组 f 来存储计算结果,这样算法复杂度优化为 O(n)。
示例代码(9):
复制代码
1 int Fibonacci(int n)
2 {
3 if (n <= 1)
4 return n;
5 else
6 {
7 int iter1 = 0;
8 int iter2 = 1;
9 int f = 0;
10
11 for (int i = 2; i <= n; i++)
12 {
13 f = iter1 + iter2;
14 iter1 = iter2;
15 iter2 = f;
16 }
17
18 return f;
19 }
20 }
复制代码
一样是斐波那契数列,因为实际只有前两个计算结果有用,咱们可使用中间变量来存储,这样就不用建立数组以节省空间。一样算法复杂度优化为 O(n)。
示例代码(10):
经过使用矩阵乘方的算法来优化斐波那契数列算法。
复制代码
1 static int Fibonacci(int n)
2 {
3 if (n <= 1)
4 return n;
5
6 int[,] f = { { 1, 1 }, { 1, 0 } };
7 Power(f, n - 1);
8
9 return f[0, 0];
10 }
11
12 static void Power(int[,] f, int n)
13 {
14 if (n <= 1)
15 return;
16
17 int[,] m = { { 1, 1 }, { 1, 0 } };
18
19 Power(f, n / 2);
20 Multiply(f, f);
21
22 if (n % 2 != 0)
23 Multiply(f, m);
24 }
25
26 static void Multiply(int[,] f, int[,] m)
27 {
28 int x = f[0, 0] m[0, 0] + f[0, 1] m[1, 0];
29 int y = f[0, 0] m[0, 1] + f[0, 1] m[1, 1];
30 int z = f[1, 0] m[0, 0] + f[1, 1] m[1, 0];
31 int w = f[1, 0] m[0, 1] + f[1, 1] m[1, 1];
32
33 f[0, 0] = x;
34 f[0, 1] = y;
35 f[1, 0] = z;
36 f[1, 1] = w;
37 }
复制代码
优化以后算法复杂度为O(log2n)。
示例代码(11):
在 C# 中更简洁的代码以下。
复制代码
1 static double Fibonacci(int n)
2 {
3 double sqrt5 = Math.Sqrt(5);
4 double phi = (1 + sqrt5) / 2.0;
5 double fn = (Math.Pow(phi, n) - Math.Pow(1 - phi, n)) / sqrt5;
6 return fn;
7 }
复制代码
示例代码(12):
插入排序的基本操做就是将一个数据插入到已经排好序的有序数据中,从而获得一个新的有序数据。算法适用于少许数据的排序,时间复杂度为 O(n2)。
复制代码 1 private static void InsertionSortInPlace(int[] unsorted) 2 { 3 for (int i = 1; i < unsorted.Length; i++) 4 { 5 if (unsorted[i - 1] > unsorted[i]) 6 { 7 int key = unsorted[i]; 8 int j = i; 9 while (j > 0 && unsorted[j - 1] > key)10 {11 unsorted[j] = unsorted[j - 1];12 j--;13 }14 unsorted[j] = key;15 }16 }17 }复制代码