Professional Documents
Culture Documents
Analysis of Algorithms
(Part 2)
Big O Notation
• Big-O is an Asymptotic Notation for the ceiling
of growth for a given function. It provides us
with an asymptotic upper bound for the growth
rate of the runtime of an algorithm.
Analysis of Algorithms 2
Big O Notation
• Example: Show that 30n+8 is O(n).
Analysis of Algorithms 3
Big O Notation
• Note 30n+8 isn’t less than
n anywhere (n>0). cn =
31n 30n+8
Value of function →
• It isn’t even less than 31n
everywhere.
30n+8
n
• But it is less than 31n = O(n)
everywhere to
the right of n=8. n>n0=8 →
Increasing n →
Analysis of Algorithms 4
Big O Notation
• Example: Is 3𝑛 + 2 = O 𝑛 ?
Analysis of Algorithms 5
Big O Notation
• Example: Is 𝑛2 + 𝑛 = 𝑂 𝑛3 ?
Analysis of Algorithms 6
Big O Notation
Big O visualization
O(g(n)) is the set
of functions
with smaller or
same order of
growth as g(n)
Analysis of Algorithms 7
Big Ω Notation
― The asymptotic lower bound
― The function 𝐟 𝐧 = Ω(𝐠(𝐧)) if and only if there exist positive constants c
and n0 such that f(n) ⩾ c.g(n) for any n > n0 .
― read as “f of n is omega of g of n”
Analysis of Algorithms 8
Big Ω Notation
• Example: 3𝑛 + 2 = Ω 𝑛 ?
• Therefore,
𝑓 𝑛 = Ω 𝑔(𝑛)
or 3𝑛 + 2 = Ω 𝑛
• with c = 1 and 𝑛0 = 1
Analysis of Algorithms 9
Big θ Notation
― The asymptotic tight bound
― The function 𝐟 𝐧 = 𝛉(𝐠(𝐧)) if and only if there exist positive
constants c1 , c2 and n0 such that c1 . 𝑔 𝑛 ≤ f 𝑛 ≤ c2 . 𝑔(𝑛) for any n > n0 .
― read as “f of n is theta of g of n”
Analysis of Algorithms 10
Big θ Notation
• Example: n2 + 5n + 7 = θ(n2 ) ?
Analysis of Algorithms 11
Big θ Notation
• Subset relations between order-of-growth sets.
R→R
O( f ) ( f )
•f
( f )
Analysis of Algorithms 12
Other Notations (Self-Study)
• Little o Notation
• Little Omega Notation (ω)
Analysis of Algorithms 13
Common Rates of Growth
According to the Big O notation, we have five different categories
of algorithms. Their running time complexities are given as:
Faster algorithms
• Constant : O(1)
• Logarithmic : O(logn)
• Linear : O(n)
Analysis of Algorithms 14
Common Rates of Growth
Analysis of Algorithms 15
Common Rates of Growth
Analysis of Algorithms 16
Properties of Asymptotic Notations
General Properties:
• If f(n) is O(g(n)) then a*f(n) is also O(g(n)) ; where a is a constant.
• Example:
f(n) = 2n²+5 is O(n²)
then 7*f(n) = 7(2n²+5)
= 14n²+35 is also O(n²)
Analysis of Algorithms 17
Properties of Asymptotic Notations
Reflexive Properties:
• If f(n) is given then f(n) is O(f(n)).
• Example: f(n) = n²
Then f(n) = O(n²) i.e O(f(n))
Analysis of Algorithms 18
Properties of Asymptotic Notations
Transitive Properties :
• If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) = O(h(n)) .
• Similarly this property satisfies for both Θ and Ω notation. We can say
If f(n) is Θ(g(n)) and g(n) is Θ(h(n)) then f(n) = Θ(h(n)) .
If f(n) is Ω (g(n)) and g(n) is Ω (h(n)) then f(n) = Ω (h(n))
Analysis of Algorithms 19
Properties of Asymptotic Notations
Symmetric Properties :
• If f(n) is Θ(g(n)) then g(n) is Θ(f(n)) .
Analysis of Algorithms 20
Properties of Asymptotic Notations
Some More Properties :
1. If f(n) = O(g(n)) and f(n) = Ω(g(n)) then f(n) = Θ(g(n))
2. If f(n) = O(g(n)) and d(n)=O(e(n))
then f(n) + d(n) = O( max( g(n), e(n) ))
Analysis of Algorithms 21
General Rules for (Iterative) Algorithm Analysis
RULE 1- FOR LOOPS:
• The running time of a for loop is at most the running time of the
statements inside the for loop (including tests) times the number of
iterations.
(Taken from Chapter 2 of ‘Data Structures & Algorithm Analysis in C’ by Mark A. Weiss.)
Analysis of Algorithms 22
General Rules for (Iterative) Algorithm Analysis
RULE 2- NESTED FOR LOOPS:
• Analyze these inside out. The total running time of a statement inside a
group of nested for loops is the running time of the statement
multiplied by the product of the sizes of all the for loops.
• As an example, the following program fragment is 𝑂(𝑛2 ):
(Taken from Chapter 2 of ‘Data Structures & Algorithm Analysis in C’ by Mark A. Weiss.)
Analysis of Algorithms 23
General Rules for (Iterative) Algorithm Analysis
RULE 3- CONSECUTIVE STATEMENTS:
• These just add (which means that the maximum is the one that
counts). As an example, the following program fragment, which has
𝑂(𝑛) work followed by 𝑂(𝑛2 ) work, is also 𝑂 (𝑛2 ):
(Taken from Chapter 2 of ‘Data Structures & Algorithm Analysis in C’ by Mark A. Weiss.)
Analysis of Algorithms 24
General Rules for (Iterative) Algorithm Analysis
RULE 4- lF/ELSE:
• For the fragment
if( cond )
S1
else
S2
(Taken from Chapter 2 of ‘Data Structures & Algorithm Analysis in C’ by Mark A. Weiss.)
Analysis of Algorithms 25
Worst-case, Average-case, Best-case Analysis
• Worst-case running time
✓ This denotes the behaviour of an algorithm with respect to
the worst possible case of the input instance.
Analysis of Algorithms 26
Worst-case, Average-case, Best-case Analysis
• Average-case running time
✓ The average-case running time of an algorithm is an estimate
of the running time for an ‘average’ input.
Analysis of Algorithms 27
Worst-case, Average-case, Best-case Analysis
• Best-case running time
✓ The term ‘best-case performance’ is used to analyse an algorithm
under optimal conditions.
✓ For example, the best case for a simple linear search on an array
occurs when the desired element is the first in the list.
Analysis of Algorithms 28
Analysis of Algorithms 29
Related Readings
➢ Introduction to Algorithms (CLRS)
• Chapter 3 (3.1)
• https://yourbasic.org/algorithms/big-o-notation-explained/
• Asymptotic Notations:
https://drive.google.com/file/d/1PaF_NqJeqykBVT63RcSJRtm_2iUFpz
MB/view?usp=sharing
Analysis of Algorithms 30
Try Yourself!
Q1. Show that, Q3. Consider the following two functions:
a) 3n2 + 4n − 2 = 𝚯(n2) 𝑛3 for 0 ≤ 𝑛 ≤ 10000
𝑔1 𝑛 = ቊ 2
b) 3logn + 100 = O(logn) 𝑛 𝑓𝑜𝑟 𝑛 ≥ 10000
𝑛 𝑓𝑜𝑟 0 ≤ 𝑛 ≤ 100
𝑔2 𝑛 = ቊ 3
Q2. Consider the following three claims 𝑛 𝑓𝑜𝑟 𝑛 ≥ 100
1) 𝐧 + 𝟓 𝟐 = 𝚯(𝐧𝟐 ) Which of the following statements are true?
2) 𝟐𝐧 + 𝟏 = 𝐎(𝟐𝐧 )
(A) g1 n is O(g 2 n )
3) 𝟐𝟐𝐧 + 𝟏 = 𝐎(𝟐𝐧 )
(B) g 2 n is O(n)
Analysis of Algorithms 31
Analysis of Algorithms 32
Iterative Algorithm Analysis
Or,
Analysis of Algorithms 33
Iterative Algorithm Analysis
Alg()
{
int i;
for i = 1 to n
{
printf(“Hello!”);
}
}
Analysis of Algorithms 34
Iterative Algorithm Analysis
Solution: For this algorithm, the running time mainly depends on the
number of times the loop is executed.
Analysis of Algorithms 35
Iterative Algorithm Analysis
Alg()
{
int i = 0, s = 0;
while (s < n)
{
i++;
s = s + i;
printf(“Hello!”);
}
}
Analysis of Algorithms 36
Iterative Algorithm Analysis
Solution: For this algorithm, the running time mainly depends on the number
of times the loop is executed.
Here, i is incremented by 1 and s is incremented by i after each iteration.
Let, the loop will be executed k times (i.e. after k iterations, 𝑠 ≥ 𝑛).
Now,
i= 1 2 3 4 5 6 … k
s= 1 3 6 10 15 21 … n
Analysis of Algorithms 37
Iterative Algorithm Analysis
Or, 𝑘 = √2√𝑛
Analysis of Algorithms 38
Iterative Algorithm Analysis
Alg()
{
int i;
for (𝒊 = 1; 𝒊𝟐 < 𝒏; 𝒊++)
{
printf(“Hello!”);
}
}
Analysis of Algorithms 39
Iterative Algorithm Analysis
Solution: For this algorithm, the running time mainly depends on the
number of times the loop is executed.
Analysis of Algorithms 40
Iterative Algorithm Analysis
Alg()
{
int i, j, k;
for (𝒊 = 1; 𝒊 ≤ 𝒏; 𝒊++){
for (𝒋 = 𝟏; 𝒋 ≤ 𝒏; 𝒋++){
for (𝒌 = 𝟏; 𝒌 ≤ 𝟏𝟎𝟎; 𝒌++){
printf(“Hello!”);
}
}
}
}
Analysis of Algorithms 41
Iterative Algorithm Analysis
Alg()
{
int i, j, k;
for (𝒊 = 1; 𝒊 ≤ 𝒏; 𝒊++){
for (𝒋 = 𝟏; 𝒋 ≤ 𝒊; 𝒋++){
for (𝒌 = 𝟏; 𝒌 ≤ 𝟏𝟎𝟎; 𝒌++){
printf(“Hello!”);
}
}
}
}
Analysis of Algorithms 42
Iterative Algorithm Analysis
Solution: For this algorithm, the running time mainly depends on the
number of times the innermost loop (loop k) is executed.
Here,
When i = 1 2 3 4 … n
Loop j is
1 times 2 times 3 times 4 times … n times
executed
Loop k is 1 x 100 2 x 100 3 x 100 4 x 100 n x 100
…
executed times times times times times
Analysis of Algorithms 43
Iterative Algorithm Analysis
= 50𝑛2 + 50𝑛
= 𝑶 𝒏𝟐
Analysis of Algorithms 44
Related Materials
➢ Time Complexity Analysis Of Iterative Programs:
https://www.youtube.com/watch?v=FEnwM-iDb2g
• Asymptotic Notations:
https://drive.google.com/file/d/1PaF_NqJeqykBVT63RcSJRtm_2iUFpzMB/view
?usp=sharing
Analysis of Algorithms 45