Analysis of Algorithm-Mmr1
Analysis of Algorithm-Mmr1
Raghuwanshi
Ph. D. (Computer Sc.) VNIT, Nagpur
M.Tech. (Computer Sc. & DP) IIT, Kharagpur
Director and Professor, Symbiosis Institute of Technology,
Nagpur
Email: [email protected]
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
490 2468
122 9872
61 19744 19744
30 39488
1210554
This algorithm is called as multiplication
á la russe, resembles the one used in
the hardware of a binary computer
THE CLASSIC MULTIPLICATION ALGORITHM
Multiplication, the American way: int american(){
int n1=981, n2=1234, prod=0;
int i=1,d;
while(n2){
d=n2%10;
n2=n2/10;
prod+=(n1*d*i);
i*=10;
}
return prod;
}
int english(){
int n1=981, n2=1234, prod=0;
Multiplication, the English way: int n=n2,i=1,d;
while(n){
n=n/10;
i*=10;
}
while(i!=1){
i=i/10;
d=n2/i;
prod=prod+(d*n1*i);
//printf("d=%d\ti=%d\tprod=%d\n",d,i,prod);
n2=n2%i;
}
return prod;
}
ALGORITHM
Definiteness: Each instruction is clear and unambiguous (e.g add salt to suit your test and
add 1 teaspoon salt).
Finiteness: if we trace out the instructions of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
(Program may or may not be finite)
Effectiveness: Every instruction must be very basic so that it can be carried out, in
principle, by a person using only pencil and paper.
PERFORMANCE ANALYSIS OF ALGORITHM
Memory and Processor are the two important part of Computer System
REASON FOR STUDY OF ALGORITHM
Answer : YES
We need to demonstrate
definiteness,
finiteness and
effectiveness
of offered solutions that is algorithms
Analysing algorithm means predicting the resources that the algorithm required.
They are mathematical entities, which can be thought of as running on some sort of
idealized computer with an infinite random access memory and an unlimited word size
(with sufficient computation power)
Once the model of computation has been defined, an algorithm can be describe using a
simple language (or pseudo language) whose syntax is close to programming language
such as C or java.
RAM MODEL
The most natural measure for instance size is the number of items in the instance but the
best measure of instance size is the total number of bits needed (e.g. instead of talking
about multiplication of two numbers we should talk about number of digits in number) to
represent the instance.
#include<stdio.h>
#define pi 3.14
int main()
{
float dia, area, perimeter;
printf("Eneter Diameter of a Circle:");
scanf("%f",&dia);
area=pi*(dia/2)*(dia/2);
perimeter=2*pi*(dia/2);
printf("Area:%5.2f\tPerimeter:%5.2f\n",area, perimeter);
return 0;
}
FOR EXAMPLE
Algorithm to sum members of a list
Statement Step frequency Total steps
1. Algorithm Sum(list, n) 0 -- 0
2. { 0 -- 0
3. sum=0.0; 1 1 1
4. for i = 1 to n do 1 n+1 n+1
5. sum=sum + list[i]; 1 n n
6. return sum; 1 1 1
7. } 0 -- 0
Step Count 2n + 3
Best-case: Element to be search present at first position (minimum number of steps that
can be executed) O(1)
Worst-case: Element to be search present at last position or not in the list (maximum
number of steps that can be executed) O(n)
Average-case: Element to be search is in the list (average number of steps executed on
instances). O(n)
Worst-case: Element to be search is at extreme ends or not in the list. (maximum number
of steps that can be executed) O(log2n)
Average-case: Element to be search is in the list (average number of steps executed on
instances).
#define N 5
int list[N]={2,4,6,8,11};
int main(){
int num,mid,lb=0,ub=N-1;
printf("Enter number to be serach:");
scanf("%d",&num);
while(lb<=ub){
//printf("num=%d\tub=%d\tlb=%d\tmid=%d\n",num, ub,lb,mid);
mid=(ub+lb)/2;
if (num == list[mid]){
printf("%d is at position %d\n", num, mid);
return mid;
}
else
if (num < list[mid])
ub=mid-1;
else
lb=mid+1;
}
printf("%d is not in list\n",num);
return -1;
}
LOGNORMAL ALGORITHM
#define n 64
int main(){
int i=1, count=0;
while(i<n){
i=i*2;
count++;
printf("n=%d\ti=%d\tcount=%d\n", n,i, count);
}
}
int main(){
int i, n=10,j,count;
for(j=1; j<=n; j++){
i=1;
count=0;
while(i<j*8){
i=i*2;
count++;
}
printf("j=%d\ti=%d\tcount=%d\n", j,j*8, count);
}
}
RUNNING TIME OF AN ALGORITHM
Let us take Multiplication of Two N-digits Numbers
9 8 2 3 9 8 8 7 6 5 4 3 2 1
X X X
1 2 1 2 4 5 1 2 3 4 5 6 7 8
1 9 6 1 1 9 9 0 7 0 1 2 3 4 5 6 8
9 8 9 5 9 2 6 1 3 5 8 0 2 4 7
4 7 9 6 5 2 5 9 2 5 9 2 6
1 1 7 6
2 3 9 8 4 3 8 2 7 1 6 0 5
3 5 0 6 1 7 2 8 4
2 9 8 5 5 1 0
2 6 2 9 6 2 9 6 3
1 7 5 3 0 8 6 4 2
8 7 6 5 4 3 6
2 1
1 0 8 2 1 5 2 0 2 2 3 7 4 6 3 8
N 2 4 8 n
Multiplication 4 16 64 n2 Quadratic Growth
Additions 3 7 15 2*n-1 Linear Growth
The running time of an algorithm typically grows with the instance size.
ORDER OF GROWTH OF ALGORITHM
To analyse the efficiency of an algorithm we are interested in analysing how the running
time increases when the input size increases.
When two algorithms are compared with respect to their behaviour (logarithmic, linear,
quadratic, cubic, exponential etc) for the large input sizes, a very useful measure is called
order of growth
GROWTH OF RUNNING TIME
1E+30
1E+28 Cubic
1E+26
1E+24 Quadratic
1E+22
Linear
1E+20
1E+18
T (n )
1E+16
1E+14
1E+12
1E+10
1E+8
1E+6
1E+4
1E+2
1E+0
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
Complexity classes
n! 2n n3
n2
f(n)
n log n
n (linear time)
log n
n
NEED FOR NOTATIONS
2. The main purpose of the analysis algorithm is to get a sense for the trend in the
algorithm’s running time. (An exact analysis is probably best done by implementing the
algorithm and measuring CPU seconds).
3. we need a language (notations) that will allow us to say that the computing time, as a
function of n, grows `on the order of n3,' or `at most as fast as n3,' etc
let f(n) and g(n) as the running times of two algorithms on inputs of size n. we say that f(n)
is in the order of g(n) if f(n) is bounded above by a positive real multiple of g(n) for all
sufficiently large n.
maximum rule: The time taken by algorithm is logically sum of the time taken by its disjoint
parts but it is in the order of the time taken by its most time-consuming part
Asymptotic notation provides us with a way to simplify the functions that arise in analysing
algorithm running times by
• Ignoring constant factors and
• Concentrating on the trends for large values of n.
EXAMPLE
1000
n 2n+3 n2 n3 n4 2n
0 3 0 0 0 1
1 5 1 1 1 2
2 7 4 8 16 4
2n+3
3 9 9 27 81 8
4 11 16 64 256 16
n2
5 13 25 125 625 32
n3
6 15 36 216 1296 64
500 7 17 49 343 2401 128
n4
8 19 64 512 4096 256
2n
f(n) = O(n2) for c=1 & n0=3
When algorithm contains a repetitive call to itself or some algorithm, its running time can be
describe by a recurrence or recurrence relation
• Check whether the number of times the basic operation is executed may vary on different
inputs of the same size.
(If it may, the worst, average, and best cases must be investigate separately.)
• Set up a recurrence relation with an appropriate initial condition expressing the number of
times the basic op. is executed.
• Solve the recurrence (or, at the very least, establish its solution’s order of growth)
METHODS TO SOLVE RECURRENCE
Types of recurrence
Homogeneous linear
Inhomogeneous linear
Homogeneous non-linear
Inhomogeneous non-linear
THE SORTING PROBLEM
Input: The sequence of n numbers (a1, a2, …….an)
Output: A permutation (reordering) (a’1, a’2,……a’n) of the input sequence such that a’1≤ a’2≤ ………≤a’n.
Selection Sort:
Logic: find the largest element in list and its position and swap it with the first element, then find second
largest element and its position and swap with the second element, and so on.
SELECTION SORT
Logic: find the largest element in list and its position and swap it with the first element, then find second
largest element and its position and swap with the second element, and so on.
14579 find the largest element & its position and swap it with first element.
94571 Total 4 Comparisons and 1 swap
94571 find the 2nd largest element & its position and swap it with 2nd element
97541 Total 3 Comparisons and 1 swap
97541 find the 3rd largest element & its position and swap it with 3rd element
97541 Total 2 Comparisons and 1 swap
97541 find the 4th largest element & its position and swap it with 4th element
97541 Total 1 Comparison and 1 swap
for(j=0; j<N-1;j++){ n
big=list[j]; n-1
pos=j; n-1
for (i=j+1; i<N; i++){ n2 (n-1)+(n-2), …..3+2+1= n(n-1)/2
if(list[i]> big){ n2
big=list[i]; -
pos=i; -
}
}
-
Comparisons≈n2
Swaps=1
-
-
//printf("big=%d\tpos=%d\n",big,pos); - Time complexity= O(n2)
list[pos]=list[j]; n-1
(Best case & worst case)
list[j]=big; n-1
}
}
for(j=0; j<N-1;j++){ n
for (i=j+1; i<N; i++){ n2
(n-1)+(n-2), …..3+2+1= n(n-1)/2
if(list[i]> list[j]){ n2
temp=list[j]; n2
list[j]=list[i]; n2
list[i]=temp; n2
} -
} - Comparisons≈n2
//printf("big=%d\tpos=%d\n",big,pos);
}
-
Swaps≈n2
}
-
Time complexity= O(n2)
(Best case & worst case)
}
if (!swapped)
It is possible to have Best case Time complexity= O(n) break;
//print_list();
}
}
INSERTION SORT
Logic: Select the key and make space and place it at proper position in the
sorted list of previous elements.
Table Comparison of Quick sort with insertion sort and Merge sort
Insertion sort Quick sort Merge sort