DSA-Chapter 2 - Complexity - Analysis
DSA-Chapter 2 - Complexity - Analysis
CHAPTER TWO
COMPLEXITY
ANALYSIS
6/28/2022 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/28/2022
Introduction to Data
Structures and Algorithms
3 Analysis
A program is written in order to solve a
problem.
Solution consists of two things:
A way to organize the data
Sequence of steps to solve the problem
The way data are organized in a
computers memory is said to be Data
Structure and
The clearly defined sequence of
computational steps the computer follows
to solve a problem is said to be an
algorithm.
Therefore, a program is nothing but data
6/28/2022
Algorithm Analysis -
4
Concepts
Refers to the process of determining the
amount of computing time and storage space
required by different algorithms.
Or it’s a process of predicting the resource
requirement of algorithms in a given environment.
To solve a problem, there are many possible
algorithms.
One has to be able to choose the best algorithm for
the problem at hand using some scientific method.
To classify some data structures and
algorithms as good, we need precise ways of
analyzing them in terms of resource
requirement.
The main resources are: Running Time, Memory
Usage and Communication Bandwidth 6/28/2022
5
6/28/2022
7
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.
6/28/2022
Theoretical Approach -
9
Complexity Analysis
Complexity Analysis is the systematic study
of the cost of computation, measured
either in time units or in operations
performed, or in the amount of storage
space required.
The goal is to have a meaningful measure that
permits comparison of algorithms independent
of operating platform.
There are two things to consider:
Time Complexity: Determine the approximate
number of operations required to solve a
problem of size n.
Space Complexity: Determine the approximate
6/28/2022
memory required to solve a problem of size n.
10
6/28/2022
12
6/28/2022
Complexity Analysis: Loops
14
6/28/2022
19
6/28/2022
21
6/28/2022
23
6/28/2022
Exercise 2
25
6/28/2022
26
6/28/2022
27
}}
sum
sum = = sum+i;
sum+i; 1 Nii 1
1
}
sum = sum+i+j; 2 2M 2MN
i 1 j 1 i 1
}
i 1 i 1 j 1
2 N
for (int
}
j = 1; j <= N; j++) {
} sum = sum+i+j;
}
} 6/28/2022
30
Conditionals: Formally
If (test) s1 else s2: Compute the
sum = sum+i+j;
}}
sum = sum+i+j;
}}
6/28/2022
Example: 4
31
6/28/2022
32
6/28/2022
Important mathematics series
33
formulas
6/28/2022
Exercises
34
for (i=0;i<n;i++)
{
for (j=0;j<n; j++)
{
sum=sum+i+j;
}
}
6/28/2022
Exercise: 4
36
6/28/2022
Exercise: 5
37
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?
6/28/2022
Exercise: 6
38
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?
6/28/2022
Exercise: 7
39
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?
6/28/2022
Exercise: 8
40
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++;
6/28/2022
CH-2 Contents
41
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
9/13/2017
Order of Magnitude Analysis -
42
Measures of Times
In order to determine the running time of
an algorithm it is possible to define three
functions T best (n) , T avg (n) and T worst (n) as
the best, the average and the worst case
running time of the algorithm respectively.
1. Average Case (T a v g ): The amount of time the
algorithm takes on an "average" set of inputs.
2. Worst Case (T w o r s t ): The amount of time the
algorithm takes on the worst possible set of
inputs.
3. Best Case (Tbest): The amount of time the
algorithm takes on the smallest possible set of
inputs.
We are interested in the worst-case
9/13/2017 time,
Asymptotic Analysis
43
9/13/2017
Example 2: Proving Big -
48
Oh
Show that f(n)= 4n +6 is O(n)
Choosek=1
Assuming n > 1, then
f(n)/g(n)=(4n +6)/n < (4n+6n)/n =
(10n)/n=10
Choose c=10. Note that 6<6n
Thus, 4n +6 is O(n), because 4n + 6 <=10n
whenever n>1.
9/13/2017
Exercise 1: Proving Big -Oh
49
9/13/2017
Standard Method to Prove
50
Not Big -oh
1. Assuming n>1
2. Show
9/13/2017
Example 3: Proving Not Big
51
-Oh
Show that f(n)= n2 – 2n +1 is not O(n).
Assuming n > 1, then
f(n)/g(n)= (n2 – 2n +1)/n > (n2 – 2n)/n = n -
2
n > c+2 implies n-2 > c and f(n) >cn.
So choosing n > 1, n > k, and n > c+2
implies n > k and f(n) > cn.
Decrease numerator to simplify fraction.
9/13/2017
Example 4: Proving Not Big
52
-Oh
Show that f(n)= n2/2 is not O(n).
Assuming n > 1, then
f(n)/g(n)= (n2/2)/n = n/2
n > 2c implies n/2 > c and f(n) >cn.
So choosing n > 1, n > k, and n > 2c
implies n > k and f(n) > cn.
9/13/2017
Exercise 2: Proving Not Big
53
-Oh
Show that f(n)= (n +1)3 is not O(n2)
Where (n +1)3 = n3 + 3n2 + 3n +1
???
9/13/2017
54
9/13/2017
Big-Oh Rules
55
9/13/2017
Big-O Theorems
56
9/13/2017
57
h (n) = n3
f (n) =O( h (n))
Theorem 5: For any base b, logb(n) is
O(log(n)).
All logarithms grow at the same rate
logbn is O(logdn) where b, d > 1
9/13/2017
58
9/13/2017
Properties of the Big-O
60
9/13/2017
Theta Notation:
63
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)
9/13/2017
Relational Properties of the
67
Asymptotic Notations
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)). 9/13/2017
Symmetry
68
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
69
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/28/2022
Common complexity classes
70
71 6/28/2022
Class 104 105 106
Constant O(1) 1 1µsec 1 1µsec 1 1µsec
72 6/28/2022
73 6/28/2022
CH-2 Contents
74
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/28/2022
Types of analysis
75
6/28/2022
76
6/28/2022
78
6/28/2022
79
Questions?
6/28/2022
80
Thank You
6/28/2022