Está en la página 1de 10

Step 1:The recurrence tree for the recursive relation T(n) = 4T(n/2) + cn can be easilty drawn (try it yourself)

Step 2: To calculate the height of the tree ( or the lowest level of the tree ) The sub problem size decreases as we get further from the root, we eventually reach the boundary condition where the cost of leave becomes c i.e. T(1) as in the figure above. But how far from the root do we reach T(1). The problem size at level 0 = n/2i = n/20 = n/1 = n The problem size at level 1 = n/2i = n/21 = n/2 The problem size at level 2 = n/2i = n/22 = n/4 . . The problem size at level i = n/2i = n/2i = n/2i Assuming level i is the lowest level where problem size hits T(1) Therefore, n/2i = 1 hich implies n = 2i Therefore i = log2n (lowest level i is the height of the tree) Thus the height of the tree ! log2n e "now the Total levels of the tree = Height of the tree + 1 Therefore the Total levels of the tree = log2n + 1 After calculating the height of the tree, we have to calculate the cost at each level of the tree. Step 3: Cost at each level of the tree #rom the recursion tree The total number of nodes at level 0 = 40 = 1 The total number of nodes at level 1 = 41 = 4 The total number of nodes at level 2 = 42 = 16 . . The total number of nodes at level i = 4i = 4i e "now that i the last level is log2n as calculated above. Therefore the total number of nodes at last level ! 4log2n . $odes at last level are "nown as leaves. Total number of nodes at last level = 4log2n Total cost of last level = Total number of leaves !ost of each "eave = 4log2n T(1) = nlog24 T(1) = n2 #c = cn2 = $(n2) $ow the total levels are i = 0% 1% 2% &% ''''##% log2n ( 1% log2n e have to now calculate the cost of i = 0% 1% 2% &% ''''##% log2n ( 1 levels only since we "now the cost of last level. !ost of each level = !ost of each node at that level Total number of nodes at that level# = ( n/2i) # 4i = n#2i = n + 2n + 4n + )n + 16n + ''''## + log2n ( 1 = n(1 + 2 + 4 + ) + 16 + '''''#) + log2n ( 1 = n(20 + 21 + 22 + 2& + 24 + '''#) + log2n ( 1 This is a %eometric series

