Unit1 Notes
Unit1 Notes
Introduction
Basic Formula:-
Properties of Logarithm (item 1):-
1. loga1 = 0
2. logaa = 1
3. loga x y = yloga x
4. loga x y = loga x + loga y
x
5. loga = loga x − loga y
y
6. a logb x = x logb a
logb x
7. loga x = = logab logb x
logba
8. a loga x = x
Combinatorics (item 2)
1. Number of permutations of an n-element set: P(n) = n!
n!
2. Number of k-combinations of an n-element set: C(n,k) =
k!(n − k)!
3. Number of subsets of an n-element set: 2n
∑
1 = 1 + 1 + 1 + . . . + 1 = u − l + 1, l ≤ u
•
i=l
∑
lg i ≈ nlgn
•
i=1
n
1 − an
k
•∑
a = ,a<1
k=0
1 − a
n
an − 1
k
• ∑
a = ,a>1
k=0
a − 1
n
n(n + 1)
∑
k=
•
k=0
2
n
n(n + 1)(2n + 1)
k2 =
• ∑ 6
k=0
n
(n(n + 1))2
3
• ∑
k = ,a<1
k=0
4
n
n(n + 1)(2n + 1)(3n 2 + 3n − 1)
4
• ∑
k =
k=0
30
n
x − (n + 1)x n+1 + n x n+2
k
• ∑
kx = , x>1
k=0
(x − 1) 2
n
x − (n + 1)x n+1 + n x n+2
k
• ∑
kx = , x<1
k=0
(1 − x) 2
∑ ∑
1. cai = c ai
i=l i=l
u u u
(ai ± bi) = ai ±
∑ ∑ ∑
2. bi
i=l i=l i=l
u m u
∑ ∑ ∑
3. ai = ai + ai, where l ≤ m < u
i=l i=l i=m+1
∑
4. (ai − ai−1) = au − al−1
i=l
Miscellaneous
(e)
n
n
1. n! ≈ 2π n as n → ∞ (Stirling′s for mula) see item 3 equation 2
1. Analysis of Algorithm:-
Analysis of algorithm is a method using which we find the complexity of
algorithms. There are two types of algorithm.
a. Non - Recursive.
b. Recursive.
∑
S(i) = 1 = (n − 1) − (i + 1) + 1 = n − i − 1
j=i+1
Outer loop:
n−2 n−2 n−2 n−2
∑ ∑ ∑ ∑
op = = (n − i − 1) = (n − 1) − i
i=0 i=0 i=0 i=0
(n − 2) * (n − 1)
op = (n − 1) * (n − 2) −
2
(n − 2) * (n − 1)
op = = O(n 2)
2
Comparing the three formulas for T(n), we can see the pattern arising after i
such substitutions:1 T(n) = T(n − i) + (n − i + 1) + (n − i + 2) + ... + n.
( )
n(n + 1)
Solution: T(n) = −1 c+d
2
Example 1
fact(n)
if (n <= 1)
return n
else
return n*fact(n-1)
In this equation one multiply operation is reducing the size of input by one hence
the recurrence relation will be:-
T(n) = T(n-1) + 1
T(1) = 1 …………..(This tells us that base case takes constant number of
operations)
Solution:-
T(n) = T(n-1) + 1 ……… eq 1
putting n —> n-1 in eq 1
T(n-1) = T(n-2) + 1………. eq 2
putting n —> n-2 in eq 1
T(n-2) = T(n-3) + 1……… eq 3
put value of T(n-1) from eq 2 to eq 1
T(n) = T(n-2)+1+1
= T(n-2) + 2 ……….. eq 4
put value of T(n-2) from eq 3 to eq 4
T(n) = T(n-3)+1+2
= T(n-3) + 3
now we can generalise as we can see the pattern here n is reduced by one
and the operation is increased by one
T(n) = T(n-k) + k
base condition is T(1) so we make n-k = 1 i.e k = n-1
T(n) = T(n-(n-1)) + n-1
T(n) = T(1) + n-1
= 1+ n-1 = n = O(n).
therefore:
T(n) = T(1) + log2 n
T(n) = 1 + log2 n = O(log2 n).
Practice problems:-
1. Program:-
for (i =1; i < n; i=i*2)
{
for(j = 1; j < n ; j = j+1)
{
//something
}
}
Answer = n logn
2. T(n) = T(n /4) + 1 for n > 1,T(1) = 1, answer = O(log4 n).
3. T(n) = 2T(n /2) + 1, for n > 1, T(1) = 1, answer = O(n).
4. T(n) = 2T(n − 1) + 1 for n > 1, T(1) = 1, answer = O(2n).
5. T(n) = T(n − 1) + n, for n > 1, T(1) = 1, answer = O(n 2).
6. T(n) = 2T(n − 1) + n, for n > 1, T(1) = 1, answer = O(2n).
7. T(n) = 2T(n /2) + n, for n > 1, T(1) = 1, answer = O(n log2 n).
∑
In Question 1 you cannot show it using notation. It can be solved if you just
see how many times the inner loop will run based on updation.