【算法概論】2.分治算法

2 分治算法

2.1 乘法

  • xy=2nxLyL+2n/2(xLyR+xRyL)+xRyR
  • xLyR+xRyL=(xL+xR)(yL+yR)xLyLxRyR
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 遞推式

nan/bO(nd)T(n)=aT(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+P4P2+P6P3+P4P1+P2P1+P5P3P7]P1=A(FH)  P5=(A+D)(E+H)P2=H(A+B)  P6=(BD)(G+H)P3=E(C+D)  P7=(AC)(E+F)P4=D(GH)T(n)=7T(n/2)+O(n2)

2.6 快速Fourier變換

  1. 多項式 係數表示法:a0,a1,...,ad
  2. 多項式 值表示法:A(x0),A(x1),...,A(xd)

    • 12evaluation21interpolation
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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章