《数据结构》详解精炼:时间复杂度(3)

第 2 章 算法分析

2.8 时间复杂度(4)

1. 一个较复杂的问题

例题 2.8.1 估算以下算法的时间复杂度:

c 复制代码
void fun(int n){
    int x = 0;
    for(int i = 0, j = 0; i < n; i += j, j++)
        x++;
}

在循环语句中,i, j 都是从 0 开始,j 的步长是 1,但 i 的步长是 j 。通过下表,观察在迭代过程中两个变量的变化(注意,先执行 i += j )。

循环次序 i(i+=j) j(j++)
1 0+0 1
2 0+0+1 2
3 0+0+1+2 3
4 0+0+1+2+3 4
<math xmlns="http://www.w3.org/1998/Math/MathML"> ⋮ \vdots </math>⋮ <math xmlns="http://www.w3.org/1998/Math/MathML"> ⋮ \vdots </math>⋮ <math xmlns="http://www.w3.org/1998/Math/MathML"> ⋮ \vdots </math>⋮
r <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 + 0 + 1 + 2 + 3 + ⋯ + ( r − 1 ) 0+0+1+2+3+\cdots+(r-1) </math>0+0+1+2+3+⋯+(r−1) r

因为 i < n ,所以: <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 + 0 + 1 + 2 + 3 + ⋯ + ( r − 1 ) < n 0+0+1+2+3+\cdots+(r-1)\lt n </math>0+0+1+2+3+⋯+(r−1)<n ,即:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> r ( r − 1 ) 2 < n r 2 − r − 2 n < 0 1 − 1 + 8 n 2 < r < 1 + 1 + 8 n 2 ∵ r > 0 ∴ 0 < r < 1 + 1 + 8 n 2 \begin{split} &\frac{r(r-1)}{2}\lt n \\&r^2-r-2n\lt0 \\&\frac{1-\sqrt{1+8n}}{2}\lt r\lt\frac{1+\sqrt{1+8n}}{2} \\&\because~r\gt0 \\&\therefore~0\lt r\lt\frac{1+\sqrt{1+8n}}{2} \end{split} </math>2r(r−1)<nr2−r−2n<021−1+8n <r<21+1+8n ∵ r>0∴ 0<r<21+1+8n

所以,时间复杂度为 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(\sqrt{n}) </math>O(n ) 。

2. 多项式时间复杂度

至此,已经学习过的时间复杂度包括: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 1 ) , O ( log ⁡ n ) , O ( n ) , O ( n ) , O ( n log ⁡ n ) , O ( n 2 ) , O ( n 3 ) O(1),O(\log{n}),O(\sqrt{n}),O(n),O(n\log{n}),O(n^2),O(n^3) </math>O(1),O(logn),O(n ),O(n),O(nlogn),O(n2),O(n3) ,在 013 节的练习题中还有 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( log ⁡ log ⁡ n ) O(\log\log{n}) </math>O(loglogn) 。这些时间复杂度之间,存在如下定性的大小关系:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> O ( 1 ) < O ( log ⁡ log ⁡ n ) < O ( log ⁡ n ) < O ( n ) < O ( n ) < O ( n log ⁡ n ) < O ( n 2 ) < O ( n 3 ) O(1)\lt O(\log\log{n})\lt O(\log{n})\lt O(\sqrt{n})\lt O(n)\lt O(n\log{n})\lt O(n^2)\lt O(n^3) </math>O(1)<O(loglogn)<O(logn)<O(n )<O(n)<O(nlogn)<O(n2)<O(n3)

其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( log ⁡ log ⁡ n ) , O ( log ⁡ n ) , O ( n ) , O ( n ) , O ( n log ⁡ n ) , O ( n 2 ) , O ( n 3 ) O(\log\log{n}), O(\log{n}), O(\sqrt{n}), O(n), O(n\log{n}), O(n^2), O(n^3) </math>O(loglogn),O(logn),O(n ),O(n),O(nlogn),O(n2),O(n3) 等称为多项式时间复杂度 (polynomial time complexity),可以统一表示为 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( f ( n ) ) O(f(n)) </math>O(f(n)) ,且 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( n ) f(n) </math>f(n) 为多项式。

在算法复杂度理论中,多项式级的运行时间成本,往往被认为是可接受的,或者是可忍受的。某问题若可以用多项式复杂度的算法求解,则称该问题是可有效求解的,或者易解的。这样的问题也称为 P 问题。

除了 P 问题之外,也就是解决问题的算法的复杂度是多项式复杂度,除此之外,还有另外一类问题,只能用指数时间复杂度的算法求解,称为 NP 问题,或者称为难解问题。

3. 指数时间复杂度 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 2 n ) O(2^n) </math>O(2n)

指数时间复杂度不是本课程重点,但是,考虑到学习本课的同学,将来的学习、工作或者研究过程中,一定不会仅仅局限于教材或者所谓考研大纲的知识范围,那么,也对此做个简单介绍。

例题 2.8.3 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 是非负整数,计算 <math xmlns="http://www.w3.org/1998/Math/MathML"> 2 n 2^n </math>2n ,其算法如下(注意,这里的算法是此计算的一种,蛮力迭代,并不是最好的算法,此处仅仅是以此为例说明指数时间复杂度)。

c 复制代码
int power(int n){
    int pow = 1;
    while(0 < n--){
        pow <<= 1;  // pow = pow * 2
    }
    return  pow;
}

很显然,第 4 行是基本语句,语句频度是 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n ,所以此算法的时间复杂度为 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n) 。

如果将输入指数 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 用二进制位数 <math xmlns="http://www.w3.org/1998/Math/MathML"> r = 1 + ⌊ log ⁡ n ⌋ r = 1+\lfloor\log{n}\rfloor </math>r=1+⌊logn⌋ 作为输入规模,则时间复杂度为 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 2 r ) O(2^r) </math>O(2r) 。

把时间复杂度可以表示为 <math xmlns="http://www.w3.org/1998/Math/MathML"> T ( n ) = O ( a n ) , ( a > 1 ) T(n)=O(a^n),(a\gt1) </math>T(n)=O(an),(a>1) 的,均是指数时间复杂度(exponential time complexity)。

一般认为,指数时间复杂度的算法无法真正应用于实际问题中,这类算法不是有效的算法。

本文由mdnice多平台发布

相关推荐
无限大62 小时前
数字生存05:在AI时代,如何保持学习能力,不断成长
程序员
无限大62 小时前
职场逻辑05:5步高效管理时间,让你的工作事半功倍
程序员
无限大62 小时前
AI实战05:设计岗专属工作流|用AI打造爆款设计
程序员
Mac的实验室3 小时前
(2026年最新)解决谷歌账号注册设备扫码短信发送失败无法验证难题(100%通过无需扫码验证)
android·google·程序员
程序员鱼皮4 小时前
AI 时代,满分的程序员简历是怎么样的?附简历模板
ai·程序员·编程·求职·简历
我就是马云飞5 小时前
大专毕业两年,我如何进入大厂,并逆袭八年的技术与认知成长
前端·程序员·全栈
我就是马云飞10 小时前
停更5年后,我为什么重新开始写技术内容了
android·前端·程序员
SimonKing11 小时前
免费!不限量!用opencode接入英伟达(NVIDIA)大模型,轻松打造你的 AI 编程助手
java·后端·程序员
dtsola11 小时前
小遥搜索v1.7.0版本更新【飞书文档+知识库支持】
程序员·飞书·dify·ai智能体·独立开发者·vibecoding·一人公司
EnoYao11 小时前
把你们开发扒个底朝天 Skill
前端·后端·程序员