Examples of Recurrence Equations - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Examples of Recurrence Equations

Description:

T(n) = T(n/2) 1 for n =2. Solution T(n)= lg n. T(n) = T(n/2) 1 = T(n/22) 1 1 ... Solution T(n) =n lg n should satisfy equations. T(1)=0. T(n)= 2 T(n/2) n ... – PowerPoint PPT presentation

Number of Views:605
Avg rating:4.0/5.0
Slides: 20
Provided by: IRE62
Category:

less

Transcript and Presenter's Notes

Title: Examples of Recurrence Equations


1
Examples of Recurrence Equations
  • CS463
  • Irena Pevac
  • CCSU

2
Recurrence Equations
  • Example 1)
  • T(0 ) 0
  • T(n) T(n-1) 1 for ngt1
  • ______________________
  • Solution T(n) n
  • Example 2)
  • T(0 ) 0
  • T(n) T(n-1) n for ngt1
  • ______________________
  • Solution T(n) n(n1)/2

3
Recurrence Equations
  • Example 3)
  • T(1) 0
  • T(n) T(n/2) 1 for
    ngt2
  • ______________________
    ___________________
  • Solution T(n) lg n
  • Example 4)
  • T(0) 0 T(1) 0
  • T(n) 2 T(n/2) n for
    ngt2
  • ______________________
    ___________________
  • Solution T(n) n lg n

4
Recurrence Equations
  • Example 5)
  • T(0)0
  • T(n)2T(n-1) 1 for ngt1
  • ________________
  • Solution T(n) 2n-1
  • Example 6)
  • T(1) 1
  • T(n) 3T(n/2) n for ngt2 n
    is power of 2
  • _________________________________________
  • Solution T(n) 3nlg3 2n

5
T(n)T(n-1) 1
  • T(n) T(n-1) 1 T(n-2) 11
  • T(n-3)111
  • .
  • T(n-n) 11 1
  • T(0) n
  • 0 n n
  • ________________________________
  • Solution is T(n) n

6
Test the solution
  • T(0) 0
  • T(n) T(n-1) 1 for ngt1
    ______________________
  • Solution T(n) n
  • 00
  • nn-11
  • ___________
  • nn Correct!!!

7
T(n) T(n-1) n
  • T(n) T(n-1) n
  • T(n-2) (n-1)n
  • T(n-3)(n-2)(n-1)n
  • .
  • T(n-n) 12 n
  • T(0) 123n
  • n(n1)/2
  • ________________________________
  • Solution is T(n) n(n1)/2

8
Test the Solution
  • T(0) 0
  • T(n) T(n-1) n for ngt1
    ______________________
  • Solution T(n) n(n1)/2
  • 01/2 0
  • n(n1)/2 (n-1)n/2 n
  • ___________
  • (n1)/2 (n-1)/2 1
  • n/21/2n/2 -1/2 1
  • 1/2 1/2 Correct!!!

9
T(n) T(n/2) 1
  • T(1) 0
  • T(n) T(n/2) 1 for ngt2
  • ______________________
    ___________________
  • Solution T(n) lg n
  • T(n) T(n/2) 1 T(n/22) 11
  • T(n/23) 111 ..
  • T(n/2k) k1 n/2k 1, k
    lg n
  • T(n) 0 lg n lg n

10
Test the solution
  • T(1) 0
  • T(n) T(n/2) 1 for ngt2
  • ____________________
  • lg1
  • lg n lg(n/2) 1
  • ____________________
  • lg n lg n - lg 2 1
  • lg n lg n -11 Correct!!!

11
T(n) 2 T(n/2) n
  • T(0) 0 T(1) 0
  • T(n) 2 T(n/2) n for ngt2
  • ________________________
  • Assume that n 2k. If n 1, k 0.
  • T(20) 0
  • T(2k)2 T(2k-1)2k
  • ___________________ multiply by 1/2k
  • T(1)0
  • T(2k)/ 2k 2 T(2k-1)/ 2k 2k/ 2k

12
T(n) 2T(n/2) n
  • Substitute T(2k)/ 2k with U(k)
  • U(0) 0
  • U(k)U(k-1) 1
  • _____________________________________________
  • Solution determined earlier U(k) k
  • T(2k)/ 2k k T(2k) 2k k
  • Finally, since 2kn
  • T(n) n lg n

13
Solution Testing
  • Solution T(n) n lg n should satisfy equations
  • T(1)0
  • T(n) 2 T(n/2) n
  • ________________________
  • T(1) 1 lg 1 1 00 base case is correct.
  • n lg n 2 (n/2) lg(n/2) n
  • n lg n n ( lg n- lg 2) n
  • n lg n n lg n n n
  • n lg n n lg n Obviously correct!!!
  • Recursive step is correct.

14
T(n)2T(n-1) 1
  • T(0)0
  • T(n)2T(n-1) 1 for ngt1
  • ________________
  • Solution T(n) 2n-1
  • T(0)0
  • T(1)2 T(0) 1 1
    1 21-1
  • T(2) 2 T(1) 1 211 3 3
    22-1
  • T(3) 2 T(2) 1 231 7 7
    23-1
  • T(4) 2 T(3) 1 2 7 1 15 15
    24-1
  • T(5) 2 T(4) 1 215 1 31 31
    25-1

15
Test the solution
  • T(0)0
  • T(n)2T(n-1) 1 for ngt1
  • ________________
  • T(n) 2n-1
  • ________________
  • 20-1 0
  • 2n-1 2 (2n-1-1 ) 1
  • 2n-1 2n-2 1
  • 2n-1 2n-1 Correct!!!

16
T(n) 3T(n/2) n
  • T(1) 1
  • T(n) 3T(n/2) n
  • T(n) 3T(n/2) n 3(3T(n/22) n/2) n
  • 32T(n/22) 3n/2 n
  • 32(3T(n/23) n/22 ) 3n/2 n
  • 33T(n/23) 32n/22 3n/2 n
  • 33(3T(n/24) n/23) 32n/22 3n/2
    n
  • 33(3T(n/24) n/23) 32n/22 3n/2
    n
  • 34T(n/24) 33n/23 32n/22 3n/2
    n
  • . . .
  • 3kT(n/2k) (3/2)k-1n (3/2)k-2n
    (3/2)2n (3/2)n n
  • where n/2k 1, n 2k, k lg n

17
T(n) 3T(n/2) n
18
T(n) 3T(n/2) n

19
Test the Solution
  • T(n) 3T(n/2) n
Write a Comment
User Comments (0)
About PowerShow.com