80 likes | 249 Views
CSC 3323 Notes – Recurrence Relations. Algorithm Analysis. 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.
E N D
CSC 3323 Notes – Recurrence Relations Algorithm Analysis
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.
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
Recurrence Relations: Block Multiplication TBM(n) = 3 * TBM(n/2) + c * n 3 * TBM(n/2) = 3 * ( 3TBM(n/4) + c * (n/2)) 32 * TBM(n/4) = 32 * ( 3TBM(n/8) + c * (n/4)) ……. 3(log2(n)-1) * TBM(2) = 3(log2(n)-1) * ( 3TBM(1)+c*(2)) log2(n)-1 TBM(n)=3log2(n)*TBM(1) + cn∑ (3i/2i)(see formula, p. 8, 6) = nlog2(3) * TBM(1) +cn(((3/2)log2(n)-1)/(3/2-1)) = nlog2(3) * TBM(1) + c’n((n)log2(3/2)-1) TBM(n) ε O(nlog2(3))for 32 bits, 322/321.59 = 4.15 for 64 bits, 642/641.59 = 5.5
Recurrence Relations: Binary Search TBS(n) = TBS(n/2) + c TBS(n/2) = TBS(n/4) + c TBS(n/4) = TBS(n/8) + c ……. TBS(2) = TBS(1)+c TBS(n) = TBS(1) + c*log2(n) TBS(n) ε O(log2(n))
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’ nlogb(a) + c ne logb(n) if a/be = 1 T(n) = c’ nlogb(a) + c (nlogb(a) – ne) otherwise
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) …… 2n/2-1T(2) ≥ 2n/2-1 (2T(0) + c) n/2 T(n) ≥ 2n/2T(0)+c ∑ 2i = 2T(0)+c(2n/2 - 1)εΩ(2n/2) i=0
Recurrence relations • Sample problems: pp. 141-145 • 3.4, 3.7, 3.8, 3.10, 3.12