11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

Sec. 17.1 Reductions 53723421793881257904859118912916434Figure 17.1 An illustr<strong>at</strong>ion of PAIRING. The two lists of numbers are paired upso th<strong>at</strong> the least values from each list make a pair, the next smallest values fromeach list make a pair, <strong>and</strong> so on.Figure 17.1 illustr<strong>at</strong>es PAIRING. One way to solve PAIRING is to use an existingsorting program to sort each of the two sequences, <strong>and</strong> then pair off items basedon their position in sorted order. Technically we say th<strong>at</strong> in this solution, PAIRINGis reduced to SORTING, because SORTING is used to solve PAIRING.Notice th<strong>at</strong> reduction is a three-step process. The first step is to convert aninstance of PAIRING into two instances of SORTING. The conversion step in thisexample is not very interesting; it simply takes each sequence <strong>and</strong> assigns it to anarray to be passed to SORTING. The second step is to sort the two arrays (i.e., applySORTING to each array). The third step is to convert the output of SORTING tothe output for PAIRING. This is done by pairing the first elements in the sortedarrays, the second elements, <strong>and</strong> so on.A reduction of PAIRING to SORTING helps to establish an upper bound on thecost of PAIRING. In terms of asymptotic not<strong>at</strong>ion, assuming th<strong>at</strong> we can find onemethod to convert the inputs to PAIRING into inputs to SORTING “fast enough,”<strong>and</strong> a second method to convert the result of SORTING back to the correct resultfor PAIRING “fast enough,” then the asymptotic cost of PAIRING cannot be morethan the cost of SORTING. In this case, there is little work to be done to convertfrom PAIRING to SORTING, or to convert the answer from SORTING back to theanswer for PAIRING, so the dominant cost of this solution is performing the sortoper<strong>at</strong>ion. Thus, an upper bound for PAIRING is in O(n log n).It is important to note th<strong>at</strong> the pairing problem does not require th<strong>at</strong> elementsof the two sequences be sorted. This is merely one possible way to solve the problem.PAIRING only requires th<strong>at</strong> the elements of the sequences be paired correctly.Perhaps there is another way to do it? Certainly if we use sorting to solve PAIR-ING, the algorithms will require Ω(n log n) time. But, another approach mightconceivably be faster.

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

Saved successfully!

Ooh no, something went wrong!