Classification of Algorithm
Classification of Algorithm
ALGORITHM:
An algorithm is a set of self contained sequence of instructions or actions that
contains finite space or sequence and that will give us a result to a specific problem in a
finite amount of time.
It is a logical and mathematical approach to solve or crack a problem using any possible
method.
Types of algorithm
Well there are many types of algorithm but the most fundamental types of algorithm are:
1. Recursive algorithms
2. Dynamic programming algorithm
3. Backtracking algorithm
4. Divide and conquer algorithm
5. Greedy algorithm
6. Brute Force algorithm
7. Randomized algorithm
It is use to solve the problems which can be broken into simpler or smaller problems of
same type.
Example:
Fact(x)
return 1
Example:
Fib(n)
if n=0
return 0
else
prev_Fib=0,curr_Fib=1
next_Fib=prev_Fib+curr_Fib
prev_Fib=curr_Fib
curr_Fib=new_Fib
return curr_Fib
3) Backtracking algorithm
How about we learn backtracking using an example so let’s say we have a problem
“Monk” and we divide it into four smaller problems “M, R, A, A”. It may be the case that
the solution of these problems did not get accepted as the solution of “Monk”.
In fact we did not know on which one it depends. So we will check each one of them one
by one until we find the solution for “Monk”.
So basically we attempt solving a subproblem but if we did not reach the desired solution
undo whatever you have done and start from the scratch again until you find the solution.
Example:
Queens Problem
Example:
5) Greedy algorithm
Greedy algorithm is an algorithm that solves the problem by taking optimal solution at
the local level (without regards for any consequences) with the hope of finding optimal
solution at the global level.
Greedy algorithm is used to find the optimal solution but it is not necessary that you will
definitely find the optimal solution by following this algorithm.
Like there are some problems for which an optimal solution does not exist (currently)
these are called NP complete problem.
Example:
Such types of algorithm are also used to find the optimal (best) solution as it checks all
the possible solutions.
And also used for finding a satisfactory solution (not the best), simply stop as soon as a
solution of the problem is found.
Example:
7) Randomized algorithm
A randomized algorithm uses a random number at least once during the computation to
make a decision.
Example:
Quick sort