Unit 3
Unit 3
Unit 3
3.0 Introduction 1
3.1 Objectives 1
3.2 A Brief Review of Asymptotic Notations 2
3.3 Analysis Of Simple Constructs Or Constant Time 2
3.4 Analysis of Simple Algorithms 4
3.4.1 A Summation Algorithm 4
3.4.2 Polynomial Evaluation Algorithm 5
3.4.3 Exponent Evaluation 10
3.4.4 Sorting Algorithm 17
3.5 Summary 21
3.6 Solutions/Answers 21
3.7 Further Readings
3.0 INTRODUCTION
3.1 OBJECTIVES
1
Complexity Analysis of
Simple Algorithms
The complexity analysis of algorithm is required to measure the time and space
required to run an algorithm. In this unit we focus on only the time required to execute
an algorithm. Let us quickly review some asymptotic notations(Please refer to the
previous unit for detailed discussion)
The central idea of these notations is to compare the relative rate of growth of
functions.
(i) 𝑇(𝑛) = 𝑂(𝑓(𝑛)) if there are two positive constants C and n0 such
that𝑇(𝑛) ≤ 𝐶𝑓 (𝑛)where n ≥ 𝑛0
(ii) 𝑇(𝑛) = Ω(𝑓(𝑛)) if there are two positive constants C and n0 such that
𝑇(𝑛) ≥ CΩ f(n) where n ≥ n0
(iii) 𝑇(𝑛) = 𝜃(𝑓(𝑛)) if and only if 𝑇(𝑛) = O(𝑓(𝑛))𝑎𝑛𝑑𝑇(𝑛) = Ω(𝑓(𝑛))
The second definition, 𝑇(𝑛) = Ω(𝑓(𝑛)) says that the growth rate of T(n) is faster
than or equal to (≥) f(n).
Ex. 𝑖𝑛𝑡 𝑥;
𝑥 = 𝑥 + 5
𝑥 = 𝑥 −5
2) O(n): This is running time of a single looping statement which
includes comparing time, increment or decrement by some constant
value looping statement.
// Here c is a positive integer constant
for (i = 1; i<= n; i += c) {
// simple statement(s)
2
Introduction to Algorithms
}
code fragment of
if – else is
if (condition)
statement 1
else
statement 2
1. int i, tempresult;
2. tempresult =0;
3. for (i=1 ; I <=n; i++)
4. tempresult = tempresult + i * i * i
5. return tempresult;
Line# 3- The for loop has several unit costs: initializingi, cost for testingi<=n
(n+1 unit cost) and cost of incrementing i(1 unit of cost) Total cost is 2n +2
Line# 4- 2units of time for multiplication, 1 unit for addition and one unit of
time for assignment operation in one cycle. Therefore the total cost of this line
is 4n
Line# 5- It will take 1 unit of time. Overall cost will be = 6n+6 which is
written as O(n).
4
Introduction to Algorithms
3.4.2 Polynomial Evaluation
Struct polynomial{
int coefficient;
int exponent;
};
Considerthepolynomial
Bruteforcemethod:
P(x)=15∗x∗x∗x∗x+17∗x∗x∗x−12∗x∗x+13∗x+16
Horner’smethod:
P(x)=(((15∗x+17)∗x−12)∗x+13)∗x+16
Please observe the basic operations are: multiplication, additionand
subtraction. Since thenumberofadditionsandsubtractionsarethesameinboth
the solutions, we will consider the number of multiplications only in worst
case analysis of both the methods.
[Thegeneralformofa polynomialofdegree n,andexpressourresultintermsofn
We’lllookattheworstcase(maximumnumber ofmultiplications) togetanupper
boundonthework]
P(x)=anxn+an-1xn-1+…..+a1x1+a0x0
5
Complexity Analysis of
Simple Algorithms
(i) AnalysisofBruteForce Method
A brute force approach to evaluate a polynomial is to evaluate all terms one by one.
First calculatexn, multiply the value with the related coefficient𝑎𝑛 ,repeat the same
steps for other terms and return the sum
+a2∗x∗x∗+a1∗x+a0
In the first term, it will take n multiplications, in the second term n-1
multiplications, in the third term it takes n-2multiplications….. In the last
two terms: a2∗x∗x∗and a1∗xit takes 2 multiplications and 1
multiplicationaccordingly.
Numberofmultiplicationsneededintheworstcaseis
=n(n+1)/2= O( 𝑛2 )
(ii) AnalysisofHorner’sMethod
P(x)=(…(((an∗x+an−1)∗x+an−2)∗x+...+a2)∗x+a1)∗x+a0
Inthefirstterm,ittakesone multiplication,inthesecondtermone
multiplication,inthethirdtermit takesone multiplication …. . Similarly in all other
terms it will take one multiplication.
AnalysisofHorner’sMethod
Numberofmultiplicationsneeded intheworstcaseis:
T(n) = ∑𝑛𝑖=1 1 = n
T(n)=n
6
Introduction to Algorithms
7. final polynomial value at x isp.
StepII.AlgorithmtoevaluatepolynomialatagivenpointxusingHorner’srule:
Evaluate_Horner(a,n,x)
{
p = A[n];
for (i = n-1; i≤0;i--)
p = p * x + A[i];
return p;
}
follows
At x=3,
p(x) = (3x+5)x+6
p(2)=(9+5).3+6
= (14).3+6
=42+6
=48
Complexity Analysis
First step is one initial assignment that takes constant time i.eO(1).
For loop in the algorithm runs for n iterations, where each iteration cost
O(1) as it includes one multiplication, one addition and one assignment
which takes constant time.
7
Complexity Analysis of
Simple Algorithms
Hence total time complexity of the algorithm will be O(n) for a polynomial of
degree n.
3. Write basic algorithm to evaluate a polynomial and find its complexity. Also
compare its complexity with complexity of Horner’s algorithm.
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
MATRIX (N X N)MULTIPLICATION
Matrix is very important tool in expressing and discussing problems which arise
from real life cases. By managing the data in matrix form it will be easy to
manipulate and obtain more information. One of the basic operations on matrices
is multiplication.
In this section matrix multiplication problem is explained in two steps as we have
discussed GCD and Horner’s Rule in previous section. In the first step we will
brief pseudo code and in the second step the algorithm for the matrix
multiplication will be discussed. This algorithm can be easily coded into any
programming language.
Further explanation of the algorithm is supported through an example.
Let us define problem of matrix multiplication formally, then we will discuss how
to multiply two square matrix of order n x n and find its time complexity. Multiply
two matrices A and B of order nxn each and store the result in matrix C of order
nxn.
8
Introduction to Algorithms
This matrix A has 3 rows and 3 columns.
Step I : Pseudo code: For Matrix multiplication problem where we will multiply two
matrices A and B of order 3x3 each and store the result in matrix C of order 3x3.
1. Multiply first row first element of first matrix with first column first
elementof secondmatrix.
2. Similarly perform this multiplication for first row of first matrix and
firstcolumn of second matrix. Now take the sum of thesevalues.
3. The sum obtained will be first element of product matrixC
4. Similarly Compute all remaining element of product matrix
C.
C= A x B
Step II : Algorithm for multiplying two square matrix of order n x n and find the
product matrix of order n x n
Matrix_Multiply(A,B,C,n)
{
1 2 3
A= 2 3 4
4 5 6
1 1 1
B= 2 3 2
3 2 1
= 14 13 8
20 19 12
9
Complexity Analysis of
32 31 20
Simple Algorithms
Complexity Analysis
First step is, for loop that will be executed n number of times i.e it will take O(n)
time. The second nested for loop will also run for n number of time and will take
O(n) time.
Assignment statement inside second for loop will take constant time i.eO(1) as it
includes only one assignment.
The third for loop i.e innermost nested loop will also run for n number of times
and will take O(n ) time . Assignment statement inside third for loop will cost
O(1) as it includes one multiplication, one addition and one assignment which
takes constant time.
Hence, total time complexity of the algorithm will be O(n 3) for matrix multiplication
of order nxn.
1. Writeaprogramin‘C’tofindmultiplicationoftwomatricesA[3x3]andB[3x3].
………………………………………………………………………………………….
………………………………………………………………………………………….
………………………………………………………………………………………….
3.4.3 EXPONENTEVALUATION
Sincethenumber𝑛has exactly⌊log2n⌋+1digitsinbase2,weonlyneedtoperform
𝑂(log𝑛)multiplications,ifweknowthepowers𝑎1,𝑎2,𝑎4,𝑎8,…,𝑎⌊log𝑛⌋.
The following example illustrates the intermediate steps in binary exponentiation.
Every subsequent multiplication is just the square of the previous multiplication
41=4
42=(41)2=42=16
44=(42)2=162=256
48=(44)2=2562=65,536
Therefore the final answer for 411, we only need to multiply three of them (skipping
10
Introduction to Algorithms
44becausethe correspondingbitin𝑛is set to zero):411=65,536⋅16⋅4=4,194,304.
Thetimecomplexityofthis
algorithmis(log𝑛):tocompute𝑙𝑜𝑔𝑛powerof𝑎andthentodoalmostlog𝑛multiplicationstoge
tthefinalresult fromthem.
Computing xn at some point x = a i.e an tends to brute force multiplication of
a by itself n-1 times. So. To reduce the number of multiplication binary
exponentiation methods to compute xn will be discussed. Processing of
binary string for exponent n to compute xn can be done by following
methods:
left to right binary exponentiation
1. result=a
2. for i=s-2 to0
3. result = result *result
4. if A[i]= 1then
5. result= result *a
6. return result (i.e an)
Iteration 1:
i=3
11
Complexity Analysis of
Simple Algorithms
result=a *a= a2
A[3] ≠ 1
Iteration 2:
i=2
result= a2 * a2 = a4
A[2] ≠ 1
Iteration 3:
i=1
result= a4 * a4 = a8
A[1] ≠ 1
Iteration 4:
i=0
result= a8 * a8 = a16
A[0] = 1
result = a16 * a = a17
return a17
Hence
1. Set x =a
2. if A[0]= 1 then set result=a
3. else set result=1
4. Initializei=1
5. compute x = x *x
6. if A[i] = 1 then compute result = result *x
7. Increment i by 1 as i=i+1 and if i is less than equal to s-1 then go to step4.
12
Introduction to Algorithms
8. return computed value asresult.
1. x=a
2. if A[0]=1then
3. result =a
4. else
5. result=1
6. for i= 1 tos-1
7. x= x * x
8. ifA[i]=1
9. result= result *x
10. return result (i.e an)
Step by step illustration of the right to left binary exponentiation algorithm for a17 :
s=5, the length of binary string of 1’s and 0’s for exponent n
Iteration 1
i=1
x=a *a=
a2
A[1] ≠
1
Iteration 2
i=2
x= a2 * a2 = a4
A[2] ≠ 1
Iteration 3
i=3
x= a4 * a4 = a8
A[3] ≠ 1
Iteration 4
i=4
x= a8 * a8 = a16
A[4] = 1
result = result * x = a * a16 = a17
return a17
13
Complexity Analysis of
Simple Algorithms
In this example total number of multiplication is 5 instead of 16 multiplications in
brute force algorithm i.e n-1
From the above discussion we can conclude that the complexity for left to right
binary exponentiation and right to left binary exponentiation is logarithmic in terms
of exponent n.
1. Compute a283 using left to right and right to left binary exponentiation.
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
Linear Search
Linear_ Search( A[ ], X)
Step 1: Initialize i to 1
Step 2: if i exceeds the end of an array then print “element not found” and Exit
Step 3: if A[i] = X then Print “Element X Found at index i in the array”and
Exit
Step 4: Increment i and go to Step 2
We are given with a list of items. The following table shows a data set for
linear search:
7 17 3 9 25 18
In the above table of data set, start at the first item/element in the list and
compared with the key. If the key is not at the first position, then we move
14
Introduction to Algorithms
from the current item to next item in the list sequentially until we either find
what we are looking for or run out of items i.e the whole list of items is
exhausted. If we run out of items or the list is exhausted, we can conclude
that the item we were searching from the list is not present.
In the given data set key 25 is compared with first element i.e7 , they are not
equal then move to next element in the list and key is again compared with
17 , key 25 is not equal to 17. Like this key is compared with element in the
list till either element is found in the list or not found till end of the list. In
this case key element is found in the list and search is successful.
Let us write the algorithm for the linear search process first and then
analyze its complexity.
{
found=false // found is a boolean variable which will store either true or
false
for(i=0;i<n;i++)
{
if (a[i]==key)
found = true
break;
}
if (i==n)
found =
false
return found
}
For the complexity analysis of this algorithm, we will discuss the following
cases:
Best Case: The best case - we will find the key in the first place we look, at the
15
Complexity Analysis of
Simple Algorithms
beginning of the list i.e the first comparison returns a match or return found as
true. In this case we only require a single comparison and complexity will be
O(1).
Worst Case: In worst case either we will find the key at the end of the list or
we may not find the key until the very last comparison i.enth comparison.
Since the search requires n comparisons in the worst case, complexity will be
O(n).
Average Case: On average, we will find the key about halfway into the list;
that is, we will compare against n/2 data items. However, that as n gets larger,
the coefficients, no matter what they are, become insignificant in our
approximation, so the complexity of the linear search, is O(n). The average
time depends on the probability that the key will be found in the collection -
this is something that we would not expect to know in the majority of cases.
Thus in this case, as in most others, estimation of the average time is of little
utility.
Most of the times an algorithm run for the longest period of time as defined in
worst case. Information provide by best case is not very useful. In average
case, it is difficult to determine probability of occurrence of input data set.
Worst case provides an upper bound on performance i.e the algorithm will
never take more time than computed in worse case. So, the worst-case time
analysis is easier to compute and is useful than average time case.
3.4.4 SORTING
16
Introduction to Algorithms
- Internal Sort: - Internal sorts are the sorting algorithms in which the
complete data set to be sorted is available in the computer’s main memory.
- External Sort: - External sorting techniques are used when the collection
of complete data cannot reside in the main memory but must reside in
secondary storage for example on a disk.
In this section we will discuss only internal sorting algorithms. Some of the
internal sorting algorithms are bubble sort, insertion sort and selection sort.
For any sorting algorithm important factors that contribute to measure their
efficiency are the size of the data set and the method/operation to move the
different elements around or exchange the elements. So counting the
number of comparisons and the number of exchanges made by an algorithm
provides useful performance measures. When sorting large set of data, the
number of exchanges made may be the principal performance criterion,
since exchanging two records will involve a lot of time.
Bubble Sort
First Pass
23 18 15 37 8 11
18 23 15 37 8 11
18 15 23 37 8 11
18 15 23 37 8 11
18 15 23 8 37 11
18 15 23 8 11 37
Second Pass
18 15 23 8 11 37
15 18 23 8 11 37
15 18 23 8 11 37
15 18 8 23 11 37
15 18 8 11 23 37
15 18 8 11 23 37
15 18 8 11 23 37
15 8 18 11 23 37
15 8 11 18 23 37
Third Pass
15 8 11 18 23 37
17
Complexity Analysis of
Simple Algorithms 8 15 11 18 23 37
8 11 15 18 23 37
Fourth Pass
8 11 15 18 23 37
8 11 15 18 23 37
Fifth Pass
8 11 15 18 23 37
In this the given list is divided into two sub list sorted and unsorted. The
largest element is bubbled from the unsorted list to the sorted sub list. After
each iteration/pass size of unsorted keep on decreasing and size of sorted
sub list gets on increasing till all element of the list comes in the sorted list.
With the list of n elements, n-1 pass/iteration are required to sort. Let us
discuss the result of iteration shown in above tables.
In pass 1, first and second element of the data set i.e 23 and 18 are compared
and as 23 is greater than 18 so they are swapped. Then second and third
element will be compared i.e 23 and 15, again 23 is greater than 15 so
swapped. Now 23 and 37 is compared and 23 is less than 37 so no swapping
take place. Then 37 and 8 is compared and 37 is greater than 8 so swapping
take place. At the end 37 is compared with 11 and again swapped. As a result
largest element of the given data set i.e 37 is bubbled at the last position in the
array. At each pass the largest element among the remaining elements in the
unsorted array bubbles up towards the sorted part of the array as shown in the
table above. This process will continue till n-1 passes.
The first version of the algorithm for above sorting method is as below:
Bubble Sort Algorithm- Version1
int i,j
for ( i= 1 to n-1
for (j = 0 to n-2
{
if (A[j]>A[j+1])
{
// swapping of two adjacent elements of an array A
exchange (A[j], A[j+1])
}
}
}
T(n)= O(𝑛2 )
Let us reanalyze the above algorithm to improve the running time algorithm
further. From the example it is visible that the Bubble sort algorithm divides
the array into unsorted and sorted sub-arrays. The inner loop rescans the
sorted sub- array in each cycle, although there will not be any exchange of
adjacent elements. The modified version (version 2) of the algorithm
overcomes this problem:
Version -2
int i,j
for ( i= 1 to n-1)
for (j = 0 to n-i-1)
{
if(A[j]>A[j+1])
{
// swapping of two adjacent elements of an array A
exchange(A[j], A[j+1])
}
}
}
There will be no change in the number of iterations i.e. n-2 iterations in the
first pass, but in the second pass it will be n-3, in the third pass it will be n-4
iterations and so on. In this case too, the complexity remains to be O(𝑛2 ) but
the number of exchange operations will be less. This requires further
improvement of the algorithm.
In some cases there is no need of running n-1 passes in the outer loop. The
array might be sorted in less than that. The following is the modified version
(Version -3) of the algorithm
19
Complexity Analysis of
Simple Algorithms function Bubble Sort(A,n)
int i,j
for ( i= 1 to n-1)
{
flag = 0;
for (j = 0 to n-i-1)
{
if(A[j]>A[j+1])
{
// swapping of two adjacent elements of an array A
exchange( A[j], A[j+1])
flag = 1;
}
if (flag = = 0)
exit;
}
}
In case there is no swapping, flag will remain set to 0 and the algorithm will
stop running.
Time Complexity
In the modified algorithm, the inner loop will execute at least once to verify
that the array is sorted but not (n-i-1) times.Therefore the time complexity
will be:
T(n) = C* (n-1)
= O(n)
3.5 SUMMARY
20
Introduction to Algorithms
exponentiation is illustrated. Time complexity of these algorithms to compute
xn is O(log n).
Different versions of bubble sort algorithm are presented and its performance
analysis is done at the end.
3.6 SOLUTIONS/ANSWERS
P(x)=anxn+an-1xn-1+…..+a1x1+a0x0
Iteration 1,
poly = x * 0 + a[4] = 3
Iteration 2,
poly = x * 3 + a[3] = 2 * 3 + 2 = 6 +2 = 8
Iteration 3,
poly = x * 8 + a[2]
= 2 * 8 + 0 = 16 + 0 = 16
Iteration 4,
poly = x * 16 + a[1]
= 2 * 16 + (-5) = 32 -5 = 27
Iteration 5,
poly = x * 27 + a[0]
= 2 * 27 + 7 = 54 + 7 = 61
function(a[n], n, x)
{
poly = 0;
21
Complexity Analysis of
Simple Algorithms
result =1;
for (j=0; j<i; j++)
{
result= result * x;
}
}
return poly.
}
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k,sum=0;
{
printf("\n");
for (j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
22
Introduction to Algorithms
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Check Your Progress 3
Right to left binary exponentiation for a 283is as follows: n=283, binary equivalent to
binary string 100011011, s=9 (length of binary string)
result = a (since A[0]=1)
23