Design and Analysis of Algorithms OCR
Design and Analysis of Algorithms OCR
1.1 Introduction
Before entering into the colorful environment of algorithms and their design, let us
question ourselves about how almost all the day-to-day industrial problems dre solved, The
immediate answer is “a systematic approach”. It enables the people all around the world
to quickly access und retrieve Jarge amounts of information, In order to do so, a
systematic approach or plan is. to be made for finding the best website of the search
requested and the best route on which the data is to be traveled to reach in time. Ln the
sume way, in manufiusturing and other commercial settings, itis often important to allocate
secures resources in the most beneficial way. An oil company may wish to know where to
pluce its wells in order to imiuximize ils expected profit. Like wise, we cun take so many
distinet cxumples whether it may be a biological industry, a software industry or a norma!
computational problem. For all the solutions there should be systematic approach which is
none other than an Algorithm,
Informally, an algorithm is any well — defined computational procedure that takes some
values as “input? and produces some value, or set of values, as “output”. An algorithm i is
thus“ @ sequence of computational stepy (uit transforny the input nite the output,’
In simple terms we can say that “An algorithm is.a step—by~step procedure for.
performing some task in a finite amount of time”.
Definition:
Aa algorithm is composed of a finite set of steps each of which muy require one or more
operations. Every operation may be characterized as either a simple or complex. Operations
performed on scalitr quantities are termed as simple, while Operations performed on vector
data normally termed as complex,
i. Finiteness a Definiteness
3. » Input 4. Output
5: Effectiveness
. Finiteness: An algorithm must always terminate after a number of steps. If we trace out
the instructions of an algorithm, then for all cases. the algorithm terminates after a finite
number of steps.
2. Definiteness: Each operation must be definite meaning that it must be perfectly clear.
Each step of an algorithm must be precisely defined. The actions to be carried out
must be rigorously and unambiguously specified for each case,
3. Input: An algorithm has zero or more “inputs” quantities thal are given to it initially
before
the algorithm begins, or dynamically as the algorithm runs. ‘These inputs are taken from
specified set of objects. These inputs ure extremely supplied to the algorithm.
4, Output: An algorithm has one or more “output” quantities that have a specified relation
to the inputs: An algonthm produces atleast one or more outputs.
§. Effectiveness: Each operation should be effective i.e., the operation must be able to
carryout in finite amount of time. An algorithm is generally expected to be “effective”, in
the sense that its operations must all be sufficiently basic that they can in principle be
done exactly and in a finite length of time by some one using peneil and paper.
Creating an algorithm is an art which may never be fully automated. A major goal of this
book is to study various design techniques that have proven to be useful in that they have
often yielded good algorithms. By mastering these design strategies, it will become easier
for
you to devise new and useful algorithms. Many of the chapters of this book are organised
Tunoduction 3
around what we believe are the major methods of algorithm design. ‘I'he reader may now
wish to glance back at the table of contents to see what these methods are called. Some of
these technologies may already be familiar, and some have been found to be so useful in
fields other than Computer science such as operations research and electrical engineering,
In
this boak, we can only hope to give an introduction to these many approaches to algorithm
formulation. All of the approaches we consider have applications in a variety of areas
inciuding Computer science. But some important design techniques such as linear,
nonlinear,
and integer programming are not covered here as they are traditionally covered in other
courses:
* Every procedure should carefully specify its input and output variables,
Once an algonihm is devised and expressed, it is necessary to show that it computes the
correct answer for all possible legal inputs. We refer to this process as algorithm
validation. The purpose of validation is to assure us that this algorithm will work correctly
independent of the issues concerning the programming language. Once the validity of the
method has been shown, a program can be written and a second phase begins. This
phase is referred to as Program Proving or sometimes as Program Verification. A
proo! of correctness requires that the solution be started in two forms one form is usually
4s 1 program which ts annotated by a set of assertions about the input and output variables
of the program. The second fornyis called a specification, and this may also be expressed
in the predicate calculus. A proof consists of showing that these two forms are equivalent
in that for every given legal input, they describe the same output. A complete proof of
program correctness requires that each statement of the programming language be
precisely defined and all basic operations be proved correct. All these details may cause a
prool to be very much longer than the program,
As an algorithm is executed, it uses the computers central processing unit (CPU) to perform
operations and its memory (both immediate and auxiliary) to hold the program and data.
Debugging is the process of executing progrims on sample data sets to determine whether
faulty results oceur and, if so, to correct them. However, as ¥. Dijkstra has pointed out,
“debugging can only point to the presence of errors, bul not fo their absence”, The cases in
which we cannot verify the correctness of output on sample data, the following strategy cin
be employed: let more than one programmer develop programs for the same problem,
and compare the outputs produced by these programs. If the outputs match, then there is as
good chance that they are correct. A proof of correctness is much more valuable than a
thousand tests (if that proof is correct). Since it guarantees that the program will work
correctly for all possible inputs.
Profiling or performance measurement is the process of executing a correct program on
data sets and measuring the time and space it lakes to compute the results. These timing
figures ure useful in that they may confirm a previously done analysis and print out logical
places to perform useful optimization.
ee
1.4 Algorithm Examples
Now as we know what aralgorithm is.and what are its features, let us move to some
examples
for illustrating the notion of an algorithm. For time being. Let us take an example of finding
the
GCD of two non-negative and not-both-zero integers with the help of the sixth feature of un
algorithm i.e., solving a single problem,by differentalgorithm techniques, .
Recall that the GCD (greatest common divisor) of two non negative, not-both-zere integers
m
and », denated by ged (nm, 7) is defined as the largest integer that divides both mand
mevenly,
Se
Buclid of Alexindria has oudines a most famous algorithm for finding the GCD and hence the