The design process is divided into three stages
Divide: The whole problem is divided into multiple sub-problems T (n) =d (n)
Conquer: Solving each sub-problem (recursive invocation of a positive design algorithm) T (n) =at (n/b)
Combine: The solution of merging sub-problems, forming the solution of the original problem T (n) =c (n)
Note: Divide the problem of size n into a sub-problem, and the size of each problem is n/b. (b may not be equal to a!) )
Time complexity: T (N) =θ (1) If N<C constant
T (n) =at (n/b) +d (n) +c (n)
Classic Question 1: merge sort
Divide stage: O (1) at intermediate node segment time
Conquer stage: Two sub-problems each problem size is N/2 so t (n) =2t (N/2)
Combine phase: O (n) in sequence
So time complexity is T (n) =2t (N/2) +o (n) Master method calculation (2nd case) T (n) = (NLOGN)
Classic question 2: large integer multiplication
Input:n -bit binary integer x and y output: Product ofx and y
Divide and conquer thought:
X = a<<n/2+b y= c<<n/2 + D divides the large number into two sub-sections
X*y = (a<<n/2+b) * (c<<n/2+ D) = (a*c) <<n + (a*d+b*c) <<N/2 +c*d
Divide the XY problem into four sub-problems of computing AC,BD,AD,BC, each with a scale of N/2 (!!). A is not equal to B)
T (n) =4t (N/2) +θ (n) Master method calculates the Master method calculation (first case) t (n) = (n^2)
Optimization:
(A-B) * (c-d) = ac+bd-(Bc+ad) so the xy equation (a*d+b*c) can be replaced with ac+bd+ (B-A) (c-d).
That is, the original calculation AC,BD,AD,BC four sub-problems, converted to AC, BD, CD three sub-problems.
T (n) =3t (N/2) +θ (n) calculated t (n) =o (n^ (log2^3)) ≈o (n^ (1.59))
Classic Question 3: matrix multiplication
Input: Two matrices A, B output: a*b
Divide and Conquer:
General thinking is divided into 8, the scale of N/2 sub-problem. So t (n) =8t (N/2) +θ (n)
But reducing the number of sub-problems is a good direction of optimization. Then there are thoughtful people who propose 7 sub-problems to achieve:
M1 = A11 (b12-b22)
M2 = (A11 + A12) B22
M3 = (A21 + A22) B11
M4 = A22 (B21-B11)
M5 = (A11 + A22) (B11 + B22)
M6 = (a12-a22) (B21 + B22)
M7= (A11-A12) (B11 + B12)
C11 = M5 + m4-m2 + M6 C12 = M1 + M2 C21 = M3 + M4 C22 = M5 + m1–m3–m7 |
|
Then it succeeds in reducing the complexity of the time. T (n) =7t (N/2) +θ (n) =o (n^ (log2^7)) ≈o (n^ (2.81))
The classical divide-and-conquer algorithm mentioned above basically reduces the number of sub-problems to reduce the complexity of time, and there is no special treatment in the division and merging phase.
The next few chapters focus on the more interesting three questions.
Divide-and-conquer Overview of divide and conquer algorithm