Chapter 1 - Algorithm Analysis Concept New
Chapter 1 - Algorithm Analysis Concept New
Algorithms Course
Course details Course Objectives
● Course title: Data Structure ● To introduce the analyze
and Algorithms complexity algorithms
● Course code: CoSc 2092 ● To introduce the most common
● Credit hours: 3 ECTS: 5 data structures like linked lists,
stacks, queues, trees, and
● lecture hrs: 2 Lab hrs: 3
graphs
● Tutorial hrs: 0
● To practice sorting, & searching
● Prerequisite: CoSc 1012 on different DS
Computer Programming
● To implement the data
● Course category: Compulsory structures with a chosen
● Year: II Semester: II programming language
DATA STRUCTURES AND
ALGORITHMS
CHAPTER ONE CHAPTER FOUR
Algorithm Analysis Concepts None-Linear Data Structures
CHAPTER THREE
Linear Data Structures
Text Books / Resources Assessment Methods:
● Algorithms and Data % of Final
Structures: The Science of Assessment Grade (100)
Computing by Baldwin/ Assignments 10
Scragg. Charles River Presentation 10
Project 20
Media. 2004.
Tests 20
● Michael T.Goodrich & Final Exam 40
Roberto Tamassia, “Data
Structures and Algorithms in
Java”Wiley.
Chapter One
Algorithm Analysis
Concept
Here starts the
lesson!
Chapter One Contents! Chapter One Contents!
7
Cont…
Main Characteristics of the Algorithm
● Must take an input: Zero or more quantities or externally
supplied.
● Must give some output: At least one quantity must be
produced.
● Definiteness: Each instruction is clear and unambiguous.
● Finiteness: It terminates after a finite number of steps.
● Effectiveness: It must be possible to perform each step exactly
and in a finite amount of time.
● Completeness: It must solve the problem completely.
8
Cont…
● Language Independence: It must not depend on any one
programming language.
● Correctness: It must compute correct answer for all possible
legal inputs.
● Efficiency: It must solve with the least amount of
computational resources such as time and space.
9
Cont…
Analysis of Algorithm
● Algorithm analysis refers to the process of determining the
amount of computing time and storage space required by
different algorithms.
● An algorithm is mainly analyzed to determine the execution
time of the program and how much memory space required.
● Algorithm analysis is required to measure the efficiency of the
algorithm and to see now the execution time varies with size
of the input.
10
Cont…
In computer science, analysis of algorithms is the determination
of the amount of resources (such as time and storage) necessary
to execute them.
Analysis of algorithm is the process of analyzing problem-
solving capability of algorithm in terms of time ,size ,network
consumption, power ,and CPU register.
11
Cont…
Analysis of algorithm is important for the following reasons:
● Analysis is more reliable than the experimental testing.
● Analysis helps to select better algorithm.
● It provides theoretical estimates for resources needed by any
algorithm.
● It provide an insight into reasonable directions to select
better/efficient algorithms.
● Analysis is used to predicts performance (run time of algorithm).
● Analysis identifies the scope of improvement of algorithm.
12
.
Cont…
Performance Analysis:
● The efficiency of the algorithm can be decided by measuring
the performance of an algorithm.
● We can measure the performance of an algorithm by
computing two factors:
I. Amount of space/storage required by an algorithm.
II. Amount of time required by an algorithm.
13
Cont…
Computing best, worst and average case
efficiency
Measuring Measuring
Space complexity input size
Analysis of Algorithm
Measuring Measuring
Time complexity Running time
14
Cont…
I. Space Complexity:
● The space complexity of an algorithm P is the amount of
memory it requires for running the algorithm.
● Or, it determine the approximate memory required to solve a
problem of size n.
● To compute the space complexity we use two factors: constant
and instance characteristics.
● It has two parts, i.e a fixed and variable part.
Thus, S(P) =c+SP
Where c is a constant, and SP is instance characteristics
15
Cont…
● The fixed part component c is independent of instance
characteristics and depends on the space for code constant and
simple variables(Each variable taken as one word!!!) Eg Data
types .
● The variable part component Sp depends upon the space
needed by the component variables, referenced variables and
the recursion stack space.
● This recursion stack space includes the space for the formal
parameters, local variables and the return address.
16
Example1
Algorithm1 abc(a,b)
{ temp=a;
a=b;
b=temp; } i.e. S(p)=3 word or O(1)
Algorithm2 sum()
{
int z=a+b+c;
return(z); } i.e. SP =16 byte
integer data type is 2/4 bytes which depends on compiler
Example2
Algorithm for the sum & product of elements of an array.
Algorithm Totprod (a[], n){ • In this algorithm the
Tot: =0.0; space needed by n, k, Tot
Prod: =1.0;
and Prod is one word for
For k: =1 to n do {
Tot: =Tot +a[k]; each and for the array a[]
Prod: =Prod*a[k]; is at least n words.
} • Thus, SP ≥ n+4
return Tot;
return Prod;
}
18
Example 3
// n is the length of array a[]
int sum(int a[], int n)
{ int x = 0; // 4 bytes for x
for(int i = 0; i < n; i++) // 4 bytes for i
{ x = x + a[i];}
return(x); }
In above code, 4*n bytes of space is required for the array a[] elements.
4 bytes each for x, n, i.
Total memory requirement = 4n + 12, (equation) or n that means linearly
increase in the input value n, hence it is called as linear space complexity
(O(n)).
Cont…
Array elements product-A recursive algorithm
Algorithm RProd (a, n){
if(n=1) then
return a[1];
else
return RProd(a, n-1)*a[n];
}
20
Cont…
● In this algorithm, the instances are characterized by n.
● The internal stack used for recursion includes space for formal
parameters, local variables and return address. The space
required by each cell to function.
● Suppose that the return address requires one word of memory.
Atleast, three words (space for n, space for the return address
and a pointer to a []) are required for each call to RProd.
● Thus, the recursion stack space is greater than or equal to
3(n+1), as depth of recursion is (n+1) (n times call to function
and one return call).
21
Generally, we should always focus on writing algorithm code
in such a way that we keep the space complexity minimum.
i.e. an efficient algorithm take space as small as possible.
22
Cont…
II. Time Complexity:
It is the amount of computer/CPU time required by an
algorithm to run/execute instruction.
It describe how much time the algorithm required to run the
completion.
Amount of time (the number of steps) needed by program to
complete its task (to execute particular algorithm).
A program can take seconds, hours or even years to finish
executing depending on which algorithm it implements.
But ,it is difficult to compute time complexity in terms of
physically clocked time.
23
Cont…
• For instance in multi user/tasking system, executing time
depends on many factors such as:
Number of other programs running
System load
Speed of underlying hardware/CPU type.
Instruction set used
Programming languages and Memory used
Input size and algorithm type used.
24
Cont…
So, time complexity is given in terms of frequency count.
Frequency count is a count denoting the number of
times of execution of statement (step count).
Each execution of program are associated with two
types of time:-
1. Compile Time
Compile time is the time taken to compile an algorithm
using different translator.
Which does not depend upon the instance characteristics.
25
Cont…
2.Run time of the program
Run time of an algorithm is the time taken to execute
compiled program and depend up on the number of
instructions present in the algorithm.
i.e Time T(P) taken by the program is sum of compile time
and run time.
But in this case only measure run/execution time, B/C ones
compile the program execute the number of time.
This execution time can measure using concept of step count.
26
Analysis Rules for Time
This rule can be Informal/static and Formal Approach
1. We assume an arbitrary time unit.
2. For algorithm heading and braces is zero unit of time.
3. Execution of the following operation takes one unit of time:
A. Assignment Operation
B. Single Input/output Operation
C. Single Boolean Operations
D. Single Arithmetic Operations
E. Function Return
4. Running time of selection statement (if, switch) is the time
for condition evaluation + maximum of the running times
for individual clauses in selection.
27
Cont…
5. Running time for a loop is equal to running time for the
statements inside the loop * number of iterations.
6. The total running time of a statement inside a group of nested
loops is the running time of the statements multiplied by the
product of the sizes of all the loops. For nested loops, analyze
inside out. Always assume that the loop executes the maximum
number of iterations possible.
7. Running time of a function call is 1 for setup + the time for any
parameter calculations + the time required for the execution of
the function body.
28
Cont…
Examples 1. selection statement
if (x==0){ 1
The
cout<<“x is zero”; 1 1+1=2 maxim
um one
cout<<“x is one”;} 1
else {
cout<<“x is out of zero”; } 1
29
Cont…
Examples 2. for loop
int count() {
T (n) = 4n+7
int k=0;
cout<< “Enter an integer”;
cin>>n;
for (i=0; i<n; i++)
k=k+1;
return 0;
}
30
Cont…
Time Units to Compute
1 for the function
1 for the assignment statement: int k=0
1 for the output statement.
1 for the input statement.
In the for loop:
1 assignment, n+1 tests, and n increments.
n loops of 2 units for an assignment, and an addition.
1 for the return statement.
------------------------------------------------------
T (n)= 1+1+1+1+(1+n+1+n)+2n+1 = 4n+7
31
Cont…
Examples 3.
int total (int n) {
T (n) = 4n+5
int sum=0;
for (int i=1;i<=n; i++)
sum = sum+1;
return sum;
}
32
Cont…
Time Units to Compute
1 for the function
1 for the assignment statement: int sum=0
In the for loop:
1 assignment, n+1 tests, and n increments.
n loops of 2 units for an assignment, and an addition.
1 for the return statement.
------------------------------------------------------
T (n)=1+1+ (1+n+1+n)+2n+1 = 4n+5
33
Cont…
Examples 4.
voidfunc() {
int x=0, i=0, j=1; T (n) = 5n+6
cout<< “Enter an Integer value”;
cin>>n;
while (i<n){
x++;
i++;
}
while (j<n){
j++;
}
}
34
Cont…
Time Units to Compute
1 for the function
1 for the first assignment statement: x=0;
1 for the second assignment statement: i=0;
1 for the third assignment statement: j=1;
1 for the output statement.
1 for the input statement.
In the first while loop:
n+1 tests
n loops of 2 units for the two increment (addition)
operations
35
Cont…
In the second while loop:
n tests
n-1 increments
------------------------------------------------------
T (n)=1+1+1+1+1+1+n+1+2n+n+n-1 = 5n+6
36
Cont…
Examples 5.
int sum (int n) {
T (n) = 6n+5
int partial_sum = 0;
for (int i=1; i<=n; i++)
partial_sum = partial_sum + (i * i * i);
return partial_sum;
}
37
Cont…
Time Units to Compute
1 for the function
1 for the assignment.
1 assignment, n+1 tests, and n increments.
n loops of 4 units for an assignment, an addition, and two
multiplications.
1 for the return statement.
------------------------------------------------------
T (n) =1+1+ (1+n+1+n) +4n+1 = 6n+5
38
1.2 - Asymptotic Analysis
● To choose the best algorithm, we need to check efficiency of
each algorithm.
● The efficiency can be measured by computing time and space
complexity of each algorithm.
● Asymptotic analysis is the process of calculating the running
time of an algorithm in mathematical units.
● In computer programming, the asymptotic analysis tells us the
execution time of an algorithm.
● We can calculate the best, average, and worst-case scenarios
for an algorithm by using asymptotic analysis.
39
Cont…
How to find time complexity or running time?
● Computing the real running time of a process is not feasible.
The running time to perform any operation depends on the size
of the input.
● Example, traversing an array of 5 elements will take less time
than traversing through an array of 500 elements.
● From example above, we observe that time complexity
depends on the input size.
● Hence, if the input size is ‘n’, then f(n) is a function of ‘n’
denoting the time complexity.
40
Cont…
Analysis of algorithm is concerned on three cases:
● Best-case complexity - it is the function defined by minimum
number of steps taken on any instance of size n.
● Average case complexity - it is the function defined by the
average number of steps taken on any instance of size n.
● Worst-case complexity - it is the function defined by the
maximum number of steps taken on any instance of size n.
41
Cont…
Asymptotic Notations
● Asymptotic notations are expressions that are used to
calculating the running time complexity of an algorithm.
Commonly used asymptotic notations are:
1. Big oh Notation (Upper bound):
● The Big oh notation is denoted by ‘’.
● It is a method of representing the upper bound of algorithm’s
running time.
● Using Big oh notation we can give longest amount of time
taken by the algorithm to complete.
42
Cont…
Definition:
● Let f(n) and g(n) be two non-negative functions.
● The function f(n)=(g(n)) if there exist positive constants c
and n0 such that f(n) c*g(n) for all n n0. Then, we can say
f(n) is big-oh of g(n).
● It is also denoted as f(n) (g(n)).
● In other way f(n) is less than g(n) if g(n) is multiple of some
constant c.
43
Cont…
● Big oh () notation gives an
upper bound for a function to
within a constant factor.
● We write f(n)=(g(n)) if there are
positive constants no and c, such
that to the right of no, the value of
f(n) is always lie on or below
c*g(n).
● Thus, always upper bound of
existing time is obtained by Big
oh notation.
44
Cont…
Example - Prove that 5n2+3n+1 = (n2)
● Solution - Let, f(n)= 5n2+3n+1 and g(n)=n2
f(n) c*g(n)
5n2+3n+1 5n2+3n2+1n2
5n2+3n+1 9n2 when n 1
So n 1 then 5n2+3n+1 9n2
● From the definition f(n)c*g(n) for n 1, Here c=9, no=1 and
g(n)=n2
● Hence, 5n2+3n+1 = (n2) proved.
45
Cont…
2. Omega Notation (Lower bound):
● It is denoted by .
● This notation is used to represent the lower bound of
algorithm’s running time.
● Using omega notation we can denote shortest amount of time
taken by algorithm.
Definition:
● Let f(n) and g(n) be two non-negative functions.
● The function f(n)=(g(n)) if there exist positive constants c
and n0 such that f(n) c*g(n) for all n n0.
46
Cont…
● It is denoted as f(n) (g(n)).
● Omega () notation gives an lower
bound for a function to within a
constant factor.
● We write f(n)=(g(n)) if there are
positive constants no and c, such
that to the right of no, the value of
f(n) is always lie on or above cg(n). no
47
Cont…
Example - Prove that 5n2+3n+1 = (n2)
● Solution - Let f(n)= 5n2+3n+1 and g(n2)
f(n) c*g(n)
5n2+3n+1 1n2 when n 0
So n 0 then 5n2+3n+1 1n2
● From the definition f(n) c*g(n) for n 0, Here c=1, no=0
and g(n)=n2
● Hence, 5n2+3n+1 = (n2) proved.
48
Cont…
3. Theta Notation (Tight bound):
● It is denoted by .
● By this method, the running time is between upper bound and
lower bound.
Definition:
● Let f(n) and g(n) be two non-negative functions.
● The function f(n)=(g(n)) if there exist positive constants c1,
c2 and n0 such that,
c1*g(n) f(n) c2*g(n) for all n n0.
● Then we can say that f(n) (g(n))
49
Cont…
● Theta () notation gives bounds for
a function to within a constant
factor.
● We write f(n)=(g(n)) if there are
positive constants no, c1 and c2,
such that to the right of no, the value
of f(n) is always lie between c1g(n)
and c2g(n) inclusive.
50
Cont…
Example - Prove that 5n2+3n+1 = (n2)
● Solution - Let f(n)= 5n2+3n+1 and g(n)=n2
c1*g(n) f(n) c2*g(n)
1n2 5n2+3n+1 9n2 when n 1
So n 1 then 1n2 5n2+3n+1 9n2
● From the definition c1*g(n) f(n) c2*g(n) for n 1, Here
c1=1, c2=9 no=1 and g(n)=n2
● Hence, 5n2+3n+1 = (n2) proved.
51
Cont…
Properties of order of growth:
1. if f1(n) is order of g1(n) and f2(n) is order of g2(n), then f1(n)
+f2(n) (max(g1(n),g2(n)).
2. Polynomials of degree m(nm) maximum degree is
considered from the polynomial.
For Eg: a1n3+a2n2+a3n+c has the order of growth (n3).
3. (1)<(logn)<()<(n)<(nlogn)<(n2)<(n3)<
…..<(2n)<(3n)<……<(nn).
4. Exponential function an have different orders of growth for
different values of a.
52
Cont…
Key points:
● (g(n)) is a class of functions, f(n) that grows less fast than
g(n), that means f(n) possess the time complexity which is
always lesser than the time complexities of g(n).
● (g(n)) is a class of functions, f(n) that grows at same rate as
g(n).
● (g(n))is a class of functions, f(n) that grows faster than or
atleast as fast as g(n).
53
Cont…
Assignment 1
Consider the following two functions
g1(n) = g2(n) =
Which one of the following are true?
a) g1(n) is (g2(n))
b) g1(n) is (n3)
c) g2(n) is (g1(n))
d) g2(n) is (n)
54
Cont…
Assignment 2
Which of the given options provides the increasing order of
asymptotic complexity of function f1, f2, f3, f4? where
● f1(n)=2n a) f3, f2, f4, f1
● f2(n)=n3/2 b) f3, f2, f1, f4
● f3(n)=nlogn c) f2, f3, f1, f4
● f4(n)=nlogn d) f2, f3, f4, f1
55
END
Thank You!