Algorithm Analysis and Design: Aad Cse Srm-Ap 1
Algorithm Analysis and Design: Aad Cse Srm-Ap 1
Algorithm Analysis and Design: Aad Cse Srm-Ap 1
Analysis
Why not just measure processing time?
Time vs. space complexity analyses
Upper and lower limits
5
Origin of the word 'Algorithm'
Algos means pain in Greek.
Algor in Latin means to be cold.
Therefore, neither is the root for the word \algorithm"!.
Algorithm stems from Abu Jaafar Mohammad Ibn Mousa Al-
Khwarizmi, the name of the 9th-century Arab scholar.
The Hindu-Arabic or Indo-Persian numerals originated from
India. It was invented between the 1st and 4th centuries by
Indian mathematicians.
It was re-introduced in the book On the Calculation with Hindu
Numerals written by Al-Khwarizmi.
Al-Khwarizmi's name was formerly Latinized as Algoritmi, led to
the term algorithm.
AAD CSE SRM-AP 6
What is an Algorithm?
Algorithm stems from al-Khwarizmi, the name of the
9th-century Arab scholar who stressed the
importance of methodical procedures for solving
problems.
A step-by-step procedure for solving well-posed
problem.
A sequence of computational steps that transform
the input into output.
An effective method expressed as a finite list of well-
defined instructions for calculating a function.
A finite set of instructions that, if followed,
accomplishes a particular task.
AAD CSE SRM-AP 7
What is algorithm?
• We need a well-specified problem that tells
what needs to be achieved
• Algorithm solves the problem
• It consists of a sequence of commands that
takes an input and gives output
8
Properties of Algorithms
An algorithm is a step-by-step procedure for solving a problem
in a finite amount of time.
1. Input
2. Output
3. Finiteness
4. Definiteness
5. Effectiveness
Running Time
80
with the input size.
60
Average case time is often
difficult to determine. 40
running time. 0
1000 2000 3000 4000
Easier to analyze
Input Size
Crucial to applications such as
games, finance and robotics
algorithm 7000
Time (ms)
inputs of varying size and 5000
composition 4000
Use a method like 3000
System.currentTimeMillis() to
2000
get an accurate measure
of the actual running time 1000
Return value
return expression
Expressions
← Assignment (like = in C)
23
Complexity
• The computational complexity, or simply complexity of
an algorithm is the amount of resources required for
running it.
• What are the resources an algorithm requires?
• CPU Time
• Memory, etc.
• Time complexity:
• How much time it takes to compute
• Measured by a function T(N)
• Space complexity:
• How much memory it takes to compute
• Measured by a function S(N) 24
Theoretical Analysis
Uses a high-level description of the algorithm
instead of an implementation
Characterizes running time as a function of the
input size, n.
Takes into account all possible inputs
Allows us to evaluate the speed of an algorithm
independent of the hardware/software
environment
An potentially unbounded
bank of memory cells, 2
1
each of which can hold an 0
arbitrary number or
character
Memory cells are numbered and accessing
any cell in memory takes unit time.
• Count Primitive Operations = time needed by RAM model
AAD CSE SRM-AP 28
Simple Example - Steps
29
Counting Primitive
Operations
By inspecting the pseudocode, we can determine the
maximum number of primitive operations executed by
an algorithm, as a function of the input size
Algorithm arrayMax(A, n)
# operations
currentMax A[0] 2
for i 1 to n 1 do 1+ n
if A[i] currentMax then 2(n 1)
currentMax A[i] 2(n 1)
{ increment counter i } 2(n 1)
return currentMax 1
Total 7n 2
AAD CSE SRM-AP 30
A[]={5,8,9,10,11,15,16,18,19,20
Estimating Running Time
Algorithm arrayMax executes 7n 2
primitive operations in the worst case.
Define:
a = Time taken by the fastest primitive
operation
b = Time taken by the slowest primitive
operation
Let T(n) be worst-case time of arrayMax.
Then
a (7n 2) AADT(n) b(7n 2)
CSE SRM-AP 31
Counting Primitive
Operations
By inspecting the pseudocode, we can determine the
maximum number of primitive operations executed by
an algorithm, as a function of the input size
Algorithm arrayMax(A, n)
# operations
currentMax A[0] 2
for i 1 to n 1 do 1+ n
if A[i] currentMax then 2(n 1)
currentMax A[i] 0
{ increment counter i } 2(n 1)
return currentMax 1
Total 5n
AAD CSE SRM-AP 32
A[]={20,5,8,9,10,11,15,16,18,19
Best, Worst, and Average-
Case Complexity
int search(int arr[], int n, int x) Worst Case Analysis: we
{ calculate upper bound on
running time of an algorithm.
int i;
Average Case Analysis:
for (i=0; i<n; i++) we take all possible inputs
{ and calculate computing time
if (arr[i] == x) for all of the inputs.
return i; Best Case Analysis: we
} calculate lower bound on
running time of an algorithm.
return -1;
}
=4n+2
Algorithm prefixAverages2 runs in O(n) time
Analysis of Algorithms 36
General Guidelines
The worst-case instructions determine
worst-case behaviour, overall. (eg.
A single n2 statement means the whole
algorithm is n2.)
Instant recognition:
Assignments/arithmetic is O(1),
Loops are O(n),
Two nested loops are O(n2).
How about three nested loops?
Analysis of Algorithms 37
Matrix-Multiplication