Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 3323 Notes – Recurrence Relations Algorithm Analysis.

Similar presentations


Presentation on theme: "CSC 3323 Notes – Recurrence Relations Algorithm Analysis."— Presentation transcript:

1 CSC 3323 Notes – Recurrence Relations Algorithm Analysis

2 Recurrence Relations Applying a “divide and conquer” approach to solving a problem will frequently lead to a recursive algorithm. Time complexity for such an algorithm is represented with a recurrence relation.

3 Algorithm comparison example Block multiplication (BM): if n ≤ 3 then return SM(x, y) m = n div 2 (rounded up) P1 = BM( X[0..m-1], Y[0..m-1]) P2 = BM( X[m..n-1], Y[m..n-1]) P3 = BM( Add( X[0..m-1], X[m..n-1]), Add( Y[0..m-1], Y[m..n-1])) D = Sub( Sub( P3, P1), P2) Z[0..2m-1] = P1 Z[2m..2n-1] = P2 Z[m..2n-1] = Add( D[0..2m+1], Z[m..2n-1]) return Z

4 Recurrence Relations: Block Multiplication T BM (n) = 3 * T BM (n/2) + c * n 3 * T BM (n/2) = 3 * ( 3T BM (n/4) + c * (n/2)) 3 2 * T BM (n/4) = 3 2 * ( 3T BM (n/8) + c * (n/4)) ……. 3 (log2(n)-1) * T BM (2) = 3 (log2(n)-1) * ( 3T BM (1)+c*(2)) log2(n)-1 T BM (n)=3 log2(n) *T BM (1) + cn∑ (3 i /2 i ) (see formula, p. 8, 6) = n log2(3) * T BM (1) +cn(((3/2) log2(n) -1)/(3/2-1)) = n log2(3) * T BM (1) + c’n((n) log2(3/2) -1) T BM (n) ε O(n log2(3) ) for 32 bits, 32 2 /32 1.59 = 4.15 for 64 bits, 64 2 /64 1.59 = 5.5

5 Recurrence Relations: Binary Search T BS (n) = T BS (n/2) + c T BS (n/2) = T BS (n/4) + c T BS (n/4) = T BS (n/8) + c ……. T BS (2) = T BS (1)+c T BS (n) = T BS (1) + c*log 2 (n) T BS (n) ε O(log 2 (n))

6 Recurrence Relations (Section 3.7.1, pp. 137ff) Generally, for a recurrence relation: T(n) = a*T(n/b) + c n e (See Theorem 3.17, p. 139) T(n) = c’ n logb(a) + c n e log b (n) if a/b e = 1 T(n) = c’ n logb(a) + c (n logb(a) – n e ) otherwise

7 Recurrence Relations: Fibonacci numbers Definition: fib(i) = fib(i-1) + fib(i-2) T(n) = T(n-1) + T(n-2) + c ≥ 2T(n-2) + c 2 T(n-2) ≥ 2(2 T(n-4) + c) …… 2 n/2-1 T(2) ≥ 2 n/2-1 (2T(0) + c) n/2 T(n) ≥ 2 n/2 T(0)+c ∑ 2 i = 2T(0)+c(2 n/2 - 1)ε Ω(2 n/2 ) i=0

8 Recurrence relations Sample problems: pp. 141-145 3.4, 3.7, 3.8, 3.10, 3.12


Download ppt "CSC 3323 Notes – Recurrence Relations Algorithm Analysis."

Similar presentations


Ads by Google