DAA E-Content - Module 1 Introduction
DAA E-Content - Module 1 Introduction
Module-I: Introduction
Dr. A K Yadav
Contents
Introduction to algorithms 3
Asymptotic notations 7
Relationship between notations 11
Recurrences 20
Recurrence Solution Methods 21
Substitution method 22
Iteration method 34
Recursion tree method 50
Master method (D&C) 58
Muster Theorem (S&C) 70
Complexity Analysis 80
Complexity analysis: Insertion sort 82
Introduction to algorithms
▶ What is an algorithm?
- An algorithm is a set of rules for carrying out calculation
either by hand or on a machine.
- An algorithm is a sequence of computational steps that
transform the input into output.
- An algorithm is a sequence of operations performed on data
that have to be organized in data structures.
- A finite set of instructions that specify a sequence of
operations to be carried out in order to solve a specific
problem or class of problems.
- An algorithm is an abstraction of a program to be executed
on a physical machine.
▶ Algoritm vs Program
- Algorithm is independent of programming language and
written in any natural language.
- Implementation of any algorithm in a programming language
is a program.
▶ Design Techniques
- Divide and Conquer (D&C)
- Dynamic Programming (DP)
- Greedy Approach
- Branch and Bound
- Backtracking
- Randomized
Asymptotic notations
1. O - notation ”Big O” : Asymptotic upper bound,
O(g(n)) = {f (n) : ∃c, n0 > 0 such that 0 ≤ f (n) ≤
cg(n) for all n ≥ n0 }
f (n) ∈ O(g(n))
f (n) = O(g(n))
Examples
1. Show that an + b = O(n2 )
2. Show that 21 n2 − 3n = Θ(n2 )
3. Show that 12 n2 − 3n = Ω(n)
Solutions
1. Show that an + b = O(n2 )
1
c1 n2 ≤ n2 − 3n ≤ c2 n2
2
1 3
⇒ c1 ≤ − ≤ c2 after dividing n2
2 n
1 3
⇒ 0 < c1 ≤ −
2 n
1 3
⇒0< −
2 n
3 1
⇒ <
n 2
⇒n>6
Let n = 7
1
⇒ 0 < c1 ≤
14
1
Let c1 =
14
1 3
Now 0 < − ≤ c2
2 n
1
⇒ 0 < ≤ c2 when n = ∞
2
1
Let c2 =
2
1 2
⇒ 0 < 14 n ≤ 12 n2 − 3n ≤ 21 n2 for all n ≥ n0 = 7 and any
1
constant 0 < c1 ≤ 14 ≤ 12 ≤ c2 .
1
Let cn ≤ n2 − 3n
2
1
c ≤ n − 3 after dividing n both side
2
1
⇒c ≤ n+3
2
1
⇒ c ≤ n + 3n for all n > 0
2
7
⇒c≤ n
2
Recurrences
A recurrence is an equation or inequality that describes a function
in terms of its value on smaller inputs.
(
nf (n − 1) if n > 1
f (n) =
1 if n = 1
(
2T (n/2) + Θ(n) if n > 1
T (n) =
Θ(1) if n = 1
(
T (n − 1) if n > 1
T (n) =
Θ(1) if n = 1
Substitution method
- In Substitution method there are two steps :
1 - First guess the solution
2 - Second use mathematical induction to find the constants and
show that the solution works.
- This method is powerful, but depends on the correctness of the
guess.
- Method can be used for either upper or lower bounds on a
recurrence.
⇒ T (n) = O(n lg n) then it must be the solution for the input size
n/2.
⇒ T (n) ≤ cn lg(n/2) + n
⇒ T (n) ≤ cn lg n − cn lg 2 + n
⇒ T (n) ≤ cn lg n − cn + n
⇒ T (n) ≤ cn lg n − n(c − 1)
⇒ T (n) ≤ cn lg n ∀c ≥ 1, n ≥ 2
∴ T (n) = O(n lg n)
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 25/87
School of Computer Science and Engineering
√
T (n) = 2T ( n) + lg n (given) (4)
Take:
lg n = m (5)
⇒ n = 2m (6)
Put back the value of S(m) = T (2m ) and m from Eq. 5 into Eq. 9
⇒ T (n) = Ω(n lg n) then it must be the solution for the input size
n/2.
substitute the value of n as n/2 in Eq. 12.
⇒ T (n) ≥ cn lg(n/2) + n
⇒ T (n) ≥ cn lg n − cn lg 2 + n
⇒ T (n) ≥ cn lg n − cn + n
⇒ T (n) ≥ cn lg n + n(1 − c)
⇒ T (n) ≥ cn lg n ∀c ≤ 1, n ≥ 2
∴ T (n) = Ω(n lg n)
⇒ T (n) = O(lg n) then it must be the solution for the input size
n/2.
substitute the value of n as n/2 in Eq. 15.
T (n) ≤ c lg(n/2)) + 1
⇒ T (n) ≤ c lg(n/2) + 1
⇒ T (n) ≤ c lg n − c lg 2 + 1
⇒ T (n) ≤ c lg n − c + 1
⇒ T (n) ≤ c lg n − (c − 1)
⇒ T (n) ≤ c lg n ∀c ≥ 1, n ≥ 2
∴ T (n) = O(lg n)
T (n) = T (n − 1) + n (17)
⇒ T (n) = O(n2 ) then it must be the solution for the input size
n − 1.
substitute the value of n as n − 1 in Eq. 18.
⇒ T (n) ≤ c(n2 − 2n + 1) + n
⇒ T (n) ≤ cn2 − 2cn + c + n
⇒ T (n) ≤ cn2 − (2c − 1)n + c
Take value of c such that (2c − 1) ≥ c
⇒ T (n) ≤ cn2 ∀c ≥ 1, n ≥ 1
∴ T (n) = O(n2 )
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 33/87
School of Computer Science and Engineering
Iteration method
- In iteration method, expend the right hand side of the recurrence
relation.
- Try to find out the series and number of terms in the series.
- Find out the sum of the series
Find the value of T (n/2) = 2T (n/4) + n/2 from Eq. 20 and put
into Eq. 20
Again find the value of T (n/4) = 2T (n/8) + n/4 from Eq. 20 and
put into Eq. 21
⇒ n/2i = 1
⇒ n = 2i
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 37/87
School of Computer Science and Engineering
⇒ lg n = i
So after lg n iteration, the recurrence term will be terminated.
Eq. 23 will be
⇒ T (n) = nT (1) + n lg n
⇒ T (n) ≤ cn + n lg n, ∀c ≥ T (1)
⇒ T (n) ≤ cn lg n + n lg n, ∀n ≥ 2
⇒ T (n) = (c + 1)n lg n
⇒ T (n) ≤ c1 n lg n, ∀c1 ≥ (c + 1) > 0, ∀n ≥ 2
∴ T (n) = O(n lg n)
⇒ T (n) = 3 3T (n/16) + c(n/4)2 + cn2
3
T (n) = 9T (n/16) + 1 + cn2 (25)
16
2 !
3 3
T (n) = 27T (n/64) + 1 + + cn2 (26)
16 16
⇒ n/4i = 1
⇒ n = 4i
⇒ log4 n = i
So after log4 n iteration, the recurrence term will be terminated.
Eq. 27 will be
2 (log4 n−1) !
3 3 3
log4 n
⇒ T (n) = 3 T (1) + 1 + + + ··· + cn2
16 16 16
2 (log4 n−1) !
3 3 3
log4 3
⇒ T (n) = n T (1) + 1 + + + ··· + cn2
16 16 16
1
⇒ T (n) ≤ c1 n + cn2
1 − 3/16
16
⇒ T (n) ≤ c1 n + cn2
13
16
2
⇒ T (n) ≤ c1 n + cn2 , ∀n ≥ 1
13
13c1 + 16c
⇒ T (n) ≤ n2
13
T (n) = T (n − 1) + n4 (28)
⇒ T (n) = T (n − 2) + (n − 1)4 + n4
⇒n−i =0
⇒i =n
So after n iteration, the recurrence term will be terminated. Eq. 31
will be
⇒ T (n) = T (0) + 14 + 24 + 34 + · · · + (n − 1)4 + n4
⇒ T (n) ≤ T (0) + n4 + n4 + · · · + n4 , ∀n ≥ 1
⇒ T (n) ≤ (c + 1)n5
√
T (n) = T ( n) + n (32)
√ √ √
Find the value of T ( 2 n) = T ( 4 n) + 2 n from Eq. 32 and put into
Eq. 32
√ √
⇒ T (n) = T ( 4 n) + 2 n + n
√ √
T (n) = T ( 4 n) + 2 n + n (33)
√ √ √
T (n) = T ( 8 n) + 4 n + 2 n + n (34)
√
2i
⇒ n=2
⇒ T (n) ≤ n + n + n + . . . lg lg n times
⇒ T (n) ≤ n lg lg n
∴ T (n) = O(n lg lg n)
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 49/87
School of Computer Science and Engineering
T ( n2 ) T ( n2 )
n n
2 2
n n
2 2
n n n n
22 22 22 22
.. .. .. .. .. .. .. ..
. . . . . . . .
n n
2 2
n n n n
22 22 22 22
.. .. .. .. .. .. .. ..
. . . . . . . .
T ( 2ni ) · · · · · · · · · · · · · · T ( 2ni )
T ( n4 ) T ( n4 ) T ( n4 )
cn2
n 2 n 2 n 2
c 4 c 4 c 4
n n n n n n n n
T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T(
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 56/87
School of Computer Science and Engineering
n 2 n 2 n
c 4 c 4 c 4
n 2 n 2 n 2 n 2 n 2 n 2 n 2 n
c 16 c 16 c 16 c 16 c 16 c 16 c 16 c 16
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
. . . . . . . . . . . . . . . . . . . . . . .
T (n) = Θ nlog3 9 = Θ n2
T (n) = Θ (f (n)) = Θ (n lg n)
nlogb a = nlog2 2 = n1
T (n) = Θ n1 lg n = Θ (n lg n)
nlogb a = nlog2 8 = n3
T (n) = Θ n3
nlog2 4 = n2
T (n) = Θ n3
T (n) = aT (n − b) + f (n)
Proof:
Expend the left hand side term of given recurrence
T (n) = aT (n − b) + f (n)
n
n n
⇒ T (n) = a b T (0)+a b −1 f n− − 1 b +· · ·+af (n−b)+f (n)
b
n
b
−1
X n
⇒ T (n) = ai f (n − ib) + a b T (0)
i=0
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 71/87
School of Computer Science and Engineering
n
Xb
⇒ T (n) ≤ ai f (n − ib)
i=0
n
b
X
⇒ T (n) = O nd ai
i=0
n
n
b
X
i1 − a b +1 1
a = ≤ = O(1)
i=0
1−a 1−a
⇒ T (n) = O nd O(1)
⇒ T (n) = O nd
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 72/87
School of Computer Science and Engineering
2. if a = 1 then
n n
b b
X
i
X n
a = 1= + 1 = O(n)
i=0 i=0
b
⇒ T (n) = O nd O(n)
⇒ T (n) = O nd+1
3. if a > 1 then
n
n
b
X a b +1 − 1 n
ai = = O(a b )
i=0
a−1
n
⇒ T (n) = O nd O(a b )
n
⇒ T (n) = O nd a b
so
d
O(n ) a<1
T (n) = O(nd+1 ) a=1
O(a bn nd )
a>1
T (n) = O (n)
2. Find the solution of T (n) = 78 T (n − 1) + n3
Here a = 78 , b = 1, f (n) = O(n3 ), d = 3,
Case 1 applies. So the solution is :
T (n) = O nd
T (n) = O n3
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 76/87
School of Computer Science and Engineering
n
T (n) = O n1 2 2
Module-I: Introduction Dr. A K Yadav Design and Analysis of Algorithm 78/87
School of Computer Science and Engineering
Complexity analysis
Analyzing an algorithm means predicting the resources that the
algorithm requires. Resources may be memory, communication
bandwidth, computer hardware or CPU time. Our primary concern
is to measures the computational time required for the algorithm.
Running time:-The running time of an algorithm is the number of
primitive operations or steps executed on a particular input.
Why do we normally concentrate on finding only the worst-case
running time?
1. The worst-case running time of an algorithm gives us an
upper bound on the running time for any input. So it
guarantees that the algorithm will never slower than this.
2. In real applications, worst case normally occurs for example
searching a non existing data.
n
X n
X n
X
⇒ T (n) = an + b + c4 tj + c5 (tj − 1) + c6 (tj − 1)
j=2 j=2 j=2
T (n) = an + b = O(n)
2. Worst Case: The algorithm performs worst if key > A[i] for
each value of j and stops only when i < 1 in step 4.
Then it will execute always j times for each value of
j = 2, 3, . . . , n
so
n
X (n − 1)(2 + n)
j=
j=2
2
Thank you
Please send your feedback or any queries to
ashokyadav@[Link]