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

Lecture 3

Uploaded by

kumargunesh370
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 3

Uploaded by

kumargunesh370
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

DATA STRUCTURES AND

ALGORITHMS
Lecture 3
by
Engr. Urooj Faiz
IN THE LAST LECTURE…
We learned about the algorithm.

We learned about abstraction a n d its types.

We explored the abstract d a t a type in detail.

We were introduced to the i dea of a d a t a structure.

We explored the operations performed on a d a t a structure.

We were introduced to different kinds of d a t a structures.


ANALYSIS OF ALGORITHMS
ANALYSIS OF ALGORITHMS
⯈ O n c e a n algorithm is given for a problem a n d
de ci de d (somehow) to b e correct, an important step
is to determine how much in the way of resources,
such as time or space, the algorithm will require.
WHY ANALYZE ⯈ An algorithm that solves a problem but requires a year is
hardly of any use.
ALGORITHMS? ⯈ Likewise, an algorithm that requires thousands ofgigabits
of main memory is not useful on most machines.
⯈ Analyzing an algorithm means predicting the
resources it requires. These resources may b e
memory, communication bandwidth, computer
hardware, a n d computational time.
ANALYSIS OF ALGORITHMS

HOW TO ANALYZE ⯈ Empirical analysis


ALGORITHMS? ⯈ Theoretical analysis
EMPIRICAL ANALYSIS
⯈ Empiricalanalysis is do n e by coding the
algorithm in a high-level language a n d running
it on a computer, over a suitable large range of
inputs.

⯈ Theresults of empirical analysis are highly


dependent on:
⯈ The chosen high-level language
⯈ The machine on which the algorithm is run.
⯈ The inputs that are provided.
PROBLEMS WITH EMPIRICAL ANALYSIS
⯈ You must c o d e all algorithms, though you will finally
ke ep only on e of them, a n d discard the rest.
⯈ A slow algorithm m ay perform better than a fast one,
if it is written by a better programmer.
⯈ Results on on e set of inputs may b e completely
different from those for another set of inputs.
⯈ The inputs chosen to b e used m ay favor one
algorithm more than the other.
⯈ All algorithms under scrutiny must b e run on the same
software a n d hardware platforms.
⯈ You may find that you d o not h av e the budget to
implement the better algorithm.
THEORETICAL ANALYSIS
⯈ As empirical analysis is costly, a n d may not lead to a
solution, we d o not use it for algorithm analysis. Instead
w e use theoretical analysis.

⯈ Theoretical analysis:
⯈ Uses ps e u d oc ode for the algorithm, instead of
programming code.
⯈ Characterizes running time as a function of the
input size, n.
⯈ Takes all possible inputs into account.
⯈ Evaluates the algorithm independent of the
hardware a n d software.
CASES FOR THEORETICAL ANALYSIS

⯈ Whenw e analyze a n algorithm theoretically, we c a n


choose to d o so in three ways:

⯈ Best ca se analysis
⯈ Worst ca se analysis
⯈ Average ca se analysis
BEST CASE ANALYSIS

⯈ Provide a set of inputs for which the algorithm takes


the shortest possible time or executes the least
number of primitive operations.
⯈ Thebest-ca se running time of the algorithm is a lower
bound on the running time of the algorithm.
⯈ If 𝑖is the running time of the algorithm for a set of
inputs , a n d 𝑏 is the best-cas e running time of the
algorithm, then,
𝑏 𝑖
WORST CASE ANALYSIS

⯈ Provide a set of inputs for which the algorithm takes


the longest possible time or executes the greatest
number of primitive operations.
⯈ The worst-ca se running time of the algorithm is an
upper bound on the running time of the algorithm.
⯈ If 𝑖is the running time of the algorithm for a set of
inputs , a n d 𝑤 is the worst-cas e running time of the
algorithm, then,
𝑖 𝑤
AVERAGE CASE ANALYSIS
⯈ Find the running time of the algorithm for
all possible sets of inputs, find the
probability of e a c h input an d calculate
the average running time as shown:

⯈ The average running time is impossible to


com pute for algorithms with a n infinite set
of inputs.
⯈ If 𝑎𝑣𝑔is the average running time of the
algorithm, 𝑏 is the best-cas e running time
of the algorithm a n d 𝑤 is the worst-cas e
running time of the algorithm, then,
𝑏 𝑎𝑣𝑔 𝑤
⯈ If we know the worst-ca se running time of an
algorithm, w e c a n b e sure that the algorithm
will never e x c e e d this duration for any input
that is given to it.
⯈ Theworst-ca se analysis is most useful to a
computer scientist.
⯈ Average ca se analysis requires a lot of effort.
⯈ We only co nduct best cas e analysis when
the best cas e occurs very frequently.

WHICH ANALYSIS IS THE


MOST USEFUL?
FACTORS AFFECTING RUNNING TIME OF
ALGORITHMS
DO WE REALLY NEED ABSOLUTE
RUNNING TIME?
The answer is NO.

⯈ When comparing two algorithms that


perform the same function, a relative
estimate is suffic ient.

⯈ If we know that algorithm A does more


operations for the same input size than
algorithm B, it can be easily deduced that B
is faster, without knowing the actual running
times of both A and B.
Asymptotic Analysis

 When we look at input sizes large enough to make only the


order of growth of the running time relevant, we are
studying the asymptotic efficiency of algorithms.

The following notations have been defined for asymptotic analysis:

1. Big Oh (O) Notation


2. Big Omega (Ω) Notation
3. Big Theta (θ) Notation
BIG – O NOTATION
BIG – O NOTATION
BIG – Ω NOTATION
BIG – Ω NOTATION
BIG – θ NOTATION

You might also like