Algorithms
Performance Analysis
Dr. V.Sireesha
Dept. of CSE
Algorithm- Introduction
The word algorithm itself is derived from
the 9th-century mathematician Muḥammad
ibn Mūsā al-Khwārizmī, Latinized Algoritmi;
English term 'algorithm' is first attested in
the 17th century
An algorithm is a step by step procedure to
solve a problem.
It is a finite set of well defined instructions
that specifies a sequence of operations to
be carried out to solve a specific problem.
Need for Algorithm
Algorithm is the best method of description
without describing the implementation details. It
is used to measure the behavior (or
performance) of the methods in all cases (best
cases, worst cases, average cases).
With the help of an algorithm, we can also
identify the resources (memory, input-output)
cycles required by the algorithm.
We can also measure and analyze the complexity
(time and space) of the problems concerning
input size without implementing and running it
thus reducing the cost of design.
Characteristics of Algorithms
Input
Output
Definiteness: clear and unambiguous
Finiteness: terminate after a finite
number of steps
Effectiveness: instruction is basic
enough to be carried out
Issues for Algorithm
How to devise algorithm
How to express algorithm
How to validate algorithm
How to analyze algorithm
How to test a program
i) Debugging
ii) Profiling (or) Performance Measuring
Criteria- Issues for Algorithm
Process for Design and Analysis of
Algorithms
Specification of Algorithm
Pseudo-Code for Expressing
Algorithms
1. Comments begin with // and continue until
the end of line.
2. A block of statements / compound statements
are represented using { and } for example if
statement, while loop, functions etc.,.
3. The delimiters [;] are used at the end of
the each statement.
Example { Statement 1; Statement 2;
Pseudo-Code for Expressing
Algorithms
4. An identifier begins with a letter.
Example: sum, sum5, a; but not in 5sum, 4a
etc.,.
5. Assignment of values to the variables is
done using the assignment operators as :=
or .
6. A There are two Boolean values TRUE
and FALSE.
Logical operators: AND, OR, NOT.
Relational operators: <, >, ≥,≤,=,≠.
Arithmetic operators: +, -, *, /, %;
Pseudo-Code for Expressing
Algorithms
7. The conditional statement if-then or if-
then-else is written in the following
form.
If (condition) then (statement)
If (condition) then (statement-1) else
(statement-2)
If a condition is true the particular block of
statements are execute.
Example if(a>b) then { write("a is big"); }
else { write("b is big"); }
Pseudo-Code for Expressing
Algorithms
8. The Case statement
Pseudo-Code for Expressing
Algorithms
9. Loop Statements : For
Pseudo-Code for Expressing
Algorithms
Sample Algorithms
Process for Design and Analysis of
Algorithms
PERFORMANCE EVALUATION
Performance evaluation can be loosely
divided into 2 phases
A priori estimates which is known as
performance analysis
A posterior testing which is known as
performance measurement.
Performance Analysis
Performance Analysis
Complexity
•A measure of the performance of an algorithm
•An algorithm’s performance depends on
internal factors
external factors
•External Factors
Speed of the computer on which it is run
Quality of the compiler
Size of the input to the algorithm
•Internal Factor
The algorithm’s efficiency, in terms of:
Time required to run
Space (memory storage)required to run
Note:
Complexity measures the internal factors (usually more interested in time than space)
Performance Analysis
Two ways of finding complexity
•Experimental study
•Theoretical Analysis
Experimental study
•Write a program implementing the algorithm
•Run the program with inputs of varying size and composition
•Get an accurate measure of the actual running time
•Use a method like System.currentTimeMillis()
•Plot the results
Limitations of Experiments
•It is necessary to implement the algorithm, which may be difficult.
•Results may not be indicative of the running time on other inputs
not included in the experiment.
•In order to compare two algorithms, the same hardware and
software environments must be used.
•Experimental data though important is not sufficient.
Theoretical Analysis
•Uses a high-level description of the algorithm
instead of an implementation
•Characterizes running time as a function of the
input size, n.
•Takes into account all possible inputs
•Allows us to evaluate the speed of an
algorithm independent of the
hardware/software environment
PERFORMANCE ANALYSIS
The efficiency of an algorithm can be
decided by measuring the performance
of an algorithm.
The performance of an algorithm by
computing amount of time (time
complexity) and storage requirement
(space complexity).
PERFORMANCE ANALYSIS
To judge an algorithm, particularly
two things are taken into
consideration
1. Space complexity
2. Time complexity
SPACE COMPLEXITY
Space Complexity: The space complexity
of an algorithm (program) is the amount
of memory it needs to run to
completion.
The space needed by an algorithm has
the following components.
Instruction Space.
Data Space.
Environment Stack Space.
SPACE COMPLEXITY
Instruction Space: Instruction space is the space needed
to store the compiled version of the program instructions.
Data Space: Data space is the space needed to store all
constant and variable values. Data space has two
components.
i)Space needed by constants, for example 0, 1, 2.134.
ii)Space needed by dynamically allocated objects such as
arrays, structures, classes.
Environmental Stack Space: Environmental stack space
is used during execution of functions. Each time function is
involved the following data are saved as the environmental
stack.
i)The return address.
ii) Value of local variables.
iii) Value of formal parameters in the function being
invoked.
SPACE COMPLEXITY
Thus, the space requirement of any program p may
therefore be written as
Space complexity S(P) = C + Sp (Instance
characteristics).
This equation shows that the total space needed by a
program is divided into two parts.
Fixed space requirements(C) is independent of
instance characteristics of the inputs and outputs.
Instruction space
Space for simple variables, fixed-size structure variables,
constants.
A variable space requirements (SP(1)) dependent on
instance characteristics 1.
This part includes dynamically allocated space and the
recursion stack space.
SPACE COMPLEXITY – EXAMPLE-1
Algorithm NEC (float x, float y, float z)
{
Return (X + Y +Y * Z + (X + Y +Z)) /(X+ Y) + 4.0;
}
In the above algorithm, there are no instance
characteristics and the space needed by X, Y, Z is
independent of instance characteristics, therefore we
can write,
S(XYZ) =3+0=3
One space each for X,Y and Z
Space complexity is O(1).
SPACE COMPLEXITY – EXAMPLE-2
Algorithm ADD ( float [], int n)
{ sum = 0.0;
for i=1 to n do
sum=sum+X[i];
return sum;
}
Here, atleast n words since X must be large enough to hold
the n elements to be summed. Here the problem instances is
characterized by n, the number of elements to be summed.
So, we can write,
S(ADD) =3+n
3-one each for n, I and sum
Where n- is for array X[],
Space complexity is O(n).
TIME COMPLEXITY
The time complexity of an algorithm is the amount of
compile time it needs to run to completion. We can measure
time complexity of an algorithm in two approaches
1. Priori analysis or compile time
2. Posteriori analysis or run (execution) time.
In priori analysis before the algorithm is executed we will
analyze the behavior of the algorithm. A priori analysis
concentrates on determining the order of execution of
statements.
In Posteriori analysis while the algorithm is executed we
measure the execution time. Posteriori analysis gives accurate
values but it is very costly.
TIME COMPLEXITY
We know that the compile time does not depend on the size
of the input. Hence, we will confine ourselves to consider
only the run-time which depends on the size of the input and
this run-time is denoted by TP(n). Hence
Time complexity T(P) = C + TP(n).
The time (T(P)) taken by a program P is the sum of the
compile time and execution time. The compile time does not
depend on the instance characteristics, so we concentrate on
the runtime of a program. This runtime is denoted by tp
(instance characteristics).
TIME COMPLEXITY
The following equation determines the number of
addition, subtraction, multiplication, division compares,
loads stores and so on, that would be made by the code
for p.
tp(n) = CaADD(n)+ CsSUB(n)+ CmMUL(n)+
CdDIV(n)+……………..
where n denotes instance characteristics, and Ca, Cs,
Cm, Cd and so on….. denote the time needed for an
addition, subtraction, multiplication, division and so on,
and ADD, SUB, MUL, DIV and so on, are functions
whose values are the number of additions, subtractions,
multiplications, divisions and so on. But this method is an
impossible task to find out time complexity.
TIME COMPLEXITY
Another method is step count. By using step count, we
can determine the number if steps needed by a program
to solve a particular problem in 2 ways.
Method 1: introduce a global variable “count”, which is
initialized to zero. So each time a statement in the signal
program is executed, count is incremented by the step
count of that statement.
Method 2: The second method to determine the step
count of an algorithm is to build a table in which we list
the total number of steps contributed by each
statement.
TIME COMPLEXITY
TIME COMPLEXITY
The S/e (steps per execution) of a statement is the amount by
which the count changes as a result of the execution of that
statement. The frequency determines the total number of times
each statement is executed.