# Copyright © 2009 Curt Hill Self Organizing Lists Another form of searchable list.

## Presentation on theme: "Copyright © 2009 Curt Hill Self Organizing Lists Another form of searchable list."— Presentation transcript:

Copyright © 2009 Curt Hill Self Organizing Lists Another form of searchable list

Normal Searched Lists Unlike an array, a list cannot be searched by a binary search A list is sorted only so that search can quit early It is thus it is largely an O(N) activity Since there are plenty of better searches that are O(log N) or better a list is seldom a good choice Copyright © 2009 Curt Hill

Self organizing lists Instead of sorting by key, sort by frequency Drive frequently used items to the front of the list and seldom accessed items to the back of the list A self organizing list modifies itself based on accesses Do this without prior information as to how frequently an item will be accessed –These lists may be kept in either vectors or lists Only uses a sequential search technique Copyright © 2009 Curt Hill

How? Several techniques Keep a count of accesses Move each item accessed to the front Move each item closer to the front Copyright © 2009 Curt Hill

Counts Keep a count of accesses in each record Keep the list sorted by this number of accesses At the beginning the list has no accesses so all counts are zero and hence any order is acceptable Each search then increments the count of just one item Copyright © 2009 Curt Hill

Counts Again Incrementing the count does not provoke a full sort of the list Rather you just move that item forward until its count is larger than the next and smaller than the prior The disadvantage of this approach –Extra storage in the list for the count Once a large number of accesses have occurred things do not move very far Copyright © 2009 Curt Hill

More on counts A number of accesses to an item in a short time normally moves the item –If that number of accesses is small compared to the total number it will not move item very far At the end of the run the list has the optimal static order Possible to save this or a variant of this for next time –Either save the exact counts or the counts divided by some constant Copyright © 2009 Curt Hill

Move to front Each accessed item becomes the first item in the list –At least until the next access Push all subsequent items down a slot Compare this to caching This works much better for lists than vectors since insertion in a list is painless while insertion in a list is painful Copyright © 2009 Curt Hill

Move to Front There is a pathlogically bad case where we always reference the last item –This case is extremely unlikely We typically use self-organizing lists where we have very few items that are very frequently used Copyright © 2009 Curt Hill

Transposition Every time a record is accessed swap it with the item before it Frequently used items will migrate toward the front and seldom used ones will move toward the back One item never changes the list much Modification: If this is an array you may want to swap the item with something closer to the front, such as halfway to the front rather than move it just one slot Copyright © 2009 Curt Hill

Example –1 is 30% –1 is 25% –1 is 20% –1 is 5% –1 is 4% –1 is 3% –2 are 2% –1 is 1.5% –1 is 1% –5 are.5% –2 are.3 % –4 are.2% –23 are.1% –6 arr.05% Copyright © 2009 Curt Hill A list of 50 items with widely differing frequencies.

Do the math Assume that the self-organizing list achieves optimal static order –The first three items account for 75% of the searches The average searches is the sum of the frequency times the position divided by total searches In this case the average search length is 3.9965 An log 2 N search would give a length of 9.9657 Copyright © 2009 Curt Hill