0% found this document useful (0 votes)
18 views5 pages

0 Analysis Sheet-1

Uploaded by

Tena Fr.angelos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

0 Analysis Sheet-1

Uploaded by

Tena Fr.angelos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ALG’22: Analysis SHEET

Contents
FIRST: Analysis of Normal Code .............................................................................................................................................. 2
SECOND: Analysis of Recursive Code ...................................................................................................................................... 3
THIRD: Asymptotic Notations ................................................................................................................................................. 4
FOURTH: Recurrence Equations.............................................................................................................................................. 5
FIRST: Analysis of Normal Code

#iteration =N
o(Body)=O(1)
o(1)*o(N)=O(N)

#iteration=R
o(Body)=O(R)
O(R)*O(R)=O(R^2)

1 R
1 2 4 8 16 2^L
SECOND: Analysis of Recursive Code
What's the exact complexity of the following code?
Fun2(A, S, E) 3T(N/3)+O(1) a. Θ(N)
IF (S == E) return A[S] master method b. Θ(N3 )
S1 = (S + E) / 3 n^logba =N c. Θ((log(𝑁))3)
1. S2 = 2 × (S + E) / 3
N>1 d. Θ(log(𝑁))
R1 = Fun2(A, S, S1)
#theta(N) e. Θ(3 × log(𝑁))
R2 = Fun2(A, S1+1, S2)
R3 = Fun2(A, S2+1, E) f. Θ(1)
Return Max(R1, R2, R3)

2. For the following “Main” function:


Main(A, N) Fun(Z, L)
I = N If (Z == 2)
Sum = 0 Return 0
For (J = 0; J < I; J += N/2) For I = 1 to L
While (I > 1) If (random(1,1000) % 2 == 0)
Sum = Sum + 1 𝑍
X += I × Fun( , 𝐿)
I = I - 1 𝐿
Else
EndWhile
𝑍
I = N X += (I + 1) × Fun(
2×𝐿
, 𝐿)
EndFor EndFor
Return 5 × Fun(N, 4) End Fun
End Main //random(1,1000): generates a random integer
between 1 & 1000 in O(1)

1. What's the complexity of the non-recursive part of code?


2. What's the recurrence equation (T(N)) that represents the LOWER BOUND of the entire code?
3. What's the LOWER BOUND complexity of entire code?
4. What's the recurrence equation (T(N)) that represents the UPPER BOUND of the entire code?
5. What's the UPPER BOUND complexity of entire code?

outer loop:-
4T(N\8)+O(1)
0 n
0 n/2 n
4T(N/4)+O(1)
# iteration =2
#iteration *o(Body)
inner loop:-
N ,N-1,N-2 N-L
N-l=1
L=n-1
#iteration*o(Body)
=N*O(1)=O(N)
________
total
2*O(N)=O(N)
THIRD: Asymptotic Notations
1. Chose the notation that BEST REPRESENTS the WORST case?
Fun2(N) a. O(1)
Sorted = true b. Θ(1)
I = 1 c. O(N)
While (I < N AND Sorted == true)
d. Θ(N)
If (A[I] > A[I + 1])
e. Ω(1)
Sorted = false
End If f. Ω(N)
I = I + 1 g. Θ(N-1)
End while h. None of the choices
Print Sorted
2. Let f(N) = 10N×Log2(N) and g(N) = N, what's the possible value(s) of N0 that makes f(N) = Ω(g(N)) for constant
C = 20?
a. 1≤ N0≤32 b. 0< N0 ≤ 2 c. N0 ≥ 2 d. N0 ≥ 4 e. No values exist. The relation is not valid
3. Given the following orders (log is base 2):
1)√ 𝑒 log(𝑁) + 𝑁 3 , 2) 1⁄√log (𝑁) , 3) 𝑁, 4) 𝑁 3 , 5) 4log (𝑁) , 6) 𝑁 log(𝑁)
Arrange them in increasing order of growth rate (with g(n) following f(n) in your list if and only if
f(n)=O(g(n))). Chose the correct order?
a. 2,3,6,5,1,4 b. 2,3,1,6,5,4 c. 2,6,3,1,5,4 d. 2,3,6,1,5,4 e. 4,5,1,3,6,2 f. None of choice
FOURTH: Recurrence Equations
1. Using the RECURSION TREE method, Answer the following questions:
𝐓(𝐍) = 𝐓(𝐍/𝟓) + 𝐓(𝟕𝐍/𝟏𝟎) + 𝚯(𝐍) ; 𝐓(𝟏) = 𝟏
Question Answer

1. What's the EXACT total number of levels in this tree?

2. What's the complexity of each level?

3. What's the complexity of LAST level?

4. What's the UPPER BOUND order of this tree? O( )

2. Given T(N) = 64 T(N / 16) + (22/7) N√N, using Master Method: what's the correct value of Ɛ

You might also like