第一章 复杂度分析
-
什么是算法?
算法是用于解决特定问题的一系列的执行步骤。
// 计算a和b的和 public static int plus(int a, int b) { return a + b; } // 计算1+2+3+···+n的和 public static int sum(int n) { int result = 0; for (int i = 1; i <= n; i++) result += i; return result; } public static int sum(int n) { return n * (n + 1) / 2; }
- 注意: 使用不同的算法解决同一个问题,效率可能相差非常大。
比如求第n个斐波那契数列(Fibonacci number)
// 方法一:递归调用自身,时间复杂度较高-> O(2^n) public static int fib(int n) { if (n <= 1) return n; else return fib(n - 1) + fib(n - 2); } // 方法二:利用for循环,求第n个斐波那契数,就要进行n-1次前面两数相加,时间复杂度低->O(n) public static int fib(int n) { if (n <= 1) return n; int fir = 0, sec = 1; for (int i = 0; i < n - 1; i++) { int sum = fir + sec; fir = sec; sec = sum; } return sec; } // 方法三:斐波那契数列的线性代数方程解法 -- 特征方程-> O(1) public static int fib(int n) { double c = Math.sqrt(5); return (int)((Math.pow((1 + c) / 2, n) - Math.pow((1 - c) / 2, n)) / c); }
-
如何评价一个算法的好坏?
- 如果单从执行效率上进行评估,可能会想到这么一种方案:比较不同算法对同一组输入的执行处理时间(也叫事后统计法)。该方案的执行时间严重依赖硬件以及运行时各种不确定的环境因素;必须编写相应的测试代码;测试数据的选择欠缺一定的公正性。
- 一般从以下维度来评估算法的优劣:
- 正确性
- 可读性
- 健壮性(对不合理输入的反应能力和处理能力)
时间复杂度(time complexity):估算程序指令的执行次数(执行时间)
空间复杂度(space complexity):估算所需占用的存储空间
-
粗略计算时间复杂度
public static void test1(int n) { /*1 + 1 + 4 + 4 + 4*/-> O(1) if (n > 10) { // 1 System.out.println("n > 10"); } else if (n > 5) { System.out.println("n > 5"); } else { System.out.println("n <= 5"); } // 1 + 4 + 4 + 4 for (int i = 0; i < 4; i++) { System.out.println("test"); } } public static void test2(int n) { /*1 + n + n + n*/-> O(n) for (int i = 0; i < n; i++) { System.out.println("test"); } } public static void test3(int n) { /*1 + n + n + n * (1 + n + n + n)*/-> O(n^2) for (int i = 0; i < n; i++) { // 1 + n + n for (int j = 0; j < n; j++) { // n * (1 + n + n + n) System.out.println("test"); } } } public static void test4(int n) { /*1 + n + n + n * (1 + 15 + 15 + 15)*/-> O(n) for (int i = 0; i < n; i++) { // 1 + n + n for (int j = 0; j < 15; j++) { // n * (1 + 15 + 15 + 15) System.out.println("test"); } } } public static void test5(int n) { /*log2^n + log2^n*/-> O(logn) while ((n = n / 2) > 0) { System.out.println("test"); } } public static void test6(int n) { /*log5^n + log5^n*/-> O(logn) while ((n = n / 5) > 0) { System.out.println("test"); } } public static void test7(int n) { /*1 + log2^n + log2^n + log2^n * (1 + n + n + n)*/-> O(nlogn) for (int i = 1; i < n; i = i * 2) { // 1 + log2^n + log2^n for (int j = 0; j < n; j++) { // log2^n * (1 + n + n + n) System.out.println("test"); } } }
-
大O表示法(Big O)
- 一般用大O表示法来描述复杂度,它表示的是数据规模n对应的复杂度
- 忽略常数、系数、低阶
- 9 >> O(1)
- 2n + 3 >> O(n)
- n^2 + 2n + 6 >> O(n^2)
- 4n^3 + 3n^2 + 22n + 100 >> O(n^3)
- 对数阶 一般省略底数,统称为logn
- 注意:大O表示法仅仅是一种粗略的分析模型,是一种估算,能帮助我们短时间内了解一个算法的执行效率。
-
常见的复杂度
执行次数 复杂度 非正式术语 12 O(1) 常数阶 4logn + 25 O(logn) 对数阶 2n + 3 O(n) 线性阶 3n + 2nlogn + 15 O(nlogn) 线性对数阶 4n^2 + 2n + 6 O(n^2) 平方阶 4n^3 + 3n^2 + 22n + 100 O(n^3) 立方阶 2^n O(2^n) 指数阶 O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)
-
算法的优化方向
- 用尽量少的存储空间
- 用尽量少的执行步骤(执行时间)
- 根据情况,可以选择空间换时间,也可以选择时间换空间
-
多个数据规模的情况
public static void test(int m, int n) { // 时间复杂度:O(m + n) for (int i = 0; i < m; i++) System.out.println("test1"); for (int i = 0; i < n; i++) System.out.println("test2"); }