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.

312 Chap. 9 Searching<strong>and</strong> the total 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 total 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 situ<strong>at</strong>ions inwhich self-organizing lists prove a valuable tool. Obviously they have an advantageover sorted lists in th<strong>at</strong> they need not be sorted. This means th<strong>at</strong> the cost to inserta new record is low, which could more than make up for the higher search costwhen insertions are frequent. Self-organizing lists are simpler to implement thansearch trees <strong>and</strong> are likely to be more efficient for small lists. Nor do they requireadditional space. Finally, in the case of an applic<strong>at</strong>ion where sequential search is“almost” fast enough, changing an unsorted list to a self-organizing list might speedthe applic<strong>at</strong>ion enough <strong>at</strong> 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-to-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 to the front of the list.2. If the word is seen for the first time, transmit the word. Place the word <strong>at</strong> 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-to-front rule), so they agree on the meaning ofthe numbers th<strong>at</strong> encode repe<strong>at</strong>ed occurrences of words. Consider the followingexample message to 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 <strong>at</strong> this point isthe third word in the list. Thus, we only need to 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 beThe car on 3 left hit 3 5 I 5.

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

Saved successfully!

Ooh no, something went wrong!