“计算复杂度”的版本间的差异
第8行: | 第8行: | ||
− | + | 在'''计算机科学 computer science'''中,一个'''算法 algorithm'''的'''计算复杂度'''或简单的'''复杂度'''就是运行这个算法所需要的资源量,特别是'''时间'''(CPU占用时间)和'''空间'''(内存占用空间)需求。 | |
− | |||
− | |||
− | |||
− | 在''' | ||
第20行: | 第16行: | ||
As the amount of resources required to run an algorithm generally varies with the size of the input, the complexity is typically expressed as a function , where is the size of the input and is either the worst-case complexity (the maximum of the amount of resources that are needed over all inputs of size ) or the average-case complexity (the average of the amount of resources over all inputs of size ). Time complexity is generally expressed as the number of required elementary operations on an input of size , where elementary operations are assumed to take a constant amount of time on a given computer and change only by a constant factor when run on a different computer. Space complexity is generally expressed as the amount of memory required by an algorithm on an input of size . | As the amount of resources required to run an algorithm generally varies with the size of the input, the complexity is typically expressed as a function , where is the size of the input and is either the worst-case complexity (the maximum of the amount of resources that are needed over all inputs of size ) or the average-case complexity (the average of the amount of resources over all inputs of size ). Time complexity is generally expressed as the number of required elementary operations on an input of size , where elementary operations are assumed to take a constant amount of time on a given computer and change only by a constant factor when run on a different computer. Space complexity is generally expressed as the amount of memory required by an algorithm on an input of size . | ||
− | 由于运行一个算法所需的资源量通常随输入规模的大小而变化,因此复杂度通常用函数{{math| ''f''(''n'')}}表示,其中{{math|''n''}}是输入量的大小,{{math|''f''(''n'')}}是''' | + | 由于运行一个算法所需的资源量通常随输入规模的大小而变化,因此复杂度通常用函数{{math| ''f''(''n'')}}表示,其中{{math|''n''}}是输入量的大小,{{math|''f''(''n'')}}是'''最坏情况复杂度 worst-case complexity'''(输入大小为 {{math|''n''}}时所需的资源量的最大值) ,或是'''平均情况复杂度 average-case complexity'''(资源总量相对于{{math|''n''}}的所有占用的平均值)。'''时间复杂度 Time complexity'''通常表示为对一个输入值长度所需基本操作(通常是加法操作或者乘法操作)的数量。我们假设基本操作在一台计算机上只占用一个不变的时间量(比如1纳秒),而在另一台计算机上运行时,只根据一个常量因子进行改变(比如k*1纳秒)。'''空间复杂度 space complexity'''通常表示为算法对一个输入值长度所需的内存量。 |
第28行: | 第24行: | ||
The study of the complexity of explicitly given algorithms is called analysis of algorithms, while the study of the complexity of problems is called computational complexity theory. Both areas are highly related, as the complexity of an algorithm is always an upper bound on the complexity of the problem solved by this algorithm. | The study of the complexity of explicitly given algorithms is called analysis of algorithms, while the study of the complexity of problems is called computational complexity theory. Both areas are highly related, as the complexity of an algorithm is always an upper bound on the complexity of the problem solved by this algorithm. | ||
− | 对一个有明确定义的算法的复杂度进行的研究叫做''' | + | 对一个有明确定义的算法的复杂度进行的研究叫做'''算法分析 analysis of algorithm''',而对'''问题 problem'''的复杂度研究叫做'''计算复杂度理论 computation complexity theory'''分析。举个例子,怎样把一串数字从小到大进行排序,是一个问题(problem);而针对这一问题,我们有多种明确定义的算法,如选择排序,归并排序等。假设我们有<math>n</math>个数字,那么排序问题的复杂度就是<math>nlogn</math>,而选择排序的复杂度是<math>n^2</math>,归并排序的复杂度是<math>6nlogn</math>。 |
− | 这两个领域是高度相关的,因为算法的复杂度一直是该算法所求解问题复杂度的''' | + | 这两个领域是高度相关的,因为算法的复杂度一直是该算法所求解问题复杂度的'''上限 upper bound'''。 |
第47行: | 第43行: | ||
The usual units of time (seconds, minutes etc.) are not used in complexity theory because they are too dependent on the choice of a specific computer and on the evolution of technology. For instance, a computer today can execute an algorithm significantly faster than a computer from the 1960s; however, this is not an intrinsic feature of the algorithm but rather a consequence of technological advances in computer hardware. Complexity theory seeks to quantify the intrinsic time requirements of algorithms, that is, the basic time constraints an algorithm would place on any computer. This is achieved by counting the number of elementary operations that are executed during the computation. These operations are assumed to take constant time (that is, not affected by the size of the input) on a given machine, and are often called steps. | The usual units of time (seconds, minutes etc.) are not used in complexity theory because they are too dependent on the choice of a specific computer and on the evolution of technology. For instance, a computer today can execute an algorithm significantly faster than a computer from the 1960s; however, this is not an intrinsic feature of the algorithm but rather a consequence of technological advances in computer hardware. Complexity theory seeks to quantify the intrinsic time requirements of algorithms, that is, the basic time constraints an algorithm would place on any computer. This is achieved by counting the number of elementary operations that are executed during the computation. These operations are assumed to take constant time (that is, not affected by the size of the input) on a given machine, and are often called steps. | ||
− | 在''' | + | 在'''复杂度理论 complexity theory'''中不使用通常的时间单位(秒、分等),因为它们过于依赖于特定计算机的选择和技术的演变。例如,今天的计算机执行算法的速度明显快于20世纪60年代的计算机; 然而,这不是算法的固有特征,而是计算机硬件技术进步的结果。复杂度理论旨在量化算法的内在时间需求,也就是算法对任何计算机的基本时间约束。这是通过统计在计算过程中执行的基本操作数量来实现的。这些基本操作假定在给定的机器上占用常量时间(即不受输入大小的影响) ,通常被称为步骤。 |
===空间=== | ===空间=== | ||
第56行: | 第52行: | ||
Another important resource is the size of computer memory that is needed for running algorithms. | Another important resource is the size of computer memory that is needed for running algorithms. | ||
− | 另一个重要的资源是运行算法所需的''' | + | 另一个重要的资源是运行算法所需的'''计算机内存 computer memory'''大小。 |
===其他=== | ===其他=== | ||
第64行: | 第60行: | ||
The number of arithmetic operations is another resource that is commonly used. In this case, one talks of arithmetic complexity. If one knows an upper bound on the size of the binary representation of the numbers that occur during a computation, the time complexity is generally the product of the arithmetic complexity by a constant factor. | The number of arithmetic operations is another resource that is commonly used. In this case, one talks of arithmetic complexity. If one knows an upper bound on the size of the binary representation of the numbers that occur during a computation, the time complexity is generally the product of the arithmetic complexity by a constant factor. | ||
− | ''' | + | '''算术运算 arithmetic operations'''的数量是另一种常用的资源。在这种情况下,人们会谈到'''算术复杂度'''。如果已知一个计算过程中出现的数的'''二进制表示 binary representation'''长度的'''上限 upper bound''',时间复杂度通常是该算术复杂度乘以一个常数因子。 |
第72行: | 第68行: | ||
For many algorithms the size of the integers that are used during a computation is not bounded, and it is not realistic to consider that arithmetic operations take a constant time. Therefore, the time complexity, generally called bit complexity in this context, may be much larger than the arithmetic complexity. For example, the arithmetic complexity of the computation of the determinant of a integer matrix is <math>O(n^3)</math> for the usual algorithms (Gaussian elimination). The bit complexity of the same algorithms is exponential in , because the size of the coefficients may grow exponentially during the computation. On the other hand, if these algorithms are coupled with multi-modular arithmetic, the bit complexity may be reduced to soft O notation|. | For many algorithms the size of the integers that are used during a computation is not bounded, and it is not realistic to consider that arithmetic operations take a constant time. Therefore, the time complexity, generally called bit complexity in this context, may be much larger than the arithmetic complexity. For example, the arithmetic complexity of the computation of the determinant of a integer matrix is <math>O(n^3)</math> for the usual algorithms (Gaussian elimination). The bit complexity of the same algorithms is exponential in , because the size of the coefficients may grow exponentially during the computation. On the other hand, if these algorithms are coupled with multi-modular arithmetic, the bit complexity may be reduced to soft O notation|. | ||
− | 对于许多算法,在计算过程中使用的整数大小是没有界限的,并且考虑算术运算占用一个常量时间是不现实的。因此,时间复杂度,在此上下文中通常称为 ''' | + | 对于许多算法,在计算过程中使用的整数大小是没有界限的,并且考虑算术运算占用一个常量时间是不现实的。因此,时间复杂度,在此上下文中通常称为 '''位复杂度 bit complexity''',可能远远大于算术复杂度。例如,根据通常的算法 '''高斯消去法 Gaussian elimination''' 计算一个{{math|''n''×''n''}} 整数矩阵行列式 的算术复杂度是<math>O(n^3)</math>。因为系数的大小可能在计算过程中呈指数增长,相同算法的位复杂度是指数级的。另一方面,如果这些算法与多模运算相结合,位复杂度可以降低到[[soft O notation|{{math|''O''<sup>~</sup>(''n''<sup>4</sup>)}}]]。 |
第80行: | 第76行: | ||
In sorting and searching, the resource that is generally considered is the number of entries comparisons. This is generally a good measure of the time complexity if data are suitably organized. | In sorting and searching, the resource that is generally considered is the number of entries comparisons. This is generally a good measure of the time complexity if data are suitably organized. | ||
− | 在''' | + | 在'''排序 sorting'''和'''搜索 searching'''中,通常考虑的资源是需要做几次''比较大小''。如果数据组织得当,这通常是一个很好的时间复杂度测量方法。 |
==复杂度:输入规模的函数== | ==复杂度:输入规模的函数== | ||
第95行: | 第91行: | ||
It is impossible to count the number of steps of an algorithm on all possible inputs. As the complexity generally increases with the size of the input, the complexity is typically expressed as a function of the size (in bits) of the input, and therefore, the complexity is a function of . However, the complexity of an algorithm may vary dramatically for different inputs of the same size. Therefore, several complexity functions are commonly used. | It is impossible to count the number of steps of an algorithm on all possible inputs. As the complexity generally increases with the size of the input, the complexity is typically expressed as a function of the size (in bits) of the input, and therefore, the complexity is a function of . However, the complexity of an algorithm may vary dramatically for different inputs of the same size. Therefore, several complexity functions are commonly used. | ||
− | 计算一个算法对于所有可能输入的所需要的步骤数是不可能的。由于复杂度通常随着输入的规模而增加,复杂度通常表示为输入值 {{math|''n''}} 长度(以''' | + | 计算一个算法对于所有可能输入的所需要的步骤数是不可能的。由于复杂度通常随着输入的规模而增加,复杂度通常表示为输入值 {{math|''n''}} 长度(以'''比特 bit'''为单位)的函数。因此,复杂度是一个关于 {{math|''n''}} 的函数。然而,对于同样长度的不同输入,算法的复杂度可能会大不相同。因此,有多种不同的复杂度函数被广泛使用。 |
The [[worst-case complexity]] is the maximum of the complexity over all inputs of size {{mvar|n}}, and the [[average-case complexity]] is the average of the complexity over all inputs of size {{mvar|n}} (this makes sense, as the number of possible inputs of a given size is finite). Generally, when "complexity" is used without being further specified, this is the worst-case time complexity that is considered. | The [[worst-case complexity]] is the maximum of the complexity over all inputs of size {{mvar|n}}, and the [[average-case complexity]] is the average of the complexity over all inputs of size {{mvar|n}} (this makes sense, as the number of possible inputs of a given size is finite). Generally, when "complexity" is used without being further specified, this is the worst-case time complexity that is considered. | ||
第101行: | 第97行: | ||
The worst-case complexity is the maximum of the complexity over all inputs of size , and the average-case complexity is the average of the complexity over all inputs of size (this makes sense, as the number of possible inputs of a given size is finite). Generally, when "complexity" is used without being further specified, this is the worst-case time complexity that is considered. | The worst-case complexity is the maximum of the complexity over all inputs of size , and the average-case complexity is the average of the complexity over all inputs of size (this makes sense, as the number of possible inputs of a given size is finite). Generally, when "complexity" is used without being further specified, this is the worst-case time complexity that is considered. | ||
− | ''' | + | '''最坏情况复杂度 worst-case complexity'''是对所有输入{{mvar|n}}长度中的最大复杂度,'''平均情况复杂度 average-case complexity'''是对所有输入{{mvar|n}}长度中的平均复杂度。一般来说,如果使用“复杂度”一词且不进行进一步说明 ,即考虑最坏情况时间复杂度。 |
==渐近复杂度== | ==渐近复杂度== | ||
第114行: | 第110行: | ||
For these reasons, one generally focuses on the behavior of the complexity for large , that is on its asymptotic behavior when tends to the infinity. Therefore, the complexity is generally expressed by using big O notation. | For these reasons, one generally focuses on the behavior of the complexity for large , that is on its asymptotic behavior when tends to the infinity. Therefore, the complexity is generally expressed by using big O notation. | ||
− | 由于这些原因,人们通常把注意力集中在大{{mvar|n}}的复杂度上,即输入规模趋于无穷大的''' | + | 由于这些原因,人们通常把注意力集中在大{{mvar|n}}的复杂度上,即输入规模趋于无穷大的'''渐近行为 asymptotic behavior'''上。因此,复杂度通常用'''大O符号 big O notation'''来表示。 |
第122行: | 第118行: | ||
For example, the usual algorithm for integer multiplication has a complexity of <math>O(n^2),</math> this means that there is a constant <math>c_u</math> such that the multiplication of two integers of at most digits may be done in a time less than <math>c_un^2.</math> This bound is sharp in the sense that the worst-case complexity and the average-case complexity are <math>\Omega(n^2),</math> which means that there is a constant <math>c_l</math> such that these complexities are larger than <math>c_ln^2.</math> The radix does not appear in these complexity, as changing of radix changes only the constants <math>c_u</math> and <math>c_l.</math> | For example, the usual algorithm for integer multiplication has a complexity of <math>O(n^2),</math> this means that there is a constant <math>c_u</math> such that the multiplication of two integers of at most digits may be done in a time less than <math>c_un^2.</math> This bound is sharp in the sense that the worst-case complexity and the average-case complexity are <math>\Omega(n^2),</math> which means that there is a constant <math>c_l</math> such that these complexities are larger than <math>c_ln^2.</math> The radix does not appear in these complexity, as changing of radix changes only the constants <math>c_u</math> and <math>c_l.</math> | ||
− | 例如,通常整数''' | + | 例如,通常整数'''乘法 multiplication'''的复杂度是<math>O(n^2),</math>,这意味着存在一个常数<math>c_u</math>,使得两个最多{{mvar|n}}位的整数乘法可以在小于<math>c_un^2</math>的时间内完成。这个界限是尖锐的,因为最坏情况复杂度和平均情况复杂度是<math>\Omega(n^2),</math>,意味着存在一个常数<math>c_l</math>,使得这些复杂度比<math>c_ln^2</math>大。 |
==计算模型== | ==计算模型== | ||
第129行: | 第125行: | ||
The evaluation of the complexity relies on the choice of a model of computation, which consists in defining the basic operations that are done in a unit of time. When the model of computation is not explicitly specified, this is generally meant as being multitape Turing machine. | The evaluation of the complexity relies on the choice of a model of computation, which consists in defining the basic operations that are done in a unit of time. When the model of computation is not explicitly specified, this is generally meant as being multitape Turing machine. | ||
− | 复杂度的评估依赖于''' | + | 复杂度的评估依赖于'''计算模型 model of computation'''的选择,包括定义在一个时间单位内完成的基本操作。当没有明确指定时,默认使用的计算模型是'''多带图灵机 multitape Turing machine'''。 |
===确定性模型=== | ===确定性模型=== | ||
第136行: | 第132行: | ||
A deterministic model of computation is a model of computation such that the successive states of the machine and the operations to be performed are completely determined by the preceding state. Historically, the first deterministic models were recursive functions, lambda calculus, and Turing machines. The model of Random access machines (also called RAM-machines) is also widely used, as a closer counterpart to real computers. | A deterministic model of computation is a model of computation such that the successive states of the machine and the operations to be performed are completely determined by the preceding state. Historically, the first deterministic models were recursive functions, lambda calculus, and Turing machines. The model of Random access machines (also called RAM-machines) is also widely used, as a closer counterpart to real computers. | ||
− | 一个''' | + | 一个'''确定性模型 deterministic model '''的计算是一种机器的后续状态和要执行的操作完全由前面的状态决定的计算模型。历史上,最早的确定性模型是'''递归函数 recursive functions'''、 '''lambda 演算 lambda calculus'''和'''图灵机 Turing machines'''。'''随机存取机器 Random access machine '''(也称 RAM-machines)的模型也被广泛使用,更接近真实的'''计算机 computer'''。 |
When the model of computation is not specified, it is generally assumed to be a [[multitape Turing machine]]. For most algorithms, the time complexity is the same on multitape Turing machines as on RAM-machines, although some care may be needed in how data is stored in memory to get this equivalence. | When the model of computation is not specified, it is generally assumed to be a [[multitape Turing machine]]. For most algorithms, the time complexity is the same on multitape Turing machines as on RAM-machines, although some care may be needed in how data is stored in memory to get this equivalence. | ||
第142行: | 第138行: | ||
When the model of computation is not specified, it is generally assumed to be a multitape Turing machine. For most algorithms, the time complexity is the same on multitape Turing machines as on RAM-machines, although some care may be needed in how data is stored in memory to get this equivalence. | When the model of computation is not specified, it is generally assumed to be a multitape Turing machine. For most algorithms, the time complexity is the same on multitape Turing machines as on RAM-machines, although some care may be needed in how data is stored in memory to get this equivalence. | ||
− | 当计算模型没有被指定时,通常假设它是一个''' | + | 当计算模型没有被指定时,通常假设它是一个'''多带图灵机 multitape Turing machine'''。对于大多数算法,在多带图灵机上的时间复杂度与在RAM-machines上的相同,尽管可能需要注意如何将数据存储在内存中,以确保这种等价性. |
===非确定性计算=== | ===非确定性计算=== | ||
第149行: | 第145行: | ||
In a non-deterministic model of computation, such as non-deterministic Turing machines, some choices may be done at some steps of the computation. In complexity theory, one considers all possible choices simultaneously, and the non-deterministic time complexity is the time needed, when the best choices are always done. In other words, one considers that the computation is done simultaneously on as many (identical) processors as needed, and the non-deterministic computation time is the time spent by the first processor that finishes the computation. This parallelism is partly amenable to quantum computing via superposed entangled states in running specific quantum algorithms, like e.g. Shor's factorization of yet only small integers (: 21 = 3 × 7). | In a non-deterministic model of computation, such as non-deterministic Turing machines, some choices may be done at some steps of the computation. In complexity theory, one considers all possible choices simultaneously, and the non-deterministic time complexity is the time needed, when the best choices are always done. In other words, one considers that the computation is done simultaneously on as many (identical) processors as needed, and the non-deterministic computation time is the time spent by the first processor that finishes the computation. This parallelism is partly amenable to quantum computing via superposed entangled states in running specific quantum algorithms, like e.g. Shor's factorization of yet only small integers (: 21 = 3 × 7). | ||
− | 在''' | + | 在'''非确定性计算模型 non-deterministic model of computation'''中,例如'''不确定的图灵机 non-deterministic Turing machine''',在计算的某些步骤中可能会进行一些选择。在复杂度理论中,非确定性时间复杂度即为,同时考虑所有可能的选择且做出最佳选择时所需的时间。换句话说,我们认为计算是多个(相同的)处理器上同时进行的,而不确定计算时间是第一个完成计算的处理器所花费的时间。这种并行性在一定程度上可以通过运行特定'''量子算法 quantum algorithm'''的叠加'''纠缠态 entangled state'''来实现'''量子计算 quantum computing'''。例如小整数上的'''肖尔因式分解 Shor's factorization'''。 |
Even when such a computation model is not realistic yet, it has theoretical importance, mostly related to the [[P = NP]] problem, which questions the identity of the complexity classes formed by taking "polynomial time" and "non-deterministic polynomial time" as least upper bounds. Simulating an NP-algorithm on a deterministic computer usually takes "exponential time". A problem is in the complexity class [[NP (complexity)|NP]], if it may be solved in [[polynomial time]] on a non-deterministic machine. A problem is [[NP-complete]] if, roughly speaking, it is in NP and is not easier than any other NP problem. Many [[combinatorics|combinatorial]] problems, such as the [[Knapsack problem]], the [[travelling salesman problem]], and the [[Boolean satisfiability problem]] are NP-complete. For all these problems, the best known algorithm has exponential complexity. If any one of these problems could be solved in polynomial time on a deterministic machine, then all NP problems could also be solved in polynomial time, and one would have P = NP. {{As of|2017}} it is generally conjectured that {{nowrap|P ≠ NP,}} with the practical implication that the worst cases of NP problems are intrinsically difficult to solve, i.e., take longer than any reasonable time span (decades!) for interesting lengths of input. | Even when such a computation model is not realistic yet, it has theoretical importance, mostly related to the [[P = NP]] problem, which questions the identity of the complexity classes formed by taking "polynomial time" and "non-deterministic polynomial time" as least upper bounds. Simulating an NP-algorithm on a deterministic computer usually takes "exponential time". A problem is in the complexity class [[NP (complexity)|NP]], if it may be solved in [[polynomial time]] on a non-deterministic machine. A problem is [[NP-complete]] if, roughly speaking, it is in NP and is not easier than any other NP problem. Many [[combinatorics|combinatorial]] problems, such as the [[Knapsack problem]], the [[travelling salesman problem]], and the [[Boolean satisfiability problem]] are NP-complete. For all these problems, the best known algorithm has exponential complexity. If any one of these problems could be solved in polynomial time on a deterministic machine, then all NP problems could also be solved in polynomial time, and one would have P = NP. {{As of|2017}} it is generally conjectured that {{nowrap|P ≠ NP,}} with the practical implication that the worst cases of NP problems are intrinsically difficult to solve, i.e., take longer than any reasonable time span (decades!) for interesting lengths of input. | ||
第155行: | 第151行: | ||
Even when such a computation model is not realistic yet, it has theoretical importance, mostly related to the P = NP problem, which questions the identity of the complexity classes formed by taking "polynomial time" and "non-deterministic polynomial time" as least upper bounds. Simulating an NP-algorithm on a deterministic computer usually takes "exponential time". A problem is in the complexity class NP, if it may be solved in polynomial time on a non-deterministic machine. A problem is NP-complete if, roughly speaking, it is in NP and is not easier than any other NP problem. Many combinatorial problems, such as the Knapsack problem, the travelling salesman problem, and the Boolean satisfiability problem are NP-complete. For all these problems, the best known algorithm has exponential complexity. If any one of these problems could be solved in polynomial time on a deterministic machine, then all NP problems could also be solved in polynomial time, and one would have P = NP. it is generally conjectured that with the practical implication that the worst cases of NP problems are intrinsically difficult to solve, i.e., take longer than any reasonable time span (decades!) for interesting lengths of input. | Even when such a computation model is not realistic yet, it has theoretical importance, mostly related to the P = NP problem, which questions the identity of the complexity classes formed by taking "polynomial time" and "non-deterministic polynomial time" as least upper bounds. Simulating an NP-algorithm on a deterministic computer usually takes "exponential time". A problem is in the complexity class NP, if it may be solved in polynomial time on a non-deterministic machine. A problem is NP-complete if, roughly speaking, it is in NP and is not easier than any other NP problem. Many combinatorial problems, such as the Knapsack problem, the travelling salesman problem, and the Boolean satisfiability problem are NP-complete. For all these problems, the best known algorithm has exponential complexity. If any one of these problems could be solved in polynomial time on a deterministic machine, then all NP problems could also be solved in polynomial time, and one would have P = NP. it is generally conjectured that with the practical implication that the worst cases of NP problems are intrinsically difficult to solve, i.e., take longer than any reasonable time span (decades!) for interesting lengths of input. | ||
− | 即使这样的计算模型还不现实,它仍然具有重要的理论意义,主要涉及''' | + | 即使这样的计算模型还不现实,它仍然具有重要的理论意义,主要涉及'''P = NP?''' 问题。如果一个问题可以在确定性图灵机上以'''多项式时间 polynomial time'''求解,则该问题属于 '''P 类问题'''。如果一个问题可以在非确定性机器上以'''多项式时间 polynomial time'''求解,则该问题属于 '''NP 类问题'''。我们知道所有P类问题都属于NP类问题,但是否所有NP类问题也属于P类问题?亦即,是否P类问题和NP类问题是等价的?这就是所谓的 P=NP? 问题 |
− | 如果一个问题属于 NP 问题,且不比其他任何 NP 问题简单,则称该问题为''' | + | 如果一个问题属于 NP 问题,且不比其他任何 NP 问题简单,则称该问题为''' NP完全问题 NP-complete'''。许多'''组合 combinatorial'''问题,例如'''背包问题 Knapsack problem'''、'''旅行推销员问题 travelling salesman problem'''和'''布尔可满足性问题 Boolean satisfiability problem'''都是NP完全问题。对于所有这些问题,最著名的算法具有指数复杂度。如果这些问题中的任何一个都可以在确定性机器上在多项式时间内求解,那就意味着所有 NP 问题都可以在多项式时间内求解,我们立即可以得出结论:P = NP,既。一般认为P类问题和NP类问题是等价的,即所有NP类问题都有在确定性图灵机上以多项式时间解决的方法。现在我们主要的猜想是,NP 问题的最坏情况本质上是难以解决的,即 <math>P \neq NP</math>。 |
===并行和分布式计算=== | ===并行和分布式计算=== | ||
第164行: | 第160行: | ||
Parallel and distributed computing consist of splitting computation on several processors, which work simultaneously. The difference between the different model lies mainly in the way of transmitting information between processors. Typically, in parallel computing the data transmission between processors is very fast, while, in distributed computing, the data transmission is done through a network and is therefore much slower. | Parallel and distributed computing consist of splitting computation on several processors, which work simultaneously. The difference between the different model lies mainly in the way of transmitting information between processors. Typically, in parallel computing the data transmission between processors is very fast, while, in distributed computing, the data transmission is done through a network and is therefore much slower. | ||
− | 并行处理器和分布式计算处理器由同时工作的多个处理器组成。不同模型之间的差异主要体现在处理器之间的信息传输方式上。通常情况下,在并行计算中处理器之间的数据传输非常快,而在分布式计算中,数据传输通过''' | + | 并行处理器和分布式计算处理器由同时工作的多个处理器组成。不同模型之间的差异主要体现在处理器之间的信息传输方式上。通常情况下,在并行计算中处理器之间的数据传输非常快,而在分布式计算中,数据传输通过'''网络'''完成,因此速度要慢得多。 |
The time needed for a computation on {{mvar|N}} processors is at least the quotient by {{mvar|N}} of the time needed by a single processor. In fact this theoretically optimal bound can never be reached, because some subtasks cannot be parallelized, and some processors may have to wait a result from another processor. | The time needed for a computation on {{mvar|N}} processors is at least the quotient by {{mvar|N}} of the time needed by a single processor. In fact this theoretically optimal bound can never be reached, because some subtasks cannot be parallelized, and some processors may have to wait a result from another processor. | ||
第184行: | 第180行: | ||
A quantum computer is a computer whose model of computation is based on quantum mechanics. The Church–Turing thesis applies to quantum computers; that is, every problem that can be solved by a quantum computer can also be solved by a Turing machine. However, some problems may theoretically be solved with a much lower time complexity using a quantum computer rather than a classical computer. This is, for the moment, purely theoretical, as no one knows how to build an efficient quantum computer. | A quantum computer is a computer whose model of computation is based on quantum mechanics. The Church–Turing thesis applies to quantum computers; that is, every problem that can be solved by a quantum computer can also be solved by a Turing machine. However, some problems may theoretically be solved with a much lower time complexity using a quantum computer rather than a classical computer. This is, for the moment, purely theoretical, as no one knows how to build an efficient quantum computer. | ||
− | ''' | + | '''量子计算机 quantum computer'''是一种基于'''量子力学 quantum mechanics'''的计算机。'''丘奇-图灵理论 Church–Turing thesis'''适用于量子计算机,也就是说,任何可以由量子计算机解决的问题也可以由图灵机解决。然而,有些问题理论上可以用量子计算机以极低的'''时间复杂度 time complexity解决,而用传统图灵机则无法解决'''。由于没有人知道如何建造一台高效的量子计算机,目前这纯粹只是理论上可行的。 |
[[Quantum complexity theory]] has been developed to study the [[complexity class]]es of problems solved using quantum computers. It is used in [[post-quantum cryptography]], which consists of designing [[cryptographic protocol]]s that are resistant to attacks by quantum computers. | [[Quantum complexity theory]] has been developed to study the [[complexity class]]es of problems solved using quantum computers. It is used in [[post-quantum cryptography]], which consists of designing [[cryptographic protocol]]s that are resistant to attacks by quantum computers. | ||
第190行: | 第186行: | ||
Quantum complexity theory has been developed to study the complexity classes of problems solved using quantum computers. It is used in post-quantum cryptography, which consists of designing cryptographic protocols that are resistant to attacks by quantum computers. | Quantum complexity theory has been developed to study the complexity classes of problems solved using quantum computers. It is used in post-quantum cryptography, which consists of designing cryptographic protocols that are resistant to attacks by quantum computers. | ||
− | ''' | + | '''量子复杂度理论 Quantum complexity theory'''研究用量子计算机解决的问题的'''复杂度类'''。它主要用于'''后量子密码学 post-quantum cryptography''',包括设计能抵御量子计算机攻击的'''安全协议 cryptographic protocol'''。 |
==问题复杂度(下限)== | ==问题复杂度(下限)== | ||
第199行: | 第195行: | ||
The complexity of a problem is the infimum of the complexities of the algorithms that may solve the problem, including unknown algorithms. Thus the complexity of a problem is not greater than the complexity of any algorithm that solves the problems. | The complexity of a problem is the infimum of the complexities of the algorithms that may solve the problem, including unknown algorithms. Thus the complexity of a problem is not greater than the complexity of any algorithm that solves the problems. | ||
− | 问题的复杂度是解决问题算法(包括未知算法)复杂度的''' | + | 问题的复杂度是解决问题算法(包括未知算法)复杂度的'''下确界 infimum,'''即解决这个问题所需的最少时间。因此,问题的复杂度比任何解决该问题的算法的复杂度都要小。 |
It follows that every complexity that is expressed with [[big O notation]] is a complexity of the algorithm as well as of the corresponding problem. | It follows that every complexity that is expressed with [[big O notation]] is a complexity of the algorithm as well as of the corresponding problem. | ||
第205行: | 第201行: | ||
It follows that every complexity that is expressed with big O notation is a complexity of the algorithm as well as of the corresponding problem. | It follows that every complexity that is expressed with big O notation is a complexity of the algorithm as well as of the corresponding problem. | ||
− | 用''' | + | 用'''大O符号 big O notation'''表示的每一个复杂度都是算法的复杂度,也是相应问题的复杂度。 |
On the other hand, it is generally hard to obtain nontrivial lower bounds for problem complexity, and there are few methods for obtaining such lower bounds. | On the other hand, it is generally hard to obtain nontrivial lower bounds for problem complexity, and there are few methods for obtaining such lower bounds. | ||
第217行: | 第213行: | ||
For solving most problems, it is required to read all input data, which, normally, needs a time proportional to the size of the data. Thus, such problems have a complexity that is at least linear, that is, using big omega notation, a complexity <math>\Omega(n).</math> | For solving most problems, it is required to read all input data, which, normally, needs a time proportional to the size of the data. Thus, such problems have a complexity that is at least linear, that is, using big omega notation, a complexity <math>\Omega(n).</math> | ||
− | 为了解决大多数问题,往往需要与数据大小成比例的时间来读取所有输入数据。因此,这些问题的复杂度至少是''' | + | 为了解决大多数问题,往往需要与数据大小成比例的时间来读取所有输入数据。因此,这些问题的复杂度至少是'''线性的 linear''',使用'''大欧米茄符号 big omega notation''',记为复杂度<math>\Omega(n).</math> |
第225行: | 第221行: | ||
The solution of some problems, typically in computer algebra and computational algebraic geometry, may be very large. In such a case, the complexity is lower bounded by the maximal size of the output, since the output must be written. For example, a system of polynomial equations of degree in indeterminates may have up to <math>d^n</math> complex solutions, if the number of solutions is finite (this is Bézout's theorem). As these solutions must be written down, the complexity of this problem is <math>\Omega(d^n).</math> For this problem, an algorithm of complexity <math>d^{O(n)}</math> is known, which may thus be considered as asymptotically quasi-optimal. | The solution of some problems, typically in computer algebra and computational algebraic geometry, may be very large. In such a case, the complexity is lower bounded by the maximal size of the output, since the output must be written. For example, a system of polynomial equations of degree in indeterminates may have up to <math>d^n</math> complex solutions, if the number of solutions is finite (this is Bézout's theorem). As these solutions must be written down, the complexity of this problem is <math>\Omega(d^n).</math> For this problem, an algorithm of complexity <math>d^{O(n)}</math> is known, which may thus be considered as asymptotically quasi-optimal. | ||
− | 一些问题的解可能是非常大的,特别是''' | + | 一些问题的解可能是非常大的,特别是'''计算机代数 computer algebra'''和'''计算代数几何 computational algebraic geometry'''。在这种情况下,复杂度以输出的最大长度为下界,因此输出必须被写入。例如,如果解的个数是有限的,'''{{mvar|n}}个{{mvar|d}}次不确定多项式方程组 system of n polynomial equations of degree d in indeterminates '''可能有多达 <math>d^n</math>个复解(这是'''贝佐定理 Bézout's theorem''')。由于这些解必须被写入,所以这个问题的复杂度是<math>\Omega(d^n).</math>。对于这个问题,已知一个复杂度为<math>d^{O(n)}</math>的算法,因此可以认为是渐近拟最优的。 |
第232行: | 第228行: | ||
A standard method for getting lower bounds of complexity consists of reducing a problem to another problem. More precisely, suppose that one may encode a problem of size into a subproblem of size of a problem , and that the complexity of is <math>\Omega(g(n)).</math> Without loss of generality, one may suppose that the function increases with and has an inverse function . Then the complexity of the problem is <math>\Omega(g(h(n))).</math> This is this method that is used for proving that, if P ≠ NP (an unsolved conjecture), the complexity of every NP-complete problem is <math>\Omega(n^k),</math> for every positive integer . | A standard method for getting lower bounds of complexity consists of reducing a problem to another problem. More precisely, suppose that one may encode a problem of size into a subproblem of size of a problem , and that the complexity of is <math>\Omega(g(n)).</math> Without loss of generality, one may suppose that the function increases with and has an inverse function . Then the complexity of the problem is <math>\Omega(g(h(n))).</math> This is this method that is used for proving that, if P ≠ NP (an unsolved conjecture), the complexity of every NP-complete problem is <math>\Omega(n^k),</math> for every positive integer . | ||
− | 获得复杂度下限的标准方法包括将一个问题转化为另一个问题。更准确地说,假设一个大小为{{mvar|n}}的问题{{mvar|A}}可以编码成大小为{{math|''f''(''n'')}}的问题{{mvar|B}},则问题{{mvar|A}}的复杂度为<math>\Omega(g(n)).</math>。不失一般性地,我们可以假设函数{{mvar|f}}随着{{mvar|n}}的增加而增加,并且有一个''' | + | 获得复杂度下限的标准方法包括将一个问题转化为另一个问题。更准确地说,假设一个大小为{{mvar|n}}的问题{{mvar|A}}可以编码成大小为{{math|''f''(''n'')}}的问题{{mvar|B}},则问题{{mvar|A}}的复杂度为<math>\Omega(g(n)).</math>。不失一般性地,我们可以假设函数{{mvar|f}}随着{{mvar|n}}的增加而增加,并且有一个'''反函数 inverse function'''{{mvar|h}},那么问题{{mvar|B}}的复杂度就是<math>\Omega(g(h(n))).</math>。这个方法可以用来证明,若'''P ≠ NP'''(一个未解决的猜想) ,对于每个正整数{{mvar|k}},每个'''NP完全问题 NP-complete problem''' 的复杂度都是 <math>\Omega(n^k),</math>。 |
==在算法设计中的应用== | ==在算法设计中的应用== | ||
第241行: | 第237行: | ||
Evaluating the complexity of an algorithm is an important part of algorithm design, as this gives useful information on the performance that may be expected. | Evaluating the complexity of an algorithm is an important part of algorithm design, as this gives useful information on the performance that may be expected. | ||
− | 评估算法的复杂度是''' | + | 评估算法的复杂度是'''算法设计 algorithm design'''的一个重要组成部分,因为这给出了一个算法关于预期性能的有用信息。 |
It is a common misconception that the evaluation of the complexity of algorithms will become less important as a result of [[Moore's law]], which posits the [[exponential growth]] of the power of modern [[computer]]s. This is wrong because this power increase allows working with large input data ([[big data]]). For example, when one wants to sort alphabetically a list of a few hundreds of entries, such as the [[bibliography]] of a book, any algorithm should work well in less than a second. On the other hand, for a list of a million of entries (the phone numbers of a large town, for example), the elementary algorithms that require <math>O(n^2)</math> comparisons would have to do a trillion of comparisons, which would need around three hours at the speed of 10 million of comparisons per second. On the other hand, the [[quicksort]] and [[merge sort]] require only <math>n\log_2 n</math> comparisons (as average-case complexity for the former, as worst-case complexity for the latter). For {{math|1=''n'' = 1,000,000}}, this gives approximately 30,000,000 comparisons, which would only take 3 seconds at 10 million comparisons per second. | It is a common misconception that the evaluation of the complexity of algorithms will become less important as a result of [[Moore's law]], which posits the [[exponential growth]] of the power of modern [[computer]]s. This is wrong because this power increase allows working with large input data ([[big data]]). For example, when one wants to sort alphabetically a list of a few hundreds of entries, such as the [[bibliography]] of a book, any algorithm should work well in less than a second. On the other hand, for a list of a million of entries (the phone numbers of a large town, for example), the elementary algorithms that require <math>O(n^2)</math> comparisons would have to do a trillion of comparisons, which would need around three hours at the speed of 10 million of comparisons per second. On the other hand, the [[quicksort]] and [[merge sort]] require only <math>n\log_2 n</math> comparisons (as average-case complexity for the former, as worst-case complexity for the latter). For {{math|1=''n'' = 1,000,000}}, this gives approximately 30,000,000 comparisons, which would only take 3 seconds at 10 million comparisons per second. | ||
第247行: | 第243行: | ||
It is a common misconception that the evaluation of the complexity of algorithms will become less important as a result of Moore's law, which posits the exponential growth of the power of modern computers. This is wrong because this power increase allows working with large input data (big data). For example, when one wants to sort alphabetically a list of a few hundreds of entries, such as the bibliography of a book, any algorithm should work well in less than a second. On the other hand, for a list of a million of entries (the phone numbers of a large town, for example), the elementary algorithms that require <math>O(n^2)</math> comparisons would have to do a trillion of comparisons, which would need around three hours at the speed of 10 million of comparisons per second. On the other hand, the quicksort and merge sort require only <math>n\log_2 n</math> comparisons (as average-case complexity for the former, as worst-case complexity for the latter). For , this gives approximately 30,000,000 comparisons, which would only take 3 seconds at 10 million comparisons per second. | It is a common misconception that the evaluation of the complexity of algorithms will become less important as a result of Moore's law, which posits the exponential growth of the power of modern computers. This is wrong because this power increase allows working with large input data (big data). For example, when one wants to sort alphabetically a list of a few hundreds of entries, such as the bibliography of a book, any algorithm should work well in less than a second. On the other hand, for a list of a million of entries (the phone numbers of a large town, for example), the elementary algorithms that require <math>O(n^2)</math> comparisons would have to do a trillion of comparisons, which would need around three hours at the speed of 10 million of comparisons per second. On the other hand, the quicksort and merge sort require only <math>n\log_2 n</math> comparisons (as average-case complexity for the former, as worst-case complexity for the latter). For , this gives approximately 30,000,000 comparisons, which would only take 3 seconds at 10 million comparisons per second. | ||
− | 有一个常见的误解,由于''' | + | 有一个常见的误解,由于'''摩尔定律 Moore's law'''假定了现代计算机功率的'''指数增长 exponential growth''',对算法复杂度的评估将变得不那么重要。这是错误的,因为这种功率增加同样也会容使得处理较大的输入数据('''大数据 big data''')成为可能。例如,当一个人想要按字母顺序对数百个条目的列表进行排序时,比如一本书的'''参考书目 bibliography''',任何算法都应该在不到一秒的时间内就能正常工作。另一方面,对于一个包含100万个条目的列表(例如一个大城镇的电话号码) ,需要<math>O(n^2)</math>次比较的基本算法,须进行1万亿次比较运算,即以每秒1000万次的速度进行比较需要大约3个小时。另一方面,'''快速排序 quicksort'''和'''合并排序 merge sort'''只需要<math>n\log_2 n</math>次比较(前者为平均情况复杂度,后者为最坏情况复杂度)。这大约是3000万次比较,以每秒1000万次比较计算,只需要3秒钟。 |
Thus the evaluation of the complexity may allow eliminating many inefficient algorithms before any implementation. This may also be used for tuning complex algorithms without testing all variants. By determining the most costly steps of a complex algorithm, the study of complexity allows also focusing on these steps the effort for improving the efficiency of an implementation. | Thus the evaluation of the complexity may allow eliminating many inefficient algorithms before any implementation. This may also be used for tuning complex algorithms without testing all variants. By determining the most costly steps of a complex algorithm, the study of complexity allows also focusing on these steps the effort for improving the efficiency of an implementation. |
2021年9月20日 (一) 15:25的版本
在计算机科学 computer science中,一个算法 algorithm的计算复杂度或简单的复杂度就是运行这个算法所需要的资源量,特别是时间(CPU占用时间)和空间(内存占用空间)需求。
As the amount of resources required to run an algorithm generally varies with the size of the input, the complexity is typically expressed as a function n → f(n), where n is the size of the input and f(n) is either the worst-case complexity (the maximum of the amount of resources that are needed over all inputs of size n) or the average-case complexity (the average of the amount of resources over all inputs of size n). Time complexity is generally expressed as the number of required elementary operations on an input of size n, where elementary operations are assumed to take a constant amount of time on a given computer and change only by a constant factor when run on a different computer. Space complexity is generally expressed as the amount of memory required by an algorithm on an input of size n.
As the amount of resources required to run an algorithm generally varies with the size of the input, the complexity is typically expressed as a function , where is the size of the input and is either the worst-case complexity (the maximum of the amount of resources that are needed over all inputs of size ) or the average-case complexity (the average of the amount of resources over all inputs of size ). Time complexity is generally expressed as the number of required elementary operations on an input of size , where elementary operations are assumed to take a constant amount of time on a given computer and change only by a constant factor when run on a different computer. Space complexity is generally expressed as the amount of memory required by an algorithm on an input of size .
由于运行一个算法所需的资源量通常随输入规模的大小而变化,因此复杂度通常用函数 f(n)表示,其中n是输入量的大小,f(n)是最坏情况复杂度 worst-case complexity(输入大小为 n时所需的资源量的最大值) ,或是平均情况复杂度 average-case complexity(资源总量相对于n的所有占用的平均值)。时间复杂度 Time complexity通常表示为对一个输入值长度所需基本操作(通常是加法操作或者乘法操作)的数量。我们假设基本操作在一台计算机上只占用一个不变的时间量(比如1纳秒),而在另一台计算机上运行时,只根据一个常量因子进行改变(比如k*1纳秒)。空间复杂度 space complexity通常表示为算法对一个输入值长度所需的内存量。
The study of the complexity of explicitly given algorithms is called analysis of algorithms, while the study of the complexity of problems is called computational complexity theory. Both areas are highly related, as the complexity of an algorithm is always an upper bound on the complexity of the problem solved by this algorithm.
The study of the complexity of explicitly given algorithms is called analysis of algorithms, while the study of the complexity of problems is called computational complexity theory. Both areas are highly related, as the complexity of an algorithm is always an upper bound on the complexity of the problem solved by this algorithm.
对一个有明确定义的算法的复杂度进行的研究叫做算法分析 analysis of algorithm,而对问题 problem的复杂度研究叫做计算复杂度理论 computation complexity theory分析。举个例子,怎样把一串数字从小到大进行排序,是一个问题(problem);而针对这一问题,我们有多种明确定义的算法,如选择排序,归并排序等。假设我们有[math]\displaystyle{ n }[/math]个数字,那么排序问题的复杂度就是[math]\displaystyle{ nlogn }[/math],而选择排序的复杂度是[math]\displaystyle{ n^2 }[/math],归并排序的复杂度是[math]\displaystyle{ 6nlogn }[/math]。
这两个领域是高度相关的,因为算法的复杂度一直是该算法所求解问题复杂度的上限 upper bound。
资源
时间
The resource that is most commonly considered is time. When "complexity" is used without qualification, this generally means time complexity.
The resource that is most commonly considered is time. When "complexity" is used without qualification, this generally means time complexity.
最常被考虑的资源是时间。当没有明确说明时,“复杂度”通常意味着时间复杂度而非空间复杂度。
The usual units of time (seconds, minutes etc.) are not used in complexity theory because they are too dependent on the choice of a specific computer and on the evolution of technology. For instance, a computer today can execute an algorithm significantly faster than a computer from the 1960s; however, this is not an intrinsic feature of the algorithm but rather a consequence of technological advances in computer hardware. Complexity theory seeks to quantify the intrinsic time requirements of algorithms, that is, the basic time constraints an algorithm would place on any computer. This is achieved by counting the number of elementary operations that are executed during the computation. These operations are assumed to take constant time (that is, not affected by the size of the input) on a given machine, and are often called steps.
The usual units of time (seconds, minutes etc.) are not used in complexity theory because they are too dependent on the choice of a specific computer and on the evolution of technology. For instance, a computer today can execute an algorithm significantly faster than a computer from the 1960s; however, this is not an intrinsic feature of the algorithm but rather a consequence of technological advances in computer hardware. Complexity theory seeks to quantify the intrinsic time requirements of algorithms, that is, the basic time constraints an algorithm would place on any computer. This is achieved by counting the number of elementary operations that are executed during the computation. These operations are assumed to take constant time (that is, not affected by the size of the input) on a given machine, and are often called steps.
在复杂度理论 complexity theory中不使用通常的时间单位(秒、分等),因为它们过于依赖于特定计算机的选择和技术的演变。例如,今天的计算机执行算法的速度明显快于20世纪60年代的计算机; 然而,这不是算法的固有特征,而是计算机硬件技术进步的结果。复杂度理论旨在量化算法的内在时间需求,也就是算法对任何计算机的基本时间约束。这是通过统计在计算过程中执行的基本操作数量来实现的。这些基本操作假定在给定的机器上占用常量时间(即不受输入大小的影响) ,通常被称为步骤。
空间
Another important resource is the size of computer memory that is needed for running algorithms.
Another important resource is the size of computer memory that is needed for running algorithms.
另一个重要的资源是运行算法所需的计算机内存 computer memory大小。
其他
The number of arithmetic operations is another resource that is commonly used. In this case, one talks of arithmetic complexity. If one knows an upper bound on the size of the binary representation of the numbers that occur during a computation, the time complexity is generally the product of the arithmetic complexity by a constant factor.
The number of arithmetic operations is another resource that is commonly used. In this case, one talks of arithmetic complexity. If one knows an upper bound on the size of the binary representation of the numbers that occur during a computation, the time complexity is generally the product of the arithmetic complexity by a constant factor.
算术运算 arithmetic operations的数量是另一种常用的资源。在这种情况下,人们会谈到算术复杂度。如果已知一个计算过程中出现的数的二进制表示 binary representation长度的上限 upper bound,时间复杂度通常是该算术复杂度乘以一个常数因子。
For many algorithms the size of the integers that are used during a computation is not bounded, and it is not realistic to consider that arithmetic operations take a constant time. Therefore, the time complexity, generally called bit complexity in this context, may be much larger than the arithmetic complexity. For example, the arithmetic complexity of the computation of the determinant of a n×n integer matrix is [math]\displaystyle{ O(n^3) }[/math] for the usual algorithms (Gaussian elimination). The bit complexity of the same algorithms is exponential in n, because the size of the coefficients may grow exponentially during the computation. On the other hand, if these algorithms are coupled with multi-modular arithmetic, the bit complexity may be reduced to O~(n4).
For many algorithms the size of the integers that are used during a computation is not bounded, and it is not realistic to consider that arithmetic operations take a constant time. Therefore, the time complexity, generally called bit complexity in this context, may be much larger than the arithmetic complexity. For example, the arithmetic complexity of the computation of the determinant of a integer matrix is [math]\displaystyle{ O(n^3) }[/math] for the usual algorithms (Gaussian elimination). The bit complexity of the same algorithms is exponential in , because the size of the coefficients may grow exponentially during the computation. On the other hand, if these algorithms are coupled with multi-modular arithmetic, the bit complexity may be reduced to soft O notation|.
对于许多算法,在计算过程中使用的整数大小是没有界限的,并且考虑算术运算占用一个常量时间是不现实的。因此,时间复杂度,在此上下文中通常称为 位复杂度 bit complexity,可能远远大于算术复杂度。例如,根据通常的算法 高斯消去法 Gaussian elimination 计算一个n×n 整数矩阵行列式 的算术复杂度是[math]\displaystyle{ O(n^3) }[/math]。因为系数的大小可能在计算过程中呈指数增长,相同算法的位复杂度是指数级的。另一方面,如果这些算法与多模运算相结合,位复杂度可以降低到O~(n4)。
In sorting and searching, the resource that is generally considered is the number of entries comparisons. This is generally a good measure of the time complexity if data are suitably organized.
In sorting and searching, the resource that is generally considered is the number of entries comparisons. This is generally a good measure of the time complexity if data are suitably organized.
在排序 sorting和搜索 searching中,通常考虑的资源是需要做几次比较大小。如果数据组织得当,这通常是一个很好的时间复杂度测量方法。
复杂度:输入规模的函数
复杂度作为关于输入长度的函数
- For clarity, only time complexity is considered in this section, but everything applies (with slight modifications) to the complexity with respect to other resources.
For clarity, only time complexity is considered in this section, but everything applies (with slight modifications) to the complexity with respect to other resources.
为了清晰起见,本节只考虑时间复杂度,不过所有内容(稍加修改)也都适用于其他资源的复杂度。
It is impossible to count the number of steps of an algorithm on all possible inputs. As the complexity generally increases with the size of the input, the complexity is typically expressed as a function of the size n (in bits) of the input, and therefore, the complexity is a function of n. However, the complexity of an algorithm may vary dramatically for different inputs of the same size. Therefore, several complexity functions are commonly used.
It is impossible to count the number of steps of an algorithm on all possible inputs. As the complexity generally increases with the size of the input, the complexity is typically expressed as a function of the size (in bits) of the input, and therefore, the complexity is a function of . However, the complexity of an algorithm may vary dramatically for different inputs of the same size. Therefore, several complexity functions are commonly used.
计算一个算法对于所有可能输入的所需要的步骤数是不可能的。由于复杂度通常随着输入的规模而增加,复杂度通常表示为输入值 n 长度(以比特 bit为单位)的函数。因此,复杂度是一个关于 n 的函数。然而,对于同样长度的不同输入,算法的复杂度可能会大不相同。因此,有多种不同的复杂度函数被广泛使用。
The worst-case complexity is the maximum of the complexity over all inputs of size n, and the average-case complexity is the average of the complexity over all inputs of size n (this makes sense, as the number of possible inputs of a given size is finite). Generally, when "complexity" is used without being further specified, this is the worst-case time complexity that is considered.
The worst-case complexity is the maximum of the complexity over all inputs of size , and the average-case complexity is the average of the complexity over all inputs of size (this makes sense, as the number of possible inputs of a given size is finite). Generally, when "complexity" is used without being further specified, this is the worst-case time complexity that is considered.
最坏情况复杂度 worst-case complexity是对所有输入n长度中的最大复杂度,平均情况复杂度 average-case complexity是对所有输入n长度中的平均复杂度。一般来说,如果使用“复杂度”一词且不进行进一步说明 ,即考虑最坏情况时间复杂度。
渐近复杂度
It is generally difficult to compute precisely the worst-case and the average-case complexity. In addition, these exact values provide little practical application, as any change of computer or of model of computation would change the complexity somewhat. Moreover, the resource use is not critical for small values of n, and this makes that, for small n, the ease of implementation is generally more interesting than a good complexity.
It is generally difficult to compute precisely the worst-case and the average-case complexity. In addition, these exact values provide little practical application, as any change of computer or of model of computation would change the complexity somewhat. Moreover, the resource use is not critical for small values of , and this makes that, for small , the ease of implementation is generally more interesting than a good complexity.
通常很难精确计算最坏情况和平均情况复杂度。此外,由于计算机或计算模型的任何变化都会改变复杂度,精确的复杂度值没多少实际意义。更多地,对于较小的n值,资源的使用并不是关键。因此对于小 n,人们通常更在乎一个算法的简单性和易实现性,而非复杂度。
For these reasons, one generally focuses on the behavior of the complexity for large n, that is on its asymptotic behavior when n tends to the infinity. Therefore, the complexity is generally expressed by using big O notation.
For these reasons, one generally focuses on the behavior of the complexity for large , that is on its asymptotic behavior when tends to the infinity. Therefore, the complexity is generally expressed by using big O notation.
由于这些原因,人们通常把注意力集中在大n的复杂度上,即输入规模趋于无穷大的渐近行为 asymptotic behavior上。因此,复杂度通常用大O符号 big O notation来表示。
For example, the usual algorithm for integer multiplication has a complexity of [math]\displaystyle{ O(n^2), }[/math] this means that there is a constant [math]\displaystyle{ c_u }[/math] such that the multiplication of two integers of at most n digits may be done in a time less than [math]\displaystyle{ c_un^2. }[/math] This bound is sharp in the sense that the worst-case complexity and the average-case complexity are [math]\displaystyle{ \Omega(n^2), }[/math] which means that there is a constant [math]\displaystyle{ c_l }[/math] such that these complexities are larger than [math]\displaystyle{ c_ln^2. }[/math] The radix does not appear in these complexity, as changing of radix changes only the constants [math]\displaystyle{ c_u }[/math] and [math]\displaystyle{ c_l. }[/math]
For example, the usual algorithm for integer multiplication has a complexity of [math]\displaystyle{ O(n^2), }[/math] this means that there is a constant [math]\displaystyle{ c_u }[/math] such that the multiplication of two integers of at most digits may be done in a time less than [math]\displaystyle{ c_un^2. }[/math] This bound is sharp in the sense that the worst-case complexity and the average-case complexity are [math]\displaystyle{ \Omega(n^2), }[/math] which means that there is a constant [math]\displaystyle{ c_l }[/math] such that these complexities are larger than [math]\displaystyle{ c_ln^2. }[/math] The radix does not appear in these complexity, as changing of radix changes only the constants [math]\displaystyle{ c_u }[/math] and [math]\displaystyle{ c_l. }[/math]
例如,通常整数乘法 multiplication的复杂度是[math]\displaystyle{ O(n^2), }[/math],这意味着存在一个常数[math]\displaystyle{ c_u }[/math],使得两个最多n位的整数乘法可以在小于[math]\displaystyle{ c_un^2 }[/math]的时间内完成。这个界限是尖锐的,因为最坏情况复杂度和平均情况复杂度是[math]\displaystyle{ \Omega(n^2), }[/math],意味着存在一个常数[math]\displaystyle{ c_l }[/math],使得这些复杂度比[math]\displaystyle{ c_ln^2 }[/math]大。
计算模型
The evaluation of the complexity relies on the choice of a model of computation, which consists in defining the basic operations that are done in a unit of time. When the model of computation is not explicitly specified, this is generally meant as being multitape Turing machine.
The evaluation of the complexity relies on the choice of a model of computation, which consists in defining the basic operations that are done in a unit of time. When the model of computation is not explicitly specified, this is generally meant as being multitape Turing machine.
复杂度的评估依赖于计算模型 model of computation的选择,包括定义在一个时间单位内完成的基本操作。当没有明确指定时,默认使用的计算模型是多带图灵机 multitape Turing machine。
确定性模型
A deterministic model of computation is a model of computation such that the successive states of the machine and the operations to be performed are completely determined by the preceding state. Historically, the first deterministic models were recursive functions, lambda calculus, and Turing machines. The model of Random access machines (also called RAM-machines) is also widely used, as a closer counterpart to real computers.
A deterministic model of computation is a model of computation such that the successive states of the machine and the operations to be performed are completely determined by the preceding state. Historically, the first deterministic models were recursive functions, lambda calculus, and Turing machines. The model of Random access machines (also called RAM-machines) is also widely used, as a closer counterpart to real computers.
一个确定性模型 deterministic model 的计算是一种机器的后续状态和要执行的操作完全由前面的状态决定的计算模型。历史上,最早的确定性模型是递归函数 recursive functions、 lambda 演算 lambda calculus和图灵机 Turing machines。随机存取机器 Random access machine (也称 RAM-machines)的模型也被广泛使用,更接近真实的计算机 computer。
When the model of computation is not specified, it is generally assumed to be a multitape Turing machine. For most algorithms, the time complexity is the same on multitape Turing machines as on RAM-machines, although some care may be needed in how data is stored in memory to get this equivalence.
When the model of computation is not specified, it is generally assumed to be a multitape Turing machine. For most algorithms, the time complexity is the same on multitape Turing machines as on RAM-machines, although some care may be needed in how data is stored in memory to get this equivalence.
当计算模型没有被指定时,通常假设它是一个多带图灵机 multitape Turing machine。对于大多数算法,在多带图灵机上的时间复杂度与在RAM-machines上的相同,尽管可能需要注意如何将数据存储在内存中,以确保这种等价性.
非确定性计算
In a non-deterministic model of computation, such as non-deterministic Turing machines, some choices may be done at some steps of the computation. In complexity theory, one considers all possible choices simultaneously, and the non-deterministic time complexity is the time needed, when the best choices are always done. In other words, one considers that the computation is done simultaneously on as many (identical) processors as needed, and the non-deterministic computation time is the time spent by the first processor that finishes the computation. This parallelism is partly amenable to quantum computing via superposed entangled states in running specific quantum algorithms, like e.g. Shor's factorization of yet only small integers (模板:As of: 21 = 3 × 7).
In a non-deterministic model of computation, such as non-deterministic Turing machines, some choices may be done at some steps of the computation. In complexity theory, one considers all possible choices simultaneously, and the non-deterministic time complexity is the time needed, when the best choices are always done. In other words, one considers that the computation is done simultaneously on as many (identical) processors as needed, and the non-deterministic computation time is the time spent by the first processor that finishes the computation. This parallelism is partly amenable to quantum computing via superposed entangled states in running specific quantum algorithms, like e.g. Shor's factorization of yet only small integers (: 21 = 3 × 7).
在非确定性计算模型 non-deterministic model of computation中,例如不确定的图灵机 non-deterministic Turing machine,在计算的某些步骤中可能会进行一些选择。在复杂度理论中,非确定性时间复杂度即为,同时考虑所有可能的选择且做出最佳选择时所需的时间。换句话说,我们认为计算是多个(相同的)处理器上同时进行的,而不确定计算时间是第一个完成计算的处理器所花费的时间。这种并行性在一定程度上可以通过运行特定量子算法 quantum algorithm的叠加纠缠态 entangled state来实现量子计算 quantum computing。例如小整数上的肖尔因式分解 Shor's factorization。
Even when such a computation model is not realistic yet, it has theoretical importance, mostly related to the P = NP problem, which questions the identity of the complexity classes formed by taking "polynomial time" and "non-deterministic polynomial time" as least upper bounds. Simulating an NP-algorithm on a deterministic computer usually takes "exponential time". A problem is in the complexity class NP, if it may be solved in polynomial time on a non-deterministic machine. A problem is NP-complete if, roughly speaking, it is in NP and is not easier than any other NP problem. Many combinatorial problems, such as the Knapsack problem, the travelling salesman problem, and the Boolean satisfiability problem are NP-complete. For all these problems, the best known algorithm has exponential complexity. If any one of these problems could be solved in polynomial time on a deterministic machine, then all NP problems could also be solved in polynomial time, and one would have P = NP. 模板:As of it is generally conjectured that P ≠ NP, with the practical implication that the worst cases of NP problems are intrinsically difficult to solve, i.e., take longer than any reasonable time span (decades!) for interesting lengths of input.
Even when such a computation model is not realistic yet, it has theoretical importance, mostly related to the P = NP problem, which questions the identity of the complexity classes formed by taking "polynomial time" and "non-deterministic polynomial time" as least upper bounds. Simulating an NP-algorithm on a deterministic computer usually takes "exponential time". A problem is in the complexity class NP, if it may be solved in polynomial time on a non-deterministic machine. A problem is NP-complete if, roughly speaking, it is in NP and is not easier than any other NP problem. Many combinatorial problems, such as the Knapsack problem, the travelling salesman problem, and the Boolean satisfiability problem are NP-complete. For all these problems, the best known algorithm has exponential complexity. If any one of these problems could be solved in polynomial time on a deterministic machine, then all NP problems could also be solved in polynomial time, and one would have P = NP. it is generally conjectured that with the practical implication that the worst cases of NP problems are intrinsically difficult to solve, i.e., take longer than any reasonable time span (decades!) for interesting lengths of input.
即使这样的计算模型还不现实,它仍然具有重要的理论意义,主要涉及P = NP? 问题。如果一个问题可以在确定性图灵机上以多项式时间 polynomial time求解,则该问题属于 P 类问题。如果一个问题可以在非确定性机器上以多项式时间 polynomial time求解,则该问题属于 NP 类问题。我们知道所有P类问题都属于NP类问题,但是否所有NP类问题也属于P类问题?亦即,是否P类问题和NP类问题是等价的?这就是所谓的 P=NP? 问题
如果一个问题属于 NP 问题,且不比其他任何 NP 问题简单,则称该问题为 NP完全问题 NP-complete。许多组合 combinatorial问题,例如背包问题 Knapsack problem、旅行推销员问题 travelling salesman problem和布尔可满足性问题 Boolean satisfiability problem都是NP完全问题。对于所有这些问题,最著名的算法具有指数复杂度。如果这些问题中的任何一个都可以在确定性机器上在多项式时间内求解,那就意味着所有 NP 问题都可以在多项式时间内求解,我们立即可以得出结论:P = NP,既。一般认为P类问题和NP类问题是等价的,即所有NP类问题都有在确定性图灵机上以多项式时间解决的方法。现在我们主要的猜想是,NP 问题的最坏情况本质上是难以解决的,即 [math]\displaystyle{ P \neq NP }[/math]。
并行和分布式计算
Parallel and distributed computing consist of splitting computation on several processors, which work simultaneously. The difference between the different model lies mainly in the way of transmitting information between processors. Typically, in parallel computing the data transmission between processors is very fast, while, in distributed computing, the data transmission is done through a network and is therefore much slower.
Parallel and distributed computing consist of splitting computation on several processors, which work simultaneously. The difference between the different model lies mainly in the way of transmitting information between processors. Typically, in parallel computing the data transmission between processors is very fast, while, in distributed computing, the data transmission is done through a network and is therefore much slower.
并行处理器和分布式计算处理器由同时工作的多个处理器组成。不同模型之间的差异主要体现在处理器之间的信息传输方式上。通常情况下,在并行计算中处理器之间的数据传输非常快,而在分布式计算中,数据传输通过网络完成,因此速度要慢得多。
The time needed for a computation on N processors is at least the quotient by N of the time needed by a single processor. In fact this theoretically optimal bound can never be reached, because some subtasks cannot be parallelized, and some processors may have to wait a result from another processor.
The time needed for a computation on processors is at least the quotient by of the time needed by a single processor. In fact this theoretically optimal bound can never be reached, because some subtasks cannot be parallelized, and some processors may have to wait a result from another processor.
在N个处理器上进行计算所需的时间至少是单个处理器所需时间的N的商。但实际上,这个理论上的最优界限永远不可能达到,由于有些子任务不能并行化,部分处理器不得不先等待另一个处理器的结果。
The main complexity problem is thus to design algorithms such that the product of the computation time by the number of processors is as close as possible to the time needed for the same computation on a single processor.
The main complexity problem is thus to design algorithms such that the product of the computation time by the number of processors is as close as possible to the time needed for the same computation on a single processor.
因此,主要的复杂度问题是如何设计算法,使得计算时间与处理器数量的乘积尽可能接近在单个处理器上进行同一计算所需的时间。
量子计算
A quantum computer is a computer whose model of computation is based on quantum mechanics. The Church–Turing thesis applies to quantum computers; that is, every problem that can be solved by a quantum computer can also be solved by a Turing machine. However, some problems may theoretically be solved with a much lower time complexity using a quantum computer rather than a classical computer. This is, for the moment, purely theoretical, as no one knows how to build an efficient quantum computer.
A quantum computer is a computer whose model of computation is based on quantum mechanics. The Church–Turing thesis applies to quantum computers; that is, every problem that can be solved by a quantum computer can also be solved by a Turing machine. However, some problems may theoretically be solved with a much lower time complexity using a quantum computer rather than a classical computer. This is, for the moment, purely theoretical, as no one knows how to build an efficient quantum computer.
量子计算机 quantum computer是一种基于量子力学 quantum mechanics的计算机。丘奇-图灵理论 Church–Turing thesis适用于量子计算机,也就是说,任何可以由量子计算机解决的问题也可以由图灵机解决。然而,有些问题理论上可以用量子计算机以极低的时间复杂度 time complexity解决,而用传统图灵机则无法解决。由于没有人知道如何建造一台高效的量子计算机,目前这纯粹只是理论上可行的。
Quantum complexity theory has been developed to study the complexity classes of problems solved using quantum computers. It is used in post-quantum cryptography, which consists of designing cryptographic protocols that are resistant to attacks by quantum computers.
Quantum complexity theory has been developed to study the complexity classes of problems solved using quantum computers. It is used in post-quantum cryptography, which consists of designing cryptographic protocols that are resistant to attacks by quantum computers.
量子复杂度理论 Quantum complexity theory研究用量子计算机解决的问题的复杂度类。它主要用于后量子密码学 post-quantum cryptography,包括设计能抵御量子计算机攻击的安全协议 cryptographic protocol。
问题复杂度(下限)
The complexity of a problem is the infimum of the complexities of the algorithms that may solve the problem, including unknown algorithms. Thus the complexity of a problem is not greater than the complexity of any algorithm that solves the problems.
The complexity of a problem is the infimum of the complexities of the algorithms that may solve the problem, including unknown algorithms. Thus the complexity of a problem is not greater than the complexity of any algorithm that solves the problems.
问题的复杂度是解决问题算法(包括未知算法)复杂度的下确界 infimum,即解决这个问题所需的最少时间。因此,问题的复杂度比任何解决该问题的算法的复杂度都要小。
It follows that every complexity that is expressed with big O notation is a complexity of the algorithm as well as of the corresponding problem.
It follows that every complexity that is expressed with big O notation is a complexity of the algorithm as well as of the corresponding problem.
用大O符号 big O notation表示的每一个复杂度都是算法的复杂度,也是相应问题的复杂度。
On the other hand, it is generally hard to obtain nontrivial lower bounds for problem complexity, and there are few methods for obtaining such lower bounds.
On the other hand, it is generally hard to obtain nontrivial lower bounds for problem complexity, and there are few methods for obtaining such lower bounds.
另一方面,问题复杂度的非平凡(nontrivial)下界一般难以获得,并且获得这种下界的方法很少。
For solving most problems, it is required to read all input data, which, normally, needs a time proportional to the size of the data. Thus, such problems have a complexity that is at least linear, that is, using big omega notation, a complexity [math]\displaystyle{ \Omega(n). }[/math]
For solving most problems, it is required to read all input data, which, normally, needs a time proportional to the size of the data. Thus, such problems have a complexity that is at least linear, that is, using big omega notation, a complexity [math]\displaystyle{ \Omega(n). }[/math]
为了解决大多数问题,往往需要与数据大小成比例的时间来读取所有输入数据。因此,这些问题的复杂度至少是线性的 linear,使用大欧米茄符号 big omega notation,记为复杂度[math]\displaystyle{ \Omega(n). }[/math]
The solution of some problems, typically in computer algebra and computational algebraic geometry, may be very large. In such a case, the complexity is lower bounded by the maximal size of the output, since the output must be written. For example, a system of n polynomial equations of degree d in n indeterminates may have up to [math]\displaystyle{ d^n }[/math] complex solutions, if the number of solutions is finite (this is Bézout's theorem). As these solutions must be written down, the complexity of this problem is [math]\displaystyle{ \Omega(d^n). }[/math] For this problem, an algorithm of complexity [math]\displaystyle{ d^{O(n)} }[/math] is known, which may thus be considered as asymptotically quasi-optimal.
The solution of some problems, typically in computer algebra and computational algebraic geometry, may be very large. In such a case, the complexity is lower bounded by the maximal size of the output, since the output must be written. For example, a system of polynomial equations of degree in indeterminates may have up to [math]\displaystyle{ d^n }[/math] complex solutions, if the number of solutions is finite (this is Bézout's theorem). As these solutions must be written down, the complexity of this problem is [math]\displaystyle{ \Omega(d^n). }[/math] For this problem, an algorithm of complexity [math]\displaystyle{ d^{O(n)} }[/math] is known, which may thus be considered as asymptotically quasi-optimal.
一些问题的解可能是非常大的,特别是计算机代数 computer algebra和计算代数几何 computational algebraic geometry。在这种情况下,复杂度以输出的最大长度为下界,因此输出必须被写入。例如,如果解的个数是有限的,n个d次不确定多项式方程组 system of n polynomial equations of degree d in indeterminates 可能有多达 [math]\displaystyle{ d^n }[/math]个复解(这是贝佐定理 Bézout's theorem)。由于这些解必须被写入,所以这个问题的复杂度是[math]\displaystyle{ \Omega(d^n). }[/math]。对于这个问题,已知一个复杂度为[math]\displaystyle{ d^{O(n)} }[/math]的算法,因此可以认为是渐近拟最优的。
A standard method for getting lower bounds of complexity consists of reducing a problem to another problem. More precisely, suppose that one may encode a problem A of size n into a subproblem of size f(n) of a problem B, and that the complexity of A is [math]\displaystyle{ \Omega(g(n)). }[/math] Without loss of generality, one may suppose that the function f increases with n and has an inverse function h. Then the complexity of the problem B is [math]\displaystyle{ \Omega(g(h(n))). }[/math] This is this method that is used for proving that, if P ≠ NP (an unsolved conjecture), the complexity of every NP-complete problem is [math]\displaystyle{ \Omega(n^k), }[/math] for every positive integer k.
A standard method for getting lower bounds of complexity consists of reducing a problem to another problem. More precisely, suppose that one may encode a problem of size into a subproblem of size of a problem , and that the complexity of is [math]\displaystyle{ \Omega(g(n)). }[/math] Without loss of generality, one may suppose that the function increases with and has an inverse function . Then the complexity of the problem is [math]\displaystyle{ \Omega(g(h(n))). }[/math] This is this method that is used for proving that, if P ≠ NP (an unsolved conjecture), the complexity of every NP-complete problem is [math]\displaystyle{ \Omega(n^k), }[/math] for every positive integer .
获得复杂度下限的标准方法包括将一个问题转化为另一个问题。更准确地说,假设一个大小为n的问题A可以编码成大小为f(n)的问题B,则问题A的复杂度为[math]\displaystyle{ \Omega(g(n)). }[/math]。不失一般性地,我们可以假设函数f随着n的增加而增加,并且有一个反函数 inverse functionh,那么问题B的复杂度就是[math]\displaystyle{ \Omega(g(h(n))). }[/math]。这个方法可以用来证明,若P ≠ NP(一个未解决的猜想) ,对于每个正整数k,每个NP完全问题 NP-complete problem 的复杂度都是 [math]\displaystyle{ \Omega(n^k), }[/math]。
在算法设计中的应用
Evaluating the complexity of an algorithm is an important part of algorithm design, as this gives useful information on the performance that may be expected.
Evaluating the complexity of an algorithm is an important part of algorithm design, as this gives useful information on the performance that may be expected.
评估算法的复杂度是算法设计 algorithm design的一个重要组成部分,因为这给出了一个算法关于预期性能的有用信息。
It is a common misconception that the evaluation of the complexity of algorithms will become less important as a result of Moore's law, which posits the exponential growth of the power of modern computers. This is wrong because this power increase allows working with large input data (big data). For example, when one wants to sort alphabetically a list of a few hundreds of entries, such as the bibliography of a book, any algorithm should work well in less than a second. On the other hand, for a list of a million of entries (the phone numbers of a large town, for example), the elementary algorithms that require [math]\displaystyle{ O(n^2) }[/math] comparisons would have to do a trillion of comparisons, which would need around three hours at the speed of 10 million of comparisons per second. On the other hand, the quicksort and merge sort require only [math]\displaystyle{ n\log_2 n }[/math] comparisons (as average-case complexity for the former, as worst-case complexity for the latter). For n = 1,000,000, this gives approximately 30,000,000 comparisons, which would only take 3 seconds at 10 million comparisons per second.
It is a common misconception that the evaluation of the complexity of algorithms will become less important as a result of Moore's law, which posits the exponential growth of the power of modern computers. This is wrong because this power increase allows working with large input data (big data). For example, when one wants to sort alphabetically a list of a few hundreds of entries, such as the bibliography of a book, any algorithm should work well in less than a second. On the other hand, for a list of a million of entries (the phone numbers of a large town, for example), the elementary algorithms that require [math]\displaystyle{ O(n^2) }[/math] comparisons would have to do a trillion of comparisons, which would need around three hours at the speed of 10 million of comparisons per second. On the other hand, the quicksort and merge sort require only [math]\displaystyle{ n\log_2 n }[/math] comparisons (as average-case complexity for the former, as worst-case complexity for the latter). For , this gives approximately 30,000,000 comparisons, which would only take 3 seconds at 10 million comparisons per second.
有一个常见的误解,由于摩尔定律 Moore's law假定了现代计算机功率的指数增长 exponential growth,对算法复杂度的评估将变得不那么重要。这是错误的,因为这种功率增加同样也会容使得处理较大的输入数据(大数据 big data)成为可能。例如,当一个人想要按字母顺序对数百个条目的列表进行排序时,比如一本书的参考书目 bibliography,任何算法都应该在不到一秒的时间内就能正常工作。另一方面,对于一个包含100万个条目的列表(例如一个大城镇的电话号码) ,需要[math]\displaystyle{ O(n^2) }[/math]次比较的基本算法,须进行1万亿次比较运算,即以每秒1000万次的速度进行比较需要大约3个小时。另一方面,快速排序 quicksort和合并排序 merge sort只需要[math]\displaystyle{ n\log_2 n }[/math]次比较(前者为平均情况复杂度,后者为最坏情况复杂度)。这大约是3000万次比较,以每秒1000万次比较计算,只需要3秒钟。
Thus the evaluation of the complexity may allow eliminating many inefficient algorithms before any implementation. This may also be used for tuning complex algorithms without testing all variants. By determining the most costly steps of a complex algorithm, the study of complexity allows also focusing on these steps the effort for improving the efficiency of an implementation.
Thus the evaluation of the complexity may allow eliminating many inefficient algorithms before any implementation. This may also be used for tuning complex algorithms without testing all variants. By determining the most costly steps of a complex algorithm, the study of complexity allows also focusing on these steps the effort for improving the efficiency of an implementation.
因此,对复杂度的评估允许在编程实现之前就消除许多低效率的算法。这也可用于在不用测试所有变量的情况下调优复杂的算法。通过确定复杂算法中最复杂的步骤,对复杂度的研究还可以将重点放在这些步骤上,从而提高实现的效率。
更多资料
引用
- Arora, Sanjeev; Barak, Boaz (2009), Computational Complexity: A Modern Approach, Cambridge, ISBN 978-0-521-42426-4, Zbl 1193.68112
- Du, Ding-Zhu; Ko, Ker-I (2000), Theory of Computational Complexity, John Wiley & Sons, ISBN 978-0-471-34506-0
- 模板:Garey-Johnson
- Goldreich, Oded (2008), Computational Complexity: A Conceptual Perspective, Cambridge University Press
- van Leeuwen, Jan, ed. (1990), Handbook of theoretical computer science (vol. A): algorithms and complexity, MIT Press, ISBN 978-0-444-88071-0
- Papadimitriou, Christos (1994), Computational Complexity (1st ed.), Addison Wesley, ISBN 0-201-53082-1
- Sipser, Michael (2006), Introduction to the Theory of Computation (2nd ed.), USA: Thomson Course Technology, ISBN 0-534-95097-3
Category:Analysis of algorithms
类别: 算法分析
Category:Computational complexity theory
编者推荐
集智文章推荐
找到你混乱生活中的秩序——Kolmogorov复杂度
如何判断一段话信息量是大是小?在香农那里,可以用“克服了多少不确定性”来描述。而在柯尔莫哥洛夫那里,要看“用多长的代码能生成这句话”。
本中文词条由小竹凉翻译,Ricky审校,糖糖编辑,如有问题,欢迎在讨论页面留言。
本词条内容源自wikipedia及公开资料,遵守 CC3.0协议。