绪论
对之后算法需要学习的内容,需要的基础知识,进行大致的讲解和概述
算法
所谓算法,即特定计算模型下,旨在解决特定问题的指令序列
输入 待处理的信息(问题)
输出 经处理的信息(答案)
正确性:的确可以解决指定的问题
确定性:任意算法都可以描述为一个由基本操作完成的序列
可行性:每一基本操作都可以实现,且在常熟时间内完成
有穷性:对于认可输入,经有穷次基本操作,都可以得到输出结果
好算法
正确,健壮,可读,效率
效率:速度尽可能快,存储空间竟可能少
算法分析
两个主要任务=正确性(不变性×单调性) + 复杂度
复杂度分析的主要方法
迭代:级数求和
递归:递归跟踪+递归方程
级数:
- 算数技术:与末项平方同阶
- T(n) = 1+2+3+…+n=n(n+1)/2 = O(n^2)
- 幂方级数:比幂次高出一级
- T(n) = 1^2+2^2+3^2+…+n^2=n(n+1)/2 = O(n^3)
- 几何级数:与末项同阶
- T(n) = a^0+a^1+a^2+…a^n = O(a^n)
- 收敛级数
- 可能未收敛,长度有限
- T(n) = 1+1/2+1/3+…+1/n = O(logn) //调和级数 log1+log2+log3+…logn = O(nlogn) //对数级数
增长速度:
迭代与递归
减而治之:
为求解一个大问题,可以将其划分为两个子问题:其一平凡,另一缩减 分别求解子问题。由子问题的解,得到原问题的解
递归跟踪:
检查每个递归实例,累计所需时间,其总和即算法执行时间
直观形象,仅适用于简明的递归模式
递推方程:
从递推的角度看,求解sum(A,n),需递归求解规模为n-1的su
m(A,n-1)再累加上A[n-1]
递归基:sum(A,0)
递推方程
T(n) = T(n-1) + O(1)
T(0) = O(1)
分而治之:
为求解一个大规模的问题,可以将其划分若干(通常两个)子问题分别求解子问题,由子问题的解,得到原问题的解