0% found this document useful (0 votes)
3 views

Analysis of Algorithms 02 _ Class Notes

The document is a lecture on the analysis of algorithms, specifically focusing on apriori and aposteriori methods. It covers the advantages and disadvantages of apriori analysis, methodologies for analyzing algorithms, and examples of time complexity calculations. Additionally, it discusses best, average, and worst-case scenarios for algorithm performance, alongside the importance of understanding the rate of growth of time complexity as a function of input size.

Uploaded by

sourabhbhaskar71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Analysis of Algorithms 02 _ Class Notes

The document is a lecture on the analysis of algorithms, specifically focusing on apriori and aposteriori methods. It covers the advantages and disadvantages of apriori analysis, methodologies for analyzing algorithms, and examples of time complexity calculations. Additionally, it discusses best, average, and worst-case scenarios for algorithm performance, alongside the importance of understanding the rate of growth of time complexity as a function of input size.

Uploaded by

sourabhbhaskar71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

CS & IT

ENGINEERING
Algorithms

Analysis of Algorithms

Lecture No.- 02 By- Aditya sir


Recap of Previous Lecture

about Course
Topic Intro
Topic
Intro
to Algo

why
what

of analysis
Topics to be Covered

Topic How to analyse


Topic

Aprion Aposterion
Topic

IT 3

Adv disadu
About Aditya Jain sir
HI
1. Appeared for GATE during BTech and secured AIR 60 in GATE in very first attempt - City topper
2. Represented college as the first Google DSC Ambassador.
3. The only student from the batch to secure an internship at Amazon. (9+ CGPA)
4. Had offer from IIT Bombay and IISc Bangalore to join the Masters program
5. Joined IIT Bombay for my 2 year Masters program, specialization in Data Science
6. Published multiple research papers in well known conferences along with the team
7. Received the prestigious excellence in Research award from IIT Bombay for my Masters thesis
8. Completed my Masters with an overall GPA of 9.36/10
9. Joined Dream11 as a Data Scientist
10. Have mentored 12,000+ students & working professions in field of Data Science and Analytics
11. Have been mentoring & teaching GATE aspirants to secure a great rank in limited time
12. Have got around 27.5K followers on Linkedin where I share my insights and guide students and
professionals.
Telegram Link for Aditya Jain sir: https://t.me/AdityaSir_PW
TTTe
Topic : Analysis of Algorithms

How to Analyses?
Methodology of Analysis

Aposteriori Apriori
After implementation Before implementation
Topic : Analysis of Algorithms

2. Apriori Analysis
Carried out before the actual implementation of Algorithm.
Topic : Analysis of Algorithms

Advantages Disadvantages
1. Platform independent 1. It gives approximate value
2. Can be carried out without actual
I (Time complexity)
implementation
3. Helps us to compare the relative (Space complexity)
performance of 2 or more algorithms.
Topic : Analysis of Algorithms

Eg.1.

O
Maggie

A2
A1
O O

O
x2 > x x units x2 units

Better fore large values of x.


Comparison can be made
With approximate values
Topic : Analysis of Algorithms

➢ Steps to carry out Apriori Analysis:



P
Rseudo code of the algorithm is required V
• Need a matrix to compare tow or more algorithm running time.
• Need some notation/ symbol to represent the running time.

Asymptotic
Topic : Analysis of Algorithms

➢ Apriori Analysis can be carried out in 2 ways.


1. Step- count method → exact gives operations (not much used)
2.
ton
Order of magnitude method → give approximate no. of operations.

Assumption:- Every fundamental operation takes..


1 unit of time.

1
1
n
C 1
P ant­i
MT
update For
condita n 1 units

1
11 1
1
21 3
ⁿᵗ

5 4 i 3
stop
Topic : Analysis of Algorithms
1. Step-count method:-
Algo. AJSir(n) {
1. p =r q + r
2. x = y * z
O
→ 2units
→ 2units
o
3. for (i = 1, i <= n ; i++) { → (4n + 2) units
a=b+c
} optional
4. for (j = 1; j <= n, j++) {
for (k = 1; k < = n; k ++) { HE
x= y+z → (4n2 + 4n + 2) units
}
}
}
Topic : Analysis of Algorithms

Eg.2. for (i =1; i <=n; i++)→ (2n+2) Total operation = (2n + 2) + 2*n
{ = 4n + 2
a = b +c → (2*n) for (i =1; i <=4; i++)
} i=1→1 1→2
i <=4 → Four 2→2
8

1. initialization i =1 i++→ 2 3→2


2. Condition i <=n 4→2
3. updation i = i +1 5× → 1
Topic : Analysis of Algorithms

For (i=1, i<=n, i++)


1 → initialization
(n +1) → comparison 1 + (n +1) +n = (2n+2)
so
n → updation
Topic : Analysis of Algorithms

runs n time → for (j =1; j<=n; j++) → (2n+2)


Boris
{
Total operation
runs n time → for (k =1; k<=n; k++) → (2n+2)
2e
(2n +2) + n*(2n+2) + n2 *2
of
{ → 2n2 + 2n+ 2n + 2 + 2n2
→ (4n2 + 4n + 2)
x=y+z→2
}
}
Mtimes
Topic : Analysis of Algorithms

Algo AJSir(n)
{ Total no. of operation
……… = 2 + 2 + (4n+2) + (4n2 + 4n + 2)
= 4n2 + 8n + 8 units of times
………
………
___
}

