← All problemsFree · Python · 45 min

Algorithm design / guarantees under an adversary

The Shelf of Binders

A shelf of binders, a cost for every binder you reach past, and a rule for reordering after each request. Break Transpose, measure move-to-front against its cruellest sequence, and write a rule that stays within twice the best fixed order.

0 / 3 graded parts passSaved in this browser · loading saved work…
Jump to your work ↓

A lab keeps its binders on one shelf, and people ask for them one at a time. Reaching the binder in position i (counting from 1 at the left) costs i: you pass i−1 binders to get to it. After each request you may slide the binder you just used forward, to any place to its left, for free. The other binders keep their order. You never know the next request.

request binder 4: it is in position 4, so the request costs 4shelf12345move-to-front41235transpose12435either way the move is free; the next request pays the new position
One request, two rules.

Which rule keeps the total cost low?

  • Move-to-front: put the requested binder first.
  • Transpose: swap it with the binder just in front of it.
  • Frequency count: keep the binders sorted by how often they have been asked for.

The benchmark is the best fixed order in hindsight: the order that, knowing all the requests in advance, sorts the binders by how often they were asked for, and never moves them. It is not the best offline algorithm that may rearrange as it goes, the rival in Sleator and Tarjan's theorem that move-to-front is 2-competitive. That theorem also counts costs differently (it charges for swaps), so its ratios and this problem's do not compare directly.

In code, a rule is move(order, x): given the current order (a list of binder numbers) and the requested binder x, it returns the new order.

Background: Online Algorithms.

easy · Part A

Make Transpose pay the most

Five binders, numbered 1 to 5 and shelved in that order. Give at least 10 requests on which Transpose pays 5 for every request.

Explore in the sandbox

Experiment here, then copy a rule into the editor and test its guarantees.