Chapter 4 Answers

Document last modified: 

Recurrence Relation Tutorial

Question 4.1 - What is T(23)? 34.

Question 4.2 - Is T(1) necessary? Yes when ⌈n/2⌉ = 1.

Question 4.3.1

Inductive proofs on the recurrence: T(n) = T(n-1) + 1

Typically assume the case for parameter n that generates a sequence term for recurrence T(n) and prove T(n+1)=T((n+1)-1)+1.

For above recurrence: T(n) = T(⌊n/4⌋) + n

Question 4.3.2    For above recurrence:

 

METHODS FOR SOLVING RECURRENCE RELATIONS

Question 4.4

T(n) = T(n-1)+1 for n > 0               

T(0)=1                                        

Generate

T(1)=2                                       

T(2)=3                                       

T(3)=4                                       

Closed form

Observe pattern T(n) = n + 1

Question 4.5 - Use substitution to prove T(n) = closed form

Question 4.6 - Use induction to prove T(n) = closed form

Substitution

T(n) = T(n-1)+1

Basis

T(1)=1+1=2

T(n) = n + 1

       = (n-1+1)+1

       = T(n-1)+1

Induction
  • Basis step

T(1)=1+1=2

  • Inductive Hypothesis

T(n) = T(n-1)+1 = n+1

  • Inductive step

Show: T(n+1) = (n+1) + 1 = n+2

T(n+1) = T(n+1-1)+1            Recurrence T(n) = T(n-1)+1

           = T(n) + 1

           = (n+1)+1                  IH: T(n) = n+1

           = n + 2                       ∴                  

SUBSTITUTION METHOD

Question 4.7 - Use backward substitution to find the closed-end equation for:

T(1) = 1              

T(n) = T(n-1) + 5  for n>1

Solution

By backward substitution the recurrence yields:

T(n) = T(n-1) + 5 = T(n-2)+5 + 5 sub. T(n-1) = T(n-2)+5
  = T(n-2)+2*5 = T(n-3)+5 + 2*5 sub. T(n-2) = T(n-3)+5
  = T(n-3)+3*5 = T(n-3)+5 + 3*5 sub. T(n-3) = T(n-4)+5
  = T(n-4)+4*5    

