Chapter Two Complexity Analysis
Chapter Two Complexity Analysis
07/07/2023 Ataklti N. 1
2.1. Introduction
A program is written in order to solve a problem.
A solution to a problem actually consists of two things:
A way to organize the data
Sequence of steps to solve the problem
There are some basic operations that can be performed on data
structure:-
Accessing/Traversing/update
Searching
Sorting, Merging
Creating/ Inserting
Destroying/ Deleting
07/07/2023 Ataklti N. 2
Abstract Data Type(ADT)
An ADT consists of an abstract data structure and operations. or
ADT is a type(class) for objects whose behavior is defined by a set of value
and set of operation.
There are lots of formalized and standard Abstract data types such as Linked
Lists, Stacks, Queues, Trees, etc.
Properties of an algorithm
Finiteness, Definiteness, Sequence, Correctness, Completeness,
language independent , generality …
Algorithms can be expressed in pseudocode, through
flowcharts or program code
07/07/2023 Ataklti N. 3
2.2. Primitive and Non-primitive Data structures
• The primitive data types are the basic data types that are available in
most of the programming languages.
• The primitive data types are used to represent single values.
• Example is integer, real, Boolean and characters.
• The data types that are derived from primary data types are known as
non-Primitive data types. These data types are used to store group of
values.
• Example Arrays, Structure, linked list, Stacks,Queue
07/07/2023 Ataklti N. 4
2.3. Computational and Asymptotic
Complexity
The field of complexity analysis is concerned with the study of the
efficiency of algorithms.
• Time Complexity: Determine the approximate number of operations
required to solve a problem of size n.
• Space Complexity: Determine the approximate memory required to solve a
problem of size n.
• The factor of time is more important than space.
These differences may not be noticeable for small amounts of data, but as
the size of the input data becomes large, differences will become significant.
It should be clear that we couldn’t use real-time units such as microseconds
to evaluate algorithms efficiency.
A better measure is to use the number of operations required to perform an
algorithm.
07/07/2023 Ataklti N. 5
…continued
Definition 1: The computational complexity of an algorithm
is a measure of the cost incurred by applying the algorithm.
Definition 2: The asymptotic complexity of an algorithm is
an approximation of the computational complexity that
holds for large amounts of input data.
Objectives of computational complexity analysis:
To determine the feasibility of an algorithm by estimating an
upper bound on the amount of work performed
To compare different algorithms before deciding on which one to
implement
07/07/2023 Ataklti N. 6
..continued
Complexity analysis involves two distinct phases:
I. Algorithm Analysis: Analysis of the algorithm to produce a
function T (n) that describes the algorithm in terms of the
operations performed in order to measure the complexity of the
algorithm.
II. Order of Magnitude Analysis: Analysis of the function T (n) to
determine the general complexity category to which it belongs.
There is no generally accepted set of rules for algorithm analysis.
However, an exact count of operations is commonly used.
07/07/2023 Ataklti N. 7
2.3.1. Analysis Rules:
07/07/2023 Ataklti N. 9
2. int sum (int n)
{
int partial_sum = 0;
for (int i = 1; i <= n; i++)
partial_sum = partial_sum +(i * i * i);
return partial_sum;
}
07/07/2023 Ataklti N. 10
…Continued
• Calculate T(n) for the following
• 3. k=0;
Cout<<“enter an integer”;
Cin>>n;
For (i=0;i<n;i++)
K++
• T(n) = 1+1+1+ (1+n+1+ n +n)
=3n+5
07/07/2023 Ataklti N. 11
Cont. …
• 4. i=0;
While (i<n)
{
x++;
i++;
}
J=1;
While(j<=10)
{
x++;
j++
}
• T(n)=1+n+1+n+n+1+11+10+10
07/07/2023 Ataklti N. 12
cont.…
5. for(i=1;i<=n;i++)
for(j=1;j<=n; j++)
k++;
T(n)=1+n+1+n+n(1+n+1+n+n)=3n2+4n+2
07/07/2023 Ataklti N. 13
Cont …
6. Sum=0;
if(test==1)
{
for (i=1;i<=n;i++)
sum=sum+i
}
else
{
cout<<sum;
}
• T(n)=1+1+Max(1+n+1+n+n+n,1)= 4n+4
07/07/2023 Ataklti N. 14
Exercise
• Calculate T(n) for the following codes
A. sum=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
Sum++;
B. sum=0;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
sum++;
07/07/2023 Ataklti N. 15
Cont..
C. int total(int n){
int sum=0; T (n)= 5n+5
for (int i=1;i<=n;i++) T (n)= 4n+4
sum=sum+1;
return sum;}
D. void func(){
int x=0;int i=0;int j=1;
cout<< “Enter value”;
cin>>n;
while (i<n){
x++;
i++;}
while (j<n){
j++;}}
07/07/2023 Ataklti N. 16
Formal Approach to Analysis
• In the previous slide examples we have seen that analysis is so complex. However, it can be simplified by
using some formal approach in which case we can ignore initializations, loop control and increments.
• for Loops: Formally
– In general, a for loop translates to a summation. The index and bounds of the summation are the
same as the index and bounds of the for loop.
for (int i = 1; i <= N; i++) {
sum = sum+i; }
Suppose we count the number of operations that are done. There is 2 operation per iteration of the loop 1 for
assignment and 1 for addition , hence 2N in total.
07/07/2023 Ataklti N. 17
Cont..
• Consecutive Statements: Formally
– Add the running times of the separate blocks of your code
for (int i = 1; i <= N; i++) {
sum = sum+i;}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
sum = sum+i+j;}}
• Conditionals: Formally
– If (test) s1 else s2: Compute the maximum of the running time for s1 and s2.
if (test == 1) {
for (int i = 1; i <= N; i++) {
sum = sum+i;}}
else
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
sum = sum+i+j; }}
07/07/2023 Ataklti N. 18
2.3.2.Asymptotic Analysis
Asymptotic analysis is concerned with how the running time of an algorithm
increases with the size of the input in the limit, or size of the input increases
without bound.
It uses to determine the growth rate of the complexity function.
There are five notations used to describe a running time function. These are:
I. Big-Oh Notation (O)
II. Big-Omega Notation ()
III. Theta Notation ()
IV. Little-o Notation (o)
V. Little-Omega ( notation)
07/07/2023 Ataklti N. 19
Finding Asymptotic Complexity: Examples
• Rules to find Big-O from a given T(n)
– Take highest order term
– Drop lower order terms and constant multiplier
T(n)=4n4+3n3+2n+4=O(n4)
07/07/2023 Ataklti N. 20
Finding Big O of given Algorithm
07/07/2023 Ataklti N. 21
Exercise
07/07/2023 Ataklti N. 22
Cont.….
3. for (i=1; i<=n;i++)
4. for (int i=1; i<=N; ++i)
{
If (i<10) for (int j=i; j>0; --j)
For(j=1;j<=n;j=j*2) {} // do nothing
Cout<<j;
Else
Cout<<i;
}
07/07/2023 Ataklti N. 23
Cont.….
07/07/2023 Ataklti N. 24
Big-O Notation
07/07/2023 Ataklti N. 25
The following points are facts that you can use for Big-Oh problems:
07/07/2023 Ataklti N. 26
Theorem on Big-O notation
Theorem 1: k is O(1)
Theorem 2: A polynomial is O(the term containing the highest power of n).
If f(n) is a polynomial of degree d, then f(n) is O(nd)
In general, f(n) is big-O of the dominant term of f(n).
Theorem 3: k*f(n) is O(f(n))
Constant factors may be ignored
E.g. f(n) =7n4+3n2+5n+1000 is O(n4)
Theorem 4(Transitivity): If f(n) is O(g(n))and g(n) is O(h(n)), then f(n) is
O(h(n)).
Theorem 5: For any base b, logb(n) is O(logn).
All any base logarithms grow at the same rate
Theorem 6: Each of the following functions is big-O of its successors:
K, logbn, n, nlogbn , n2,n3, 2n , n!, nn
07/07/2023 Ataklti N. 27
Example
07/07/2023 Ataklti N. 28
Ω, Θ and Little-o Notations
We have seen that big-O notation refers to an smallest upper
bound on the rate of growth of a function.
07/07/2023 Ataklti N. 31
Little-Omega ( notation)
Little-omega () notation is to big-omega () notation as
little-o notation is to Big-Oh notation.
We use notation to denote a lower bound that is not
asymptotically tight.
Formal Definition: f(n)= (g(n)) if there exists a
constant no>0 such that c. g(n)<f(n) for all n>=k.
Example: 2n2=(n) but it’s not (n).
OO Notation
Definition 7: The function f (n) is OO (g (n)) if it is O
(g(n)) but the constant c is too large to be of practical
significance.
07/07/2023 Ataklti N. 32
2.4. Complexity Classes
07/07/2023 Ataklti N. 33
….Continued
07/07/2023 Ataklti N. 35
Best, average and worst case complexity
Worst case analysis is used to find an upper bound on algorithm
performance for large problems (large n).
We must know the case that causes maximum number of operations to be
executed.
Average Case Analysis
in average case analysis, we take all possible inputs and calculate the average
computing time for all of the inputs.
Best Case Analysis
In the best case analysis, we calculate lower bound on running time of an
algorithm.
We must know the case that causes minimum number of operations to be
executed.
07/07/2023 Ataklti N. 36
2.5. Relational Properties of the
Asymptotic Notations
• Transitivity:
– f(n) = (g(n)) and g(n) = (h(n)) f(n) = (h(n))
– Same for O and
• Reflexivity:
– f(n) = (f(n))
– Same for O and
• Symmetry:
– f(n) = (g(n)) if and only if g(n) = (f(n))
• Transpose symmetry:
– f(n) = O(g(n)) if and only if g(n) = (f(n))
07/07/2023 Ataklti N. 37
2.6. Amortized Analysis
• Amortized analysis computes the average time required to
perform a sequence of n operations on a data structure.
• Example. Priority queue.
07/07/2023 Ataklti N. 38
MORE Exercises
1. An algorithm takes 0.5 ms for input size 100. How long will it take for input size
500 if the running time is the following (assume low-order terms are negligible)
(a) linear
(b) O(N log N )
(c) quadratic
(d) Cubic
2.An algorithm takes 0.5 ms for input size 100. How large a problem can be solved in
1
min if the running time is the following (assume low-order terms are negligible.
07/07/2023 Ataklti N. 39
»I Thank You
07/07/2023 Ataklti N. 40