Chapter 1 Introduction
About introduction
(Algorithm + data structure) = program
Turing-the father of computer science and artificial intelligence
Turing Award-the Nobel Prize in the computer world
Turing Test
Algorithm related properties: input, output, certainty, finiteness
Algorithm: a method or process to solve a problem
Algorithm correctness
An algorithm is correct if it eventually stops for every input and produces the correct output.
Program debugging can only prove that the program is wrong, not that it is error-free!
Algorithm complexity
Time complexity
The time required for an algorithm to run on an abstract computer
(the number of atomic operations or "steps" required to produce a result on a specific input)
T = T (N, I) T = T(N,I)T=T(N,I)
N: Scale of the problem
I: Input
1. The time complexity in the worst case 2. The time complexity in the
best case 3. The time complexity in the
average case
Time complexity token
f(N) and g(N) are positive functions defined on the set of positive numbers.
If there is a normal number CCC and natural numberN 0 N_0N0
1, upper bounded OOTHE
f ( N ) f(N) f ( N ) is bounded when N is sufficiently large: whenN ≥ N 0 N \geq N_0N≥N0When, f (N) ≤ C g (N) f(N) \leq Cg(N)f(N)≤Cg(N), g ( N ) g(N) g ( N ) isf (N) f(N)an upper bound of f ( N )
f(N)=O(g(N))
O ( 1 ) < O ( l o g 2 n ) < O ( n ) < O ( n l o g 2 n < O ( n 2 ) < ⋅ ⋅ ⋅ < O ( 2 n ) < O ( n ! ) < O ( n n ) O(1) < O({log}_2n) <O(n) <O(n{log_2}n <O(n^2)<···<O(2^n)<O(n!)<O(n^n) O ( 1 )<O(log2n)<O ( n )<O ( n l o g2n<O ( n2)<⋅⋅⋅<O ( 2n)<O ( n ! )<O ( nn)
2. Lower bound Ω \OmegaΩ
f ( N ) f(N) f ( N ) is bounded when N is sufficiently large: whenN ≥ N 0 N \geq N_0N≥N0When, f (N) ≥ C g (N) f(N) \geq Cg(N)f(N)≥Cg(N), g ( N ) g(N) g ( N ) isf (N) f(N)a lower bound of f ( N )
f(N)=Ω(g(N))
Polynomial complexity
Efficient algorithm, stacking hardware
Exponential complexity
Inefficient algorithms, stacking hardware is useless
3. The same order θ \thetaθ
f ( N ) f(N) f ( N ) is equivalent tog (N) g(N)g ( N ) same order: if and only iff (N) = O (g (N)) f(N) = O(g(N))f(N)=O(g(N))且 f ( N ) = Ω ( g ( N ) ) f(N) =\Omega(g(N)) f(N)=Ω ( g ( N ) )
f(N)=θ(g(N))
Algorithm
O(f) + O(g) = O(max(f,g))
O(f) + O(g) = O(f + g)
O(f)O(g) = O(fg)
若g(N) = O(f(N)),则O(f) + O(g) = O(f)
O(Cf(N)) = O(f(N)),其中C是一个正的常数
f = O(f)
Space complexity
The amount of storage space required by an algorithm to produce results for a specific input
S = S (N, I) S = S(N, I)S=S(N,I)