算法及其量度
In practical terms,a goal of algorithmic analysis is“to obtain sufficient understanding about the relative merits of complicated algorithms to be able to provide useful advice to someone undertaking an actual computation”(Gentleman,1973)[1].In broader interpretation,however,algorithmic analysis includes the study of all aspects of performance in computational problem solving,from the preliminary formulation,through the programming stages,to the final task of interpreting the results obtained[2].We would also like to prove lower bounds on the computation time of various classes of algorithms.In order to show that there is no algorithm to perform a given task in less than a certain amount of time,we need a precise definition of what constitutes an algorithm.First,then,what is an algorithm?
A procedure consisting of a finite set of unambiguous rules which specify a finite sequence of operations that provides the solution to a problem,or to a specific class of problems,is called an algorithm[3].
Several important features of this definition must now be emphasized.
First,each step of an algorithm must be unambiguous and precisely defined.The actions to be carried out must be rigorously specified for each case.
Secondly,an algorithm must always arrive at a problem solution after a finite number of steps.Indeed,the general restriction of finiteness is not sufficient in practice,as the number of steps needed to solve a specific problem,although finite,may be too large for practicable computation.A useful algorithm must require not only a finite number of steps,but a reasonable numher.
Thirdly,every meaningful algorithm possesses zero or more inputs and provides one or more outputs.The inputs may he defined as quantities which are given to the algorithm initially,before it is executed,and the outputs as quantities which have a specified relation to the inputs and which are delivered at the completion of its execution.
Fourthly,it is preferable that the algorithm should be applicable to any member of a class of problems rather than only to a single problem.This property of generality,though not a necessity,is certainly a desirable attribute of a useful algorithm[4].
Finally,we would like to mention that although the concept of an algorithm is a very broad one,in this book we restrict ourselves to algorithms designed to be executed on a computer.
It is relatively easy to invent algorithms.In practice,however,one wants not only algorithms,one wants good algorithms.Thus,the objective is to invent good algorithms and prove that they are good.The“goodness”of an algorithm can be appraised by a variety of criteria.One of the most important is the time taken to execute it.
The performance time of an algorithm is a function of the size of the computational problem to be solved.However,assuming we have a computer program which eventually terminates, solving a particular problem requires only sufficint time and sufficient storage.Of more general interest are algorithms which can be applied to a collection of problems of a certain type.For these algorithms,the time and storage space required by a program will vary with the particular problem being solved.Consider, for example, the following classes of problems,and note the role of the value of the parameter n.
(1)Find the largest in a sequence of n integers.
(2)Solve a set of linear algebraic equations AX = b,where A is an n×n real matrix and b is a real vector of length n.
(3)Let W be a one-dimensional array of n distinct integers.Sort the entries of W into descending order.
(4)Evaluate a polynomial
In each of these problems,the parameter n provides a measure of the size of the problem in
the sense that the time required to solve the problem,or the storage space required,or both,will increase as n increases.
从实际应用的角度讲,算法分析的一个目标是使人们对复杂的算法的相对价值有足够多的了解,从而向进行具体计算的人提供有用的建议。然而,更广义地讲,算法分析包括进行问题求解计算的各个方面的研究,从先列出基本公式,到程序化阶段,到最后解释获得的结果,人们也希望证明各种各样算法的计算时间下界。为了说明没有一种算法能在小于规定的时间内完成给定任务,我们需要精确定义算法是由什么组成的。那么,什么是算法呢?
若一过程由无歧义的规则的有限集合组成,这些规则规定了解决某个问题或某类特定问题的操作的有限序列,该过程就叫算法。
在此必须强调此定义的几个重要特性。
首先,一个算法的每一步必须是无歧义的和精确定义的。在各种情况下所要执行的动作必须严格规定。
第二,一个算法经过有限步骤之后必须能解决某个问题。事实上,一般的有限性限制在实践中是不够的,因为尽管解决某个特定问题的执行步骤数量是有限的,但可能对实际计算来说仍太大。一个有用的算法不仅要求步骤有限,同时也要求步数合理。
第三,每个有意义的算法都有零个或多个输入,并且提供一个或多个输出。输入可定义为算法执行前初始化时给定的一些量,输出指与输入有某种特定关系,在算法执行完成时产生的一些量。
第四,一个算法最好适用于某类问题而不只适用于一个问题。这种通用性,尽管不是必备的,但肯定是人们期望的有用的算法特性。
最后应提及的是,尽管算法的概念非常广泛,但在本书中仅限于为在计算机上运行所设计的算法。
发明算法比较容易,不过人们实际上想要的不只是算法,而是要好的算法。因此,发明好的算法并且证明它们是好算法才是目的。一个算法的优越性可通过多种准则来评定,其中最重要的一条就是执行该算法所花的时间。
一个算法的执行时间是要解决的计算问题规模的函数。但是,假设有一个能终止的计算程序,它解决特定问题只需要足够的时间和存储空间。一般而言,人们对那些可用于解决某一类问题的算法更感兴趣,对这些算法,程序需要的时间和存储空间随被解决的实际问题而变化。例如考虑下面的各类问题,并注意参数n的值的作用。
(1)在n个整数序列中找出最大值。
(2)解一组线性代数方程AX = b,其中A是一个n×n实矩阵,b是长度为n的实向量。
(3)假设W是n个不同的整数构成的一维数组,将W按降序排序。
(4)对多项式 求值。
在上述每一个问题中,由于解决问题所需要时间或存储空间或两者同时随n的增加而增加,因此参数n是提供问题规模的一种度量。