12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 14.3 Amortized <strong>Analysis</strong> 499A similar argument was used in our analysis for the partition function in theQuicksort algorithm (Section 7.5). While on any given pass through the whileloop the left or right pointers might move all the way through the remainder of thepartition, doing so would reduce the number of times that the while loop can befurther executed.Our final example uses amortized analysis <strong>to</strong> prove a relationship between thecost of the move-<strong>to</strong>-front self-organizing list heuristic from Section 9.2 <strong>and</strong> the costfor the optimal static ordering of the list.Recall that, for a series of search operations, the minimum cost for a staticlist results when the list is sorted by frequency of access <strong>to</strong> its records. This isthe optimal ordering for the records if we never allow the positions of records <strong>to</strong>change, because the most frequently accessed record is first (<strong>and</strong> thus has leastcost), followed by the next most frequently accessed record, <strong>and</strong> so on.Theorem 14.2 The <strong>to</strong>tal number of comparisons required by any series S of n ormore searches on a self-organizing list of length n using the move-<strong>to</strong>-front heuristicis never more than twice the <strong>to</strong>tal number of comparisons required when series S isapplied <strong>to</strong> the list s<strong>to</strong>red in its optimal static order.Proof: Each comparison of the search key with a record in the list is either successfulor unsuccessful. For m searches, there must be exactly m successful comparisonsfor both the self-organizing list <strong>and</strong> the static list. The <strong>to</strong>tal number ofunsuccessful comparisons in the self-organizing list is the sum, over all pairs ofdistinct keys, of the number of unsuccessful comparisons made between that pair.Consider a particular pair of keys A <strong>and</strong> B. For any sequence of searches S,the <strong>to</strong>tal number of (unsuccessful) comparisons between A <strong>and</strong> B is identical <strong>to</strong> thenumber of comparisons between A <strong>and</strong> B required for the subsequence of S made uponly of searches for A or B. Call this subsequence S AB . In other words, includingsearches for other keys does not affect the relative position of A <strong>and</strong> B <strong>and</strong> so doesnot affect the relative contribution <strong>to</strong> the <strong>to</strong>tal cost of the unsuccessful comparisonsbetween A <strong>and</strong> B.The number of unsuccessful comparisons between A <strong>and</strong> B made by the move<strong>to</strong>-frontheuristic on subsequence S AB is at most twice the number of unsuccessfulcomparisons between A <strong>and</strong> B required when S AB is applied <strong>to</strong> the optimal staticordering for the list. To see this, assume that S AB contains i As <strong>and</strong> j Bs, with i ≤ j.Under the optimal static ordering, i unsuccessful comparisons are required becauseB must appear before A in the list (because its access frequency is higher). Move-<strong>to</strong>frontwill yield an unsuccessful comparison whenever the request sequence changes

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

Saved successfully!

Ooh no, something went wrong!