0% found this document useful (0 votes)
15 views

4_Algorithms and analysis - part 1

The document discusses algorithms and their analysis, focusing on definitions, goals, and fundamental questions related to algorithm efficiency. It covers types of analysis including worst case, best case, average case, and amortized analysis, emphasizing the importance of comparing algorithms based on time and space complexity. The lecture aims to equip students with the ability to analyze algorithms and their complexities effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

4_Algorithms and analysis - part 1

The document discusses algorithms and their analysis, focusing on definitions, goals, and fundamental questions related to algorithm efficiency. It covers types of analysis including worst case, best case, average case, and amortized analysis, emphasizing the importance of comparing algorithms based on time and space complexity. The lecture aims to equip students with the ability to analyze algorithms and their complexities effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

19CCE202 Data Structures and Algorithms

LECTURE 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

• An algorithm is the step-by-step • Algorithm analysis helps us to determine


unambiguous instructions to solve a which algorithm is most efficient in terms
given problem. of time and space consumed.
• In computer science, multiple • The goal of the analysis of algorithms is
algorithms are available for solving to compare algorithms (or solutions)
the same problem. mainly in terms of running time but also
in terms of other factors (e.g., memory,
developer effort, etc.)

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.

2. Does it really do what it is Verifying whether the algorithm is indeed correct.


supposed to do? – In general, testing on a few particular inputs can be
3. How efficiently does it do it? enough to show that the algorithm is incorrect.
– the number of different potential inputs for most
algorithms is infinite in theory, and huge in practice
Three aspects of algorithm are: – correctness proofs / Formal verification techniques
1. Specification.
Efficiency or performance of an algorithm relates to the
2. Verification. resources required by it, such as how quickly it will run, or
3. Performance analysis. how much computer memory it will use.
– usually depends on the problem instance size, the
choice of data representation, and the details of the
algorithm.
Complexity of – Time Complexity: Running time of the program
as a function of the size of input
Algorithms
– Space Complexity: Amount of computer
memory required during the program execution, as
It is very convenient to classify a function of the input size
algorithms based on the relative
amount of time or relative What is Running
amount of space they require and
specify the growth of time /space Time Analysis?
requirements as a function of the
It is the process of determining how processing time
input size increases as the size of the problem (input size -
number of elements ) increases.
• Size of an array • Polynomial degree • Number of
elements in a matrix • Number of bits in the binary
representation of the input • Vertices and edges in a
graph.
How to Compare Algorithms?

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”.

You might also like