Substituting i times yields (we're in luck, no summation!):

T(n)= T(n-i) + i*5

Closed-end

Base:  T(1) = T(n-i)

n-i = 1 or i=n-1

T(1) = T(1-i) = T(1-(n-1)) = T(1-(1-1)) = T(1)

Closed-end solution by substitution:

T(n) = T(n-i) + i*5
  = T(n-(n-1)) + (n-1)*5
  = T(1) + (n-1)*5
  = 1 + (n-1)*5
  = 5n-4

Proof: T(n) = 5n-4

T(1) = 1

T(n) = T(n-i) + 5 for n>1

Base:

T(1) = 5n-4 = 1*5-4 = 1

Assumption:

T(n) = 5n-4

Show:

T(n+1) = 5(n+1) - 4

T(n+1) = T(n+1-1) + 5
  = T(n) + 5
  = 5n-4+5
  = 5(n+1) - 4

 

Question 4.8 - Is T(n) ≤ 2n lg n = O(n lg n) an exact solution for T(n)? No. But holds for definition of O(n lg n).

Question 4.9 - A reasonable recurrence for the following factorial algorithm is:

T(n) = T(n-1) + 1

FACTORIAL(n)
    if n = 1 then return 1
    else return FACTORIAL(n-1) * n

Use the substitution method to show that T(n) ∈ O(n)

T(1) =
T(n) =
1                           
T(n-1) + 1
  1. Guess:

    T(n) = O(n)

    By definition of Big-O, must find c > 0 and n ≥ n0

    0 ≤ T(n) ≤ cn

  2. IH

    T(k-1) ≤ c(k-1)

    Show

     T(k) ≤ T(k-1) + 1 ≤ ck

  3. Substitution

    T(k) = T(k-1) +1   Recurrence
      c(k-1) + 1   IH substitution
      = ck - c + 1  
      ≤ ck   
    Find constant c so the last two lines hold. 
     
    ck - c + 1 ≤  ck
    -c + 1 0
    c 1

    c ≥ 1 satisfies the substitution

     

  4. Base or boundary conditions: Now find c and n0 to satisfy O definition

    Verify: TR(n) ≤ TC(n)

    TR(n) = T(n)                             recurrence

    TC(n) = cn                                closed-end bounds solution for O(n)

    where TR(n) = TR(n-1) + 1  
    Try n0 = 2
    TR(n) TC(n)
    TR(2) TC(2)
    T(2-1) + 1 c(2)
    1 + 1 2c
    c 1

     Succeeds for n0 = 2 

    Note:

    Choose c ≥ 1 for boundary condition since it also satisfies above substitution, c ≥ 0.

    The base case of the recurrence, T(1) = 1, is not used for the induction.

    The base case was established by finding c ≥ 1 and n0 = 2.

  5. Closed-end bounds form:

    T(n) ≤ n for n ≥ 2, c = 1

  6. Check, verify closed-end bounds form by O definition:

0 ≤ T(n) ≤ cn

T(n) ≤ n = O(n)

c ≥ 1 and n n0 = 2

0 ≤ T(n) ≤ cn
0 ≤ n ≤ cn
0 ≤ n/n ≤ c
0 ≤ 1 ≤ c
  Holds for c ≥ 1  

Question 4.9.1

Show that substitution fails to demonstrate that T(n) O(n2):

           | 2                        if n = 1
T(n) = |
           | 9
T(n/3) + n        if n > 1

O(n2): 0 ≤ T(n) ≤ cn2

IH: Assume holds for n/3, that is:

T(n/3) ≤ c(n/3)2 is true

Show: T(n) ≤ cn2

T(n) = 9T(n/3) + n         
  ≤ 9(c(n/3)2)+n
  = cn2+n
  ≤ cn2
Recurrence
IH substitution
fails for c > 0 

Question 4.9.2

Subtract a lower-order term and use substitution to show that T(n) O(n2):

           | 2                        if n = 1
T(n) = |
           | 9
T(n/3) + n        if n > 1

O(n2): 0 ≤ T(n) ≤ cn2 - bn

IH: Assume holds for n/3, that is:

T(n/3) ≤ c(n/3)2 - bn/3 is true

Show: T(n) ≤ cn2 - bn

T(n) = 9T(n/3) + n                   
  ≤  9( c(n/3)2 - bn/3 ) + n
  = cn2 - 3bn + n
  ≤ cn2 - bn
  which holds for c > 0 choosing b ≥ 1/2
Solve for b

IH substitution

Base case: Find n0 

Verify: T(n) ≤ cn2 - bn

where T(n) = 2                      when n = 1
9T(n/3) + n      when n > 1

and      cn2 - bn                                            solution for O(n2)

Try n0 = 3
9T(n/3) + n cn2 - bn  
9T(3/3) + n c32 - b3    n0=3
21 9c - 3b  
9c 3b + 21   b ≥ 1/2
9c 22 1/2

  b = 1/2

9c 45/2  
c 5/2  

 holds for n0 = 3 and c ≥ 2 1/2

 

Question 4.10

           | d                        if n = 1
T(n) = |
           |
4T(⌊n/2⌋) + dn     if n > 1

Show that substitution fails to demonstrate that T(n) O(n2):

O(n2): 0 ≤ T(n) ≤ cn2

Basis:

Choose n0=1

Prove basis holds for n0

T(1) = d ≤ c12 = c        holds for c ≥ d

IH: Assume holds for n/2, that is:

T(⌊n/2) ≤ c(⌊n/2⌋)2 is true

Show: T(n) ≤ cn2

T(n) = 4T(⌊n/2⌋) + dn               Recurrence
  = 4[c(⌊n/2⌋)2] + dn            IH substitution
  ≤ 4[c(n/2)2] + dn
  = cn2+dn
  ≤ cn2
cn2+dn ≤ cn2
 
(⌊n/2⌋)2 ≤ (n/2)2

 

fails for d > 0 

Question 4.11

    Substitution subtlety

T(n) = 4T(⌊n/2⌋) + dn                    Recurrence
  = 4[c(n/2)2 - bn/2] + dn              IH substitution
  = cn2 - 2bn + dn
  ≤ cn2 - bn
  which holds for c > 0 choosing b ≥ d
Solve for b

cn2 - 2bn + dn ≤ cn2 - bn

     -2bn + dn ≤ -bn

         -2b + d ≤ -b

                d ≤ b

                b ≥ d

 

Question 4.10.1

Show that substitution fails to demonstrate that T(n) O(nlg 3):

where T(n) = 1                      when n = 1
3T(n/2) + n      when n > 1
  1. Guess:

    T(n) = O(nlg 3)

    By definition of Big-O, must find c > 0 and n ≥ n0 > 0

    0 ≤ T(n) ≤ cnlg 3

  2. IH:     T(n/2) ≤ c(n/2)lg 3
     

  3. Induction Step:

    T(n) = 3T(n/2) + n   Recurrence
      3c(n/2)lg 3 + n   substitution of IH
      = 3c(nlg 3/2lg 3) + n  
      = 3c(nlg 3/3) + n 2lg 3 = 3lg 2 = 3
      = cnlg 3 + n  
      cnlg 3   fails

    Induction fails, because must show exactly that:

    T(n) ≤ cnlg 3

Question 4.11.1

Subtract a lower-order term and use substitution to show that T(n) O(nlg 3):

where T(n) = 1                      when n = 1
3T(n/2) + n      when n > 1
  1. Guess:

    T(n) = O(nlg 3)

    By definition of Big-O, must find c > 0 and n ≥ n0 > 0

    0 ≤ T(n) ≤ cnlg 3 - bn

  2. IH:     T(n/2) ≤ c(n/2)lg 3 - bn/2
     

  3. Induction Step:

    T(n) = 3T(n/2) + n   Recurrence
      3[c(n/2)lg 3 - bn/2] + n   substitution of IH
      = 3[c(nlg 3/2lg 3) - bn/2] + n  
      = 3[cnlg 3/3) - bn/2] + n 2lg 3 = 3lg 2 = 3
      = cnlg 3 - 3bn/2 + n  
      cnlg 3 - bn  

     

  4. Find b
