Analysis of Algorithm
CSC4321/4421
Lecture #1
Synopsis
This course provides an advanced understanding to
theory and practice of data structure and the study of
algorithms analysis. Students will learn the advanced
concepts of the data structure such as B-trees, heaps
and priority queues. Further, students will be exposed
to the techniques used in the analysis of data
structures and its algorithms.
The analytical abilities of the students in this course are
to analyze the performance of data structures and
algorithms.
Why Analyze an Algorithm ?
The most straightforward reason for analyzing an
algorithm is to discover its characteristics in order to
evaluate its suitability for various applications or compare
it with other algorithms for the same application.
Moreover, the analysis of an algorithm can help us
understand it better, and can suggest informed
Computational Complexity
The branch of theoretical computer science where the goal is to
classify algorithms according to their efficiency and
computational problems according to their inherent difficulty is
known as computational complexity. Paradoxically, such
classifications are typically not useful for predicting performance
or for comparing algorithms in practical applications because they
focus on order-of-growth worst-case performance. In this course,
Algorithm Efficiency Analysis
Worse Case/Best Case/Average Case
Example
Complexity time of Algorithm
Time complexity of an algorithm signifies the total time
required by the program to run till its completion.
The time complexity of algorithms is most commonly
expressed using the big O notation. It's an asymptotic
notation to represent the time complexity.
Big ‘O’ notation
Big ‘O’ notation
Notation being used to show the complexity time of
algorithms.
Big ‘O’ notation
Order-of-Magnitude Analysis and Big O
Notation
Order-of-Magnitude Analysis and Big O
Notation
Order of increasing complexity
Example of algorithm
(only for cout operation):
Example of algorithm
(only for cout operation):
Example of algorithm
(only for cout operation):
Example of algorithm
(only for cout operation):
Example of algorithm
(only for cout operation):
Example of algorithm
(only for cout operation):
Example of algorithm
(only for cout operation):
Determine the Complexity time of Algorithm
Can be determined by;
Practically – by experiment or implementation
Theoretically – by calculation
Practically
Implement the algorithms in any programming language and run the programs
Depend on the compiler, computer, data input and programming style.
Determine the Complexity time of Algorithm
Theoretically
The complexity time is related to the number of steps/operations.
Count the number of steps and then find the class of complexity.
Find the complexity time for each steps and then count the total.
Example 1: Determine the number of steps
The following algorithm is categorized as O(n).
int counter = 1;
int i = 0;
for (i = 1; i <= n; i++) {
cout << “Analysis of Algorithm" << counter << "\n";
counter++;
how ?
Example 1: Solution
Number Statements
1 int counter = 1;
2 int i = 0;
3 i=1
4 i <= n
5 i++
6 cout << “Analysis of Algorithm" << counter << "\n";
7 counter++;
Example 1: Solution
Statement 3, 4 & 5 are the loop control and can be assumed as one statement.
Number Statements
1 int counter = 1;
2 int i = 0;
3,4,5 i = 1; i <= n; i++
6 cout << “Analysis of Algorithm" << counter << "\n";
7 counter++;
Example 1: Solution
Statement 3, 6 & 7 are in the repetition structure.
It can be expressed by summation series
Example 1: Solution
example:- if n = 5, i = 1
Example 1: Solution
example:- if n = 5, i = 3
Example 1: Solution
example:- if n = 1, i = 1
Example 1: Solution
statements Number of steps
int counter = 1; 1
∑ f(i) = 1
i=1
int i = 0; 1
∑ f(i) = 1
i=1
i = 1; i= n; i++ n
∑ f(i) = n
i=1
cout << “Analysis of Algorithm" n 1
∑ f(i) ∑ f(i) = n.1 = n
<< counter << "\n"; i=1 i=1
counter++ n 1
∑ f(i) ∑ f(i) = n.1 = n
i=1 i=1
Example 1: Solution
Example 2:
Algorithm
void example2( )
for (int a=2; a<=n; a++)
cout << “ example 2“;
}
Example 2: Solution
statements Number of steps
void example2( ) 0
{ 0
int a=2; a<=n; a++ n-2+1=n-1
cout << “ example 2“; n-1.1=n-1
} 0
Total 2(n-1)
Total steps =2(n-1), Complexity Time = O (n)
Class work
void sample1 ( )
for (int a=1; a<=n-1; a++)
cout << “Class work1“;
void sample2 ( )
for (int a=1; a<=n; a++)
for (int b=1; b<=n; b++)
cout << “ Sample2“;
}
Class work
void sample3 ( )
for (int a=1; a<=n; a++)
for (int b=1; b<=a; b++)
cout << “ Sample 3“;