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

Algorithm Quiz Topics For Cse Students (On Recurrence Relation)

The document discusses three problems related to analyzing algorithms and their time complexities. It provides sample solutions and approaches to showing that three different recurrence relations are O(n), O(sqrt(n)), and O(n^2) respectively.

Uploaded by

hairy.puds.mint
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Algorithm Quiz Topics For Cse Students (On Recurrence Relation)

The document discusses three problems related to analyzing algorithms and their time complexities. It provides sample solutions and approaches to showing that three different recurrence relations are O(n), O(sqrt(n)), and O(n^2) respectively.

Uploaded by

hairy.puds.mint
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE211: Algorithms Spring 2024

Assignment 1 Sample Solution


Assignment Quiz: 18th March 2024

There are multiple ways of solving these problems. Here, each problem is solved
with a different technique for demonstration, but each presented technique below
can be used for all three problems. There may be other methods to solve these
problems, besides those that are presented here.
1. Given the recurrence relation:
n n
T (n) = 2T +T + 3n
5 2
with T (1) = 0, show that T (n) ∈ O(n).
Solution with Approach 1: Recursion Tree

T (n) 3n

n n n 9
  
T 5
T 5
T 2
3n 10

n n n n n n n n n 81
        
T 25
T 25
T 10
T 25
T 25
T 10
T 10
T 10
T 4
3n 100

.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
. . . . . . . . . . . . . . . . . . . . . . . . . . .

Cost at level 0 = 3n
Cost at level 1 = 3(n/5) + 3(n/5) + 3(n/2) = 3n(9/10)
Cost at level 2 = 3(n/25) + 3(n/25) + 3(n/10)+ 3(n/25) + 3(n/25) + 3(n/10) + 3(n/10) +
3(n/10) + 3(n/4) = 3n 4+4+10+4+4+10+10+10+25
100
= 3n(81/100) = 3n(9/10)2
In general, cost at level i = cost at level (i−1)×(1/5+1/5+1/2) = cost at level (i−1)×9/10
Since the cost at level 0 is 3n, this means the cost at level i is 3n(9/10)i
Tree Height = max(log2 n, log5 n) = log2 n.

height log2 n log2 n


X X X
i
Total Cost ≤ Cost at level i = 3n(9/10) = 3n (9/10)i = 3n × O(1) ∈ O(n)
i=0 i=0 i=0

1
2. Given the recurrence relation:


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

with T (1) = 0, show that T (n) ∈ O( n).
Solution with Approach 2: Substitution (Induction)

Assume T (n) ≤ c n. Then


n  
4n
T (n) = T +T +2 n
4 25
r

r
n 4n
≤c +c +2 n
4 25
√ √
c n 2c n √
= + +2 n
2  5 
√ 1 2 √
=c n + +2 n
2 5

9c n √
= +2 n
10

9c n √ √
We must choose a value of c that satisfies 10
+2 n≤c n


9c n √ √
+2 n≤c n
10
9c
⇐⇒ +2≤c
10
9c
⇐⇒ 2 ≤ c −
10
c
⇐⇒ 2 ≤
10
⇐⇒ 20 ≤ c


9c n √ √
√ c = 23, we have c ≥ 20, which means T (n) ≤
If we choose 10
+ 2 n ≤ c n, i.e.,
T (n) ∈ O( n).

2
3. Given the recurrence relation:

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

with T (1) = 0, show that T (n) ∈ O(n2 ).


Solution with Approach 3: Akra-Bazzi Method
p p
Let p be the real number such that 2 13 + 23 = 1.
1 1 2 2 2
Observe that 2 31 + 23 = 23 + 23 = 43 > 1 and 2 13 + = 29 + 49 = 2

3 3
< 1, which means
1 < p < 2.
Using the Akra-Bazzi method, with f (n) = 4 we have:

  Z n 
p f (u)
T (n) ∈ Θ n 1 + p+1
du
1 u
  Z n 
p 4
=Θ n 1+ p+1
du
1 u
   n 
p 4
=Θ n 1+ note that p + 1 ̸= 1 since p > 1
−pup 1
  
p 4 4
=Θ n 1− p +
pn p
p
= Θ(n )

We have T (n) ∈ Θ(np ). Since p < 2, this means T (n) ∈ O(n2 ).

You might also like