Chapter 1.1 - Analysis of Algorithms-1
Chapter 1.1 - Analysis of Algorithms-1
Introduction
Informally, an algorithm is any well-defined computational procedure that takes some value, or set
of values, as input and produces some value, or set of values, as output. An algorithm is thus a
sequence of computational steps that transform the input into the output.
We can also view an algorithm as a tool for solving a well-specified computational problem. The
statement of the problem specifies in general terms the desired input/output relationship. The
algorithm describes a specific computational procedure for achieving that input/output relationship.
For example, one might need to sort a sequence of numbers into nondecreasing order. This problem
arises frequently in practice and provides fertile ground for introducing many standard design
techniques and analysis tools. Here is how we formally define the sorting problem:
o Input: A sequence of n numbers <a1, a2... an>.
o Output: A permutation (reordering) of the input sequence such that a1<a2<... <an.
For example, given the input sequence <31, 41, 59, 26, 41, 58>, a sorting algorithm returns as output
the sequence <26, 31, 41, 41, 58, 59>. Such an input sequence is called an instance of the sorting
problem. In general, an instance of a problem consists of the input (satisfying whatever constraints
are imposed in the problem statement) needed to compute a solution to the problem.
Sorting is a fundamental operation in computer science (many programs use it as an intermediate
step), and as a result a large number of good sorting algorithms have been developed. Which
algorithm is best for a given application depends on-among other factors the number of items to be
sorted, the extent to which the items are already somewhat sorted, possible restrictions on the item
values, and the kind of storage device to be used: main memory, disks, or tapes.
An algorithm can be specified in English, as a computer program, as a pseudo code or even as a
hardware design. The only requirement is that the specification must provide a precise description of
the computational procedure to be followed.
Characteristics of an Algorithm
All algorithms must satisfy the following criteria
o Input. Zero or more quantities are externally supplied.
o Output. At least one quantity is produced.
o Definiteness. Each instruction is clear and unambiguous.
o Finiteness. If we trace out the instructions of an algorithm, then for all cases, the
algorithm terminates after a finite number of steps.
o Effectiveness. Every instruction must be very basic so that it can be carried out,
in principle, by a person using only pencil and paper. It is not enough that each
operation be definite as in criterion 3; it also must be feasible.
Criteria 1 and 2 require that an algorithm produce one or more outputs and have zero or
more inputs that are externally supplied. According to criterion 3, each operation must
be definite, meaning that it must be perfectly clear what should be done. Directions
such as "add 6 or 7 to x" or "compute5/0" are not permitted because it is not clear
which of the two possibilities should be done or what the result is.
Criteria 4 for algorithms are that they terminate after a finite number of operations. A
related consideration is that the time for termination should be reasonably short. For
example, an algorithm could be devised that decides whether any given position in the
game of chess is a winning position. The algorithm works by examining all possible
moves and countermoves that could be made from the starting position. The difficulty
with this algorithm is that even using the most modern computers, it may take billions
of years to make the decision. We must be very concerned with analyzing the efficiency
of each of our algorithms.
Criterion 5 requires that each operation be effective; each step must be such that it
can, at least in principle, be done by a person using pencil and paper in a finite
amount of time. Performing arithmetic on integers is an example of an effective
operation, but arithmetic with real numbers is not, since some values may be
expressible only by infinitely long decimal expansion. Adding two such numbers would
violate the effectiveness property.
1|Page
Algorithms as a technology
Suppose computers were infinitely fast and computer memory was free. Would you have any reason
to study algorithms? The answer is yes, if for no other reason than that you would still like to
demonstrate that your solution method terminates and does so with the correct answer.
If computers were infinitely fast, any correct method for solving a problem would do. You would
probably want your implementation to be within the bounds of good software engineering practice
(i.e., well designed and documented), but you would most often use whichever method was the
easiest to implement.
Of course, computers may be fast, but they are not infinitely fast. And memory may be cheap, but it
is not free. Computing time is therefore a bounded resource, and so is space in memory. These
resources should be used wisely, and algorithms that are efficient in terms of time or space will help
you do so.
By using an algorithm whose running time grows more slowly, even with a poor compiler, computer
B runs 20 times faster than computer A! The advantage of merge sort is even more pronounced when
we sort ten million numbers: where insertion sort takes approximately 2.3 days, merge sort takes less
than 20 minutes. In general, as the problem size increases, so does the relative advantage of merge
sort.
5|Page