Data Structures and Algorithm
Data Structures and Algorithm
Algorithm analysis
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
n Isa major field that provides tools for evaluating
the efficiency of different solutions
What is an efficient algorithm?
n Faster is better (Time)
How do you measure time? Wall clock? Computer
clock?
n Less space demanding is better
But if you need to get data out of main memory it
takes time
Analysis of algorithms
Algorithm analysis should be independent of :
n Specific implementations and coding tricks
(programming language, control statements –
Pascal, C, C++, Java)
n Specific Computers (hw chip, OS, clock speed)
n Particular sets of data (string, int, float)
11
Order-of-Magnitude Analysis and
Big O Notation
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
Big O Notation
n Example of algorithm (only for cout operation):
notation code
int counter = 1;
O(1)
cout << "Arahan cout kali ke " << counter << "\n";
int counter = 1;
O(n2)
int i = 0;
int j = 0;
int counter = 1;
O(n3) int i = 0;
int j = 0;
int k = 0;
int counter = 1;
O(2n) int i = 1;
int j = 1;
while (i <= n) {
j = j * 2;
i++;
}
int counter = 1;
1
int i = 0;
2
i = 1; i <= n; i++
3
cout << "Arahan cout kali ke " << counter << "\n"
6
counter++
7
Determine the number of steps-
summation series
n statement 3, 6 & 7 are in the repetition
structure.
n 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
n 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
n Total steps:
1 + 1 + n + n + n = 2 + 3n
31
Continued…
32
Continued……
Algorithm Number of Steps
void sample7 ( ) 0
{ 0
for (int a=1; a<=n; a++) n-1+1=n
for (int b=1; b<=a; b++) n.(n+1)/2
cout << “ Contoh kira langkah “; n.(n+1)/2
} 0
Total steps 2n+n2
33
Continued…
n n
∑ ∑1 =
a=1 b=1
=n( 1 + 2 + 3 + 4 +…+n)
= n (n+1)
2
= n2 + n
2
34
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 << "Arahan cout kali ke " << counter << "\n";
counter++;
j++;
}
}
Determine the number of steps
- solution
statements Number of steps
int counter = 1;
1
∑ f(i)= 1
int i = 0; i=1
1
∑ f(i)= 1
int j = 1; i=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 << "Arahan cout kali ke " 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:
void sample8 ( ) 0
{ 0
int n, x, i=1; 1
while (i<=n) n
{ 0
x++; n.1 = n
i++; n.1 = n
} 0
} 0
Number of Steps 1 + 3n
40
Continued…
Algorithm Number of Steps
void sample9 ( ) 0
{ 0
int n, x, i=1; 1
while (i<=n) 1+log2n
{ 0
x++; 1+log2n
i=i*2; 1+log2n
} 0
} 0
Jumlah langkah 1+ 3(1+log2n)
•Notasi O = O (log2n)
41
Continued…
Algorithm Number of Steps
void sample10 ( ) 0
{ 0
int n, x, i=1; 1
while (i<=n) 1+log4n
{ 0
x++; 1+log4n
i=i*4; 1+log4n
} 0
} 0
Number of steps 3(1+log4n)
•While loop iterate from i=1 until i=n ; i increment 4 times at each
iteration
• Number of iteration for while loop = ( 1 + log4n )
42
Continued…
Number of steps =3(1+log4n)
logan = logbn/logba
Then;
log4n=log2n/log24=(log2n)/2
43