Week 8 Algorithm Analysis
Week 8 Algorithm Analysis
Analysis
Objectives
At the end of the class, students are expected
to be able to do the following:
Algorithm analysis:-
to study the efficiency of algorithms when the
input size grow, based on the number of
steps, the amount of computer time and
space.
Analysis of algorithms
notation code
O(1) int counter = 1;
cout << "Welcome to C++ " << counter <<
Constant
"\n";
O(logxn) int counter = 1; int i = 0;
Logarithmic for (i = x; i <= n; i = i * x) { // x must be
> than 1
cout << "Welcome to C++ " <<
counter << "\n";
counter++;
}
Order of increasing complexity
Order of growth for some common function:
• O(1) < O(logxn) < O(n) < O(n log2n) < O(n2) < O(n3) < O(2n)
Notasi n=8 n = 16 n = 32
O(log2n) 3 4 5
O(n) 8 16 32
O(n log2n) 24 64 160
O(n2) 64 256 1024
O(n3) 512 4096 32768
O(2n) 256 65536 4294967296
Order-of-Magnitude Analysis and
Big O Notation
2n n3 n2 n * log2n
growth-rate function value
log2n
1
n
Big O Notation
• Example of algorithm for common function:
O(n) int counter = 1; int i = 0;
Linear
for (i = 1; i <= n; i++) {
cout << "Welcome to C++ " << counter << "\n";
counter++;
}
counter++;
}
}
Big O Notation
• Example of algorithm for common function:
while (i <= n) {
j = j * 2;
i++;
}
int counter = 1;
int i = 0;
for (i = 1; i <= n; i++) {
cout << "Welcome to C++ “;
cout << counter << "\n";
counter++;
}
Determine the number of steps
Num statements
int counter = 1;
1
int i = 0;
2
i=1
3
i <= n
4
i++
5
6 cout << "Welcome to C++ " << counter << "\n"
counter++
7
Determine the number of steps
• Statement 3, 4 & 5 are the loop control and
can be assumed as one statement.
Num Statements
int counter = 1;
1
int i = 0;
2
i = 1; i <= n; i++
3
6 cout << "Welcome to C++" << counter << "\n"
counter++
7
Determine the number of steps-
summation series
• statement 3, 6 & 7 are in the repetition
structure.
• It can be expressed by summation series
n
∑ f(i) = f(1) + f(2) + . . . + f(n) = n
i=1
Where
f(i) – statement executed in the loop
Determine the number of steps-
summation series
• example:- if n = 5, i = 1
5
∑ f(i) = f(1) + f(2) + f(3) + f(4) + f(5) = 5
i=1
5
∑ f(i) = f(3) + f(4) + f(5) = 3
i=3
1
∑ f(i) = f(1) = 1
i=1
• Total steps:
1 + 1 + n + n + n = 2 + 3n
__ __ __
Determine the number of steps
Determine the number of steps
- exercise
Count the number of steps and find the Big ‘O’
notation for the following algorithm
int counter = 1;
int i = 0;
int j = 1;
for (i = 3; i <= n; i = i * 3) {
while (j <= n) {
cout << "Welcome to C++ " << counter << "\n";
counter++;
j++;
}
}
Determine the number of steps
- solution
statements Number of steps
int counter = 1; 1
∑ f(i) = 1
i=1
int i = 0; 1
∑ f(i) = 1
i=1
int j = 1; 1
∑ f(i) = 1
i=1
i = 3; i <= n; i = i * 3 n
∑ f(i) = f(3) + f(9) + f(27) + … + f(n) = log3n
i=3
j <= n n n
∑ f(i) . ∑ f(i) = log3n . n
i=3 j=1
Determine the number of steps
- solution
cout << "Welcome to C++ " n n 1
<< counter ∑ f(i) . ∑ f(i) . ∑ f(i) = log3n . n . 1
i=3 j=1 i=1
<< "\n";
n n 1
counter++; ∑ f(i) . ∑ f(i) . ∑ f(i) = log3n . n . 1
i=3 j=1 i=1
n n 1
j++; ∑ f(i) . ∑ f(i) . ∑ f(i) = log3n . n . 1
i=3 j=1 i=1
Determine the number of steps
- solution
Total steps: