Chapter 1
Chapter 1
1
outlines
Introduction to Algorithm analysis
Asymptotic Notations
Analysis of Algorithm
Review of elementary Data Structures
Heaps
Hashing
Set Representation
UNION, FIND Operation
2
Algorithm
• An algorithm is a set of steps of operations to solve a
problem performing calculation, data processing, and
automated reasoning tasks.
• An algorithm is an efficient method that can be
expressed within finite amount of time and space.
• An algorithm is the best way to represent the solution of
a particular problem in a very simple and efficient way.
• If we have an algorithm for a specific problem, then we
can implement it in any programming language, meaning
that the algorithm is independent from any
programming languages.
3
Algorithm Design
• The important aspects of algorithm design include creating an
efficient algorithm to solve a problem in an efficient way using
minimum time and space.
• To solve a problem, different approaches can be followed. Some of
them can be efficient with respect to time consumption, whereas
other approaches may be memory efficient.
• However, one has to keep in mind that both time consumption and
memory usage cannot be optimized simultaneously.
• If we require an algorithm to run in lesser time, we have to invest
in more memory and if we require an algorithm to run with lesser
memory, we need to have more time.
4
cont..
5
Problem Development Steps
The following steps are involved in solving computational
problems.
Problem definition
Development of a model
Specification of an Algorithm
Designing an Algorithm
Checking the correctness of an Algorithm
Analysis of an Algorithm
Implementation of an Algorithm
Program testing
Documentation
6
How to Write an Algorithm?
• There are no well-defined standards for writing algorithms.
Rather, it is problem and resource dependent. Algorithms are
never written to support a particular programming code.
• As we know that all programming languages share basic code
constructs like loops (do, for, while), flow-control (if-else), etc.
These common constructs can be used to write an algorithm.
• We write algorithms in a step-by-step manner, but it is not always
the case. Algorithm writing is a process and is executed after the
problem domain is well-defined. That is, we should know the
problem domain, for which we are designing a solution.
7
Cont..
8
Characteristics of Algorithms
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −
• Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
• Input − An algorithm should have 0 or more well-defined inputs.
• Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.
9
Algorithm analysis
Why we should analyze algorithms?
Predict the resources that the algorithm requires
Computational time (CPU consumption)
Memory space (RAM consumption)
10
Time Complexity
Worst-case
An upper bound on the running time for any input
of given size
Average-case
Assume all inputs of a given size are equally likely
Best-case
The lower bound on the running time
11
Time Complexity – Example
12
Asymptotic notations
Algorithm complexity is rough estimation of the
number of steps performed by given
computation depending on the size of the input
data
Measured through asymptotic notation
O(g) where g is a function of the input data size
Examples:
Linear complexity O(n) – all elements are processed once (or
constant number of times)
Quadratic complexity O( n2) – each of the
elementsis processed n times
13
O-notation
Asymptotic upper bound
14
f(n)=O(g(n)) iff there exist a positive constant c and
non-negative integer n0 such that
f(n) cg(n) for all
nn0.
g(n) is said to be an upper bound of f(n).
15
Example
16
Ω notation
Asymptotic lower bound
17
Example
18
Θ notation
19
Example
20
part II cont..
21
Q &A
22