0% found this document useful (0 votes)
53 views6 pages

Alg PDF

Uploaded by

sahilmulani2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

Alg PDF

Uploaded by

sahilmulani2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter – 2

Algorithm Analysis

Algorithm Analysis
Objectives :

Define an Algorithm
Space Complexity and Time
Complexity
Asymptotic Notations

Introduction

The word "algorithm" has a great significance in computer


science.
It refers to a method that can be used by a computer for the
solution of a problem.
A lot of research has gone into developing different algorithms for
a variety of problems.
The choice of a good algorithm affects the efficiency of the
program.

Definition
An algorithm is a finite set of instruction that, if followed,
accomplishes a particular task.
It is a method that can be used by a computer for the solution of a
problem.
Step by step process of program using English like words is called as
algorithm.
Characteristics of an Algorithm

All algorithms must satisfy the following criteria:


1. Finiteness : If we trace the steps of an algorithm,
the algorithm must terminate after a finite number
of steps for all cases.
No infinity loops in the algorithm.
2. Definiteness : (no complications) Each step of an algorithm must
be precisely defined i.e. each
instruction must be clear and unambiguous.
Chapter – 2
Algorithm Analysis

Effectiveness : Each instruction must be sufficiently basic that it can


in
principle, be done exactly and in finite time by a
person using
only paper and pencil.

Example : Add num1 to num2 is an effective operation


if num1
and num2 are integers , but if they were real
numbers with
infinite decimal expansion, the instruction is
ineffective.
3. Input : An algorithm takes zero or more inputs; quantities
that are
given to it initially before the algorithm begins or
dynamically
as the algorithm runs.
4. Output : An algorithm generates one or more outputs;
quantities that
have a specified relation to the inputs.

5
Chapter – 2
Algorithm Analysis

Performance Analysis

There are many criteria, upon which an algorithm can be evaluated,


1. Does it perform the desired task?
2. Does it work correctly according to the original specifications of the
task?
3. Does it contain documentation describing how it works?
4. Are procedures created in such a way that they perform logical sub
functions?
(Does functions depends on each-other?)
5. Is the code readable?
6. Does it works properly?

These criteria are vital while writing algorithms especially for complex
problems.
There are other criteria for judging algorithms, which have a direct
relationship to performance. These are

a. Space Complexity : Deals with storage requirements.


b. Time Complexity : Deals with computing time.

Space Complexity

The space complexity of an algorithm is the amount of memory it


needs to run to completion.
Space complexity is algorithm that calculate total space required for
program Storage.

Components of Space Complexity

The space needed by an algorithm is the sum of the following two


components.
1. Fixed Part: This part is independent of the characteristics of the inputs
and outputs. It includes the instruction space for simple variables, fixed
size component variables, constants etc.
2. Variable Part(changeable Part) It consists of the space needed by
variables whose size depends on the particular problem instance being
solved, the space needed by referenced variables and the recursion
stack space.
The space requirement S(P) for an algorithm is written as

S(P) = c +
Sp

6
Chapter – 2
Algorithm Analysis

where c is a constant and is the fixed part Sp is the variable part


depending on the instance characteristics.
Example :
float area (float r)
{
return 3.142 *r *r;
}
The space needed is independent of the input and output
characteristics. It uses only ' the value of r (assuming one word of
storage)
Therefore, Sp = 0
c = 1

Example :
int sum (int a[ ],int n)
{
int r=0,i;
for (i = 0 ; i < n ; i++)
s = s+a[i];
return s;
}
The algorithm depends on n, which is the instance characteristic.
The variables is and n' are the fixed part
Therefore c = 3
Sp = n (i.e. the size of a[ ])
Example :
int rsum(int a[ ], int n)
{
if (n <=0)
return 0;
return rsum(a, n-l) + a[n];
}

This is a recursive function characterized by n. The recursion depth


depends upon n2 and n+1.
The recursion stack space includes space for the formal parameters,
local variables and return address. Thus the recursion stack space is
(n+l)*3 (i.e. one word for n, pointer to a and return address each).

Time Complexity :
The amount of time taken by a program for
execution is the running time of a program. The total number of times a
statement gets executed is defined as its

7
Chapter – 2
Algorithm Analysis

Defination: Time Complexity is an algorithm that calculates total


time required for program execution.

Time complexity calculate using BIG-O Notation.


Big 0 Notation:
It is formula to calculate time Complexity.
The time complexity of a program in terms of the order of
calculation of frequency count using the "Big - O" notation. The running
time of an algorithm is the sum of frequency counts of all executable
statements, which may yield a polynomial.

There are three cases to calculate Time Complexity:

1)Best Case
2) Average Case
3)Worst Case

Best Case :
The Best Case step count is the minimum number of steps that
can be executed for the given parameters.(program)
Eg:
Big-O(0)
That means 0 steps to execute program
Worst Case :
The Worst Case step count is the maximum number of steps
that can be executed for the given parameters.(program) This is the
slowest possible time an algorithm may take.
Eg:
Big-O(n)
That means “n” steps to execute program

Average Case :
The average Case step count is the average number of steps
that can be executed for the given parameters.(program) Eg:
Big-O(n/2)
That means “n/2” steps to execute program

Asymptotic Notations (O, ,)

8
Chapter – 2
Algorithm Analysis

The time and space complexity of an algorithm can be expressed in


several ways:
1. The algorithm never takes more than (some function of n)
operations.
2. The algorithm's running time is always less than some function of n.
3. The algorithm's running time is of the order of some function of n.
The above can be expressed using asymptotic notations 0,,. They are
called asymptotic because they apply for large values of n. Before study
these notations, let’s study some common functions, which are used as
complexity functions (running time functions)

Notation (Big - Oh notation)


This notation is used to denote upper bounds.
It is expressed as O(f(n)) which means that the time taken never
exceeds roughly f(n) operations. It is the most commonly used notation.
Definition: The function f(n) = O(g(n)) [read as f of n is big oh of g of n] if
there exist positive constants c and n such that
f(n) < = c * g(n) for all n, n > = n

Ω Notation (Omega Notation)

This is used to denote lower bounds. It is written as T = Ω(f(n)).


This indicates that T takes at least about f(n) operations.
Definition: The function f(n) = Q (g(n)) iff there exist positive constants c
and n such that f(n) >= c*g(n) for all n, n >= n

θ Notation (Theta Notation)

This notation denotes both, upper and lower bounds of f(n) and
written as
T(n) = θ(g(n))
Definition: The function f(n) = θ(g(n)) iff there exist positive constants c,
c and n such that c * g(n) < f(n) < c * g(n) for all n, n >= n .

You might also like