Ind Assignement PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CSC645: INDIVIDUAL ASSIGNMENT #02 (GROUP:CS2305B)

Name:MUHAMMAD AFIQ IZZUDDIN BIN ROSLI Student ID: 2022919817

Show your working steps to obtain the order of growth class of the following
algorithms:

A) Set up a recurrence relation for the basic operation of the following


algorithms:

1) Algorithm 1
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in
n’s binary representation
if (n == 1)
return 1
else
return BinRec(n/2) + 1

2) Algorithm 2
Algorithm Q(n)
//Input: A positive integer n
if (n == 1)
return 1
else
return Q(n − 1) + 2 ∗ n – 1

B) Solve the following recurrence relations:

1) T(n) = T(n-1) + 5n for n > 1, T(0) = 0


2) T(n) = T(n/3) + 1 for n > 1, T(1) = 1 (solve for n = 3k)
Instruction:
1) The due date is on ______________________________

2) Please submit the assignment via Google Classroom. Late submission will be
rejected!

3) DO NOT FORGET to write your name and student id for the assignment!

A) ALGORITHM 1

T(1) = 1 (Base Case)

T(n) = T(n/2) + 1 (Recursive Case)

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

= T(n/4) + 1 + 1

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

= ….

= T(1) + k

= 1 + log n (since n = 2k)

Therefore, T(n) = T(n/2) + 1 #[ O(log n) ]

ALGORITHM 2

T(n) = T(n-1) + 2

= T(n-2) + 4

= T(n-3) + 6

….

= T(1) + 2(1) + 2(2) + … + 2(n-1)

= T(1) + 2(1 + 2 + … + (n-1))

= T(1) + n(n-1)
Therefore, T(n) = n(n-1) #[ O(n2) ]

B)
1. T(n) = T(n-1) + 5n for n > 1, T(0) = 0

T(1) = T(0) + 5(1) = 5

T(2) = T(1) + 5(2) = 15

T(3) = T(2) + 5(3) = 30

T(4) = T(3) + 5(4) = 50

….

T(n) = 5 + 10 + 15 + … + 5n

= 5(1 + 2 + 3 + … + n)

= 5n(n+1)/2

#[ 0(n2) ]

2. T(n) = T(n/3) + 1 for n > 1, T(1) = 1 (solve for n = 3k)

T(n) = T(n/3) + 1

= T(n/9) + 1 + 1

= T(n/27) + 1 + 1 +1

=…

= T(n) = T(n/3k) + k, where k is number of times we can divide by 3 before we reach 1.

#[ O(log(n)) ]

You might also like