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

2.5 时间复杂度(1)

1. 时间复杂度

例题 2.5.1 : 两个 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 阶方阵 <math xmlns="http://www.w3.org/1998/Math/MathML"> A \pmb{A} </math>AA 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> B \pmb{B} </math>BB 相加 <math xmlns="http://www.w3.org/1998/Math/MathML"> C = A + B \pmb{C}=\pmb{A}+\pmb{B} </math>CC=AA+BB 的算法

c 复制代码
#define MAX 20
void matrix_add(int n, int A[MAX][MAX], int B[MAX][MAX], int C[MAX][MAX])
{
    int i, j;
    for(i=0; i<n; i++){                    //语句频度:n+1
        for(j=0; j<n; j++){                //语句频度:n(n+1)
            C[i][j] = A[i][j] + B[i][j];   //语句频度:n*n
        }
    }
}

所有语句的语句频度: <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( n ) = ( n + 1 ) + [ n ( n + 1 ) ] + n 2 = 2 n 2 + 2 n + 1 f(n)=(n+1)+[n(n+1)]+n^2=2n^2+2n+1 </math>f(n)=(n+1)+[n(n+1)]+n2=2n2+2n+1 。

运行时间记作 <math xmlns="http://www.w3.org/1998/Math/MathML"> T ( n ) T(n) </math>T(n) ,当问题规模 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 增大时,算法的执行时间也会增加,根据前面知识(008节:问题规模和语句频度)可知, <math xmlns="http://www.w3.org/1998/Math/MathML"> T ( n ) T(n) </math>T(n) 与 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( n ) f(n) </math>f(n) 有相同的增长率,于是使用大 O 记号:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> T ( n ) = O ( f ( n ) ) T(n) = O(f(n)) </math>T(n)=O(f(n))

则称之为算法的渐进时间复杂度 ,简称时间复杂度(time complexity)。

根据 009 节关于大 O 记号的知识,可知例题 2.5.1 算法的时间复杂度是: <math xmlns="http://www.w3.org/1998/Math/MathML"> T ( n ) = O ( n 2 ) T(n)=O(n^2) </math>T(n)=O(n2) 。

2. 基本语句

要估算时间复杂度,必须找出所有语句的语句频度。但是,根据大 O 记号的性质可知,最关心的应该是对时间复杂度贡献最大的语句,也就是运行次数最多的语句,这样的语句称为基本语句 (或称基本操作)。

基本语句指的是算法中重复执行次数和算法的执行时间成正比的语句,它对算法运行时间的贡献最大。

一般,以算法中最深层循环内的语句作为基本语句。

在例题 2.5.1 中,第 7 行作为基本语句。基本语句的频度为 <math xmlns="http://www.w3.org/1998/Math/MathML"> n 2 n^2 </math>n2 ,于是时间复杂度为 <math xmlns="http://www.w3.org/1998/Math/MathML"> T ( n ) = O ( n 2 ) T(n)=O(n^2) </math>T(n)=O(n2) 。

因此,估算算法的时间复杂度,关键是找出基本语句的语句频度。

例题 2.5.2:分析一下算法的时间复杂度

c 复制代码
void func(int n){
    int i=0, s=0;
    while(s < n){
        i++;
        s = s + i;
    }
}

解:本题中第 4、5 两行都是基本语句,因此关注它们的语句频度。

外层循环次数 i(i=0;i++) s(s=0;s=s+i)
1 1 1
2 2 1+2
3 3 1+2+3
4 4 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 r <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 + 2 + 3 + 4 + ⋯ + r 1+2+3+4+\cdots+r </math>1+2+3+4+⋯+r

因为 s < n 为真才循环,所以:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> 1 + 2 + 3 + 4 + ⋯ + r < n r ( 1 + r ) 2 < n − 1 − 8 n + 1 2 < r < − 1 + 8 n + 1 2 ∵ r > 0 ∴ 0 < r < − 1 + 8 n + 1 2 \begin{split} &1+2+3+4+\cdots+r\lt n \\&\frac{r(1+r)}{2}\lt n \\ &\frac{-1-\sqrt{8n+1}}{2}\lt r\lt\frac{-1+\sqrt{8n+1}}{2} \\&\because r\gt 0 \\&\therefore~0\lt r\lt\frac{-1+\sqrt{8n+1}}{2} \end{split} </math>1+2+3+4+⋯+r<n2r(1+r)<n2−1−8n+1 <r<2−1+8n+1 ∵r>0∴ 0<r<2−1+8n+1

由于外层循环的次数,就是第 4 行语句的执行次数,即语句频度。

故:基本语句的语句频度上界是函数: <math xmlns="http://www.w3.org/1998/Math/MathML"> − 1 + 8 n + 1 2 \frac{-1+\sqrt{8n+1}}{2} </math>2−1+8n+1

所以算法的时间复杂度是: <math xmlns="http://www.w3.org/1998/Math/MathML"> T ( n ) = O ( − 1 + 8 n + 1 2 ) = O ( n ) T(n)=O(\frac{-1+\sqrt{8n+1}}{2})=O(\sqrt{n}) </math>T(n)=O(2−1+8n+1 )=O(n )

注意:本节课后的练习题,要求都使用这里所介绍的解法求解。

本文由mdnice多平台发布

相关推荐
Yvemil71 小时前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
2401_854391081 小时前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
虽千万人 吾往矣1 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
这孩子叫逆2 小时前
Spring Boot项目的创建与使用
java·spring boot·后端
coderWangbuer3 小时前
基于springboot的高校招生系统(含源码+sql+视频导入教程+文档+PPT)
spring boot·后端·sql
攸攸太上3 小时前
JMeter学习
java·后端·学习·jmeter·微服务
Kenny.志3 小时前
2、Spring Boot 3.x 集成 Feign
java·spring boot·后端
sky丶Mamba4 小时前
Spring Boot中获取application.yml中属性的几种方式
java·spring boot·后端
千里码aicood5 小时前
【2025】springboot教学评价管理系统(源码+文档+调试+答疑)
java·spring boot·后端·教学管理系统
程序员-珍5 小时前
使用openapi生成前端请求文件报错 ‘Token “Integer“ does not exist.‘
java·前端·spring boot·后端·restful·个人开发