DAA Unit 1
DAA Unit 1
INTRODUCTION TO ALGORITM
DESIGN
Session - 1
Syllabus
Introduction, Fundamentals of algorithm(Line
count, operation count), Algorithm Design Techniques
(Approaches, Design Paradigms), Designing an algorithm
and its Analysis(Best ,Worst & Average case),
Asymptotic Notations(O, Ω, ʘ) based on Orders of
Growth, Mathematical Analysis – Induction, Recurrence
Relation - Substitution method, Recursion method,
Master's Theorem
Algorithms
A finite set of instructions or logic, written in order, to
accomplish a certain predefined task.
Cannot be executed
Session - 2
Why to design an algorithm?
General approaches to the construction of efficient
solutions to problems
◦ They provide templates suited for solving a broad range of
diverse problems.
Characteristics
Examples:
◦ Minimal spanning tree
Characteristics
1. Optimal substructure:
2. Overlapping subproblems:
3. Bottom up approach:
Session - 3
Algorithm Analysis
An algorithm is said to be efficient and fast,
◦ if it takes less time to execute
◦ consumes less memory space.
The performance of an algorithm is measured on the
basis of
◦ Time Complexity
◦ Space Complexity
Space Complexity
◦ The amount of memory space required by the algorithm in its
life cycle.
◦ A fixed part For example simple variables & constant used and
program size etc.
The first loop runs in O(n) time, the second - O(n^2) time, the
maximum is O(n^2)
if C
S1;
else
S2;
The running time is the maximum of the running times of S1
and S2.
Session - 3
Asymptotic Notations
Main idea of asymptotic analysis
◦ To have a measure of efficiency of algorithms
◦ That doesn’t depend on machine specific
constants,
◦ That doesn’t require algorithms to be
implemented
◦ Time taken by programs to be compared.
Asymptotic notations
◦ Asymptotic notations are mathematical tools to
represent time complexity of algorithms for
asymptotic analysis.
Asymptotic Analysis
Asymptotic analysis refers to computing
the running time of any operation in
mathematical units of computation.
◦ Ο Notation
◦ Ω Notation
◦ θ Notation
Big Oh Notation, Ο
◦ It measures the worst case time complexity
or longest amount of time an algorithm can
possibly take to complete.
O(g(n)) = { f(n): there exist positive constants c and n0 such that
0 <= f(n) <= cg(n) for all n >= n0}
0 <= f(n) <= cg(n) for all n >= n0}
If f(n) = 3n+2
g(n)=n
Then
c1*g(n) <= f(n) <= c2*g(n)
C1,c2>0 and N>n0
For instance c2=4
f(n)<= c2g(n)
3n+2 <= 4n n0=1
f(n)>= c1g(n)
For instance c1=1
3n+2 >= n
Hence f(n) = theta(n)
Order of Growth Function
Worksheet No. 4
UNIT I
INTRODUCTION TO ALGORITM
DESIGN
Session - 5
Recursion recall
Functions calls itself
consists of one or more base cases and
one or more recursive cases.
Mathematical Analysis - Induction
Consider a recursive algorithm to compute the
maximum element in an array of integers.
You may assume the existence of a function “max(a,b)”
that returns the maximum of two integers a and b .
Solution
Let p(n) stand for the proposition that Algorithm finds and returns the maximum
integer in the locations A[1] through A[n]. Accordingly, we have to show that (∀n)
p(n) is true.
BASIS: When there is only one element in the array , i.e., n=1, then this element is
clearly the maximum element and it is returned on Line 2. We thus see that p(1) is
true.
INDUCTIVE STEP: Assume that Algorithm finds and returns the maximum
element, when there are exactly k elements in A.
Now consider the case in which there are k + 1 elements in A. Since (k + 1) > 1,
Line 4 will be executed.
From the inductive hypothesis, we know that the maximum elements in A[1]
through A[k] is returned. Now the maximum element in A is either A[k+1] or the
maximum element in A[1] through A[k] (say r). Thus, returning the maximum of
A[k+1] and r clearly gives the maximum element in A, thereby proving that p(k) →
p(k+1).
By applying the principle of mathematical induction, we can conclude that (∀n) p(n)
is true, i.e., Algorithm is correct.
Find the exact solution to the recurrence
relation using mathematical induction
method
Solution is T(n)=nlogn
Solution
Basis: At n = 1, both the closed form and the recurrence
relation agree (0=0) and so the basis is true.
Inductive step: Assume that T(r) = rlogr for all 1≤ r ≤ k . Now
consider T(k+1). As per the recurrence relation, we have,
Session – 6
Substitution method
Guess the solution.
Use induction to find the constants and
show that the solution works.
1. Guess the solution.
Example
2. Use induction to find the constants and show that the solution works.
Example:
1 if n = 1 ,
T (n) =
2T (n/ 2) + n if n > 1 .
1. Guess: T (n) = n lg n + n. [Here, we have a recurrence with an exact func-
tion, rather than asymptotic notation, and the solution is also exact rather than
asymptotic. Weíll have to check boundary conditions and the base case.]
2. Induction:
Basis: n = 1 ⇒ n lg n + n = 1 = T (n)
Inductive step: Inductive hypothesis is that T (k) = k lg k + k for all k < n.
Weíll use this inductive hypothesis for T (n/ 2).
n
T (n) = 2T + n
2
n n n
= 2 lg + + n (by inductive hypothesis)
2 2 2
n
= n lg + n + n
2
= n(lg n − lg 2) + n + n
= n lg n − n + n + n
= n lg n + n .
Lecture Notes for Chapter 4: Recurrences 4-3
Example: T (n) = 2T (n/ 2)+ (n). If we want to show an upper bound of T (n) =
2T (n/ 2) + O(n), we write T (n) ≤ 2T (n/ 2) + cn for some positive constant c.
Another Example
How to prove by the substitution method that
if T(n)=T(n−1)+Θ(n)then T(n)=Θ(n2)
Video Link
https://www.youtube.com/watch?v=Zhhh9qpAVN0
Worksheet No. 6
UNIT I
INTRODUCTION TO ALGORITM
DESIGN
Session – 7
Recurrence relation - recursion
solving recurrences
◦ expanding the recurrence into a tree
◦ summing the cost at each level
Example
Expand T(n/4)
Expand T(n/16)
Summing the cost at each level
Adding up the costs
Applying the geometric sum
Polishing the result we get
Verify the guess by applying substitution method
Lets see another example
Lets practice
Consider T(n) = 3T(n/2) + n. Use a
recursion tree to derive a guess for an
asymptotic upper bound for T(n) and
verify the guess with the substitution
method.
T(n) = T(n/2) + n2.
T(n) = 2T(n − 1) + 1.
Worksheet No. 7
UNIT I
INTRODUCTION TO ALGORITM
DESIGN
Session – 8
Master’s theorem method
Master Method is a direct way to get the solution. The master method
works only for following type of recurrences or for recurrences that
can be transformed to following type.
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1 and f(n) is an asymptotically
positive function.
3. If f(n) > Ω (nlogba), and f(n) satisfies the regularity condition, then
T (n) = ϴ (f(n)).
Solve the problems
T(n) = 3T(n/2) + n2
T(n) = 7T(n/2) + n2
T(n) = 4T(n/2) + n2
T(n) = 3T(n/4) + n lg n
T(n) = 3T(n/2) + n2
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
a=3 b=2 f(n) = n2
1. If f(n) < O(nlogba), then T (n) = ϴ (nlogba).
2. If f(n) = ϴ (nlogba) , then T (n) = ϴ (nlogbalogn).
3. If f(n) > Ω (nlogba), and f(n) satisfies the regularity condition,
then T (n) = ϴ (f(n)).
Step 1: Calculate nlogba = nlog23 = n1.58
Step 2: Compare with f(n)
Since f(n) > nlogba
i.e. n2 > n1.58
Step 3: Case 3 is satisfied hence complexity is given as
T(n) = Θ(f(n)) = Θ (n2)
T(n) = 7T(n/2) + n2
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
a=7 b=2 f(n) = n2
1. If f(n) < O(nlogba), then T (n) = ϴ (nlogba).
2. If f(n) = ϴ (nlogba) , then T (n) = ϴ (nlogbalogn).
3. If f(n) > Ω (nlogba), and f(n) satisfies the regularity
condition, then T (n) = ϴ (f(n)).
Step 1: Calculate nlogba = nlog27= n2.80
Step 2: Compare with f(n)
since f(n) < nlogba
Step 3: Case 1 is satisfied hence complexity is given as
T(n) = Θ (nlogba) = Θ (n2.8)
T(n) = 4T(n/2) + n2
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
a=4 b=2 f(n) = n2
3. If f(n) > Ω (nlogba), and f(n) satisfies the regularity condition, then T
(n) = ϴ (f(n)).