0% found this document useful (0 votes)
28 views

Tutorial 2

This document discusses solving a recurrence equation for the runtime of an algorithm. It provides steps to draw the recursion tree, determine the number of nodes and cost at each level of the tree, and write the total cost as a summation. It then solves the summation to determine the asymptotic runtime is O(n^log_2(4)).

Uploaded by

mrout0333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Tutorial 2

This document discusses solving a recurrence equation for the runtime of an algorithm. It provides steps to draw the recursion tree, determine the number of nodes and cost at each level of the tree, and write the total cost as a summation. It then solves the summation to determine the asymptotic runtime is O(n^log_2(4)).

Uploaded by

mrout0333
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Princess Nora Bint Abdulrahman University First Semester 1433/1434H

Faculty of Computer and Information Science Tutorial # 2


CS 411 : Algorithms Design and Analysis Recursion Tree

Q: Regard to the recurrence equation below:


T(n) = 4T(n/2) +n
a. Draw the recursion tree.
b. Determine the number of nodes and cost at each level.
c. Determine the total cost for all levels (i.e. in summation form).

Steps:

1. Draw the tree based on the recurrence


2. From the tree determine:
a. # of levels in the tree
b. cost per level
c. # of nodes in the last level
d. cost of the last level (which is based on the number found in
2c)
3. Write down the summation using ∑ nota*on – this summation
sums up the cost of all the levels in the recursion tree
4. Recognize the sum or look for a closed form solution for the
summa*on created in 3). Use Appendix A.
5. Apply that closed form solution to your summation coming up
with your “guess” in terms of Big-O, or Θ, or Ω (depending on
which type of asymptotic bound is being sought).

٤ of ١Page
Princess Nora Bint Abdulrahman University First Semester 1433/1434H
Faculty of Computer and Information Science Tutorial # 2
CS 411 : Algorithms Design and Analysis Recursion Tree

Solution :

n
T(n) = 4T(n/2) +n
T(n/2) T( n/2) T(n/2 ) T( n/2)

n
T(n/2)=4T(n/4)+n/2
n/2 n/2 n/2 n/2

T(n/4 ) T(n/4 ) T(n/4 ) T(n/4) -----

T(n/4) = 4T(n/8) +(n/4)


n/2 n/2 n/2 n/2

n/4 n/4 n/4 n/4 - - - - - - - - - -

T(n/8)-----------------------------------------

٤ of ٢Page
Princess Nora Bint Abdulrahman University First Semester 1433/1434H
Faculty of Computer and Information Science Tutorial # 2
CS 411 : Algorithms Design and Analysis Recursion Tree

Level #of
Recursion tree Level Sum
# node
0 1 n n

1 4 n/2 n/2 n/2 n/2 4(n/2) = 2n

2 42 n/4 n/4 n/4 n/4 - - - - - - - 42 (n/4) = 4n

3 43 n/8 n/8 ------------- 43(n/8) = 8n


. . .
. . .
. . .
i 4i n/2i 4i(n/2i) = 2in
.
.
4h-1(n/2h-1) = 2h-1n
.

h 4h T(1) T(1) T(1) T(1) 4h

٤ of ٣Page
Princess Nora Bint Abdulrahman University First Semester 1433/1434H
Faculty of Computer and Information Science Tutorial # 2
CS 411 : Algorithms Design and Analysis Recursion Tree



 = 4 1 + 2 


= 1 → ℎ =   ←  #1
2
from  #1


 = 4$%&'( 1 + 2 





 = $%&' ) 1 + 2 




$%&' ) =  *+  4 = 2 ←  #2


from  #2


 =  1 +  2

 =  1 +  ,1 + 2 + 2 + ⋯ . . +2 /


0+1* 231+ 41* 1=2 =1

 6
1 6 − 1
4 =  + 1 + 1 + … + 1 = 1>1
1−1
4+ 1 = 2  = 1
12 − 1
 ,1 + 2 + 2 + ⋯ + 2
  /
= = 2 − 1
2−1
913 : #1
= 2$%&'( − 1
= $%&'  − 1 =  − 1 4+  2 = 1
 =  1 +  − 1
 = ; 

٤ of ٤Page

You might also like