4_Algorithms and analysis - part 1
4_Algorithms and analysis - part 1
Dr. R. Ramanathan
Associate Professor
Department of Electronics and Communication Engineering
Amrita School of Engineering, Coimbatore
r_ramanathan@cb.amrita.edu
Objective
To understand the concept of algorithm, its fundamental aspects, analysis of algorithm,
comparison and types of analysis.
Key concepts
• Definition of Algorithm [CO03]
• Goal of analysis of algorithms [CO03]
• Fundamental questions about algorithms
• Analysis of algorithms [CO03]
o Complexity
o Running time CO3: Ability to analyze the
• How to compare algorithms? [CO03] algorithms and its complexity
• Types of analysis [CO03]
o Worst case
o Best case
o Average case
Algorithm
Algorithm. A finite sequence of instructions, each of which has a clear meaning and
can be executed with a finite amount of effort in finite time.
Specification should formalize the crucial details of the
Fundamental questions problem that the algorithm is intended to solve.
about algorithms – how the inputs and outputs of the algorithm are
related, though there is no general requirement that
1. What is it supposed to do? the specification is complete or non-ambiguous.
Execution times? Not a good measure as execution times are specific to a particular
computer.
Number of statements executed? Not a good measure, since the number of statements
varies with the programming language as well as the style of the individual programmer.
Ideal solution? Let us assume that we express the running time of a given algorithm as a
function of the input size n (i.e., f(n)) and compare these different functions corresponding
to running times.
This kind of comparison is independent of machine time, programming style, etc.
Types of Analysis
To analyze an algorithm we need some kind of syntax, and that forms the base for asymptotic analysis
/notation.
Worst case Best case
o Defines the input for which the algorithm o Defines the input for which the algorithm takes
takes a long time (slowest time to complete). the least time (fastest time to complete).
o Input is the one for which the algorithm runs o Input is the one for which the algorithm runs the
the slowest. fastest.
o upper bound on the running time for any input o Lower bound on the running time for any input
Average case
o Provides a prediction about the running time of the algorithm.
o Run the algorithm many times, using many different inputs that come from some distribution that
generates these inputs (Assumes that the input is random).
o compute the total running time (by adding the individual times), and divide by the number of trials.
o Amortized Running Time - time required to perform a sequence of operations is averaged over all the
operations performed; guarantees the average performance of each operation in the worst case
Amortized analysis
Amortized Analysis – a worst-case analysis, but for a sequence of operations
rather than for individual operations.
– applies to a method that consists of a sequence of
Amortized analysis refers to operations, where the vast majority of the operations
determining the time-averaged are cheap
running time for a sequence of – a correct way of understanding the overall running
operations. time
Motivation
It is different from average – to better understand the running time of certain
case analysis, because techniques, where standard worst case analysis
amortized analysis does not provides an overly pessimistic bound.
make any assumption about the Approach
distribution of the data values, – to assign an artificial cost (amortized cost) to each
operation in the sequence, such that the total of the
whereas average case analysis artificial costs for the sequence of operations bounds
assumes the data are not the total of the real costs for the sequence.
“bad”.