0% found this document useful (0 votes)
3 views7 pages

Asymptotic Problems

The document contains practice problems for a CSE211 Algorithms course focusing on asymptotic analysis, including proofs of O, Ω, and Θ notations. It also includes questions on the runtime analysis of code fragments with for and while loops, as well as best-case, worst-case, and average-case runtimes for various algorithms. Additionally, it presents recurrence relations for which students must derive Θ bounds.

Uploaded by

Abu Musa Sakib
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)
3 views7 pages

Asymptotic Problems

The document contains practice problems for a CSE211 Algorithms course focusing on asymptotic analysis, including proofs of O, Ω, and Θ notations. It also includes questions on the runtime analysis of code fragments with for and while loops, as well as best-case, worst-case, and average-case runtimes for various algorithms. Additionally, it presents recurrence relations for which students must derive Θ bounds.

Uploaded by

Abu Musa Sakib
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/ 7

CSE211: Algorithms Spring 2023

Practice Problems: Asymptotic Analysis


Note: The base for log is 2, unless a different base is indicated.
1. Prove the following O() notations from defintion:

a) 3n2 + 2n + 5 ∈ O(n2 )
b) 2n3 − 4n2 + 3 ∈ O(n4 )
2n3 +8
c) 4n2 +6n
∈ O(n)
d) n! ∈ O(nn )

2. Prove the following Ω() notations from defintion:

a) 3n2 + 2n + 5 ∈ Ω(n2 )
b) 2n3 − 6n2 + 3 ∈ Ω(n3 )
2n3 +8
c) 4n2 +6n
∈ Ω(n)
d) 3n ∈ Ω(2n )

3. Prove the following Θ() notations from defintion:

a) 3n2 + 2n + 5 ∈ Θ(n2 )
b) log5 n ∈ Θ(log n)
c) 14 + 3 sin(n) ∈ Θ(1)
d) log(n!) ∈ Θ(n log n)

4. For each of the following statements, indicate whether it is true or false. If true, prove
the statement. If false, provide a counterexample by defining functions f (n) and g(n).

a) If f (n) ∈ O(n) and g(n) ∈ O(n2 ), then f (n) ∈ O(g(n)).


b) If f (n) ∈ O(n2 ) and g(n) ∈ O(log n), then f (n)g(n) ∈ O(n2 log n)
f (n)
c) If f (n) ∈ O(n3 ) and g(n) ∈ O(n2 ), then g(n)
∈ O(n).
 
d) If f (n) ∈ O(g(n)), then 2f (n) ∈ O 2g(n) .
5. For each of the following code fragments featuring for loops, give a Θ() bound on the
runtime as a function of n.

a)
1 a ← 24;
2 for i ← 1 to 1000 do
3 for j ← 1 to 21000 do
4 print(a);
5 end
6 end

b)
1 b ← 1;
2 for i ← 1 to n do
3 for j ← 1 to n do
4 for k ← 1 to n do
5 b ← b + i + j − k2;
6 end
7 end
8 end

c)
1 for i ← 1 to n do
2 for j ← 1 to min(i, 500000) do
3 print(i + j);
4 end
5 end

d)
1 sum ← 0;
2 for i ← 1 to n do
3 for j ← i to n do
4 for k ← i to j do
5 sum ← sum + k;
6 end
7 end
8 end
6. For each of the following code fragments featuring while loops, give a Θ() bound on the
runtime as a function of n.

a)
1 k ← n;
2 while k > 0 do
3 if k is even then
4 k ← k/2;
5 end
6 else
7 k ← k − 1;
8 end
9 end

b)
1 x ← 2;
2 while x < n do
3 x ← x2 ;
4 end

c)
1 j ← 0;
2 k ← 1;
3 while j ≤ n do
4 j ← j + k;
5 k ← k + 2;
6 end

d)
1 k ← 1;
2 for i ← 1 to n do
3 j ← 0;
4 while j ≤ n do
5 j ← j + k;
6 end
7 k ← 2k;
8 end
7. Give the best-case, worst-case, and average-case runtimes for each of the following algo-
rithms as Θ() bounds. The input for each algorithm is an array A, which contains integers
from 0 to n − 1 exactly once in any order.

a)

1 val ← 0;
2 if A[n − 1] is even then
3 for i ← 0 to n − 1 do
4 for j ← i + 1 to n − 1 do
5 val ← val + (A[i] × A[j]);
6 end
7 end
8 end
9 else
10 if A[n − 1] is divisible by 3 then
11 for i ← 0 to n − 1 do
12 val ← val + A[i];
13 end
14 end
15 end
16 return val;

b)
1 sorted ← T rue;
2 for i ← 1 to n − 1 do
3 if A[i] < A[i − 1] then
4 sorted ← F alse;
5 end
6 end
7 if sorted == T rue then
8 for i ← 0 to n − 1 do
9 for j ← 0 to n − 1 do
10 for k ← 0 to n − 1 do
11 print("Timing Attack Backdoor!")
12 end
13 end
14 end
15 end
c)
1 m ← A[0];
2 for i ← 1 to n − 1 do
3 if A[i] > m then
4 m ← A[i];
5 end
6 end
7 if m == A[0] then
8 for i ← 0 to n − 1 do
9 for j ← 0 to n − 1 do
10 k ← 1;
11 while k < n do
12 A[k] ← A[i]+A[j]
2
;
13 k ← 2k;
14 end
15 end
16 end
17 end

d)
1 for i ← 0 to n − 1 do
2 while A[i] ̸= i do
3 swap(A[i], A[A[i]]);
4 end
5 end
(Recall that each A[i] contains an integer from 0 to n − 1, with each value appearing
exactly once.)

8. Derive a Θ() bound for each of the following recurrence relations:

a) T (n) = 9T (n/3) + 10n3

b) T (n) = 4T (n − 7) + n2

c) T (n) = 8T (n/4) + 2n n log3 n

d) T (n) = 2T (n/5) + T (n/2) + 3n


9. Give the best-case, worst-case and average-case runtimes for the following algorithms as
Θ() bounds.

Algorithm 1: IsPalindrome(A, ℓ, r)
Input: Array A of n bits (0s and 1s), index ℓ (initially 0), index r (initially n − 1)
1 if ℓ >= r then
2 Return True;
a) 3 end
4 if A[ℓ] ̸= A[r] then
5 Return False;
6 end
7 Return IsPalindrome(A, ℓ + 1, r − 1);

Algorithm 2: GetSum(A, ℓ, r)
Input: Array A of size n, index ℓ (initially 0), index r (initially n − 1)
1 if ℓ > r then
b) 2 Return 0;
3 end
4 mid = ℓ+r2
;
5 Return A[mid] + GetSum(A, ℓ, mid − 1) + GetSum(A, mid + 1, r);

Algorithm 3: Arrange(A, i)
Input: Array A of n integers from 0 to n − 1 each appearing exactly once, index i
(initially 0)
1 if i == n then
2 Return;
3 end
c) 4 for j ← i to n − 1 do
5 if A[j] == i then
6 Swap (A[j], A[i]);
7 Break;
8 end
9 end
10 Arrange(A, i + 1);
Algorithm 4: Stooge(A, ℓ, r)
Input: Array A of size n, index ℓ (initially 0), index r (initially n − 1)
1 if A[r] < A[ℓ] then
2 Swap(A[ℓ], A[r])
3 end
d) 4 if r − ℓ +
j 1 > k2 then
5 t ← r−ℓ+1
3
;
6 Stooge(A, ℓ, r − t);
7 Stooge(A, ℓ + t, r);
8 Stooge(A, ℓ, r − t);
9 end

You might also like