Chaptcer 03 Algorithms 算法

covering 3.1~3.3


1. Definition 定义

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

Pseudocode(伪代码): Instructions given in a generic language similar to a computer language such as C++ or Pascal.

e.g. A pseudocode description of the algorithm for finding the maximum element in a finite sequence follows.

procedure  max(a1, a2, ..., an :integers)
max := a1
for i := 2 to n
    if max < ai then max:= ai
return max{max is the largest element}

2. Properties 属性

  • 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.

3. Examples 算法举例

3.1. Searching Algorithms 查找算法

3.1.1. Linear Search or sequential search 线性查找

3.1.2. Binary Search 二分查找

3.2. Sorting 排序算法

3.3. Optimization problem 最优化问题

3.3.1. Greedy Algorithm 贪心算法

Algorithms that make what seems to be “best” choice at each step

4. Halting Problem 停机问题

Can we develop a procedure that takes as input a computer program along with its input and determines whether the program will eventually halt with that input.

5. The Growth of Functions 函数的增长

Factors to be considered for choosing an algorithm

  • Simplicity - Easy to implement and obviously correct
  • Clarity - Easy to read and to maintain
  • Extensibility - Can easily be extended to new tasks
  • Reusability - Can be adapted to different tasks
  • Efficiency - Uses time and space well (computation complexity)

5.1. Asymptotic Running Time 渐进运行时间

A rough measurement of the running time of an algorithm based on the input size

  • Gives you the relative rate of growth but not specific time of execution

  • Allows for comparison of algorithms without implementation

  • Not hardware specific

Asymptotic running time : the number of operations used by the algorithm as the input size approaches infinity

5.2. Big-O Notation 大O记号

“f(x) is O(g(x))” if there are constants C and k such that |f(x)| ≤ C| g(x)| whenever x > k.

The constants C and k in the definition of big-O notation are called witnesses凭证) to the relationship f (x) is O(g(x)).

  1. Equivalent expressions:
  2. The pair (C,k) is never unique
  3. When f(x) is O(g(x)) , and h(x) is a function that has larger absolute values than g(x) does for sufficiently large values of x, it follows that f(x) is O(h(x))

If we have two standards, then two functions f(x) and g(x) that satisfy both of these big-O relationships are of the same order.

if a is the Big-O of b, then b grows faster.

5.2.1. Some Important Big-O Results

Let , where are real numbers, then f(x) is

5.3. Growth of Combinations of Functions

5.3.1. Addition of functions

If is and is , then is

5.3.2. Multiplication of functions

If is and is , then is

5.4. Big-Omega and Big-Theta Notation

is if there are constants C and k such that whenever x > k.

is if is and is

is when there are real numbers and and a positive real number k such that

6. Complexity of Algorithms 算法复杂度

6.1. Space Complexity 空间复杂度

Gives the approximate amount of memory required to solve a problem of size n.

Often tied in with the particular data structures used to implement the algorithm

6.2. Time Complexity 时间复杂度

Gives the approximate number of operations required to solve a problem of size n.

6.3. Different types of analysis 复杂度分析

6.3.1. Worst-case analysis 最坏情形

  • Maximum number of operations

  • Is a guarantee over all inputs of a given size

6.3.2. Best-case analysis 最好情形

  • Minimum number of operations

  • Not very practical

6.3.3. Average-case analysis 平均情形

  • Average number of operations assuming an input probability distribution

  • An average over all the possible inputs of a given size

  • Can be complicated

7. Understanding the complexity of algorithm

Tractable易解的): A problem is solvable using an algorithm with polynomial worst-case complexity.

Intractable难解的): A problem cannot be solved using an algorithm with worst-case polynomial time complexity.

Solvable可解

Unsolvable不可解): Some problems even exist for which it can be shown that no algorithm exists for solving them.

7.1. P&NP

Class PP类问题): tractable problems

Class NP (nondeterministic polynomial time(非确定性多项式时间))(NP类): problems for which a solution can be checked in polynomial time.

NP-completeNP完全问题): an important class of problems with the property that if any of these problems can be solved by a polynomial worst-case time algorithm, then all can be solved by polynomial worst-case time algorithms.

The P versus NP problemP与NP问题) asks whether NP, the class of problems for which it is possible to check solutions in polynomial time, equals P, the class of tractable problems. If P=NP, there would be some problems that cannot be solved in polynomial time, but whose solutions could be verified in polynomial time.

P与NP问题有点类似非对称加密,无法直接解密,但是可以快速验证答案真伪

Complexity Terminology
Constant complexity 常量复杂度
Logarithmic complexity 对数复杂度
Linear complexity 线性复杂度
Linearithmic complexity 线性对数复杂度
Polynomial complexity 多项式复杂度
Exponential complexity 指数复杂度
Factorial complexity 阶乘复杂度

results matching ""

    No results matching ""