Professional Documents
Culture Documents
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
4
Introduction
Input to algorithm
5
Motivation
Even a simple program can be noticeably
inefficient
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
8
Measuring Algorithm Efficiency
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
14
Big Oh Notation
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
16
Picturing Efficiency
17
Picturing Efficiency
20
Picturing Efficiency
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)
22
Efficiency of Implementations of ADT List
23
Comparing Implementations
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