Try   HackMD
tags: ADA 3.1 Solving Recurrences Substitution Method

ADA 3.1: Solving Recurrences & Substitution Method 取代法

Textbook Chapter 4.3 - The substitution method for solving recurrences 課本 p.104
Textbook Chapter 4.4 - The recursion-tree method for solving recurrences 課本 p.109
Textbook Chapter 4.5 - The master method for solving recurrences 課本 p.114

D&C Algorithm Time Complexity

  • T(n)
    : running time for input size n
  • D(n)
    : time of Divide for input size n
  • C(n)
    : time of Combine for input size n
  • a: number of subproblems
  • n/b: size of each subproblem

T(n)={O(1)if ncaT(n/b)+D(n)+C(n)otherwise 


Solving Recurrences

  1. Substitution method(取代法)
    • Guess a bound and then prove by induction (怎麼猜是有策略的)
  2. Recursion-Tree method(遞迴樹法)
    • Expand the recurrence into a tree and sum up the cost
    • 這個方法最直覺,但要把式子嚴謹地展開寫出來會非常的麻煩
  3. Master method(套公式大法/大師法)
    • Apply Master Theorem to a specific form of recurrences
  • Useful simplification tricks
    • Ignore floors, ceilings, boundary conditions (proof in Ch. 4.6)
    • Assume base cases are constant (for small n)

Substitution Mehtod

Textbook Chapter 4.3 - The substitution method for solving recurrences

Review

  • Time Complexity for Merge Sort
  • Theorem

T(n)={O(1)if n=12T(n/2)+O(n)if n2
T(n)=O(nlogn)

  • Proof

    • There exists positive constant a,b s.t.
      T(n)={O(1)if n=12T(n/2)+O(n)if n2
    • Use induction to prove
      T(n)bnlogn+an
      (會講怎麼知道要加後面這個an)
      • n = 1 , trivial

      • n > 1,

        T(n)2T(n/2)+bn

        2[bn2logn2+an2]+bn

        =bnlognbn+an+bn

        =bnlogn+an

        💡 這裡需要查一下對數運算

        logabc=logablogac
        所以
        2[bn2logn2+an2]+bn

        bnlogn2+an+bn

        =bn(lognlog2)+an+bn

        =bnlognbn+an+bn

        =bnlogn+an

    💡 Substitution method (取代法)
    guess a bound and then prove by induction

取代法流程: 猜 → 驗證 → 解決(沒辦法解決就重猜)

  • Guess the form of the solution
  • Verify by mathematical induction (數學歸納法)
  • Solve constants to show that the solution works
  • Prove O and
    Ω
    separately

Example

T(n)={O(1)if n=14T(n/2)+O(n)if n2

  • Proof
    • T(n)=O(n3)

      There exists postive constant

      n0,c s.t. for all
      nn0,T(n)cn3

    • Use induction to find the constant

      n0,c

      • n=1
        , trivial
      • n>1
        ,

      T(n)4T(n/2)+bn

      4c(n/2)3+bn inductive hypothesis

      =cn3/2+bn

      =cn3(cn3/2bn)

      💡

      cn3/2bn>0
      e.g.
      c2b,n1

      cn3

    • T(n)cn3 holds when
      c=2b,n0=1

上面的例子太鬆了找一個再緊一點的試試看

  • Proof
    • T(n)=O(n2)

      There exists postive constant

      n0,c s.t. for all
      nn0,T(n)cn2

    • Use induction to find the constant

      n0,c

      • n=1
        , trivial
      • n>1
        ,

      T(n)4T(n/2)+bn

      4c(n/2)2+bn inductive hypothesis

      =cn2+bn

      💡 證不出來…
      猜錯了?還是推導錯了?

      💡 沒猜錯,推導也沒有錯
      這次取代法的小盲點

所以策略就是,希望在計算的過程中生出一個負的東西可以把 bn 消掉

  • Proof
    • T(n)=O(n2)

      There exists postive constant

      n0,c1,c2 s.t. for all
      nn0,T(n)c1n2c2n

      💡 Strengthen the induction hypothesis by substracting a low-order term

    • Use induction to find the constant

      n0,c1,c2

      • n=1,T(1)c1c2 holds for
        c1c2+1

      • n>1,

        T(n)4T(n/2)+bn

        4[c1(n/2)2c2(n/2)]+bn inductive hypothesis

        =c1n22c2n+bn

        =c1n2c2n(c2nbn)

        💡

        c2nbn0
        e.g.
        c2b,n0

        c1n2c2n

    • T(n)c1n2c2n holds when
      c1=b+1,c2=b,n0=0

Useful Tricks

  • Guess based on seen recurrences
  • Use the recursion-tree method
  • From loose bound to tight bound
  • Strengthen the inductive hypothesis by subtracting a low-order term
  • Change variables
    • E.g.,
      T(n)=2T(n)+logn
    1. Change vairable:
      k=logn,n=2k
      T(2k)=2T(2k/2)+k
    2. Change variable again:
      S(k)=T(2k)
      S(k)=2S(k/2)+k
    3. Solve recurrence
      S(k)=Θ(klogk)
      T(2k)=Θ(klogk)
      T(n)=Θ(lognloglogn)