Ch3 Algorithm

Algorithms

Introduction

An 算法(algorithm) is a finite set of precise instructions for performing a computation or for solving a problem.

Properties of Algorithms

算法一般都有一些共同性质,在描述与评价算法时这种性质是常用的。

  • 输入(input):An algorithm has input values from a specified set.
  • 输出(output):From each set of input values, an algorithm produces output values from a specified set.
  • 确定性(definiteness):算法的每一步都应该被精确定义。The steps of an algorithm must be defined precisely.
  • 正确性(correctness):算法应该给出正确的输出结果。An algorithm should produce the correct output values for each set of input values.
  • 有限性(finiteness):算法应当在有限步内结束。An algorithm should produce the desired output after a finite number of steps for any input in the set.
  • 有效性(effectiveness):算法的每一步都可以被有效执行。Each step of an algorithm must be executed exactly and in a finite amount of time.
  • 通用性(generality):我们的算法应该对于任意符合条件的输入都应用,而不是只适用某些特定的输入。The procedure should be applicable for all problems of the desired form, not just for a particular set of input values.
correctness 和 effectiveness 辨析 ★

correctness 主要将算法应该给出 correct output values,而 effectiveness 则主要指出算法应当是可以被 exactly execute。作业题中有一题是一算法会出现 1/01/0 的情况,应该判定为缺少 effectiveness.

The Growth of Functions

Asymptotic Running Time

渐进运行时间(asymptotic running time) is the number of operations used by the algorithm as the input size approaches infinity.

Asymptotic Notation

大 O 记号(Big-O notation):Let ff and gg be functions from ZZ (or RR) to RR. We say that “f(x)f(x) is O(g(x))O(g(x))” if there are constants CC and kk such that f(x)Cg(x)|f(x)| \le C|g(x)| whenever x>kx>k.

Ω\Omega记号(Big-Omega notation):Let ff and gg be functions from ZZ (or RR) to RR. We say that “f(x)f(x) is Ω(g(x))\Omega(g(x))” if there are constants CC and kk such that f(x)Cg(x)|f(x)|\ge C|g(x)| whenever x>kx>k.

Θ\Theta记号(Big-Theta notation):Let ff and gg be functions from ZZ (or RR) to RR. We say that “f(x)f(x) is Θ(g(x))\Theta(g(x))” if “f(x)f(x) is O(g(x))O(g(x))” and “f(x)f(x) is Ω(g(x))\Omega(g(x))”, i.e., there are constants C1C_1, C2C_2, and kk such that 0C1g(x)f(x)C2g(x)0\le C_1 g(x) \le f(x) \le C_2 g(x) whenever x>kx>k.

Complexity of Algorithms

Commonly used Terminology for the Complexity of Algorithms
ComplexityTerminology
Θ(1)\Theta(1)常数复杂度(constant complexity)
Θ(logn)\Theta(\log n)对数复杂度(logarithmic complexity)
Θ(n)\Theta(n)线性复杂度(linear complexity)
Θ(nlogn)\Theta(n \log n)
Θ(nb)\Theta(n^b)多项式复杂度(polynomial complexity)
Θ(bn)\Theta(b^n)指数复杂度(exponential complexity) (b>1)
Θ(n!)\Theta(n!)阶乘复杂度(factorial complexity)

Comments