Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

STIA2024

DATA STRUCTURES
& ALGORITHM
ANALYSIS
TOPIC 1 : ALGORITHM ANALYSIS

1
• Introduction
• Motivation
• Measuring an Algorithm's Efficiency
• Big Oh Notation
LECTURE • Picturing Efficiency
OUTLINE • The Efficiency of Implementations of the
ADT List
• The Array-Based Implementation
• The Linked Implementation
• Comparing Implementations
2
• To determine the efficiency
of a given algorithm, and
• To compare the expected
LEARNING
execution times of two
OBJECTIVE
methods, given the
efficiencies of their
algorithms.

3
INTRODUCTION
• An algorithm is a clearly set of simple instructions to
be followed to solve a problem.
• Different algorithms give different solutions varying
in time and complexity
• Properties of algorithms
• Correctness (behave as the specified specifications), concrete steps,
unambiguous, finite number of steps, terminate (non-infinite loop)
• A program is an implementation of an algorithm in a
programming language

4
Introduction
• Algorithm analysis is the practice of examining code to objectively
decide which code is better (which code uses less resource).
• Purpose : to estimate resources for algorithm
(How much longer does the algorithm take when the input gets
larger? Which of several different algorithms is faster? Does the
algorithm work fast enough for my needs?)
• Resource – running/processing time, memory, size of program, etc.
• Several factors affect the running time of program, such as
• Algorithm used
• Input to algorithm
• Computer performance : hardware, OS, compiler, etc.
5
long firstOperand = 7562;
long secondOperand = 423;
long product = 0;
for (; secondOperand > 0;
secondOperand--)
product = product +
firstOperand;
MOTIVATION System.out.println(product);
When the 423 is changed to 100,000,000
there is a significant delay in seeing the
result 6

Even a simple program can be noticeably


inefficient
MEASURING ALGORITHM
EFFICIENCY
 An algorithm has both time and space requirements, calls its
complexity (can be measured).
 Types of complexity
 Space complexity : the memory it needs to execute
 Time complexity : the time it takes to execute
 Analysis of algorithms
 The process of measuring the complexity of algorithms
 Measuring of algorithm’s complexity, are NOT measuring how
involved or difficult it is.
 Cannot compute the actual time requirement of an algorithm, but
estimate its
 Worst-case time : the maximum number of required operations for
inputs of given size (n), or
 Best-case time : the fewest number of operations required for7 a
given n, or
 Average-case time : the average number of operations required for
a given n.
MEASURING
ALGORITHM
EFFICIENCY Fig. 1 : Three algorithms for computing
1 + 2 + … n for an integer n > 0

8
Measuring Algorithm Efficiency

Fig. 2 : The number of operations required by the


algorithms for Fig 1
MEASURING ALGORITHM
EFFICIENCY

Algorithm A:
1. sum =0; //1 assignment
2. for i = 1 to n //n iterations of the for loop
3. sum = sum +1; //1 assignment and 1 addition per each iteration

10
MEASURING ALGORITHM
EFFICIENCY
Algorithm B
1. sum = 0; //1 assignment
2. for i =1 to n {
3. for j=1 to i // n(n+1)/2 iterations of the for loops
4. sum = sum +1; //1 assignment and 1 addition per each iteration
5. }

11
MEASURING ALGORITHM EFFICIENCY

Algorithm C
1. sum = n*(n+1)/2;
// one assignment + one addition + one multiplication + one
//division.

12
Measuring Algorithm Efficiency

Fig. 3 : The number of operations required by the


algorithms in Fig. 1 as a function of n 13
BIG OH NOTATION

• Notation to represent an algorithm’s complexity


(based on the number of executed operations).
• To say "Algorithm A has a worst-case time
requirement proportional to n"
• We say A is O(n)
• Read "Big Oh of n“ or “order of at most n”
• For the other two algorithms
• Algorithm B is O(n2)
• Algorithm C is O(1)
14
Big Oh Notation

Fig. 4 : Typical growth-rate functions (GRF)


evaluated at increasing values of n
GRF : measure how an algorithms time requirement grows as the problem
size grows (determine which algorithm is faster). It is a function of the
problem size that behaves like the algorithm’s actual time requirement.

**Note : When analyzing the time efficiency of an algorithm,


15
consider large problem**
THE COMPLEXITIES OF PROGRAM
CONSTRUCT
• The time complexity of the if statement:
if (condition)
S1
else
S2
is the sum of complexity of the condition and the complexity of
S1 or S2, whichever is largest.
• The time complexity of a loop statement:
while (condition)
S1

is the complexity of its body times the number of times the body
16
executes (number of looping).
PICTURING EFFICIENCY

Fig. 5 : An O(n) algorithm.

17
Picturing Efficiency

18

Fig. 6 : An O(n2) algorithm.


Picturing Efficiency

19

Fig. 7 : Another O(n2) algorithm.


Picturing Efficiency

Fig. 8 : The effect of doubling the problem size


on an algorithm's time requirement.
20
Picturing Efficiency

Fig. 9 : The time to process one million items by


algorithms of various orders at the rate of one
million operations per second.
21
COMMENTS ON EFFICIENCY
• A programmer can use O(n2), O(n3) or O(2n) as long as
the problem size is small
• At one million operations per second it would take 1
second …
• For a problem size of 1000 with O(n2)
• For a problem size of 1000 with O(n3)
• For a problem size of 20 with O(2n)
22
Conclusion

Q & A Session

23

You might also like