log2n ( 1 n 2i = (2 log2n ( 1 + 1 ( 1) / ( 2 ( 1) i=0 = n(2 log2n ( 1) = n(n log22 ( 1) = n(n1 ( 1) = n(n ( 1) Total cost of tree ! !ost of 0% 1% 2% &% ''''##% log2n ( 1 levels + !ost of last level = n(n ( 1) + $(n2) = n2 *1 + cn2 = $(n2) Tight asymptotic bound for the given recurrence T(n) = 4T(n/2) + cn can be given as T (n) = O (n2) &olution 1' Step 1: Draw the recurrence tree (t is drawn below Step 2 To calculate the height of the tree ( or the lowest level of the tree ) To calculate the height of the tree we have to first find the lowest level of the tree, since we "now that lowest level of the tree is e)ual to the height of the tree. $ow how to calculate the height of the tree. *eight of tree is here is dependent on the order of reduction of subproblem which is +. The sub problem size decreases as we get further from the root, we eventually reach the boundary condition where the cost of leave becomes c i.e. T(1) as in the figure shown. But how far from the root do we reach T(1), The problem size at level 0 = n/2i = n/20 = n/1 = n The problem size at level 1 = n/2i = n/21 = n/2 The problem size at level 2 = n/2i = n/22 = n/4 . . The problem size at level i = n/2i = n/2i = n/2i Assuming level i is the lowest level where problem size hits T(1) Therefore, n/2i = 1 hich implies n = 2i Therefore i = log2n (lowest level i is e)ual to the height of the tree) Thus the height of the tree = log2n e "now that'

Total levels of the tree ! Height of the tree + 1 Therefore, Total levels of the tree ! log2n + 1 After calculating the height of the tree, we have to calculate the cost at each level of the tree. Step 3 Cost at each level of the tree #rom the recursion tree of T(n) = 2T(n/2) + cn we can easily conclude that cost of each level is cn. Step ! Total cost of the recursion tree Total cost ! Total no# of levels cost of each level ! (log2n + 1) cn ! cn log2n + cn By ignoring lower order terms we get Total cost ! cn log2n T(n) = (n log2n)

Step 1: Draw the recurrence tre (t is drawn as shown Step 2 To calculate the height of the tree ( or the lowest level of the tree ) To calculate the height of the tree we have to first find the lowest level of the tree, since we "now that lowest level of the tree is e)ual to the height of the tree. $ow how to calculate the height of the tree. *eight of tree is here is dependent on the order of reduction of subproblem which is +. The sub problem size decreases as we get further from the root, we eventually reach the boundary condition where the cost of leave becomes c i.e. T(1) as in the figure shown. But how far from the root do we reach T(1), #or this we have to see the order of reduction of the subproblem. The problem size at level 0 = n/2i = n/20 = n/1 = n The problem size at level 1 = n/2i = n/21 = n/2 The problem size at level 2 = n/2i = n/22 = n/4 . . The problem size at level i = n/2i = n/2i = n/2i Assuming level i is the lowest level where problem size hits T(1) Therefore, n/2i = 1 hich implies n = 2i Therefore i = log2n (lowest level i is e)ual to the height of the tree) Thus the height of the tree = log2n e "now that' Total levels of the tree ! Height of the tree + 1 Therefore, Total levels of the tree ! log2n + 1 After calculating the height of the tree, we have to calculate the cost at each level of the tree. Step 3: Cost at each level of the tree #rom the recursion tree The total number of nodes at level 0 =&0 = 1 The total number of nodes at level 1 = &1 = & The total number of nodes at level 2 = &2 = + . . The total number of nodes at level i = &i = &i e "now that i% the last level is log2n as calculated above. Therefore the total number of nodes at last level i = &log2n . $odes at last level are "nown as leaves. Total number of nodes at last level = &log2n Total cost of last level = Total number of leaves !ost of each "eave = &log2n T(1) = nlog2& T(1)

= $(nlog2&) $ow the total number of levels are i = 0% 1% 2% &% ''''##% log2n ( 1% log2n e have to now calculate the cost of i = 0% 1% 2% &% ''''##% log2n ( 1 levels only since we "now the cost of last level i.e level log2n. !ost of each level = !ost of each node at that level Total number of nodes at that level# = c( n/2i) # &i = cn#(2/&) i By using summation ! cn

Total cost of recursion tree = cost of 0% 1% 2% '' (log2n ( 1) levels + cost of last level ! cn + $(nlog2&) *ere we got an e)uation, now we to find the highest degree of polynomial, it is nlog2& Therefore, the solution of recurrence T (n) = &T (n/2) + n is T(n) = $(nlog2&)#

Step 1: Draw the recurrence tree (t is drawn below Step 2 To calculate the height of the tree ( or the lowest level of the tree ) To calculate the height of the tree we have to first find the lowest level of the tree, since we "now that lowest level of the tree is e)ual to the height of the tree. $ow how to calculate the height of the tree. *eight of tree is here is dependent on the order of reduction of subproblem. But here there are two order of reductions i.e. 1-. and /-.. $ow among these two we have to ta"e the order of reduction that will go to the deeper level, since that will decide the height of tree. Therefore among these two order of reductions 1-. and /-., we select /-., since it reaches more deeper then 1-.. The problem size at level 0 = (2/&)in = n 1=n The problem size at level 1 = (2/&)in = (2/&)n The problem size at level 2 = (2/&)in = (4/+)n . . The problem size at level i = (2/&)in Assuming level i is the lowest level where problem size hits T(1) Therefore, (2/&)in = 1 hich implies n = (&/2)i Therefore i = log&/2n (lowest level i is e)ual to the height of the tree) Thus the height of the tree = log3"2n e "now that' Total levels of the tree ! Height of the tree + 1 Therefore, Total levels of the tree ! log&/2n + 1 After calculating the height of the tree, we have to calculate the cost at each level of the tree. Step 3 Cost at each level of the tree #rom the recursion tree of T (n) = T (n/&) + T (2n/&) + cn we can easily conclude that cost of each level is cn. Step ! Total cost of the recursion tree Total cost ! Total no# of levels cost of each level ! (log&/2n + 1) cn ! cn log&/2n + cn By ignoring lower order terms we get Total cost ! cn log&/2n % by rounding the base value of we get T(n) = 0 (n log n).

Step 1: Draw the recurrence tree (t is drawn as shown Step 2 To calculate the height of the tree ( or the lowest level of the tree ) To calculate the height of the tree we have to first find the lowest level of the tree, since we "now that lowest level of the tree is e)ual to the height of the tree. $ow how to calculate the height of the tree. *eight of tree is here is dependent on the order of reduction of subproblem which is 1-1. The sub problem size decreases as we get further from the root, we eventually reach the boundary condition where the cost of leave becomes c i.e. T(1) as in the figure shown. But how far from the root do we reach T(1), #or this we have to see the order of reduction of the subproblem. The problem size at level 0 = n/4i = n/40 = n/1 = n The problem size at level 1 = n/4i = n/41 = n/4 The problem size at level 2 = n/4i = n/42 = n/16

. . The problem size at level i = n/4i = n/4i Assuming level i is the lowest level where problem size hits T(1) Therefore, n/4i = 1 hich implies n = 4i Therefore i = log4n (lowest level i is e)ual to the height of the tree) Thus the height of the tree = log!n e "now that' Total levels of the tree ! Height of the tree + 1 Therefore, Total levels of the tree ! log4n + 1 After calculating the height of the tree, we have to calculate the cost at each level of the tree. Step 3: Cost at each level of the tree #rom the recursion tree The total number of nodes at level 0 =&0 = 1 The total number of nodes at level 1 = &1 = & The total number of nodes at level 2 = &2 = + . . The total number of nodes at level i = &i = &i e "now that i% the last level is log4n as calculated above. Therefore the total number of nodes at last level i = &log4n . $odes at last level are "nown as leaves. Total number of nodes at last level = &log4n Total cost of last level = Total number of leaves !ost of each "eave = &log4n T(1) = nlog4& T(1) = $(nlog4&) $ow the total number of levels are i = 0% 1% 2% &% ''''##% log4n ( 1% log4n e have to now calculate the cost of i = 0% 1% 2% &% ''''##% log4n ( 1 levels only since we "now the cost of last level i.e level log4n. !ost of each level = !ost of each node at that level Total number of nodes at that level# = c( n/4i)2 # &i = cn2#(&/16) i By using summation ! cn2

Total cost of recursion tree = cost of 0% 1% 2% '' (log4n ( 1) levels + cost of last level ! cn2 + $(nlog4&) *ere we got an e)uation, now we to find the highest degree of polynomial, it is nlog2& Therefore, the solution of recurrence T (n) ! .T (23 n-1 32) 4 5(n/) is T(n) = $(n2)

Step 1: Draw the recurrence tree (t is drawn as shown Step 2 To calculate the height of the tree ( or the lowest level of the tree ) #rom the recurrence tree it can be easily noted that the height of this tree in n# &ince at each step its size is reduced by only one. Step 3 Cost at each level of the tree #rom the recursion tree of T(n) ! T(n 6 1) 4 n we can easily see the cost of each level if we sum up the cost of each level we got, 1 4 (n 6 1) 4 (n 6 /) 4 (n 6 .) 4777 4 n Step ! Total cost of the recursion tree Total cost of recursion tree is 1 4 (n 6 1) 4 (n 6 /) 4 (n 6 .) 4777 4 n e "now that the sum of this series is Therefore, we finally got the solution as T(n) ! (n/) Step 1: Draw the recurrence tree (t is drawn as shown

Step 2 To calculate the height of the tree ( or the lowest level of the tree ) #rom the recurrence tree it can be easily noted that the height of this tree in n# &ince at each step its size is reduced by only one. Step 3 Cost at each level of the tree #rom the recursion tree The total number of nodes at level 0 =20 = 1 The total number of nodes at level 1 = 21 = 2

The total number of nodes at level 2 = 22 = 4 . . The total number of nodes at level i = 2i e "now that i% the last level is n. Therefore the total number of nodes at last level i = /n . $odes at last level are "nown as leaves. Total number of nodes at last level = 2n Total cost of last level = Total number of leaves !ost of each "eave = 2n T(1) = $(2n) $ow the total number of levels are i = 0% 1% 2% &% ''''##%n ( 1% n e have to now calculate the cost of i = 0% 1% 2% &% ''''##% n ( 1 levels only. !ost of each level = !ost of each node at that level Total number of nodes at that level# = c # 2i By using summation ! c = 2n* 1

Step ! Total cost of the recursion tree Total cost of recursion tree = cost of 0% 1% 2% '' (n ( 1) levels + cost of last level = 2n* 1 + $(2n) Therefore, the solution of recurrence T(n) ! /T(n 6 1) 4 1 is T(n) ! (2n).

#$a%ple &: Use a recursion tree to determine a good asymptotic upper bound on the recurrence T(n) = T(n 1) + 1. Use the substitution method to verify your answer.
*int' cost of last level ! 1 8ost of remaining levels ! n 6 1 Total cost ! n 6 1 4 1 T(n) ! (n) Step 1: Draw the recurrence tree (t is drawn Step 2 To calculate the height of the tree ( or the lowest level of the tree ) To calculate the height of the tree we have to first find the lowest level of the tree, since we "now that lowest level of the tree is e)ual to the height of the tree. $ow how to calculate the height of the tree. *eight of tree is here is dependent on the order of reduction of subproblem. But here there are three order of reductions i.e. +, 9, and 1-:. $ow among these three we have to ta"e the order of reduction that will go to the deeper level, since that will decide the height of tree. Therefore among these three order of reductions +, 9, and 1-:, we select +, since it reaches more deeper then 9, and 1-:. The problem size at level 0 = n/2i = n/20 = n/1 = n The problem size at level 1 = n/2i = n/21 = n/2 The problem size at level 2 = n/2i = n/22 = n/4

. . The problem size at level i = n/2i = n/2i = n/2i Assuming level i is the lowest level where problem size hits T(1) Therefore, n/2i = 1 hich implies n = 2i Therefore i = log2n (lowest level i is e)ual to the height of the tree) Thus the height of the tree = log2n e "now that' Total levels of the tree ! Height of the tree + 1 Therefore, Total levels of the tree ! log2n + 1 After calculating the height of the tree, we have to calculate the cost at each level of the tree. Step 3 Cost at each level of the tree #irst we find the cost of last level. #rom the recursion tree The total number of nodes at level 0 =&0 = 1 The total number of nodes at level 1 = &1 = & The total number of nodes at level 2 = &2 = + . . The total number of nodes at level i = &i = &i e "now that i% the last level is log2n as calculated above. Therefore the total number of nodes at last level i = &log2n . $odes at last level are "nown as leaves. Total number of nodes at last level = &log2n Total cost of last level = Total number of leaves !ost of each "eave = &log2n T(1) = nlog2& T(1) = $(nlog2&) $ow the total number of levels are i = 0% 1% 2% &% ''''##% log2n ( 1% log2n e have to now calculate the cost of i = 0% 1% 2% &% ''''##% log2n ( 1 levels only since we "now the cost of last level i.e level log2n. (n this case let us try by adding the heights of all levels manually. 8ost of zero level ! n 8ost of first level ! 8ost of second level ! 7777777777. e can now conclude that the cost of each level is By using summation ! ; cn !

Total cost of recursion tree = cost of 0% 1% 2% '' (log2n ( 1) levels + cost of last level ! ; cn + $(nlog2&) *ere we got an e)uation, now we to find the highest degree of polynomial, it is nlog2& Therefore, the solution of recurrence T(n) ! T(n-/) 4 T(n-1) 4 T(n-:) 4 n is T(n) = $(nlog2&)#

'nsolve( )ro*le%s Prob em 1! 8onsider the following recurrence T (n) = &T (n/4) + n. <btain asymptotic bound for the recurrence by the recursion tree method. Prob em "! &olve the following recurrences using recursion tree method a) T(n) ! =T(n-=) 4 n-log n b) T(n) ! /T(n-/) 4 n-log n Prob em #! &olve the following recurrences using recursion tree method a) T(n) ! T(n-/) 4 1 b) T(n) ! /T(n-/) 4 1 c) T(n) ! 1T(n-/) 4 1

También podría gustarte