# 算法导论

算法是为完成某项任务而给出的一串无歧义步骤；计算机科学研究把它视为一种计算过程，对每一个合法输入都能解决某个指定的问题<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup><sup> • </sup><sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。这个词来自花拉子米（al-Khwarizmi，الخوارزميّ）名字的转写，他是穆罕默德·伊本·穆萨·花拉子米（Muhammad ibn Musa al-Khwarizmi），其9世纪关于位值算术与代数的著作把这些思想带到了欧洲<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup>。现代对算法的研究围绕两个问题展开：一个过程是否正确，以及随着输入增大，它需要多少时间和内存<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 为完成某项任务而给出的一串无歧义步骤<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup> |
| 词源 | 源自花拉子米（الخوارزميّ）；“代数”一词来自他著作的标题，位值制数字则被称为阿拉伯数字<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup> |
| 现存最早的例子 | 求最大公约数的欧几里得（Euclid）算法，通常被称为第一个算法；欧几里得约在公元前300年活动于亚历山大城<sup>[3](https://coral.ise.lehigh.edu/~ted/files/talks/Algorithms17.pdf)</sup><sup> • </sup><sup>[4](https://www.cs.umd.edu/class/spring2019/cmsc330/lectures/history.pdf)</sup> |
| 核心设计范式 | 分治、动态规划与贪心技术<sup>[5](https://users.cs.duke.edu/~reif/courses/compsci430/)</sup> |
| 代价度量 | 大O记号：若存在常数 \( a > 0 \) 与 \( n_0 \)，使得对所有 \( n \geq n_0 \) 都有 \( f(n) \leq a \cdot g(n) \)，则 \( f(n) \in O(g(n)) \)<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup> |
| 可处理性的边界 | P类包含可在多项式时间内求解的问题；人们普遍认为NP完全问题在传统计算机上需要指数时间<sup>[6](https://www.sciencedirect.com/topics/social-sciences/approximation-algorithms)</sup> |
| 现代界定 | 今天“算法”一词的绝大部分含义形成于20世纪，数字计算机把算法与执行算法的人分离开来<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup> |

## 问题、实例与数据表示

计算问题规定了一类输入、一类输出以及二者之间的关系；问题实例则是某一个具体的输入。以整数排序问题为例，它接受一个序列 \( A = (A_0, \dots, A_{n-1}) \)，要求给出一个排列，使得对所有 \( i = 1, \dots, n-1 \) 都有 \( A_{i-1} \leq A_i \)。算法是求解该问题每一个实例的过程，通常用伪代码或数学语言描述，而不是用某种具体的编程语言，这样正确性与代价就可以脱离硬件和语言来研究<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。

**表示方式很重要。** 同一任务的算法，会因数据表示方式的不同而可行程度相差极大。花拉子米的位值算术之所以能在欧洲立足，部分原因正在于此：逐位相加 78 + 59 只需反复执行同一个带进位的步骤，而用[罗马数字](https://www.edgepedia.org/zh/roman-numerals)计算 LXXVIII + LIX 则要动用分组、重排、消去和缩写等规则。两者的费力程度大致相当，但位值制的过程要简单得多，因为它依靠的是重复，而不是许多各不相同的步骤<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup>。

## 历史渊源

对算法的研究至少可以追溯到欧几里得，他求两个整数最大公约数的算法被普遍认为是第一个有记载的算法<sup>[3](https://coral.ise.lehigh.edu/~ted/files/talks/Algorithms17.pdf)</sup>。约1200年前写作、常被称为代数之父的花拉子米，描述了通过一系列方程变换求解二次方程的方法<sup>[7](https://www.sthu.org/teaching/lecturenotes/AlgorithmsInIndustry-ss2026.pdf)</sup>。欧洲读者以为他的书讲的是他自己的发明，正因如此，"算法"以他命名，"代数"以他的书名命名，位值制数字则被称为[阿拉伯数字](https://www.edgepedia.org/zh/arabic-numerals)。这些数字本身远早于他，似乎起源于[印度次大陆](https://www.edgepedia.org/zh/indian-subcontinent)；至于他书中的其他内容究竟是他自己的发明、借自其他中东学者、传自印度，还是独立重新发现，则并无共识<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup>。

欧几里得与花拉子米之间的联系，显示出数学知识在不同文化之间的延续与变形：花拉子米的工作为算法推理与代数形式体系奠定了基础，为后来的数学革命搭好了舞台<sup>[8](https://www.seresearch.qmul.ac.uk/content/pce/ediresources/files/History_of_Maths_QMUL_2025.pdf)</sup>。决定性的重新框定发生在20世纪。一旦数字计算机能够在没有人类中介的情况下执行算法，算法本身就成了理论研究的对象，并通过[图灵机](https://www.edgepedia.org/zh/turing-machine)（Turing machine）这类模型被形式化——图灵机是一组有限的指令与内部状态，它定义了“一个过程能进行计算”究竟意味着什么<sup>[1](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)</sup><sup> • </sup><sup>[9](https://bitsavers.computerhistory.org/magazines/Computers_And_Automation/196301.pdf)</sup>。

## 设计范式

大学的标准课程围绕少数几种技术来组织算法设计：分治，把问题拆成更小的子问题；动态规划，由相互重叠的子问题构造解；以及贪心技术，在优化问题中作出局部最优的选择<sup>[5](https://users.cs.duke.edu/~reif/courses/compsci430/)</sup>。这些技术被应用于本领域反复出现的一系列任务类型：

- 排序与选择，既有确定性方法，也有随机化方法<sup>[5](https://users.cs.duke.edu/~reif/courses/compsci430/)</sup>。
- 图算法，包括深度优先搜索与广度优先搜索、迪杰斯特拉算法（Dijkstra's algorithm）这类最短路径算法，以及路径与匹配问题<sup>[5](https://users.cs.duke.edu/~reif/courses/compsci430/)</sup><sup> • </sup><sup>[10](https://nptel.ac.in/courses/106105157)</sup>。
- 用于快速乘法与线性方程组求解的矩阵与代数算法<sup>[5](https://users.cs.duke.edu/~reif/courses/compsci430/)</sup>。
- [数据结构](https://www.edgepedia.org/zh/data-structure)算法，包括搜索树、优先队列与散列<sup>[5](https://users.cs.duke.edu/~reif/courses/compsci430/)</sup>。

通用性是一种设计选择。能对任何带序关系的对象排序的算法，比只能排序整数的算法适用范围更广；但更专门的算法在其较窄的输入类上可以更快，或耗费更少资源<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。

## 正确性

算法最重要的性质是正确性：对每一个实例，它最终都必须终止，并给出解决该问题的输出。由于算法是数学描述，正确性要靠数学证明来确立，通常用到两件工具：不变式与数学归纳法。不变式是算法状态的一种性质，在整个执行过程中始终成立；对插入排序而言，“有序后缀”不变式说的是：第 \( i \) 轮迭代之后，前 \( i \) 个元素已按序排列。证明要表明该不变式在第一轮迭代之前成立，且每一轮迭代都保持它，于是由归纳法可知最终状态是一个正确的解<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。

## 代价分析

**渐近分析。** [时间复杂度](https://www.edgepedia.org/zh/time-complexity)衡量运行时间如何随输入规模增长，空间复杂度以同样的方式衡量内存占用<sup>[11](https://openstax.org/books/introduction-computer-science/pages/3-3-formal-properties-of-algorithms)</sup>。精确的代价取决于硬件和语言，但起主导作用的是算法与问题规模，因此代价用大O记号表示，它隐去了依赖具体实现的常数：当对所有 \( n \geq n_0 \) 都有 \( f(n) \leq a \cdot g(n) \) 时，\( f(n) \in O(g(n)) \)<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。由于这个界必须普遍成立，最坏情况的输入就成了标准；平均情况分析则是在输入分布上取平均<sup>[2](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)</sup>。[渐近分析](https://www.edgepedia.org/zh/asymptotic-analysis)让设计者在写出任何程序之前，就能估计一个方案能否满足资源约束<sup>[12](https://opendsa.cs.vt.edu/ODSA/Books/nyu/csci-ua102/spring-2021/section_5/html/AnalChap.html)</sup>。

**实验分析。** 有了可运行的程序，运行时间就可以直接测量。这种方法对找出性能缺陷很有效——所谓性能缺陷，是指那些仍能给出正确输出、却消耗异常多的计算量或系统资源的代码；性能剖析器把程序在何处耗费资源可视化，常以火焰图呈现<sup>[11](https://openstax.org/books/introduction-computer-science/pages/3-3-formal-properties-of-algorithms)</sup>。

**摊还分析。** 有些操作几乎总是很便宜，偶尔才昂贵，因此对单次操作作最坏情况分析就过于悲观了。[摊还分析](https://www.edgepedia.org/zh/amortized-analysis)确定的是在一串操作上的时间平均代价。它与平均情况分析的区别在于，它对数据分布不作任何假设：它是针对一串操作的最坏情况界，而不是针对单次操作的界。常用的有三种技术：聚合法、带令牌的银行家方法，以及带势函数的物理学家方法。用数组中的一个二进制计数器自增可以说明这一思路：当计数达到 \( n \) 时，至多有 \( 1 + \lfloor \lg n \rfloor \) 个二进制位发生变化，即 \( n \) 的二进制表示的位数<sup>[13](https://www.cs.cornell.edu/courses/cs312/2007fa/lectures/lec20.html)</sup>。

## 界限：复杂性与近似

复杂性类刻画的是问题本身的难度，与任何特定的计算机或算法无关。P类包含可在多项式时间内求解的问题；NP中最难的那些NP完全问题，人们普遍认为在传统计算机上需要 \( O(c^n) \)（\( c > 1 \)）的指数时间，而且NP中没有任何问题已知有多项式时间算法。这种刻画有其实际局限：它对大规模实例才最要紧，它是一种最坏情况的度量，而一个难以处理的问题仍可能由高成功概率的随机化算法，或由近似算法来对付<sup>[6](https://www.sciencedirect.com/topics/social-sciences/approximation-algorithms)</sup>。

近似算法在NP难的优化问题上用精确性换取可处理性，并带有可证明的质量保证。例如，当旅行代价满足三角不等式时，基于最小生成树的算法保证给出的解的代价不超过最优代价的两倍<sup>[6](https://www.sciencedirect.com/topics/social-sciences/approximation-algorithms)</sup>。这套理论也界定了近似所能达到的极限：有些问题至今不知道有任何常数比的近似，还有些问题除非 P = NP，否则根本不可能有这样的近似<sup>[14](https://www.sciencedirect.com/science/article/abs/pii/S0377221704002814)</sup>。

## 作为一门工程学科的算法

如今，算法研究遍及计算机科学的许多分支，而算法工程这一领域研究的是算法知识如何产生、如何得到验证。有一个框架区分了该领域所探究的四类知识：性能知识、敏感性知识、不确定性知识与解释性知识<sup>[15](https://doi.org/10.1145/3769071)</sup>。由于关于算法性能的主张依赖于评测，基准测试的实践就很重要。比较性评测中被记录在案的缺陷包括：挑选有利的工作负载、使用不切实际的威胁模型、省略基线比较；而可复现性要求评测数据与实现能够获取、可以被引用<sup>[15](https://doi.org/10.1145/3769071)</sup>。在强化学习这类领域，由于算法方差、环境随机性以及未报告的超参数，结果很难复现，因此要用恰当的度量与统计做标准化评测，以实现公平比较<sup>[16](https://proceedings.mlr.press/v100/lynnerup20a.html)</sup>。

## References

1. [Algorithms, CS 102 course text, University of Illinois](https://courses.grainger.illinois.edu/cs102/sp2026/text/algorithms.html)
2. [Chapter 1: Problems and algorithms, B16 Algorithms and Data Structures, University of Oxford](https://www.robots.ox.ac.uk/~vedaldi/assets/teach/2023/b16/notes/1-problems-and-algorithms-revision.html)
3. [Algorithms: Why and How, Lehigh University talk notes](https://coral.ise.lehigh.edu/~ted/files/talks/Algorithms17.pdf)
4. [CMSC330 lecture: history of computing, University of Maryland](https://www.cs.umd.edu/class/spring2019/cmsc330/lectures/history.pdf)
5. [CPS 430: Algorithm Paradigms, Duke University](https://users.cs.duke.edu/~reif/courses/compsci430/)
6. [Approximation Algorithms, ScienceDirect Topics overview](https://www.sciencedirect.com/topics/social-sciences/approximation-algorithms)
7. [Algorithms in Industry lecture notes](https://www.sthu.org/teaching/lecturenotes/AlgorithmsInIndustry-ss2026.pdf)
8. [History of Maths, Queen Mary University of London](https://www.seresearch.qmul.ac.uk/content/pce/ediresources/files/History_of_Maths_QMUL_2025.pdf)
9. [Computers and Automation, January 1963, Computer History Museum archive](https://bitsavers.computerhistory.org/magazines/Computers_And_Automation/196301.pdf)
10. [NOC: Fundamental Algorithms: Design and Analysis, IIT Kharagpur, NPTEL](https://nptel.ac.in/courses/106105157)
11. [3.3 Formal Properties of Algorithms, OpenStax Introduction to Computer Science](https://openstax.org/books/introduction-computer-science/pages/3-3-formal-properties-of-algorithms)
12. [Chapter Introduction, Algorithm Analysis, OpenDSA, NYU CSCI-UA 102](https://opendsa.cs.vt.edu/ODSA/Books/nyu/csci-ua102/spring-2021/section_5/html/AnalChap.html)
13. [Lecture 20: Amortized Algorithms, Cornell CS312](https://www.cs.cornell.edu/courses/cs312/2007fa/lectures/lec20.html)
14. [Demange, M., Paschos, V. Th. Polynomial approximation algorithms with performance guarantees: An introduction-by-example, European Journal of Operational Research 165(3):555-568](https://www.sciencedirect.com/science/article/abs/pii/S0377221704002814)
15. [Methodology of Algorithm Engineering, ACM Computing Surveys 58(4), Article 94](https://doi.org/10.1145/3769071)
16. [Lynnerup et al., A Survey on Reproducibility by Evaluating Deep Reinforcement Learning Algorithms on Real-World Robots, PMLR 100:466-489](https://proceedings.mlr.press/v100/lynnerup20a.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Algorithms overview

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/introduction-to-algorithms

English original: https://www.edgepedia.org/introduction-to-algorithms
