Introduction

  • Recursively:

    • Divide the problem into a number of sub-problems
    • Conquer the sub-problems by solving them recursively
    • Combine the solutions to the sub-problems into the solution for the original problem
  • General recurrence: T(N)=aT(N/b)+f(N)T(N)=aT(N/b)+f(N)

  • Cases solved by divide and conquer

    • The maximum subsequence sum – the O(NlogN)O( N\log N ) solution
    • Tree traversals – O(N)O( N )
    • Mergesort and quicksort – $O( N\log N ) $

Closest Points Problem

  • Given NN points in a plane. Find the closest pair of points. (If two points have the same position, then that pair is the closest with distance 0.)
  • Check N(N1)/2N(N-1)/2 pairs of points
  • T=O(N2)T=O(N^2)

Divide and Conquer

  • Step1: Sort according to x-coordinates and divide
  • Step2: Conquer by forming a solution from left, right, and cross

T(N)=2T(N/2)+cN=22T(N/22)+2cN==2kT(N/2k)+kcN=N+cNlogN=O(NlogN)T(N)=2T(N/2)+cN=2^2T(N/2^2)+2cN=\cdots=2^kT(N/2^k)+kcN=N+cN\log N=O(N\log N)

T(N)=2T(N/2)+cN2=22T(N/22)+cN2(1+1/2)==2kT(N/2k)+cN2(1+1/2++1/2k1)=O(N2)T(N)=2T(N/2)+cN^2=2^2T(N/2^2)+cN^2(1+1/2)=\cdots=2^kT(N/2^k)+cN^2(1+1/2+\cdots+1/2^{k-1})=O(N^2)

δ\delta-strip

  • δ\delta is the smallest distance

image-20210421223431268

  • If NumPointInStrip = O(N)O(\sqrt N), we have

    1
    2
    3
    4
    5
    /* points are all in the strip */
    for (i = 0; i < NumPointsInStrip; i++)
    for (j = i+1; j < NumPointsInStrip; j++)
    if (Dist(Pi, Pj) < delta)
    delta = Dist(Pi, Pj);

    The worst case: NumPointInStrip = N

  • Consider both vertical and horizontal δ\delta-strip

    1
    2
    3
    4
    5
    6
    7
    8
    /* points are all in the strip */
    /* and sorted by y coordinates */
    for (i = 0; i < NumPointsInStrip; i++)
    for (j = i+1; j < NumPointsInStrip; j++)
    if ( Dist_y(Pi, Pj) > delta)
    break;
    else if (Dist(Pi, Pj) < delta)
    delta = Dist(Pi, Pj);

    The worst case: 8 points sitting all at corners

    image-20210421224322951

    For any pip_i, at most 7 points are considered

  • f(N)=O(N)f(N)=O(N)

Three methods for solving recurrences

  • T(N)=aT(N/b)+f(N)T(N)=aT(N/b)+f(N)
  • Details to be ignored:
    • if (N/b)(N/b) is an integer or not
    • always assume T(n)=Θ(1)T(n)=\Theta(1) for small nn

Substitution Method 代入法

  • 猜测解的形式,然后用数学归纳法求出解中的常数并证明

  • Example: T(N)=2T(N/2)+NT(N)=2T(\lfloor N/2\rfloor)+N

    • Guess: T(N)=O(NlogN)T(N)=O(N\log N)

    • Proof: Assume it is true for all m<Nm<N, in particular for m=N/2m=\lfloor N/2\rfloor

      Then there exists a constant c>0c > 0 so that T(N/2)cN/2logN/2T(\lfloor N/2\rfloor)\leq c\lfloor N/2\rfloor\log\lfloor N/2\rfloor

      Substituting into the recurrence:

      T(N)=2T(N/2)+N2cN/2logN/2+NcN(logNlog2)+NcNlogN(c1)T(N)=2T(\lfloor N/2\rfloor)+N\leq2c\lfloor N/2\rfloor\log\lfloor N/2\rfloor+N\leq cN(\log N-\log2)+N\leq cN\log N(c\geq1)

  • 需要证出与归纳假设严格一致的形式

    image-20210509110533347
  • 改变变量

    • T(N)=2T(N)+logNT(N)=2T(\lfloor\sqrt N\rfloor)+\log N
    • M=logNM=\log NT(2M)=2T(2M/2)+MT(2^M)=2T(2^{M/2})+M
    • S(M)=T(2M)S(M)=T(2^M)S(M)=2S(M/2)+M=O(MlogM)S(M)=2S(M/2)+M=O(M\log M)
    • T(N)=O(logNloglogN)T(N)=O(\log N\log\log N)

