Design and Analysis of Algorithms
L T P : 3 0 3
Total Credits : 4.5
Pre-requisites : Data Structures
Course Outcomes
2
Text books/ Reference Books
Text Books:
3
Syllabus of the Course
1. Introduction to Algorithms
2. Divide and Conquer Approaches
3. Greedy Algorithms
4
Syllabus of the Course (con…)
4. Dynamic Programming
5. Graph Algorithms
6. Advanced Topics
5
Evaluation Scheme (Theory + Practical)
6
Particular | Marks Weightage (%) |
Mid Term | 20 |
End Term | 40 |
Continuous Evaluation through Class Tests/Assignments/ Online Quiz | 20 |
Lab Work | 20 |
Motivation
We use algorithms all the time:
7
What is an algorithm?
8
Algorithm to reach SAU
9
Algorithm
1. Go straight.
2. Take left turn towards Gowshala Road.
3. You will reach SAU.
Take 3rd left turn
(No ambiguity)
10
Which Left???
Characteristics of a good algorithm
11
Unambiguous
Effective/Correct
Termination
Defined set of inputs and outputs
Properties of Algorithms
12
Steps Required to Develop an Algorithm
13
Performance Analysis of algorithm
14
A Priori
A Posteriori
A Priori vs A Posteriori analysis
15
A Priori
A Posteriori
Analysis of algorithm
16
Time & Space Complexity
17
Importance of analysing algorithms
18
Which algorithm is better?
There can be many algorithms to solve a problem….
All the algorithms are correct….
But which is the best??
The running time of the algorithms increase as the size of the input increases.
19
Asymptotic Notations
Running times of algorithm A = TA(n)
Running times of algorithm B = TB(n),
where n is a measure of the problem size.
if TA(n)< TB(n) Algorithm A is better than Algorithm B.
Unfortunately, we usually don't know the problem size beforehand.
20
Asymptotic Notations
(Asymptotes : Tangents to the Curve at Infinity)
21
Which is better
Two algorithms: A1 A2
Time needed by each: n 100000n
22
Which is better
Two algorithms: A1 A2
Time needed by each: n 100000n
Mathematically A2 needs more time but Asymptotically they are same
23
Which is better
Two algorithms: A1 A2
Time needed by each: n2 100000n
24
Which is better
Two algorithms: A1 A2
Time needed by each: n2 10000n
Mathematically A2 needs more time but Asymptotically they are same
Till 9999 I/Ps they A2 needs more time, for 10000 I/Ps both need same time, beyond 10000 I/Ps A1 needs more time
25
Asymptotic Analysis
Reasons
Example: f(n) = n2+4n + 20
n2 is the dominant term and the term 4n + 20 becomes insignificant as n grows larger.
We need ways to represent time/space complexity of algorithms which is independent of Machine Specifications. These ways are Asymptotic Notations
26
Asymptotically Same
n 10000n 10000000n
n n/2 n/10000
Note
n2 >>> 100000n
(1/10000) n2 >>> 100000n
Big Oh Notation(O)
Definition:
f(n) = O(g(n))
if there are two positive constants c and n0
such that:
f(n) ≤ c g(n) for all n ≥ n0
g(n) is an asymptotic upper bound for f(n),
which means the growth rate of f(n) is no more
than the growth rate of g(n).
27
If f(n) = 2n2
g(n) is O(n3)
Big Oh Notation(O)
28
Questions on Big Oh Notation(O)
f(n) = O(g(n)) ???
g(n) = O(f(n)) ???
29
Questions on Big Oh Notation(O)
1. f(n) = O(g(n)) & g(n) = O(f(n))
2. f(n) = O(g(n)) & g(n) = O(f(n))
3. f(n) = O(g(n)) & g(n) =! O(f(n))
30
31
3n+2 = O(n)
3n+5 = O(n)
2n+3 = O(n) ….
32
f(n)=3n+2
f(n)=O(n)
f(n)=O(nlogn)
f(n)=O(n2)
f(n)=O(n3)
logn = O(n)
loglogn = O(n)
……….
O(n2) represents infinite functions
5n2 + 6
8n2 + 5
n
nlogn
logn ……..
Q. If f(n) = n2 + 6n + 2; then f(n) is …….
Which of the following options is true
33
Most Common Big Oh Functions
34
Definition:
f(n) = Ω(g(n)
if there exist positive constants c and n0
such that:
0 < c g(n) < f(n) for all n≥ n0
g(n) is asymptotic lower bound for f(n), which
means the growth of f(n) is no lesser than the
growth rate of g(n).
35
36
37
Questions on Big Oh Notation(O)
38
39
40
41
Theta Notation(Ɵ)
Definition:
f(n) = Ɵ (g(n))
if there are two positive constants c and n0
such that:
0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)
42
This is known as asymptotic tight bound. For reasonably large values of n, the function f(n) is within the range of constant multiples of g(n). f(n) is bounded below as well as above.
Example Theta Notation(Ɵ)
1. f(n) = 3n+2 g(n) = n
2. f(n) = 5n2+6n+2 g(n) = n2
3. f(n) = 1000n g(n) = 5n2
f(n) = Ɵ(g(n) ??
g(n) = Ɵ(f(n)) ??
43