DSA Chapter 2
DSA Chapter 2
CHAPTER TWO
COMPLEXITY
ANALYSIS
08/13/2024 DSA- CH-2: Complexity Analysis
CH-2 Contents
2
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
08/13/2024
CH-2 Contents
3
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
08/13/2024
Introduction to Data Structures and Algorithms
Analysis
4
08/13/2024
8
08/13/2024
CH-2 Contents
9
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
08/13/2024
Important terms
10
Computational complexity
It is a measuring of the amount of time, storage, or other
resources needed to execute the algorithm in order to compare the
efficiency of algorithms.
Asymptotic complexity
Asymptotic time complexity
The limiting behavior of the execution time of an algorithm when the size
of the problem goes to infinity. This is usually denoted in big-O notation
Asymptotic space complexity
The limiting behavior of the use of memory space of an algorithm when
the size of the problem goes to infinity. This is usually denoted in big-O
notation.
08/13/2024
Theoretical Approach - Complexity Analysis
11
08/13/2024
12
08/13/2024
Algorithm Analysis Rules:
13
08/13/2024
14
08/13/2024
Complexity Analysis: Loops
16
08/13/2024
21
08/13/2024
23
08/13/2024
Example: 3
24
08/13/2024
25
08/13/2024
Exercise 1
26
08/13/2024
Exercise 2
27
08/13/2024
28
08/13/2024
29
your code
for (int i = 1; i <= N; i++) {
for (int i = 1; i <= N; i++) { sum = sum+i;
sum = sum+i; } N N N
N N N
2
} 1 2 N 2 N
1 2 N 2 N
2 for (int i = 1; i <= N; i++) {
for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { i 1 i 1 j 1
for (int j = 1; j <= N; j++) { i 1 i 1 j 1 }
sum = sum+i+j;
sum = sum+i+j; }
}
} 08/13/2024
32
Conditionals: Formally
If (test) s1 else s2: Compute the maximum of the
08/13/2024
34
08/13/2024
Important mathematics series formulas
35
08/13/2024
Exercises
36
08/13/2024
Exercise: 3
37
for (i=0;i<n;i++)
{
for (j=0;j<n; j++)
{
sum=sum+i+j;
}
}
08/13/2024
Exercise: 4
38
08/13/2024
Exercise: 5
39
int k=0;
for (int i=0; i<n; i++)
{
for (int j=i; j<n; j++)
{
k++;
}
}
What is the value of k when n is equal to 20?
08/13/2024
Exercise: 6
40
int k=0;
for (int i=1; i<n; i*=2)
{
for(int j=1; j<n; j++)
{
k++;
}
}
What is the value of k when n is equal to 20?
08/13/2024
Exercise: 7
41
int x=0;
for(int i=1;i<n;i=i+5)
x++;
What is the value of x when n=25?
int x=0;
for(int k=n;k>=n/3;k=k-5)
x++;
• What is the value of x when n=25?
08/13/2024
Exercise: 8
42
int x=0;
for (int i=1; i<n;i=i+5)
for (int k=n;k>=n/3;k=k-5)
x++;
• What is the value of x when n=25?
int x=0;
for(int i=1;i<n;i=i+5)
for(int j=0;j<i;j++)
for(int k=n;k>=n/2;k=k-3)
x++;
08/13/2024
CH-2 Contents
43
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
9/13/2017
Order of Magnitude Analysis - Measures of
Times
44
1. Choose k=1
2. Assuming n>1, find/derive a c such that
9/13/2017
Example 1: Proving Big -Oh
49
9/13/2017
Example 2: Proving Big -Oh
50
9/13/2017
Exercise 1: Proving Big -Oh
51
9/13/2017
Standard Method to Prove Not Big -oh
52
1. Assuming n>1
2. Show
9/13/2017
Example 3: Proving Not Big -Oh
53
9/13/2017
Example 4: Proving Not Big -Oh
54
9/13/2017
Exercise 2: Proving Not Big -Oh
55
9/13/2017
56
9/13/2017
Big-Oh Rules
57
9/13/2017
Big-O Theorems
58
9/13/2017
60
9/13/2017
Properties of the Big-O
62
9/13/2017
64
9/13/2017
Theta Notation:
65
9/13/2017
66
Example:
1. If f(n)=2n+1, then f(n) = (n)
2. f(n) =2n2 then
f(n)=O(n4)
f(n)=O(n3)
f(n)=O(n2)
All these are technically correct, but the last
expression is the best and tight one. Since 2n 2 and n2
have the same growth rate, it can be written as f(n)=
(n2).
9/13/2017
Little-o Notation
67
9/13/2017
Little-Omega ( notation)
68
9/13/2017
Relational Properties of the Asymptotic
Notations
69
Transitivity
if f(n)=(g(n)) and g(n)= (h(n)) then f(n)=(h(n)),
if f(n)=O(g(n)) and g(n)= O(h(n)) then f(n)=O(h(n)),
if f(n)=(g(n)) and g(n)= (h(n)) then f(n)= (h(n)),
if f(n)=o(g(n)) and g(n)= o(h(n)) then f(n)=o(h(n)), and
if f(n)= (g(n)) and g(n)= (h(n)) then f(n)= (h(n)).
Symmetry
f(n)=(g(n)) if and only if g(n)=(f(n)).
9/13/2017
70
Transpose symmetry
f(n)=O(g(n)) if and only if g(n)=(f(n)),
f(n)=o(g(n)) if and only if g(n)=(f(n)).
Reflexivity
f(n)=(f(n)),
f(n)=O(f(n)),
f(n)=(f(n)).
9/13/2017
CH-2 Contents
71
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
08/13/2024
Common complexity classes
72
08/13/2024
Class 10 102 103
Constant O(1) 1 1µsec 1 1µsec 1 1µsec
73 08/13/2024
Class 104 105 106
Constant O(1) 1 1µsec 1 1µsec 1 1µsec
74 08/13/2024
75 08/13/2024
CH-2 Contents
76
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
08/13/2024
Types of analysis
77
08/13/2024
78
08/13/2024
80
08/13/2024
CH-2 Contents
81
1. Introduction
2. Computational and asymptotic complexity
3. Big-O, Ω, Θ, little-o and OO notations
4. Common complexity classes
5. Best, average and worst case complexity
6. Amortized complexity
08/13/2024
Amortized Time Complexity
82
08/13/2024
Three Methods of Amortized Analysis
83
Aggregate analysis:
Total cost of n operations/n,
Accounting method:
Assign each type of operation an (different) amortized cost
Overcharge some operations,
Store the overcharge as credit on specific objects,
Then use the credit for compensation for some later
operations.
Potential method:
Same as accounting method
But store the credit as “potential energy” and as a whole.
08/13/2024
84
08/13/2024
87
08/13/2024
88
Questions?
08/13/2024
89
Thank You
08/13/2024