Chapter 10: Algorithms 10.1. Deterministic and Non-Deterministic Algorithm
Chapter 10: Algorithms 10.1. Deterministic and Non-Deterministic Algorithm
Deterministic algorithms can be defined in terms of a state machine: a state describes what a machine
is doing at a particular instant in time. State machines pass in a discrete manner from one state to
another. Just after we enter the input, the machine is in its initial state or start state. If the machine is
deterministic, this means that from this point onwards, its current state determines what its next state
will be; its course through the set of states is predetermined. Note that a machine can be deterministic
and still never stop or finish, and therefore fail to deliver a result.
A nondeterministic algorithm is one in which for a given input instance each intermediate step has one
or more possibilities. This means that there may be more than one path from which the algorithm may
arbitrarily choose one. Not all paths terminate successfully to give the desired output. The
nondeterministic algorithm works in such a way so as to always choose a path that terminates
successfully, thus always giving the correct result.
The key operation of the merge sort algorithm is the merging of two sorted sequences in the
"combine" step. To perform the merging, we use an auxiliary procedure MERGE(A, p, q, r), where A
is an array and p, q, and r are indices numbering elements of the array such that p q < r. The
procedure assumes that the subarrays A[p q] and A[q + 1 r] are in sorted order. It merges them to
form a single sorted subarray that replaces the current subarray A[p r].
Another famous NP-hard problem is independent set: given a graph G = (V, E), nd a maximum-size
independent set V V . (A subset is independent if no two vertices in the subset are connected by
an edge.) What can we do when faced with such dicult problems, for which we cannot expect to nd
polynomial-time algorithms? Unless the input size is really small, an algorithm with exponential
running time is not useful. We therefore have to give up on the requirement that we always solve the
problem optimally, and settle for a solution close to optimal. Ideally, we would like to have a
guarantee on how close to optimal the solution is. For example, we would like to have an algorithm
for Euclidean TSP that always produces a tour whose length is at most a factor times the minimum
The objective of a heuristic is to produce quickly enough a solution that is good enough for solving
the problem at hand. This solution may not be the best of all the actual solutions to this problem, or it
may simply approximate the exact solution. But it is still valuable because finding it does not require a
prohibitively long time. Heuristics may produce results by themselves, or they may be used in
conjunction with optimization algorithms to improve their efficiency (e.g., they may be used to
generate good seed values).
It is difficult to imagine the variety of existing computational tasks and the number of algorithms
developed to solve them. Algorithms that either give nearly the right answer or provide a solution not
for all instances of the problem are called heuristic algorithms . This group includes a plentiful
spectrum of methods based on traditional techniques as well as specific ones.
Based on the time complexity representation of the big Oh notation, the algorithm can be categorized
as :
1. Constant time O(1)
2. Logarithmic time Olog(n)
3. Linear time O(n)
4. Polynomial time O(nc) Where c >1
5. Exponential time O(cn) Where c > 1
The outer loop count is N but the inner loop executes N times for each time. So the body of the inner
loop will execute N*N and the entire performance will be O(N2).
In this case outer count trip is N, but the trip count of the inner loop depends not only N, but the value
of the outer loop counter as well. If outer counter is 1, the inner loop has a trip count 1 and so on. If
outer counter is N the inner loop trip count is N.
How many times the body will be executed?
1+2+3+(N-1)+N = N(N+1) / 2 = ((N2) +N )/2
Therefore the performance is O(N2).
Generalization: A structure with k nested counting loops where the counter is just incremented or
decrement by one has performance O(Nk) if the trip counts depends on the problem size only.