You are on page 1of 26

Week 1 : Algorithm Analysis

STIA2024
Data Structures & Algorithm
Analysis

1
Lecture Outline
 Introduction
 Motivation
 Measuring an Algorithm's Efficiency
 Big Oh Notation
 Picturing Efficiency
 The Efficiency of Implementations of the ADT
List
 The Array-Based Implementation
 The Linked Implementation
 Comparing Implementations
2
Learning Objective
 To determine the efficiency of a given
algorithm, and
 To compare the expected execution times of
two 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, 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
Motivation
 Even a simple program can be noticeably
inefficient

long firstOperand = 7562;


long secondOperand = 423;
long product = 0;
for (; secondOperand > 0; secondOperand--)
product = product + firstOperand;
System.out.println(product);

 When the 423 is changed to 100,000,000 there is


a significant delay in seeing the result

6
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 , or
 Best-case time : the fewest number of operations required
for a given n, or
 Average-case time : the average number of operations
required for a given n.

7
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

9
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)
**Note : When analyzing the time efficiency of an algorithm, consider large
problem**

15
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 executes (number of looping).

16
Picturing Efficiency

Fig. 5 : An O(n) algorithm.

17
Picturing Efficiency

Fig. 6 : An O(n2) algorithm. 18


Picturing Efficiency

Fig. 7 : Another O(n2) algorithm. 19


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
Efficiency of Implementations of ADT List

 For array-based implementation


 Add to end of list O(1)
 Add to list at given position O(n)
 For linked implementation
 Add to end of list O(n)
 Add to list at given position O(n)
 Retrieving an entry O(n)

23
Comparing Implementations

Fig. 10 : The time efficiencies of the ADT list operations


for two implementations, expressed in Big Oh notation

24
Exercise
For each of the following task:
 Adding of 3 real numbers.
 Display all the integers in an array of integers.
 Display the nth integer in an array of integers.
 Compute the total of odd numbers in an array of
integers.
 Compute the sum of all even numbers in an array of
integers.
i. Write a program fragment using Java.
ii. Indicate the time requirement using big-Oh notation in the
worst case
25
Conclusion

Q & A Session

26

You might also like