Recursion-tree Method 递归树法

  • 用来生成好的猜测
image-20210424220934390 $$ T(N)=\sum^{\log_4N-1}_{i=0}(\frac3{16})^icN^2+\Theta(N^{\log_43})<\sum^\infin_{i=0}(\frac3{16})^icN^2+\Theta(N^{\log_43})=\frac{cN^2}{1-3/16}+\Theta(N^{\log_43})=O(N^2) $$

image-20210425221651118

  • Proof by substitution:

    T(N)=T(N/3)+T(2N/3)+cNd(N/3)log(N/3)+d(2N/3)log(2N/3)+cN=dNlogNdN(log232/3)+cNdNlogN(dc/(log232/3))T(N)=T(N/3)+T(2N/3)+cN\\\leq d(N/3)\log(N/3)+d(2N/3)\log(2N/3)+cN\\=dN\log N-dN(\log_23-2/3)+cN\\\leq dN\log N(d\geq c/(\log_23-2/3))

Master Method 主方法

[Master Theorem] Let a1a\geq 1 and b>1b > 1 be constants, let f(N)f(N) be a function, and let T(N)T(N) be defined on the nonnegative integers by the recurrence T(N)=aT(N/b)+f(N)T(N)=aT(N/b)+f(N), then:

  1. If f(N)=O(Nlogbaδ)f(N)=O(N^{\log_b {a-\delta}}) for some constant δ>0\delta>0, then T(N)=Θ(Nlogba)T(N)=\Theta(N^{\log_b a})
  2. If f(N)=Θ(Nlogba)f(N)=\Theta(N^{\log_ba}), then T(N)=Θ(NlogbalogN)T(N)=\Theta(N^{\log_b a}\log N)
  3. If f(N)=Ω(Nlogba+δ)f(N)=\Omega(N^{\log_b a+\delta}) for some constant δ>0\delta>0, and if af(N/b)<cf(N)af(N/b)<cf(N) for some constant c<1c<1 and all sufficiently large NN, then T(N)=Θ(f(N))T(N)=\Theta(f(N))
  • Master Method cannot cover all the cases of f(N)f(N)

[Master Theorem] The recurrence T(N)=aT(N/b)+f(N)T(N)=aT(N/b)+f(N) can be solved as follows:

  1. If af(N/b)=Kf(N)af(N/b)=Kf(N) for some constant K>1K>1, then T(N)=Θ(Nlogba)T(N)=\Theta(N^{\log_b a})
  2. If af(N/b)=f(N)af(N/b)=f(N), then T(N)=Θ(f(N)logbN)T(N)=\Theta(f(N)\log_b N)
  3. If af(N/b)=kf(N)af(N/b)=kf(N) for some constant k<1k<1, then T(N)=Θ(f(N))T(N)=\Theta(f(N))

[Theorem] The solution to the equation T(N)=aT(N/b)+Θ(NklogpN)T(N)=aT(N/b)+\Theta(N^k\log^pN) where a1a\geq1, b>1b>1 and p0p\geq0 is

T(N)={O(Nlogba)if a>bkO(Nklogp+1N)if a=bkO(NklogpN)if a<bkT(N) = \begin{cases} O(N^{\log_b a}) & \text{if $a>b^k$} \\ O(N^k\log^{p+1}N) & \text{if $a=b^k$} \\ O(N^k\log^pN) & \text{if $a<b^k$} \end{cases}