Unit 1-Introduction
Unit 1-Introduction
UNIT 1 – INTRODUCTION
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e.,
for obtaining a required output for any legitimate input in a finite amount of time.
problem
algorithm
input output
computer
pg. 1
DESIGN AND ANALYSIS OF ALGORITHM
pg. 2
DESIGN AND ANALYSIS OF ALGORITHM
• Analysing an algorithm
After correctness, by far the most important is efficiency. In fact, there are two kinds of
algorithm efficiency: time efficiency, indicating how fast the algorithm runs, and space
efficiency, indicating how much extra memory it uses.
Other desirable characteristic of an algorithm is simplicity and generality.
• Coding an algorithm
After analysing the algorithm, we need to code the program. The validity of programs is
established by testing.
Analysis Framework
There are two kinds of efficiency: time efficiency and space efficiency. Time efficiency, also
called time complexity, indicates how fast an algorithm in question runs. Space efficiency,
also called space complexity, refers to the amount of memory units required by the algorithm
in addition to the space needed for its input and output.
pg. 3
DESIGN AND ANALYSIS OF ALGORITHM
The established framework for the analysis of an algorithm’s time efficiency suggests
measuring it by counting the number of times the algorithm’s basic operation is executed on
inputs of size n.
Let cop be the execution time of an algorithm’s basic operation on a particular computer, and
let C(n) be the number of times this operation needs to be executed for this algorithm. Then
we can estimate the running time T (n) of a program implementing this algorithm on that
computer by the formula T (n) ≈ copC(n)
Orders of Growth
A difference in running times on small inputs is not what really distinguishes efficient
algorithms from inefficient ones, It is the larger input size that matters.
For large values of n, it is the function’s order of growth that counts: just look at Table, which
contains values of a few functions particularly important for analysis of algorithms. The
magnitude of the numbers in Table has a profound significance for the analysis of
algorithms. The function growing the slowest among these is the logarithmic function. It
grows so slowly, in fact, that we should expect a program
n log2 n n n log2 n n2 n3 2n n!
10 3.3 101 3.3.101 102 103 103 3.6.106
102 6.6 102 6.6.102 104 106 1.3.1030 9.3.10157
103 10 10 3 1.0.104 106 109
104 13 104 1.3.105 108 1012
105 17 105 1.7.106 1010 1015
106 20 106 2.0.107 1012 1018
pg. 4
DESIGN AND ANALYSIS OF ALGORITHM
will not be checked if the first one, which checks that the array’s index does not exceed its
upper bound, fails.
ALGORITHM SequentialSearch(A[0..n − 1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n − 1] and a search key K
//Output: The index of the first element in A that matches K
//or −1 if there are no matching elements
i←0
while i < n and A[i] != K do
i=i+1
if i < n return i
else return -1
Clearly, the running time of this algorithm can be quite different for the same list size n. In
the worst case, when there are no matching elements or the first matching element happens to
be the last one on the list, the algorithm makes the largest number of key comparisons among
all possible inputs of size n: Cworst(n) = n
The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n,
which is an input (or inputs) of size n for which the algorithm runs the longest among all
possible inputs of that size. The way to determine the worst-case efficiency of an algorithm
is, in principle, quite straightforward: analyze the algorithm to see what kind of inputs yield
the largest value of the basic operation’s count C(n) among all possible inputs of size n and
then compute this worst-case value Cworst(n). (For sequential search, the answer was
obvious. The methods for handling less trivial situations are explained in subsequent sections
of this chapter.) Clearly, the worst-case analysis provides very important information about an
algorithm’s efficiency by bounding its running time from above. In others words, it
guarantees that for any instance of size n, the running time will not exceed Cworst(n), its
running time on the worst-case inputs. The best-case efficiency of an algorithm is its
efficiency for the best-case input of size n, which is an input (or inputs) of size n for which
the algorithm runs the fastest among all possible inputs of that size. Accordingly, we can
analyze the bestcase efficiency as follows. First, we determine the kind of inputs for which
the count C(n) will be the smallest among all possible inputs of size n. (Note that the best
case does not mean the smallest input; it means the input of size n for which the algorithm
runs the fastest.) Then we ascertain the value of C(n) on these most convenient inputs. For
example, the best-case inputs for sequential search are lists of size n with their first element
equal to a search key; accordingly, Cbest(n) = 1 for this algorithm.
However, that neither the worst-case analysis nor its best-case counterpart yields the
necessary information about an algorithm’s behaviour on a “typical” or “random” input. This
is the information that the average-case efficiency seeks to provide. To analyse the
pg. 5
DESIGN AND ANALYSIS OF ALGORITHM
algorithm’s average case efficiency, we must make some assumptions about possible inputs
of size n. Average-case efficiency cannot be obtained by taking the average of the worst-case
and the best-case efficiencies.
pg. 6