Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH, EEE, CIVIL & MCT)

DEPARTMENT OF M.TECH-CSE

Course : Design and Analysis of Algorithms


Module : Fundamentals of Algorithm Analysis -
Analysis of Algorithm efficiency and Analysis
Framework or Parameters
Faculty :S.SREERAJ/AP-M.TECH/CSE

04/07/2023 1
INTRODUCTION

• In theoretical analysis of algorithms, it is


common to estimate their complexity in the
asymptotic sense.
• i.e., to estimate the complexity function for
arbitrarily large input.
• The term "analysis of algorithms" was coined
by Donald Knuth.
• Algorithm analysis is an important part of
computational complexity theory.

04/07/2023 2
CONT…

• Provides theoretical estimation for the required


resources of an algorithm to solve a specific
computational problem.
• Most algorithms are designed to work with
inputs of arbitrary length.
• Analysis of algorithms is the determination of the
amount of time and space resources required
to execute it.

04/07/2023 3
CONT…

• Usually, the efficiency or running time of an


algorithm is stated as a function relating the
input length to the number of steps, known as
time complexity, or volume of memory, known
as space complexity.

04/07/2023 4
The Need for Analysis

• The need for analysis of algorithms and how to


choose a better algorithm for a particular
problem as one computational problem can be
solved by different algorithms.
• By considering an algorithm for a specific
problem, we can begin to develop pattern
recognition so that similar types of problems can
be solved by the help of this algorithm.
• Algorithms are often quite different from one
another, though the objective of these algorithms
are the same.
04/07/2023 5
CONT…

• For example, we know that a set of numbers


can be sorted using different algorithms.
• Number of comparisons performed by one
algorithm may vary with others for the same
input.
• Hence, time complexity of those algorithms
may differ.
• At the same time, we need to calculate the
memory space required by each algorithm.

04/07/2023 Dr.K.Rama Abirami, ASP/CSE 6


CONT…

• Analysis of algorithm is the process of analyzing


the problem-solving capability of the algorithm
in terms of the time and size required (the size
of memory for storage while implementation).
• The main concern of analysis of algorithms is
the required time or performance.

04/07/2023 7
CONT…

• Generally, we perform the following types of


analysis:
• Worst-case − The maximum number of steps
taken on any instance of size a.
• Best-case − The minimum number of steps
taken on any instance of size a.
• Average case − An average number of steps
taken on any instance of size a.
• Amortized − A sequence of operations applied
to the input of size a averaged over time.
04/07/2023 8
CONT…

• To solve a problem, we need to consider time as


well as space complexity as the program may
run on a system where memory is limited but
adequate space is available or may be vice-
versa.
• In this context, if we compare bubble sort and
merge sort.
• Bubble sort does not require additional
memory, but merge sort requires additional
space.
04/07/2023 9
CONT…

• Though time complexity of bubble sort is higher


compared to merge sort, we may need to apply
bubble sort if the program needs to run in an
environment, where memory is very limited.

04/07/2023 10
Algorithmic Cost and Complexity
There are two aspects of algorithmic performance:
• Time
• Instructions take time.
• How fast does the algorithm perform?
• What affects its runtime? 

• Space
• Data structures take space
• What kind of data structures can be used?
• How does choice of data structure affect
the runtime?
04/07/2023 11
CONT…

 We will focus on time complexity:


– How to estimate the time required for an
algorithm (program)

04/07/2023 12
Analysis of Algorithms

int sum( int n ) STATEMENT STEP COUNT

{ int Sum NO TIME


Sum = 0 1 unit
int Sum;
i=0 1 unit
Sum = 0;
i<n n+1 units
for (int i = 0; i <n; i++)
i++ n units
Sum=Sum+i Sum=Sum + i n units
} Total 3n+3 Units

The Time Complexity is O(n)


04/07/2023 13
Cont…(Analysis of Algorithms)
int sum( int n )
int sum( int n ) {
{ int partialSum,i;
int partialSum; partialSum = 0,i=0;
while(i<=n)
partialSum = 0; {
for (int i = 0; i <=n; i++) Statement;
statement; i++;
} }
}

T(N) = 1 + 1 + (N+2) + N +1 + N + 1 = 3N + 6 = O(N)


So, our Running Time Estimate is O(N)
04/07/2023 14
Analysis Framework
Computing
Best, worst and
average case
Measuring efficiencies Measuring
Space Input Size
Complexity

ANALYSIS OF
ALGORITHMS
Measuring
Time Measuring
Complexity running
Computing time
Order of
Growth of
algorithms
04/07/2023 15
Time & Space Complexity

• Amount of computer time required by an


algorithm to run to completion.
• It is measured in terms of Frequency Count.
• The amount of memory required by an
algorithm to run.

04/07/2023 16
Cont...(Analysis Framework)

Measuring running time


 Time Complexity is measured in terms of unit
called Frequency Count.
 Time measured for analysing an algorithm is
generally running time.
 Identify the basic operation of an algorithm.

04/07/2023 17
Cont...

Let
T(n) : running time

cop : execution time for basic operation

C(n) : number of times basic operation is executed.

T(n) ≈ cop C(n)

04/07/2023 18
Examples: Basic Operation

Problem Operation
Find x in an array Comparison of x with
an entry in the array
Multiplying two matrices Multiplication of two real
with real entries numbers
Sort an array of Comparison of two
numbers array entries plus
moving elements in the
array
Traverse a tree Traverse an edge

04/07/2023 19
The Analysis Framework

1. Measuring an Input’s Size.


2. Units for Measuring Running Time.
3. Orders of Growth.
4. Worst-Case, Best-Case and Average-Case
Efficiencies.
5. Recapitulation of the Analysis Framework.

04/07/2023 20
Cont...(Analysis Framework)
Measuring Input Size:
• If the input size is larger, then usually algorithm
takes longer time.
• How to validate algorithm?
• Time efficiency: the number of repetitions of the
basic operation as a function of input size.
• Input size is influenced by
 the data representation, e.g. matrix
 the operations of the algorithm,
 the properties of the objects in the problem.
e.g. checking if a given integer is a prime
number
04/07/2023 21
Examples
Problem Size of input
Find x in an array The number of the elements in
the array
Multiply two matrices The dimensions of the matrices
Sort an array The number of elements in the
array
Traverse a binary tree The number of nodes
Solve a system of The number of equations, or
linear equations the number of the unknowns,
or both

04/07/2023 22
Cont...(Analysis Framework)
Change in behaviour of the algorithm according to
input size is called Order of Growth.

04/07/2023 23
Computing Algorithmic Efficiencies
 Each of these complexities defines a numerical
function: time versus size.
– The worst case complexity of the algorithm is
the function defined by the maximum
number of steps taken on any instance of
size n.
– The best case complexity of the algorithm is
the function defined by the minimum number
of steps taken on any instance of size n.
– The average-case complexity of the
algorithm is the function defined by an
average number of steps taken on any
instance of size n.
04/07/2023 24
04/07/2023 25
04/07/2023 26
04/07/2023 27

You might also like