Algorithm Quiz Topics For Cse Students (On Recurrence Relation)
Algorithm Quiz Topics For Cse Students (On Recurrence Relation)
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.
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
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 )