17-5 Competitive analysis of self-organizing lists with move-to-front
A self-organizing list is a linked list of elements, in which each element has a unique key. When we search for an element in the list, we are given a key, and we want to find an element with that key.
A self-organizing list has two important properties:
- To find an element in the list, given its key, we must traverse the list from the beginning until we encounter the element with the given key. If that element is the th element from the start of the list, then the cost to find the element is .
- We may reorder the list elements after any operation, according to a given rule with a given cost. We may choose any heuristic we like to decide how to reorder the list.
Assume that we start with a given list of elements, and we are given an access sequence of keys to find, in order. The cost of the sequence is the sum of the costs of the individual accesses in the sequence.
Out of the various possible ways to reorder the list after an operation, this problem focuses on transposing adjacent list elements-switching their positions in the list—with a unit cost for each transpose operation. You will show, by means of a potential function, that a particular heuristic for reordering the list, move-to-front, entails a total cost no worse than times that of any other heuristic for maintaining the list order—even if the other heuristic knows the access sequence in advance! We call this type of analysis a competitive analysis.
For a heuristic and a given initial ordering of the list, denote the access cost of sequence by Let be the number of accesses in .
a. Argue that if heuristic does not know the access sequence in advance, then the worst-case cost for on an access sequence is .
With the move-to-front heuristic, immediately after searching for an element , we move to the first position on the list (i.e., the front of the list).
Let denote the rank of element in list , that is, the position of in list . For example, if is the fourth element in , then . Let denote the cost of access using the move-to-front heuristic, which includes the cost of finding the element in the list and the cost of moving it to the front of the list by a series of transpositions of adjacent list elements.
b. Show that if accesses element in list using the move-to-front heuristic, then .
Now we compare move-to-front with any other heuristic that processes an access sequence according to the two properties above. Heuristic may transpose elements in the list in any way it wants, and it might even know the entire access sequence in advance.
Let be the list after access using move-to-front, and let be the list after access using heuristic . We denote the cost of access by for move-to-front and by for heuristic . Suppose that heuristic performs transpositions during access .
c. In part (b), you showed that . Now show that .
We define an inversion in list as a pair of elements and such that precedes in and precedes in list . Suppose that list has inversions after processing the access sequence . Then, we define a potential function that maps to a real number by . For example, if has the elements and has the elements , then has 5 inversions , and so . Observe that for all and that, if move-to-front and heuristic start with the same list , then .
d. Argue that a transposition either increases the potential by or decreases the potential by .
Suppose that access finds the element . To understand how the potential changes due to , let us partition the elements other than into four sets, depending on where they are in the lists just before the th access:
- Set consists of elements that precede in both and .
- Set consists of elements that precede in and follow in .
- Set consists of elements that follow in and precede in .
- Set consists of elements that follow in both and .
e. Argue that and .
f. Show that access causes a change in potential of
where, as before, heuristic performs transpositions during access .
Define the amortized cost of access by .
g. Show that the amortized cost of access is bounded from above by .
h. Conclude that the cost of access sequence with move-to-front is at most times the cost of withany other heuristic , assuming that both heuristics start with the same list.
a. Since the heuristic is picked in advance, given any sequence of requests given so far, we can simulate what ordering the heuristic will call for, then, we will pick our next request to be whatever element will of been in the last position of the list. Continuing until all the requests have been made, we have that the cost of this sequence of accesses is .
b. The cost of finding an element is and since it needs to be swapped with all the elements before it, of which there are , the total cost is .
c. Regardless of the heuristic used, we first need to locate the element, which is left where ever it was after the previous step, so, needs . After that, by definition, there are transpositions made, so, .
d. If we perform a transposition of elements and , where is towards the left. Then there are two cases. The first is that the final ordering of the list in is with in front of , in which case we have just increased the number of inversions by , so the potential increases by . The second is that in occurs before , in which case, we have just reduced the number of inversions by one, reducing the potential by .
In both cases, whether or not there is an inversion between or and any other element has not changed, since the transposition only changed the relative ordering of those two elements.
e. By definition, and are the only two of the four categories to place elements that precede in , since there are elements preceding it, it's rank in is . Similarly, the two categories in which an element can be if it precedes in are and , so, in , has .
f. We have from part d that the potential increases by if we transpose two elements that are being swapped so that their relative order in the final ordering is being screwed up, and decreases by two if they are begin placed into their correct order in .
In particular, they increase it by at most , since we are keeping track of the number of inversions that may not be the direct effect of the transpositions that heuristic made, we see which ones the Move to front heuristic may of added. In particular, since the move to front heuristic only changed the relative order of with respect to the other elements, moving it in front of the elements that preceded it in , we only care about sets and . For an element in , moving it to be behind created an inversion, since that element preceded in . However, if the element were in , we are removing an inversion by placing in front of it.
g.
h. We showed that the amortized cost of each operation under the move to front heuristic was at most four times the cost of the operation using any other heuristic. Since the amortized cost added up over all these operation is at most the total (real) cost, so we have that the total cost with movetofront is at most four times the total cost with an arbitrary other heuristic.