(Algorithm Analysis -- the base axiom)
算法分析包括许多方面,最重要的是“
determine the running time of a program as a function of its inputs
”,即“时间复杂度”,它与输入有关。此外,算法分析的时候,还需要忽略硬件和操作系统的影响,为此,我们引入了“model”(模型)。按我的理解,所谓模式就是对时间的抽象,它不计算绝对时间,而是计算一些抽象意义的时间,比如Tfecth、Tstore、Tcall等。而模型是基于一些基本的公理,这些公理包括:
Axiom
The time required to fetch an operand from memory is a constant,
, and the time required to store a result in memory is a constant,
.
公理一:从内存中取操作数的时间是恒定的(Tfetch),将结果存储到内存中的时间也是恒定的(Tstore)。
Axiom
The times required to perform elementary arithmetic operations, such as addition, subtraction, multiplication, division, and comparison, are all constants. These times are denoted by
,
,
,
, and
, respectively.
公理二:所有的基本算术运算的时间是恒定的,如加减乘除和比较。
Axiom
The time required to call a method is a constant,
, and the time required to return from a method is a constant,
.
公理三:函数调用和函数返回的时间是恒定的。
“
While the method call/return overhead may be rather large, nevertheless it entails a constant amount of work.
”这句话是说,尽管函数调用和返回的开销相当大,但时它是一个固定量值的工作。
Axiom
The time required to pass an argument to a method is the same as the time required to store a value in memory,
.
公理四:函数调用过程中的参数传递(实参到形参),它的时间开销与“将值存到内存中是相同的”。
综上,这四个公理揭示了算法模型中的几项基本操作,这几项基本操作的时间值都是常量。除了这些时间值为常量的基本操作外,还有一类时间值随输入不同而不同的操作。比如数组元素的个数不同,在数组中进行匹配搜索的所消耗的时间也不同。