18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introduction to <strong>Algorithm</strong>s<br />

Next: Correctness and Efficiency Up: Techniques Previous: Techniques<br />

Introduction to <strong>Algorithm</strong>s<br />

What is an algorithm? An algorithm is a procedure to accomplish a specific task. It is the idea behind any computer program.<br />

To be interesting, an algorithm has to solve a general, well-specified problem. An algorithmic problem is specified by describing<br />

the complete set of instances it must work on and what properties the output must have as a result of running on one of these<br />

instances. This distinction between a problem and an instance of a problem is fundamental. For example, the algorithmic<br />

problem known as sorting is defined as follows:<br />

Input: A sequence of n keys .<br />

Output: <strong>The</strong> permutation (reordering) of the input sequence such that . An instance of sorting might be an array<br />

of names, such as {Mike, Bob, Sally, Jill, Jan}, or a list of numbers like {154, 245, 568, 324, 654, 324}. Determining whether<br />

you in fact have a general problem to deal with, as opposed to an instance of a problem, is your first step towards solving it. This<br />

is true in algorithms as it is in life.<br />

An algorithm is a procedure that takes any of the possible input instances and transforms it to the desired output. <strong>The</strong>re are many<br />

different algorithms for solving the problem of sorting. For example, one method for sorting starts with a single element (thus<br />

forming a trivially sorted list) and then incrementally inserts the remaining elements so that the list stays sorted. This algorithm,<br />

insertion sort, is described below:<br />

InsertionSort(A)<br />

1])<br />

for i = 1 to n-1 do<br />

for j = i+1 to 2 do<br />

if (A[j] < A[j-1]) then swap(A[j],A[j-<br />

Note the generality of this algorithm. It works equally well on names as it does on numbers, given the appropriate < comparison<br />

operation to test which of the two keys should appear first in sorted order. Given our definition of the sorting problem, it can be<br />

readily verified that this algorithm correctly orders every possible input instance.<br />

In this chapter, we introduce the desirable properties that good algorithms have, as well as how to measure whether a given<br />

algorithm achieves these goals. Assessing algorithmic performance requires a modest amount of mathematical notation, which<br />

we also present. Although initially intimidating, this notation proves essential for us to compare algorithms and design more<br />

efficient ones.<br />

While the hopelessly ``practical'' person may blanch at the notion of theoretical analysis, we present this material because it is<br />

useful. In particular, this chapter offers the following ``take-home'' lessons:<br />

file:///E|/BOOK/BOOK/NODE6.HTM (1 of 2) [19/1/2003 1:27:42]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!