11 Algorithm
11 Algorithm
Algorithms
Deterministic and Non-Deterministic algorithm
In a deterministic algorithm, for a given particular input, the computer will
always produce the same output going through the same states but in the case
of the non-deterministic algorithm, for the same input, the compiler may
produce different output in different runs. In fact, non-deterministic
algorithms can’t solve the problem in polynomial time and can’t determine
what is the next step. The non-deterministic algorithms can show different
behaviors for the same input on different execution and there is a degree of
randomness to it.
To implement a non-deterministic algorithm, we have a couple of languages
like Prolog but these don’t have standard programming language operators
and these operators are not a part of any standard programming languages.
Some of the terms related to the non-deterministic algorithm are defined
below:
choice(X): chooses any value randomly from the set X.
failure(): denotes the unsuccessful solution.
success(): The solution is successful and the current thread terminates.
Deterministic and Non-Deterministic algorithm
Deterministic Algorithm Non-deterministic Algorithm
Can solve the problem in polynomial Can’t solve the problem in polynomial
time. time.
Like linear search and binary search like 0/1 knapsack problem.
Divide and Conquer Algorithm
A divide and conquer algorithm is a strategy of solving a large problem by breaking the
problem into smaller sub-problems solving the sub-problems, and combining them to
get the desired output.
How Divide and Conquer Algorithms Work?
Here are the steps involved:
Divide: Divide the given problem into sub-problems using recursion.
Conquer: Solve the smaller sub-problems recursively. If the sub problem is small
enough, then solve it directly.
Combine: Combine the solutions of the sub-problems that are part of the recursive
process to solve the actual problem.
Let the given array be:
Divide and Conquer Algorithm
Divide the array into two halves.
Again, divide each subpart recursively into two halves until you get individual
elements.
Divide and Conquer Algorithm
Now, combine the individual elements in a sorted manner. Here, conquer and combine
steps go side by side.
Divide and Conquer Algorithm
Divide and Conquer Algorithm
Serial and parallel Algorithm
In computer science, a parallel algorithm, as opposed to a traditional serial algorithm, is an
algorithm which can do multiple operations in a given time. It has been a tradition of computer
science to describe serial algorithms in abstract machine models, often the one known as random-
access machine.
For example, take a look at Merge Sort, its normal recursive implementation is already a parallel
algorithm (therefore merge sort is embarrassingly parallel). Note that with n threads, the time
complexity of merge sort will be O(log n) instead of O(n log n).
Serial algorithms are algorithms that are not parallel algorithms (i.e. it doesn't allow parallel
processing)
For example if you want to find maximum element of an array, you can do it as follow:
a = [2, 4, 3, 2]
ans = k
For i From 0 To Len(a) - 1
ans = Max(ans, a[i])
Return ans
There is no way for the compiler/interpreter to run this as parallel because it is iterative. It's
nature is to run sequentially
Heuristic and approximation algorithm
A heuristic is a technique designed for solving a problem more quickly when classic methods are
too slow. The objective of a heuristic is to produce quickly enough a solution that is good enough
for solving the problem at hand. ".
Heuristic could derive from theory or experimental experience, but approximation algorithms
have solid theory foundation (provable solution).
A heuristic is essentially a hunch is a heuristic. As is solving the traveling salesman problem by
starting at a random vertex and going to the nearest not yet visited each step. It is a plausible
idea, that should not give a too bad solution. In this case, it can be shown that it won't always
give a good solution.
An approximation algorithm is usually understood to give an approximate solution, with some
kind of guarantee of performance.
An approximation algorithm is a way of dealing with NP-completeness for an optimization
problem. This technique does not guarantee the best solution. The goal of the approximation
algorithm is to come as close as possible to the optimal solution in polynomial time.
An approximation algorithm guarantees to run in polynomial time though it does not
guarantee the most effective solution.
An approximation algorithm guarantees to seek out high accuracy and top quality
solution(say within 1% of optimum)
Approximation algorithms are used to get an answer near the (optimal) solution of an
optimization problem in polynomial time