1. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
1
S. Hassan Adelyar, Ph.D
Instructor of Computer Science Faculty
Kabul University
December 2020
Lesson III: Algorithm-Analysis Part II
2. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
2
Learning Outcome
By the end of this presentation, students will be
able to:
Count the number of steps for simple operations
Count the number of steps for loops &
conditional statements.
Count the number of steps for simple
Algorithms.
p
3. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
3
Simple Operation
Two classes of operations:
Comparison
Arithmetic.
Comparison operations
Include equal, not equal, less than, greater
than, less than or equal, and greater than or
equal.
The comparison operators are counted as a
single step.
p
4. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
4
Arithmetic operators are counted in three
groups:
Additive
Multiplicative
Logarithms and geometric functions
Additive operators include:
addition, subtraction, increment, and
decrement.
p
5. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
5
Multiplicative operators include:
multiplication, division, and modulus.
Additive & multiplicative are counted separately
because multiplications are considered to take
longer than additions.
Reduce the number of multiplications.
Logarithms and geometric functions:
Consume more time than multiplications.
p
6. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
6
A special case is integer multiplication or division
by a power of 2.
This operation can be reduced to a shift operation,
which is considered as fast as an addition.
Multiplication or division by 2 is commonly found
in divide and conquer algorithms.
p
7. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
7
We define a set of primitive operations such as the
following:
Assigning a value to a variable.
Calling a method.
Performing an arithmetic operation (for
example, adding two numbers).
Comparing two numbers.
Indexing into an array.
p
8. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
8
Instead of trying to determine the specific
execution time of each primitive operation, we will
simply count how many primitive operations are
executed, and use this number as a measure of the
running-time of the algorithm.
p
9. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
9
String operation
Linear, but indexing and length are constant.
For loops
The total running time of a statement inside a
nested loops is the running time of the statement
multiplied by the product of the loops.
p
10. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
10
If/else
For the fragment if (condition) S1 else S2, the
running time of an if/else statement is never more
than the running time of the test plus the larger of
the running times of S1 and S2.
p
11. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
11
Examples
Examples
1) x = 0;
2) for(i=0; i<n; i++)
3) x++;
The number of steps can be calculate as follow:
p
12. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
12
Line Time Number
1 c1 1
2 c2 n+1
3 c3 n
Therefore, T(n) is:
T(n) = c1+c2(n+1)+c3n
Now considering c as the larger of c1, c2 & c3:
T(n)= c(2n+2)
p
13. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
13
Example 2:
1) x=0;
2) for(i = 0; i<n; i++)
3) for(j=0; j< n; j++)
4) x++;
The number of steps can be calculate as follow:
p
14. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
14
Line Time Number
1 c1 1
2 c2 n+1
3 c3 n(n+1)
4 c4 n x n
Therefore, T(n) is:
T(n) = c1 + c2(n+1) + c3n(n+1)+c4n2
T(n) = C(2n2
+2n +2)
p
15. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
15
Example 3:
1) int Factorial(int n) {
2) int fact = 1;
3) for(int i=1; i<n; i++)
4) fact *=i;
5) return fact;
}
The number of steps can be calculate as follow:
p
16. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
16
Line Time Number
2 c1 1
3 c2 n
4 c3 n-1
5 c4 1
T(n) = c1+c2n+c3(n-1)+c4
T(n) = C(2n+1)
p
17. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
17
Summary
In this presentation we study Algorithm analysis by
counting the number of steps for simple operations.
In addition to counting the number of steps for
simple operation we studied how to count the
number of steps for loops & conditional statements.
Furthermore, we studied the ways for counting the
number of steps for simple Algorithms.
p
18. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
18
:نمائید دریافت را ذیل کود زمانی تابع :تمرین
p
for (i=0; i<10; i++){
a = a + i;
b = b * i;
}
19. Algorithm Analysis – Part II
Algorithm
Analysis
&
Design April 2020
19
Exercises
1) Count the number of steps for the following code (5
score):
int a=0;
for(i = 0; i<n; i++)
for(j=0; j< n; j++)
a++;
2) Why we measure the time of Algorithm by counting the
number of steps instead of measuring the time by
seconds?
p