Chapter # 2 Complexity Analysis
Chapter # 2 Complexity Analysis
Chapter 2
Complexity Analysis
1
2/26/2020
Analysis of Algorithms
Analyzing an algorithm has come to mean predicting the
resources that it requires
Algorithm Efficiency
Time efficiency remains an important consideration when
developing algorithms
Algorithms designed to solve the same problem may differ
dramatically in efficiency
These differences can be much more significant than
differences due to hardware and software
Example
Sequential search vs. Binary search
Next slide …
2
2/26/2020
3
2/26/2020
Analysis Approaches
Basically three approaches can be adopted to analyze
algorithm running time in terms of input size
Empirical Approach
Running time measured experimentally
Analytical Approach
Running time estimated using mathematical modeling
Visualization
Performance is studied through animation for different data
sets
Empirical Approach
The running time of algorithm is measured for different
data sizes and time estimates are plotted against the input
The graph shows the trend
4
2/26/2020
Software environment
Compiler, Programming Language
Analytical Approach
Is a measure that is independent of
The computer, programming language, and complex details
of the algorithm
Usually this measure is in terms of
How many times a basic operation is carried out for each
value of the input size
Strategy
Running time is estimated by analyzing the primitive
operations which make significant contributions to the
overall algorithm time
These may broadly include
Comparing data items
Design and Analysis of Algorithms
5
2/26/2020
Algorithm Specifications
Algorithm is written in
Pseudo Code
Low level to facilitate analysis and implementation
Design and Analysis of Algorithms
Example
Specification Using Natural Language
6
2/26/2020
Example
7
2/26/2020
Algorithm Design
There are many approaches to design an algorithms
Divide-and-Conquer
Greedy
Dynamic Programming
Brute Force
Approximation
Complexity Analysis
Traditionally
The running time of a program described as a function of the
size of the input
For example,
In a sorting problem, the input size is the number of items
n in array
Input size can also contain more than one parameter e.g. for a
graph
The running time of an algorithm on a particular input is the
number of primitive operations or steps executed
The notion (conception) of step should be taken in a machine
independent way
Design and Analysis of Algorithm
8
2/26/2020
Example
As an example,
Take the pseudo code for an algorithm that finds the
maximum element in an array of size n
Problem
Develop an algorithm to find maximum element in an array
of size n
Analyze the algorithm for
Time efficiency
Space efficiency
Correctness
Design and Analysis of Algorithm
9
2/26/2020
Pseudocode
The function FIND-MAX finds the maximum element in an
array.
The array A, of size n, is passed as argument to the function
Primitive Operations
10
2/26/2020
Time Complexity
Time complexity
Three type of time complexity
Best running time (best case running time)
Average running time
Worst running time
11
2/26/2020
Best Case
Best case occurs when the statement is not executed at all
This happen when maximum value is at first position
In this case k = 0
Best (minimum) running time will be
12
2/26/2020
Worst Case
In this case the statement is executed n times
This happen when maximum value lies at the last position
of the array
In this case k = n
The worst case (maximum) time will be
13
2/26/2020
Space Efficiency
Space analysis of algorithm that find maximum element in
the array is simple and straight
Determining the space as a function of Array Size
Space required by the Program Instructions
The amount of storage requirement of the array
Depends on nature of the data
Integers, Floating point, String etc…
Space increases in direct proportion to array size
So
Loop Invariant
Is set of conditions or relationship that is either true or false
during loop execution
Depends on nature of the problem being analyze
14
2/26/2020
Correctness (Cont...)
Loop Invariant Method
The algorithm/loop is correct if we establish the following
Initialization
Loop invariant is true prior to execution of first iteration
Maintenance
Loop invariant is true prior to some iteration, will remain
true before next iteration, till last iteration
Termination
After termination of the loop, the post condition can be
evaluated as true
Correctness (Cont...)
The correctness of FIND-MAX algorithm, will establish by
loop invariant techniques
15
2/26/2020
Correctness (Cont...)
Initialization
Condition requires that the prior the first iteration the statement
S should be true
So; this is trivially (vacuously) true
Because
The max contain 1st element of the array &
Loop start with index 2
Maintenance
Condition requires that if S is true before an iteration of loop, it
should remain true before the next iteration
Can be verified as if max holds the largest value of k element,
then it holds the largest of k+1 element, and so on
Design and Analysis of Algorithm
Correctness (Cont...)
Termination
Condition requires that the post condition should be true
max should return the maximum value of the array
At the end of the array
The loop terminates when j exceeds n
16
2/26/2020
Complexity Analysis
Problem
Adding array element
Algorithm
ARRAY-ADD (A, n) Cost Times
1. result 0 c1 1
2. for ( i 1; i < = n; i++) c2 n+1
3. result result + A[i] c3 n
17
2/26/2020
18
2/26/2020
End
End of chapter
19