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.

328 Chap. 9 Searching<strong>and</strong> the <strong>to</strong>tal number of comparisons required is 54.Finally, if the list is organized by the transpose heuristic, then the finallist will beA B F D G E C H,<strong>and</strong> the <strong>to</strong>tal number of comparisons required is 62.While self-organizing lists do not generally perform as well as search trees or asorted list, both of which require O(log n) search time, there are many situations inwhich self-organizing lists prove a valuable <strong>to</strong>ol. Obviously they have an advantageover sorted lists in that they need not be sorted. This means that the cost <strong>to</strong> inserta new record is low, which could more than make up for the higher search costwhen insertions are frequent. Self-organizing lists are simpler <strong>to</strong> implement thansearch trees <strong>and</strong> are likely <strong>to</strong> be more efficient for small lists. Nor do they requireadditional space. Finally, in the case of an application where sequential search is“almost” fast enough, changing an unsorted list <strong>to</strong> a self-organizing list might speedthe application enough at a minor cost in additional code.As an example of applying self-organizing lists, consider an algorithm for compressing<strong>and</strong> transmitting messages. The list is self-organized by the move-<strong>to</strong>-frontrule. Transmission is in the form of words <strong>and</strong> numbers, by the following rules:1. If the word has been seen before, transmit the current position of the word inthe list. Move the word <strong>to</strong> the front of the list.2. If the word is seen for the first time, transmit the word. Place the word at thefront of the list.Both the sender <strong>and</strong> the receiver keep track of the position of words in the listin the same way (using the move-<strong>to</strong>-front rule), so they agree on the meaning ofthe numbers that encode repeated occurrences of words. For example, consider thefollowing example message <strong>to</strong> be transmitted (for simplicity, ignore case in letters).The car on the left hit the car I left.The first three words have not been seen before, so they must be sent as fullwords. The fourth word is the second appearance of “the,” which at this point isthe third word in the list. Thus, we only need <strong>to</strong> transmit the position value “3.”The next two words have not yet been seen, so must be sent as full words. Theseventh word is the third appearance of “the,” which coincidentally is again in thethird position. The eighth word is the second appearance of “car,” which is now inthe fifth position of the list. “I” is a new word, <strong>and</strong> the last word “left” is now inthe fifth position. Thus the entire transmission would be

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

Saved successfully!

Ooh no, something went wrong!