Anda di halaman 1dari 3

NILE UNIVERSITY OF NIGERIA

DEPARTMENT OF COMPUTER SCIENCE


Course: Analysis of Algorithm
Assignment 1

Ezeogu Chinonso Apollos


PhD reg No.: 171333007
ae0600@my.bristol.ac.uk;
kbittraining@gmail.com

Q1 Q2

Solution:
Solution:
The initialization, int i=1 will execute one time in
There are three loops: I,j, and k. first initialization; the outer while loop executes
upto N -1 times

The k loop will execute if the condition is satisfied. Int j = N executes one time; then the inner while
loop executes starts executing N times until 1,
In this case, k will always be executed since for all since j>0 in the condition; also decrease by j=j/2 ,
j%i in the modulus operation, the result is 0. then lastly the I is incremented by a factor of two,
Thus: i.e i=2*i.
I: 1 2 3 4 ….. n Hence, the time complexity is measured as,
J: 1 4 9 16 ….. (𝑛)2

K: 1.j 4.j 9.j 16.j …. (𝑛)2 . 𝑗 I: 1 2 4 8 ….. N-1


The series gives for K results to, J: N N/2 N/4 N/8 ….. 1
𝑛(𝑛+1)(2𝑛)
𝑗. 6
but j has an order of 𝑛2 The outer while loop is a geometric progression of
value = 2𝑘 = 𝑁 which leads to k=𝑙𝑜𝑔2 𝑁
𝑛(𝑛+1)(2𝑛)
Thus, 𝑛2 . 6
≈ 𝑂(𝑛5 )

1
NILE UNIVERSITY OF NIGERIA
DEPARTMENT OF COMPUTER SCIENCE
Course: Analysis of Algorithm
Assignment 1

Q4
The series given for j results to,

1 1 1 1
𝑁 (1 + 2 + 4 + 8 + ⋯ + 𝑁) = 2𝑁 as the sum to
infinitesimal converges to 2 as N gets larger .
How? The is sum of converging series, because as
1 𝑁
Solution:
1 (1−( ) )
2
N gets larger then 𝑁
→0 𝑆= 1 =
1−
2 I: 1 2 3 4 ….. n
1
2 (1 − 2𝑁
) = 2(1 − 0) = 2
J: 1 2 4 8 …… k term
Therefore, total time complexity is The j loop is a geometric sequence or ratio 2;
So the sum is,
𝑙𝑜𝑔2 𝑁 ∗ 2𝑁 = 𝑂(𝑁𝑙𝑜𝑔2 𝑁)
𝑎(𝑟 𝑛 − 1) 1. (2𝑘 − 1)
Q3 = ≅ 2𝑘
𝑟−1 2−1
Then 𝑘 = 𝑙𝑜𝑔2 𝑁

Thus, time complexity for the two loops will


become, 𝑁 ∗ 𝑙𝑜𝑔2 𝑁
= 𝑂(𝑁𝑙𝑜𝑔2 𝑁)
Solution
Q5
Notice that the j increment is j +=I which implies
j=j+i and it dependents on the value of I for each
iteration. Thus, j is initialized to 1 in the first
iteration. Subsequent looping when i=2, gives
j=j+i=1+2=3; when i=3, gives j=3+3=6, when i=4,
j=6+4=10, and so on.

I: 1 2 3 4 ….. n Solution
J: 1 3 6 10 ….. k term 𝑁
I: 1 2 3 4 ….. 3

Where the sequence of the j loop is a sum of J: 1 5 9 13 ….. N term


the consecutive terms of the i-th loop with the 𝑁
current j value: Th I will execute 3 times.The j-th increments by 4 to
execute N times and no dependencies; therefore
Thus, Sn= N/2(2a+(N-1)d) as a is first term 𝑁
total execution time will be ( 3 ) (𝑁)
which is 1, d is common difference =1
Therefore, we have : Hence, time complexity is 𝑂(𝑁 2 )
𝑁
(𝑁 + 1) Q6
2
Hence, time complexity is 𝑂(𝑁 2 ) Same as Q 2

Q7
Same as Q 1

2
NILE UNIVERSITY OF NIGERIA
DEPARTMENT OF COMPUTER SCIENCE
Course: Analysis of Algorithm
Assignment 1

Anda mungkin juga menyukai