Algorithm:: Analysis of An Algorithm
Algorithm:: Analysis of An Algorithm
An Algorithm is a finite sequence of instructions, each of which has a clear meaning and can be
performed with a finite amount of effort in a finite length of time. No matter what the input values may
be, an algorithm terminates after executing a finite number of instructions. In addition every algorithm
must satisfy the following criteria:
Input: there are zero or more quantities, which are externally supplied;
Output: at least one quantity is produced
Definiteness: each instruction must be clear and unambiguous;
Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will
terminate after a finite number of steps;
Analysis of an Algorithm
The goal of analysis of an algorithm is to compare algorithm in running time and also Memory
management.
Running time of an algorithm depends on how long it takes a computer to run the lines of code
of the algorithm.
Running time of an algorithm depends on
1.Speed of computer
2.Programming language
3.Compiler and translator
Examples: binary search, linear search
Different time complexities Suppose ‘M’ is an algorithm, and suppose ‘n’ is the size of the input data.
Clearly the complexity f(n) of M increases as n increases. It is usually the rate of increase of f(n) we want
to examine. This is usually done by comparing f(n) with some standard functions. The most common
computing times are: O(1), O(log2 n), O(n), O(n. log2 n), O(n2 ), O(n3 ), O(2n ), n! and n.
Expressing the complexity in term of its relationship to know function. This type analysis is called
asymptotic analysis.
The main idea of Asymptotic analysis is to have a measure of efficiency of an algorithm , that
doesn’t depends on
1. Machine constants.
2.Doesn’t require algorithm to be implemented.
3.Time taken by program to be prepare.
ASYMPTOTIC NOTATION:
There are 3 asymptotic notations are mostly used to represent time complexity of algorithm.
1.Big oh (O)notation
2.Big omega (Ω) notation
3.Theta(Θ) notation
4.Little oh notation
5.Little omega(Ω) notation
2.Ω-Omega notation
Ω-Omega notation : Asymptotic “greater than”(faster rate). It represent Lower bound of algorithm run
time. By using Big Omega notation we can calculate minimum amount of time. We can say that it is best
case time complexity. Formula : f(n)>=c g(n) n>=n0 , c>0 ,n0 >=1
where c is constant, n is function, Lower bound and Best case.
Divide and conquer strategy is as follows: divide the problem instance into two or more smaller
instances of the same problem, solve the smaller instances recursively, and assemble the solutions to
form a solution of the original instance. The recursion stops when an instance is reached which is too
small to divide. When dividing the instance, one can either use whatever division comes most easily to
hand or invest time in making the division carefully so that the assembly is simplified.
Given a function to compute on ‘n’ inputs the divide-and-conquer strategy suggests splitting the
inputs into ‘k’ distinct subsets, 1<k<=n, yielding ‘k’ sub problems.
These sub problems must be solved, and then a method must be found to combine sub
solutions into a solution of the whole.
If the sub problems are still relatively large, then the divide-and-conquer strategy can possibly
be reapplied.
If the problem p and the size is n , sub problems are n1, n2 ….nk, respectively, then the
computing time of D And C is described by the recurrence relation.
T(n)= { g(n) n small
T(n1)+T(n2)+……………+T(nk)+f(n);
“Where T(n) is the time for D And C on any I/p of size n.
g(n) is the time of compute the answer directly for small I/p s. f(n) is the time for dividing P &
combining the solution to sub pro
APPLICATIONS
1. Quick sort :
Quick sort is a sorting algorithm. The quick sort algorithm partitions the original array by rearranging it
into two groups. The first group contains those elements less than some arbitrary chosen value taken
from the set and the second group contains those elements greater than or equal to the chosen value.
The algorithm picks a pivot element, rearranges the array elements in such a way that all elements
smaller than the picked pivot element move to left side of pivot, and all greater elements move to right
side. Finally, the algorithm recursively sorts the sub arrays on left and right of pivot element.
In Quick sort, the division into 2 sub arrays is made so that the sorted sub arrays do not need to
be merged later.
This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for all I between 1
& n and all j between (m+1) & n for some m, 1<=m<=n.
Thus the elements in a[1:m] & a[m+1:n] can be independently sorted.
No merge is needed. This rearranging is referred to as partitioning.
2. Binary Search
Binary Search is a searching algorithm. In each step, the algorithm compares the input element x with
the value of the middle element in array. If the values match, return the index of middle. Otherwise, if x
is less than the middle element, then the algorithm recurs for left side of middle element, else recurs for
right side of middle element.
1. Algorithm Bin search(a,n,x)
2. // Given an array a[1:n] of elements in non-decreasing
3. //order, n>=0,determine whether x is present and
4. // if so, return j such that x=a[j]; else return 0.
5. {
6. low:=1; high:=n;
7. while (low<=high) do
8. {
9. mid:=[(low+high)/2];
10. if (x<a[mid]) then high;
11. else if(x>a[mid]) then
12. low=mid+1;
13. else return mid;
14. }
return 0; }
A brute force approach is an approach that finds all the possible solutions to find a satisfactory solution
to a given problem. The brute force algorithm tries out all the possibilities till a satisfactory solution is
not found. Let's understand the brute force search through an example.
Brute force search considers each and every state of a tree, and the state is represented in the form of a
node. As far as the starting position is concerned, we have two choices, i.e., A state and B state. We can
either generate state A or state B. In the case of B state, we have two states, i.e., state E and F.
In the case of brute force search, each state is considered one by one. As we can observe in the above
tree that the brute force search takes 12 steps to find the solution.
o This algorithm finds all the possible solutions, and it also guarantees that it finds the correct
solution to a problem.
o This type of algorithm is applicab
applicable to a wide range of domains.
o It is mainly used for solving simpler and small problems.
o It can be considered a comparison benchmark to solve a simple problem and does not require
any particular domain knowledge.
The following are the disadvantages of the brute-force algorithm: