Professional Documents
Culture Documents
CHAPTER TWO
COMPLEXITY
ANALYSIS
6/4/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. Amortized complexity
6/4/2022
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
6/4/2022
Introduction to Data Structures and
4
Algorithms 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 structures plus
algorithms.
6/4/2022
Algorithm Analysis - Concepts
5
6/4/2022
6
6/4/2022
8
6/4/2022
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
6/4/2022
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.
6/4/2022
Theoretical Approach - Complexity
11
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 memory
required to solve a problem of size n.
6/4/2022
12
6/4/2022
Algorithm Analysis Rules:
13
6/4/2022
14
6/4/2022
Complexity Analysis: Loops
16
6/4/2022
Complexity Analysis: Loops (with <)
17
6/4/2022
21
6/4/2022
23
6/4/2022
Example: 3
24
6/4/2022
25
6/4/2022
Exercise 1
26
6/4/2022
Exercise 2
27
6/4/2022
28
6/4/2022
29
}
sum = sum+i+j; 2 2M 2MN
i 1 j 1 i 1
}
6/4/2022
31
i 1 i 1 j 1
2 N
for (int
}
j = 1; j <= N; j++) {
} sum = sum+i+j;
}
} 6/4/2022
32
Conditionals: Formally
If (test) s1 else s2: Compute the maximum of the
running time for s1 and s2.
if (testif ==
(test1)=={1) {
for (intfori(int
= 1;i =i 1;
<=i <=
N;N; i++){ {
i++) N N N NN N
sum sum = sum+i;
= sum+i; max1,1
max , 2 2
}} }} i 1i 1 i 1 i j11 j 1
else(int
for i(int i =i1;<=
i <=
N;N;i++)
i++){{
else for = 1;
for (int j = 1; j <= N; j++) {
for (int j = 1; j <= N; j++) { max N ,
2 N 2
max N , 2 N 2 N 2 2 2N
2
sum = sum+i+j;
}}
sum = sum+i+j;
}}
6/4/2022
Example: 4
33
6/4/2022
34
6/4/2022
Important mathematics series formulas
35
6/4/2022
Exercises
36
6/4/2022
Exercise: 3
37
for (i=0;i<n;i++)
{
for (j=0;j<n; j++)
{
sum=sum+i+j;
}
}
6/4/2022
Exercise: 4
38
6/4/2022
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?
6/4/2022
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?
6/4/2022
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?
6/4/2022
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++;
6/4/2022
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 -
44
Measures of Times
In order to determine the running time of an algorithm it
is possible to define three functions Tbest(n), Tavg(n) and
T worst (n) as the best, the average and the worst case
running time of the algorithm respectively.
1. Average Case (Tavg): The amount of time the algorithm takes
on an "average" set of inputs.
2. Worst Case (Tworst): 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 time, since it provides
a bound for all input – this is called the “Big-Oh” estimate.
9/13/2017
Asymptotic Analysis
45
9/13/2017
The Big-Oh Notation
46
9/13/2017
47
9/13/2017
Standard Method to Prove Big -oh
48
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
59
h (n) = n3
f (n) =O( h (n))
Theorem 5: For any base b, logb(n) is O(log(n)).
Alllogarithms grow at the same rate
logbn is O(logdn) where b, d > 1
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)
9/13/2017
Little-Omega ( notation)
68
9/13/2017
Relational Properties of the
69
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)).
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
6/4/2022
Common complexity classes
72
6/4/2022
Class 10 102 103
Constant O(1) 1 1µsec 1 1µsec 1 1µsec
73 6/4/2022
Class 104 105 106
Constant O(1) 1 1µsec 1 1µsec 1 1µsec
74 6/4/2022
75 6/4/2022
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
6/4/2022
Types of analysis
77
6/4/2022
78
6/4/2022
Example
79
6/4/2022
80
6/4/2022
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
6/4/2022
Amortized Time Complexity
82
6/4/2022
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.
6/4/2022
84
6/4/2022
85
6/4/2022
87
6/4/2022
88
Questions?
6/4/2022
89
Thank You
6/4/2022