![8ecddd0eb1971b2b8a85dc1f7377d70b.png](https://img-blog.csdnimg.cn/img_convert/8ecddd0eb1971b2b8a85dc1f7377d70b.png)
这一节即将介绍算法分析与设计中的一些基本概念,包括算法的定义、算法的正确性证明、时间复杂度、空间复杂度以及相应的数学理论分析基础!
1.算法的定义
![e060fad0e9d00bab595bbfe181e633b3.png](https://img-blog.csdnimg.cn/img_convert/e060fad0e9d00bab595bbfe181e633b3.png)
![15da0ef8ef0879315bf238a8bad72786.png](https://img-blog.csdnimg.cn/img_convert/15da0ef8ef0879315bf238a8bad72786.png)
有意思的是,最早的算法是欧几里得的“求最大公因子算法”!
2.算法的正确性
![cf656eb1f31486c8d253db11f3ffe9b6.png](https://img-blog.csdnimg.cn/img_convert/cf656eb1f31486c8d253db11f3ffe9b6.png)
所以设计了一个算法后,怎么说明它是正确的呢?严格的话需要进行算法的正确性证明。
![6f7452a8acb788db80ade5925bb185e4.png](https://img-blog.csdnimg.cn/img_convert/6f7452a8acb788db80ade5925bb185e4.png)
其实算法的正确性证明是一个很复杂的问题,很多算法的正确性证明特别复杂!通常一个很常用的证明方法叫做:循环不变量方法,这个方法通常用来证明主要结构是循环结构的算法的正确性证明上!
![285fabe9d4e2b77abf9650542ad02122.png](https://img-blog.csdnimg.cn/img_convert/285fabe9d4e2b77abf9650542ad02122.png)
例如下面一个例子:证明插入排序算法的正确性。
![9932224f7ba086c759230171057c8c58.png](https://img-blog.csdnimg.cn/img_convert/9932224f7ba086c759230171057c8c58.png)
循环不变量是什么?
![f06a14e048a4df039aa3c368bdb84772.png](https://img-blog.csdnimg.cn/img_convert/f06a14e048a4df039aa3c368bdb84772.png)
3. 算法的复杂性分析
一般会有时间复杂度和空间复杂度。
![2d788a8a8c5f3ac7a32b61606eb087fa.png](https://img-blog.csdnimg.cn/img_convert/2d788a8a8c5f3ac7a32b61606eb087fa.png)
![a58e8923d61f826237c242e89d21c8c4.png](https://img-blog.csdnimg.cn/img_convert/a58e8923d61f826237c242e89d21c8c4.png)
4. 计算复杂性的阶-同阶(
(1)同阶函数的定义:
![f11a5813955380b694ce041bb9d7c1ac.png](https://img-blog.csdnimg.cn/img_convert/f11a5813955380b694ce041bb9d7c1ac.png)
从定义中可以看到,要证明同阶的关系,就是要找到满足条件的
![7a88fb2efc8f1290203cdf120f0f399b.png](https://img-blog.csdnimg.cn/img_convert/7a88fb2efc8f1290203cdf120f0f399b.png)
![1858d63498dea28188c9576c9e1b433d.png](https://img-blog.csdnimg.cn/img_convert/1858d63498dea28188c9576c9e1b433d.png)
![e42041e744bd5a5493611766a3682062.png](https://img-blog.csdnimg.cn/img_convert/e42041e744bd5a5493611766a3682062.png)
(2)低阶函数的定义:
![7bfc6417b11d3e2ca874d8a4b7afc782.png](https://img-blog.csdnimg.cn/img_convert/7bfc6417b11d3e2ca874d8a4b7afc782.png)
从定义中可以看到,要证明低阶的关系,关键是要要找到满足条件的
![1c6d81f75e3bce5f2f13e4809fd378d3.png](https://img-blog.csdnimg.cn/img_convert/1c6d81f75e3bce5f2f13e4809fd378d3.png)
![666ab8f48885899800a7ff52ac9c6a6a.png](https://img-blog.csdnimg.cn/img_convert/666ab8f48885899800a7ff52ac9c6a6a.png)
(3)高阶函数的定义
![98d61c9f406445a2a81b603e03e67c4d.png](https://img-blog.csdnimg.cn/img_convert/98d61c9f406445a2a81b603e03e67c4d.png)
从定义中可以看到,要证明高阶的关系,关键是要要找到满足条件的
例子1:
![8539bc2c4840101f4230e73c5a7d6981.png](https://img-blog.csdnimg.cn/img_convert/8539bc2c4840101f4230e73c5a7d6981.png)
例子2:
![e15ff1e8bef08886be913cb587b18ae2.png](https://img-blog.csdnimg.cn/img_convert/e15ff1e8bef08886be913cb587b18ae2.png)
(4)严格低阶函数(
![cb99aed9c4c1ea94be5715765d25b8cd.png](https://img-blog.csdnimg.cn/img_convert/cb99aed9c4c1ea94be5715765d25b8cd.png)
要证明严格低阶的关系,只需找到满足恒等式条件的
![0de0daa3cd792a9dcef084c2546ad7c1.png](https://img-blog.csdnimg.cn/img_convert/0de0daa3cd792a9dcef084c2546ad7c1.png)
![b886b88e861e9a9db4f4aa1a9c145a37.png](https://img-blog.csdnimg.cn/img_convert/b886b88e861e9a9db4f4aa1a9c145a37.png)
例3:
![fcb21f2d634e6541e0f7119a3546f1c5.png](https://img-blog.csdnimg.cn/img_convert/fcb21f2d634e6541e0f7119a3546f1c5.png)
(5)严格高阶函数(
![1c7e9217d9373804643e90ed01406c2a.png](https://img-blog.csdnimg.cn/img_convert/1c7e9217d9373804643e90ed01406c2a.png)
由定义可知:
![3bacc593b2bf2d39197de22a150894c0.png](https://img-blog.csdnimg.cn/img_convert/3bacc593b2bf2d39197de22a150894c0.png)
同样的,
![9e54dddb97a9105f3cfc551709dfb0ec.png](https://img-blog.csdnimg.cn/img_convert/9e54dddb97a9105f3cfc551709dfb0ec.png)
![253c0b382cfe0d6bff6443de75d7cab8.png](https://img-blog.csdnimg.cn/img_convert/253c0b382cfe0d6bff6443de75d7cab8.png)
(6)函数阶的性质
可以将函数阶与
![07ca918d42dbcc04232a7eaf676ed6c7.png](https://img-blog.csdnimg.cn/img_convert/07ca918d42dbcc04232a7eaf676ed6c7.png)
![90f8367b1affd7427a826ef84852f0d9.png](https://img-blog.csdnimg.cn/img_convert/90f8367b1affd7427a826ef84852f0d9.png)
另外,有一些特殊的函数是没法比较的,例如:
![d8324ee2710dbec018dc877f638ad94e.png](https://img-blog.csdnimg.cn/img_convert/d8324ee2710dbec018dc877f638ad94e.png)
5. Floor和Ceiling函数
![fb02e949a5d252b2fefcda82850bad8a.png](https://img-blog.csdnimg.cn/img_convert/fb02e949a5d252b2fefcda82850bad8a.png)
它有一下性质:
![4ee57558b83960cd023daebd469dd7b1.png](https://img-blog.csdnimg.cn/img_convert/4ee57558b83960cd023daebd469dd7b1.png)
![7f1e9664661ef88aee17435880778e28.png](https://img-blog.csdnimg.cn/img_convert/7f1e9664661ef88aee17435880778e28.png)
6.和式的估计与界限
![607028f79b6997207faf922af748521c.png](https://img-blog.csdnimg.cn/img_convert/607028f79b6997207faf922af748521c.png)
![dc28bf73f8925f442967e9171f39a76b.png](https://img-blog.csdnimg.cn/img_convert/dc28bf73f8925f442967e9171f39a76b.png)
![909fc4444439387db2899ef5b0b61696.png](https://img-blog.csdnimg.cn/img_convert/909fc4444439387db2899ef5b0b61696.png)
(1)直接求和的界限
![fe389e5ac938ddbb0561725d7f9c533f.png](https://img-blog.csdnimg.cn/img_convert/fe389e5ac938ddbb0561725d7f9c533f.png)
![50528259f9ce3a5513f4a637f050b93c.png](https://img-blog.csdnimg.cn/img_convert/50528259f9ce3a5513f4a637f050b93c.png)
![a765012399cf64b76e817201cfc196d1.png](https://img-blog.csdnimg.cn/img_convert/a765012399cf64b76e817201cfc196d1.png)
![dc88e22e23425dcbb687064d922e8e18.png](https://img-blog.csdnimg.cn/img_convert/dc88e22e23425dcbb687064d922e8e18.png)
![03ab57a5d4034871d863d4f1cb8a2729.png](https://img-blog.csdnimg.cn/img_convert/03ab57a5d4034871d863d4f1cb8a2729.png)
![f94a95bdbdf8fba5586ee2b132b831b9.png](https://img-blog.csdnimg.cn/img_convert/f94a95bdbdf8fba5586ee2b132b831b9.png)
(2)递归方程
![ec102baa92a3faaa0232a26d95372f69.png](https://img-blog.csdnimg.cn/img_convert/ec102baa92a3faaa0232a26d95372f69.png)
求解递归方程的三个主要方法:
![fbb5b91865e75418562db21fe9aa9ea2.png](https://img-blog.csdnimg.cn/img_convert/fbb5b91865e75418562db21fe9aa9ea2.png)
a.迭代法
![929ad9bcb5e92738a29aa0787d27296b.png](https://img-blog.csdnimg.cn/img_convert/929ad9bcb5e92738a29aa0787d27296b.png)
来看下面的例子:
![704abf50b1c9278dc91a6b7a05b1767c.png](https://img-blog.csdnimg.cn/img_convert/704abf50b1c9278dc91a6b7a05b1767c.png)
![f262a5ba7572dbe23ec3e0a031741491.png](https://img-blog.csdnimg.cn/img_convert/f262a5ba7572dbe23ec3e0a031741491.png)
b. 替换法
![6aa95ccf6461dc6f98e0fdeb84797192.png](https://img-blog.csdnimg.cn/img_convert/6aa95ccf6461dc6f98e0fdeb84797192.png)
来看下面的例子:
![b502bccd72c399556d14be21a61ff5bb.png](https://img-blog.csdnimg.cn/img_convert/b502bccd72c399556d14be21a61ff5bb.png)
![951bc0720f56100d0b772cea27a92f51.png](https://img-blog.csdnimg.cn/img_convert/951bc0720f56100d0b772cea27a92f51.png)
还可以“先猜再证”:
![1fbae70a8ef17b59943ea96c33e4e863.png](https://img-blog.csdnimg.cn/img_convert/1fbae70a8ef17b59943ea96c33e4e863.png)
![2a837a27348ed777727eb78f91156d06.png](https://img-blog.csdnimg.cn/img_convert/2a837a27348ed777727eb78f91156d06.png)
![ede8bd76a6e85fc8b6b5781c03771f0a.png](https://img-blog.csdnimg.cn/img_convert/ede8bd76a6e85fc8b6b5781c03771f0a.png)
例如下面的例子:
![3b5cb026671121ce4f6e03d1d8614469.png](https://img-blog.csdnimg.cn/img_convert/3b5cb026671121ce4f6e03d1d8614469.png)
c.Master 定理
![1bb58775acf0481231b131d36d8ce986.png](https://img-blog.csdnimg.cn/img_convert/1bb58775acf0481231b131d36d8ce986.png)
![6ed710e481397451c5472fc9bc94d5bf.png](https://img-blog.csdnimg.cn/img_convert/6ed710e481397451c5472fc9bc94d5bf.png)
直观的理解Master定理:
![f8012e022d65ffa0a3b4f4ec5ea1fcab.png](https://img-blog.csdnimg.cn/img_convert/f8012e022d65ffa0a3b4f4ec5ea1fcab.png)
所以为了应用Master定理,
![e5bf53085656c3e89e8e0292bb2ed8d5.png](https://img-blog.csdnimg.cn/img_convert/e5bf53085656c3e89e8e0292bb2ed8d5.png)
下面来看Master定理的几个例子:
![75034e12ff7b3eca5f5db4581aa066c6.png](https://img-blog.csdnimg.cn/img_convert/75034e12ff7b3eca5f5db4581aa066c6.png)
![f3f5f51f60e1a0336c27b561a30afb28.png](https://img-blog.csdnimg.cn/img_convert/f3f5f51f60e1a0336c27b561a30afb28.png)
![5ebcaea957d6a580a9f0069232485c0e.png](https://img-blog.csdnimg.cn/img_convert/5ebcaea957d6a580a9f0069232485c0e.png)
![ca442e52323bd1cfc96e5a2c93600518.png](https://img-blog.csdnimg.cn/img_convert/ca442e52323bd1cfc96e5a2c93600518.png)
(例4中
小结:至此,本小节讲解了算法分析与设计中的基本概念以及基本的数学知识,包括时间复杂度、空间复杂度,以及递归方程、Master定理等内容,这些知识对后面的算法分析与设计将会很有帮助!
参考资源:
【1】哈工大骆吉洲《算法分析与设计》PPT