CSPC24 Chapter 3 - Analysis of The Efficiency of Algorithm
CSPC24 Chapter 3 - Analysis of The Efficiency of Algorithm
Algorithm and
Complexity
Analysis of The Efficiency of
Algorithm
Objectives
• Define and understand analysis algorithm
Approaches:
• theoretical analysis
• empirical analysis
THEORETICAL ANALYSIS OF TIME
EFFICIENCY
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size.
SUM (P, Q)
Step 1 – START
Step 2 - R ← P + Q + `1235099.790
Step 3 – Stop
BEST-CASE, AVERAGE-CASE,
WORST-CASE
For some algorithms efficiency depends on form of input:
Most important:
• Order of growth within a constant multiple as n→∞
Example:
• How much faster will algorithm run on computer that is twice as
fast?
ALGORITHM CASE EFFICIENCY
An algorithm can have different time for different inputs.
• Best case: This is the lower bound on running time of
an algorithm. We must know the case that causes the
minimum number of operations to be executed.
• Average case: We calculate the running time for all
possible inputs, sum all the calculated values and divide
the sum by the total number of inputs.
• Worst case: This is the upper bound on running time of
an algorithm. We must know the case that causes the
maximum number of operations to be executed.
ALGORITHM CASE EFFICIENCY
Example:
sum = 0;
for(int i = 0; i < n; i++)
{
sum = sum + arr_x[i];
}
Example 1(Time Complexity) i=0
i=1
arr_x 1 20 5 4 9 Calculate the Big O of each operation. i = 2
i = 3
sum = 0; 1
i = 4
for(int i = 0; i < n; i++) 1+ n + 1+ n
i = 5
{
sum = sum + arr_x[i]; 1 * n = n
}
Example 1(Time Complexity)
arr_x 1 20 5 4 9 Add up the Big O of each operation together.
sum = 0; 1
{
n2
sum = sum + arr_x[y];
} 2n2 + n +
} 1 2)
Time Complexity : O(n
Example 2(Space Complexity)
sum = 0; arr_x n
for(int x = 0; x < n; x++) sum 1
{ x 1
for(int y = 0; y < n; y++) y 1
{ n 1
sum = sum + arr_x[y];
n+4
}
} Space Complexity : O(n)
Thank you!
REFERENCES:
Cormen T., Leiserson C., Rivest R., & Stein C. (2009). Introduction to Algorithms Third
Edition. The MIT Press, Cambridge, Massachusets
Fleck, Margaret M., (2013). Building Blocks for Theoretical Computer Science. Version
1.3 (January 2013)
Lehman, Eric F., Leighton, Thomson & Meyer, Albert R. (2018). Mathematics for
Computer Science. June 2018
ONLINE REFERENCES:
https://www.geeksforgeeks.org/
http://www.freebookcentre.net/CompuScience/free-computer-algorithm-books.html