Unit 1
Unit 1
Introduction:-
What is an Algorithm?
Performance Analysis:
-Space Complexity & Time Complexity
Asymptotic notations
Divide and Conquer :-
General method
Binary search
Finding Maximum and Minimum
Merge sort
Quick sort
Strassen’s Matrix Multiplication
What is an Algorithm?
An algorithm is a finite set of instructions that, if followed, accomplishes a
particular task.
Characteristics of an Algorithm:
1) Input: Zero or more quantities are externally supplied.
2) Output: At least one quantity is produced.
3) Definiteness: Each instruction is clear and unambiguous.
4) Finiteness: If we trace out the instructions of an algorithm, then for all
cases, the algorithm terminates after a finite number of steps.
5) Effectiveness: Every instruction must be basic enough to be carried out,
in principle, by a person using only pencil and paper. It must be feasible.
What is Pseudo code?
Pseudo code is an informal way of programming description that
does not require any strict programming language syntax or
underlying technology considerations.
It is used for creating an outline or a rough draft of a program.
Pseudo code summarizes a program's flow, but excludes
underlying details.
What is Pseudo code?
[Pseudo code for printing sum of n natural numbers]
begin
Integer counter, sum=0
for counter=1 to 100 step by 1 do
sum=sum + counter
end for
print sum
end
Performance Analysis
The performance analysis is a process of estimating the
efficiency of an algorithm.
There are two fundamental parameters based on which we can
analyze the algorithm:
Space Complexity: The total amount of memory required by
an algorithm to complete its execution expressed as a function
of number of inputs.
Time Complexity: The total amount of time required by an
algorithm to complete its execution expressed as a function of
number of inputs.
Space Complexity
When we design an algorithm to solve a problem, it needs some
computer memory to complete its execution.
For any algorithm, memory is required for the following
purposes...
Memory required to store program instructions
Memory required to store constants
Memory required to store variables
And for few other things (functions, dynamic allocation,
arguments of recursive calls, return address)
O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n)
Asymptotic Notations
The word Asymptotic means approaching a value or curve
arbitrarily closely.
Asymptotic notations are the mathematical notations used to describe
the running time of an algorithm when the input tends towards a
particular value or a limiting value.
For example: In bubble sort, when the input array is already sorted, the
time taken by the algorithm is linear i.e. the best case.
But, when the input array is in reverse condition, the algorithm takes the
maximum time (quadratic) to sort the elements i.e. the worst case.
When the input array is neither sorted nor in reverse order, then it takes
average time. These are denoted using asymptotic notations.
There are mainly three asymptotic notations:
Big-O notation
Omega notation
Theta notation
Asymptotic Notations
Asymptotic analysis is a technique of representing limiting
behavior. The methodology has the applications across
science. It can be used to analyze the performance of an
algorithm for some large data set.
T(n)= T(n/2) + 1
T(n/2)=T(n/4)+1
T(n)=T(n/4)+2
T(n)=T(n/2k)+k
Recursion will stop If n=2k then T(1)=1
T(n)=T(1)+k
T(n)=1+log2n
T(n)=log2n
Finding Max and Min in Array
Method 1: if we apply the general approach to the array of size
n, the number of comparisons required are 2n-2.
Algorithm: Max and Min Element (a [])
Max= a [1]
Min= a [1]
For i= 2 to n do
if a[i]> max then max = a[i]
else if a[i] < min then min= a[i]
print(max, min)
Method-2: In another approach, we will divide the problem
into sub-problems and find the max and min of each group.
Now max of each group will compare with the only max of
another group and min with min.
Divide and Conquer Method
Max1=Min1=a[mid+1];
return(Max,Min);
Max and Min Problem - Analysis
T(n)= T(n/2) + T(n/2) + 2 (if n>2)
=1 (if n=2)
T(n)= 2T(n/2) + 2
T(n/2)=2T(n/4)+2
T(n)=2[2T(n/4)+2 ]+2
T(n)=22T(n/22)+22+2
T(n)=2kT(n/2k)+2k+2k-1+.....+2
Recursion will stop if n/2k =2 then T(2)=1
T(n)=2kT(2)+2(2k-1)/(2-1) [GP: a(rn-1)/(r-1)]
T(n)=2k+2(2k-1)=n/2+n-2=3n/2-2
T(n)=(3n/2)-2
Merge Sort
The Merge Sort function keeps on splitting an array into two
halves until a condition is met where we try to perform Merge
Sort on a sub array of size 1.
And then, it combines the individually sorted sub arrays into
larger arrays until the whole array is merged.
Algorithm MERGESORT (low, high)
{ // a (low : high) is an array to be sorted.
if (low < high)
{
mid = (low + high)/2 //finds where to split the set
call MERGESORT(low, mid) //sort one subset
call MERGESORT(mid+1, high) //sort the other subset
call MERGE(low, mid, high) // combine the results
}
}
Merge Sort
Algorithm MERGE (low, mid, high)
{ k=low; i := low; j:= mid + 1;
while ((i < mid) and (j < high)) do
{ if (a[i] < a[j]) then { b[k] := a[i]; i := i + 1;}
else { b[k] :=a[j]; j := j + 1; }
k := k + 1;
}
while(i<mid) do
{ b[k] := a[i]; i := i + 1; k:=k+1}
while(j<high) do
{ b[k] := a[j]; j := j + l; k:=k+1 }
for k := low to high do
a[k] := b[k]; }
Merge Sort - Example
Merge Sort - Analysis
T(n)= T(n/2) + T(n/2) + n (if n>1)
=1 (if n=1)
T(n)= 2T(n/2) + n
T(n/2)=2T(n/4)+n/2
T(n)=2[2T(n/4)+n/2 ]+n
T(n)=22T(n/22)+2n
T(n)=2kT(n/2k)+kn (kth step)
Recursion will stop if n/2k =1 then T(1)=1
T(n)=2kT(1) + kn
T(n)=n+ (log2n)n
T(n)= O(nlog2n)
Quick Sort
The quick sort algorithm partitions the original array by rearranging
it into two groups. The first group contains those elements less than
some chosen value taken from the set, and the second group
contains those elements greater than or equal to the chosen value.
The chosen value is known as the pivot element. Once the array has
been rearranged in this way with respect to the pivot, the same
partitioning is recursively applied to each of the two subsets. When
all the subsets have been partitioned and rearranged, the original
array is sorted.
swap i & j
(58 79 57)
pivot i 70 j
57 79
j i
(57) 58 (70 79) swap pivot
&j
57
pivot,
j, i
(70 79)
swap pivot
pivot, j &j
i
70
79
pivot,
j, i
(45 56 57 58 70 79)
02 04 06 08 16 24 38 45 56 57 58 70 79
Quick Sort - Analysis
• T(n) is the total time required to sort the n elements using quick sort .
• T(n) = P(n) + T(j-lb) + T(ub-j)
• P(n) is the time for partitioning the array into two parts and T(j-lb) is the time
required to sort the left sub array and T(ub-j) is the time to sort the right sub
array.
• The time to partition the array P(n) is equal to O(n).
• Worst Case: The worst case occurs when at each invocation, the array is
partitioned into two sub tables such that one of them is empty. i.e j=lb or j=ub
T(n) = P(n) + T(0) + T(n-1)
= c*n + T(n-1)
= c*n + c*(n-1) + T(n-2)
= c*n + c*(n-1) + c*(n-2) + T(n-3)
= c*n + c*(n-1) + c*(n-2) + ..........+ c*1 +T(0) and so on
= c( n+ (n-1) + (n-2) + (n-3) + .....+ 1) + 0
= c (n(n+1)/2) = c(n2 + n)/2
T(n) = O(n2 )
Quick Sort - Analysis
• Best Case: The best case occurs when at each invocation, the array is partitioned
into two equal arrays . i.e j = (lb +ub)/2
T(n) = P(n) + T(j-lb) + T(ub-j)
= c*n + 2T(n/2)
= c*n + 2[c*(n/2) + 2 T(n/4)]
= 2cn + 4T(n/4)
= 2cn + 4[c* n/4) +2T(n/8)]
= 3cn + 8T(n/8)
= 3cn + 23 T(n/23)
= 4cn + 24 T(n/24) and so on
substitute k in place of 4
= kcn + 2kT(n/2k)
If n is a power of 2 then substitute 2k = n
= log n *cn + n T(n/n)
= n(c*log n +1)
T(n) = Ω(nlogn)
1 1 1 1 1 0 1 1 3 3
C11
1 1 1 1 1 0 0 0 3 3
1 1 1 0 1 0 1 0 3 0
C12
1 1 1 0 1 0 0 0 3 0
1 1 1 1 1 0 1 1 3 3
C 21
0 0 1 1 0 0 0 0 0 0
1 1 1 0 1 0 1 0 3 0
C 22
0 0 1 0 0 0 0 0 0 0
52
The Time Complexity :
In order to compute the product, we need 8 multiplications of (n/2 X n/2)
matrices and 4 additions of (n/2 X n/2) matrices. Generally for addition , it is
O(n2).
If the matrix multiplications are done recursively then the running time is:
T(n) = 8T(n/2) + O(n2)
Recurrence Theorem of equation:-
T(n)= a T(n/b) + O(nk) a>=1 & b>1
= O(nlogba ) if a>bk
= O(nk logn) if a=bk
= O(nk) if a<bk So , T(n) = O(n3)
so, there is no improvement compared with the traditional method .
Strassen’s had derived some equations called strassen’s equations and
these are applied after Dividing the problem into sub matrices.
53
Strassen’s matrix multiplication equations:
P ( A11 A22 )( B11 B22 )
Q ( A21 A22 ) B11
R A11 ( B12 B22 )
S A22 ( B21 B11 )
T ( A11 A12 ) B22
U ( A21 A11 )( B11 B12 )
V ( A12 A22 )( B21 B22 )
C11 P S T V
C12 R T
C 21 Q S
C 22 P R Q U
= O(nlogba ) if a>bk
= 7 > 22
= O(nlog27 )
55
56