notify
Eft

step Fitood
Startup

yrs or 2026 5 emp

2027 20 emp
yrs
5L
2030 100 emp
dominating
E
T

2040
mpp
2050 oemp interview
Topic : Analysis of Algorithms

Approximate - Order of Magnitude


2
(independent of n) → we just need to consider the order of the number of
Off

fundamental operation in the fundamental statement.

→1
Constant
4 2 ARE

Linear → n
Quadratic → n2
Topic : Analysis of Algorithms
approach
1. Joffmagnituda
Step-count method:-
Algo. AJSir(n) {
1. p = q + r → 1 or 2 → constant →1
2. x = y * z → 1 or 2 → constant →1
3. for (i = 1, i <= n ; i++) { → n units
a=b+c n
nt2
}
4. for (j = 1; j <= n, j++) {
for (k = 1; k <= n; k ++) { Un 4n 2
x= y+z → n2 units
}
}
}
Topic : Analysis of Algorithms
Total units as per order of Magnitude approach

 1 + n + n2
 (n2 + n+ 1)

0
for same algorithm the number of units by step- count method → (4n2 + 8n + 8)

 notation = O(n2)
Algo ATC
SameAlge
2
4 8 8 units
1
Step Count mtd
n tn
magnitude
2 Order of
8fzpqtg

rate of 0 m

growth k n
12asymptoticnotation
Topic : Analysis of Algorithms

Note.
Aim of apriori analysis is to get/represent the running time of on algo. As a
mathematical function of input size ‘n’.
ITAL

E.g.3.
T(n) = 2n2 + 2
Order of magnitude refers to the rate
ec

T(n) = 4n2 + 8n + 8
of growth of time w.r.t. ‘n’
T(n) = 5n2 + 2
Topic : Analysis of Algorithms

Rate of growth of time as a mathematical function


mm

Logarithmic Polynomial Exponential


0
  Exponential
Form: Log(n) Constant Sq.root 
E.g. log5(n), log2(n)   a
1
x2
Csf

log(log(n) n0,n1, n0.5 2, n2 → quadratic E.g.


n3 3n, 2n, 7n…
↓ nn, n!
cubic
login linear N* log(n)
121m
Topic : Analysis of Algorithms

Note. Rate of growth comparison [in general]


Usually [Decreasing function < Constant < logarithm < Polynomial < Exponential]
1
E.g.4. < 10 < log 2 𝑛 < 𝑛 < 𝑛2 . . . 2𝑛 < 3𝑛 . . 𝑛𝑛
𝑛 O O

8.5
E

on
c
Topic : Analysis of Algorithms

How to compare the rate of growth of 2 function?


Larger input size (n)
(Linear) (expo) (quadratic)
(n) 2n n2
1
2
04
2 1
4
3 8
10
4 16
9
0
16
5 32 25 9
6 64 36
0

7 128 49
(2n > n2 for larger values of n)
Topic : Analysis of Algorithms
values
(Poly vs Expo)
Inff Compass for larger
of
o
Topic : Analysis of Algorithms

Algo→ n2 → Lower rate of growth → takes less time → better


Algo→ 2n → High rate of growth → takes more time
F
or
not good

Note:-
Gore
[Algorithm that takes polynomial unit of time all ore efficient that those with
exponential time.]
Topic : Analysis of Algorithms

➢ Apriori Analysis ordenofmagm.ee


(n2 + n+1)
To
1. Time complexity/ Running time as a function of ‘n’ → (input size)
2. To understand to change in nature or running time for a given algo and a
Pm
fixed input size ‘n’ but for different input classes. → (test cases)
Best case (Bc)
of
Average case (Ac)
egoing
ypg

Worst case (Wc) y


p

as
Topic : Analysis of Algorithms

Example to understand point (2)


Algorithm linear search (A, n key)
{
for (i = 1, i <=n, i++)
{
if (A[i] == key)

V
}
return I
If
} Print (“element not fund”)
0
Topic : Analysis of Algorithms

Eg.5. n = 5 (fixed) Case2:-


Key is not present or is present at last
Case1:- input case-1 position
n
Input case-2

→ Loop runs how many times?


Loop runs how many times?
mmF

 5 time →(n times)


 1 time → Constant
Linear Rate of growth
Best case TC  TC  O(1)
Worst case TC of  TC= O(n) linear
search
Topic : Analysis of Algorithms

➢ Different cases for time complexity for a fixed input size.


Ine­os

1. Best case → The input for which the algo. Runs min no. of times (does min
work.)
The time complexity for such input  Best case TC
2. Worst case→ The input which of the algorithm run max no. of times
The time complexity for such input  worst case TC
0
E

3. Average case→ deals with probability.


a

Ex. Linear search


WIT

BC TC → O(1) WC TC → O(n) AC TC → O(n) → Bonus.


Topic : Analysis of Algorithms
Best case TC → B(n)
➢ For any given algo, in general: Try
8 Worst case TC → W(n)
Always: B(n)  A(n)  W(n) Avg. case TC → A(n)

Examples:
1.
HI
B(n) < [A(n) = W(n)]  Linear search, Binary search
2. [B(n) = A(n)] < W(n)  Quick sort
3. [B(n) = A(n) = W(n)] Merge sort, Heap sort, Selection sort.
Summary

1 Apsion Analysis

Order of magnitude
count mtd
Step

Cases
27 Types
of Best case
27 Worst Case
THANK - YOU

32

You might also like