Chapter - Algorithm Analysis
Chapter - Algorithm Analysis
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
8
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
is the complexity of its body times the number of times the body
16
executes (number of looping).
PICTURING EFFICIENCY
17
Picturing Efficiency
18
19
Q & A Session
23