Download presentation
1
Recurrence Examples
2
Recursion Tree for T(n)=3T(n/4)+(n2)
cn2 cn2 c(n/4)2 T(n/16) (c) T(n/4) T(n/4) T(n/4) (a) (b) cn2 cn2 (3/16)cn2 c(n/4)2 c(n/4)2 c(n/4)2 log 4n (3/16)2cn2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 (nlog 43) T(1) T(1) T(1) T(1) T(1) T(1) 3log4n= nlog 43 Total O(n2) (d)
3
Solution to T(n)=3T(n/4)+(n2)
The height is log 4n, #leaf nodes = 3log 4n= nlog 43. Leaf node cost: T(1). Total cost T(n)=cn2+(3/16) cn2+(3/16)2 cn2+ +(3/16)log 4n-1 cn2+ (nlog 43) =(1+3/16+(3/16)2+ +(3/16)log 4n-1) cn2 + (nlog 43) <(1+3/16+(3/16)2+ +(3/16)m+ ) cn2 + (nlog 43) =(1/(1-3/16)) cn2 + (nlog 43) =16/13cn2 + (nlog 43) =O(n2).
4
Recursion Tree of T(n)=T(n/3)+ T(2n/3)+O(n)
5
Recursion tree for T(n)=aT(n/b)+f(n)
6
General Divide-and-Conquer Recurrence
T(n) = aT(n/b) + f (n) where f(n) (nd), d 0 Master Theorem: If a < bd, T(n) (nd) If a = bd, T(n) (nd log n) If a > bd, T(n) (nlog b a ) Examples: T(n) = T(n/2) + n T(n) (n ) Here a = 1, b = 2, d = 1, a < bd T(n) = 2T(n/2) + 1 T(n) (n log 2 2 ) = (n ) Here a = 2, b = 2, d = 0, a > bd
7
Examples T(n) = T(n/2) + 1 T(n) (log(n) )
Here a = 1, b = 2, d = 0, a = bd T(n) = 4T(n/2) + n T(n) (n log 2 4 ) = (n 2 ) Here a = 4, b = 2, d = 1, a > bd T(n) = 4T(n/2) + n2 T(n) (n2 log n) Here a = 4, b = 2, d = 2, a = bd T(n) = 4T(n/2) + n3 T(n) (n3) Here a = 4, b = 2, d = 3, a < bd
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.