2 分治算法
2.1 乘法
- xy=2nxLyL+2n/2(xLyR+xRyL)+xRyR
- xLyR+xRyL=(xL+xR)(yL+yR)−xLyL−xRyR
function multiply(x,y)
Input:Two n-bit integers x and y
Output:Their product
if n=1: return xy
xL,yL = leftmost ⌊ n/2 ⌋ bits of x,y
xR,yR = rightmost ⌈ n/2 ⌉ bits of x,y
P1 = multiply(xL,yL)
P2 = multiply(xR,yR)
P3 = multiply(xL+xR,yL+yR)
return P1*2^n+(P3-P1-P2)*2^(n/2)+P2
2.2 递推式
主定理:规模n的问题,递归求解a个规模为n/b的子问题,在O(nd)的时间内合并子问题的解有:T(n)=a∗T(⌈n/b⌉)+O(nd)T(n)=⎧⎩⎨⎪⎪O(nd)O(ndlogn)O(nlogba)d>logbad=logbad<logba
2.3 合并排序
function mergesort(a[1...n])
Input: An array of numbers a[1..n]
Output:A sorted version of array a
if n>1:
return merge(mergesort(a[1...⌊ n/2 ⌋]),mergesort(a[⌊ n/2 ⌋+1...n]))
else:
return a
function merge(x[1...k],y[1...j])
if k=0: return y[1...j]
if l=0: return x[1...k]
if x[1]≤y[1]:
return x[1] ⋃ merge(x[2...k],y[1...j])
else:
return y[1] ⋃ merge(x[1...k],y[2...j])
2.4 寻找第k小元素
k_small(S,k)=⎧⎩⎨k_small(SL,k)vk_small(SR,k)k≤|SL||SL|<k≤|SL|+|SV|k>|SL|+|SV|
2.5 矩阵乘法
XY=[P5+P4−P2+P6P3+P4P1+P2P1+P5−P3−P7]其中,P1=A(F−H) P5=(A+D)(E+H)P2=H(A+B) P6=(B−D)(G+H)P3=E(C+D) P7=(A−C)(E+F)P4=D(G−H)算法复杂度:T(n)=7T(n/2)+O(n2)
2.6 快速Fourier变换
- 多项式 系数表示法: a0,a1,...,ad
多项式 值表示法: A(x0),A(x1),...,A(xd)
- 1→2为“计算evaluation”,2→1为“插值interpolation”
function Polynomial_multiplication(a[0...d],b[0..d])
Input: Coefficients of two polynomials, A(x) and B(x), of degree d
Output: Their product C = A · B
Selection
Pick some points x0, x1, . . . , xn − 1, where n ≥ 2d + 1
Evaluation
Compute A(x0), A(x1), . . . , A(xn − 1) and B(x0), B(x1), . . . , B(xn − 1)
Multiplication
Compute C(xk) = A(xk)B(xk) for all k = 0, . . . , n − 1
Interpolation
Recover C(x) = c0 + c1*x + · · · + c2d*x^2d
- 快速Fourier变换:分治算法
function FFT(A,w)
Input:Coefficeient representation of a polynomial A(x)
of degreee d<=n-1 ,where n is a power of 2
w,an nth root of unity
Output:Value representation A(w^0),...,A(w^n-1)
if w=1: return A(1)
express A(x) in the form Ae(x^2)+Ao(x^2)
call FFT(Ae,w^2) to evaluate Ae at even powers of w
call FFT(Ao,w^2) to evaluate Ao at odd powers of w
for j=0 to n-1:
compute A(w^j)=Ae(w^2j)+w^j*Ao(w^2j)
return A(w^0),...,A(w^n-1)
- 快速Fourier变换的内部机制
function FFT(a,w)
Input:An array a=(a0,...,an-1),for n a power of 2
A primitive nth root of unity, w
Output:Mn(w)*a
if w=1: return a
(s0,s1,...,sn/2-1)=FFT((a0,a2,...,an-2),w^2)
(h0,h1,...,hn/2-1)=FFT((a1,a3,...,an-1),w^2)
for j=0 to n/2-1
rj = sj + w^j*hj
rj+n/2=sj-w^j*hj
return (ro,r1,...,rn-1)