Professional Documents
Culture Documents
Basic Techniques For Design and Analysis of Algorithms
Basic Techniques For Design and Analysis of Algorithms
EDWARD M. REINGOLD
Department of Computer Science, University of Illinois at Urbana-Champaign ^reingold@cs.uiuc.edu&
H
# x[2] # . . . # x[n]? The simplest way to
put the values in order is to mimic what Q ~1! if m 5 1 ,
we might do by hand: take item after sm
item and insert each one into the proper s m21 1 Q ~ 1 ! otherwise .
place among those items already inserted: With a bit of algebra we discover that
1. void insert (float x[ ], int i, tn 5 Q(n2). The analysis of the average
float a) { behavior is nearly identical; only the con-
2. // Insert a into x[1] . . . stants hidden in the Q-notation change.
x[i]
3. // x[1] . . . x[i21] are sort- DIVIDE AND CONQUER
ed; x[i] is unoccupied
We can design better sorting methods
4. if (i 55 1 i x[i21] d a)
using the idea of divide-and-conquer, in
5. x[i] 5 a; which we decompose a problem into
6. else { subproblems that resemble the original
7. x[i] 5 x[i21]; problem on a reduced scale. For exam-
8. insert (x, i21, a); ple, we can sort by splitting the n values
9. } arbitrarily into two piles of n/2 values
each, sort each of the piles separately, observation that many optimization prob-
and then merge the two piles into a lems can be solved by solving similar sub-
single sorted pile. This sorting tech- problems and then composing the solu-
niques is called merge sort. Let T(n) be tions of those subproblems into a solution
the time required by merge sort for sort- for the original problem. In addition, the
ing n values. The time needed to do the problem is viewed as a sequence of deci-
merging is proportional to the number sions, each decision leading to different
of elements being merged, so that subproblems; if a wrong decision is made,
a suboptimal solution results, so it is nec-
T ~ n ! 5 cn 1 2T ~ n/ 2 ! , essary to account for all possible deci-
sions. To eliminate duplicate computa-
because we must sort the two halves tions that occur in such sequences of
(time T(n/2) each) and then merge (time decisions, we use the idea of caching the
proportional to n). The growth rate of solutions to subproblems.
T(n) is Q(n log n).
GREEDY HEURISTICS
LOWER BOUNDS
Optimization problems always have an
Can we find sorting algorithms that take objective function to be minimized or
time less than Q(n log n)? The answer is maximized, but it is not often clear
no if we are restricted to sorting algo- what steps to take to reach the optimum
rithms that derive their information from value. We may use dynamic program-
comparisons between the values of ele- ming to examine systematically all pos-
ments. The flow of control in such sorting sible choices, but perhaps there is a
algorithms can be viewed as binary trees simple rule that leads directly to good
in which there are n! leaves, one for every choices at each step. Such an approach
possible sorted output arrangement. Be- may be less time-consuming than dy-
cause a binary tree with height h can namic programming, but decisions are
have at most 2h leaves, it follows that the made that are locally optimum, though
height of a tree with n! leaves must be at perhaps not globally optimum. But such
least log2 n! 5 Q(n log n). Because the a “greedy” sequence of locally optimum
height of this tree corresponds to the choices can lead to a globally optimum
longest sequence of element comparisons solution in some circumstances.
possible in the flow of control, any such How do we know that such a process
sorting algorithm must, in its worst case, leads to an optimal solution? The key is
use time proportional to n log n. to assume, by way of contradiction, that
the greedy solution found is not opti-
DYNAMIC PROGRAMMING mum. In this case, the greedy strategy
must have erred in one of its choices, so
In designing algorithms to solve optimiza- we look at the first error this strategy
tion problems, we need to make the opti- made. We then show that this “first
mal (lowest cost, highest value, shortest error” is not really an error at all, so
distance, and so on) choices from among a that we are forced to conclude that the
large number of alternative solutions; dy- greedy strategy never errs.
namic programming is an organized way We can apply the greedy heuristic to
to find an optimal solution by systemati- many optimization problems, and even if
cally exploring all possibilities without the results are not optimal, they are often
unnecessary repetition. Often dynamic quite good. For example, in the n-city
programming leads to efficient, polynomi- traveling salesman problem, we can get
al-time algorithms for problems that ap- near-optimal tours in time O(n2) when
pear to require searching through expo- the intercity costs are symmetric and sat-
nentially many possibilities. isfy the triangle inequality. The closest
Like the divide-and-conquer method, insertion algorithm starts with a “tour”
dynamic programming is based on the consisting of a single, arbitrarily chosen
city, and successively inserts the remain- Mathematics for the Analysis of Algorithms,
ing cities to the tour, making a greedy 3rd ed. Birkhäuser, Boston.
choice about which city to insert next and KNUTH, D. E. 1973. The Art of Computer Pro-
where to insert it: the city chosen for gramming, Vol. 1: Fundamental Algorithms,
2nd ed. Addison-Wesley, Reading, MA.
insertion is the city not on the tour but
KNUTH, D. E. 1973. The Art of Computer Pro-
closest to a city on the tour; the chosen gramming, Vol. 3: Sorting and Searching, Ad-
city is inserted adjacent to the city on the dison-Wesley, Reading, MA.
tour to which it is closest. LEUKER, G. S. 1980. Some techniques for solving
ACKNOWLEDGMENTS recurrences, ACM Comput. Surv. 12, 419 – 436.
REINGOLD, E. M. AND HANSEN, W. J. 1986. Data
The comments of Ken Urban are gratefully ac- Structures in Pascal. Little, Brown, Boston.
knowledged. REINGOLD, E. M., NIEVERGELT, J., AND DEO,
N. 1977. Combinatorial Algorithms: The-
REFERENCES ory and Practice. Prentice-Hall, Englewood
Cliffs, NJ.
CORMEN, T. H., LEISERSON, C. E., AND RIVEST, R. ROSENCRANTZ, D. J., STEARNS, R. E., AND LEWIS, P.
L. 1990. Introduction to Algorithms. M. 1977. An analysis of several heuristics
McGraw-Hill, New York. for the traveling salesman problem. SIAM J.
GREENE, D. H. AND KNUTH, D. E. 1990. Comput. 6, 563–581.