cnlg 3 - 3bn/2 + n cnlg 3 - bn
n 3bn/2-bn
  = 3bn/2-2bn/2
  = (3bn-2bn)/2
  = bn/2
2n bn
2 b
b 2

Base case: Find n0 

where T(n) = 1                      when n = 1
3T(n/2) + n      when n > 1

Verify: 0 ≤ T(n) ≤ cnlg 3 - bn

Try n0 = 2 an even number
3T(n/2) + n cnlg 3 - bn   nlg 3=3lg n
3T(2/2) + n c3lg 2 - b2      n0=2
3T(1) + 2 3c-2b   3lg 2=31
3*1 + 2 3c-2b    
5 3c-2b    
2b+5 3c    
3c 2b+5       b ≥ 2
3c 9    
c 3    

 holds for n0 = 2 and c ≥ 3

5.  Choose: c ≥ 3 and n0 = 2

Since T(n) ≤ cnlg 3 - bn, ∀n ≥ n0 when b ≥ 2, c ≥ 3 and n0 = 2

 T(n) ∈ O(nlg 3)

 

Question 4.12 - Draw the recursion tree.

           | c                        if n = 1
T(n) = |
           |
4T(⌊n/2⌋) + cn     if n > 1

Question 4.13 - For:

           | c                        if n = 1
T(n) = |
           |
4T(⌊n/2⌋) + cn     if n > 1
  1. From the tree determine:
    1. # of levels in the tree of height lg n:

      0..lg n

    2. cost per level at level i:

      2icn

    3. # of nodes in the last level, problem size n=1:

      4lg n = nlg 4 = n2

    4. cost of the last level:

      T(1) * n2 = c * n2 = cn2 = Θ(n2)

  2. Write down the summation using ∑ notation. Summation of cost for lg n levels in the recursion tree + bottom level.

    T(n) =

  3. Find closed form solution for the summation created in 3) using Appendix A.
     

    T(n) =

     
    = cn(2lg n-1)/(2-1)+cn2 App. A5 p. 1147
    = cn(2lg n-1)/(2-1)+cn2  
    = cn(2lg n-1)+cn2  
    = cn(nlg 2-1)+cn2  
    = cn(n-1)+cn2  
    = cn2-cn+cn2  
    = 2cn2-cn  

    Total: cost of each level * the number of levels + cost of bottom level.

  4. Use closed form solution as “guess” in terms of Big-O, or Θ, or Ω (depending on which type of asymptotic bound is being sought).
    T(n) = 2cn2-cn
      = O(n2)
  5. Then use Substitution Method or Master Method to prove that the bound is correct.

    See Question 4.10 and 4.11 for Substitution Method.

     

Question 4.14 - Use Case 1 to determine:

T(n)=16T(n/4)+n ⇒T(n)=Θ(n2)

T(n) = 5T(n/2) + Θ(n2)

a = 16       b = 4       f(n) = n = O(n)

nlog416 = nlog442= n2

If f(n) = O(nlog416 - ε ) for some constant ε > 0, then T(n) Θ(nlog416)

f(n) = O(nlog416-ε ) = O( nlog416-1 ) = O( n2-1 ) = O(n) where ε = 1 can apply Case 1 and conclude:

T(n) = Θ (nlog416) = Θ(n2)

 

Question 4.15 - Use Case 2 to determine:

T(n)=4T(n/2)+n2 ⇒T(n)=Θ(n2 lg n)

a = 4       b = 2         f(n) = 1

nlog24 = n2

If f(n) = Θ( nlogba ) then T(n) ∈ Θ( nlogba * lg n )

f(n) = n2 = Θ(nlog24) = Θ(n2)

T(n) = Θ (nlog24 lg n) = Θ(n2 lg n)

Question 4.16 - Use Case 3 to determine:

T(n) = 3T(n/2) + n2 ⇒ T(n) = Θ(n2)

a = 3       b = 2       f(n) = n2

nlog23 ≈ n1.58

If f(n) = Ω( nlogba+ε ) for some constant ε > 0,

f(n) = n2 = Ω( nlog23) = Ω( n1.58+ε ) = Ω( n2 ) for ε ≈ 0.42

and if af(n/b) ≤ cf(n) for some constant c < 1 and all sufficiently large n

a = 3    b = 2    f(n) = n2

af( n/b ) = 3(n/2)2
  = 3n2/4
  3/4 n2
for c = 3/4 = c n2
  = c f(n)

then T(n) ∈ Θ( f(n) )

   T(n) = Θ(n2)

Question 4.17 - Use Chip-and-Conquer to determine:

T(n) = T(n-4) + n2 ⇒ T(n) = Θ(n3)

 f(n) is a polynomial n2, then T(n) ∈ Θ(n2+1) =  Θ(n3)

Question 4.18 - Use Chip-and-be-Conquered to determine:

T(n) = 7T(n-4) + n2 ⇒ T(n) = Θ(7n/4)