How often should you clean your room?

We introduce and study a combinatorial optimization problem motivated by the question in the title. In the simple case where you use all objects in your room equally often, we investigate asymptotics of the optimal time to clean up in terms of the number of objects in your room. In particular, we prove a logarithmic upper bound, solve an approximate version of this problem, and conjecture a precise logarithmic asymptotic.


Introduction
Suppose you have n objects in your room which are totally ordered. For simplicity, let us say they are books on shelves alphabetized by author and title. If you are looking for a book (assume you remember the author and title, but not its location on the shelves), the most efficient algorithm is a binary search. Namely, look at the book in the middle of the shelf, and because of the ordering, now you can narrow your search by half. Repeat this process of halving your search list, and you can find your book in about log 2 n steps. (Here is perhaps a better model for humans naturally search: go to where you think the book should be, scan that area, and if need be jump to a different area based on the ordering. However a logarithmic cost still seems like a good model for this process.) The theory of searching (and sorting) algorithms is of course well studied in computer science-what is not, however, is what happens after that for humans. Namely, after you are done with your book, you can do one of two things: either put it back on the shelf, which we will also say takes about log 2 n time, or leave it on your desk, which takes no time. The latter is of course more efficient now, but if you keep doing this, eventually all of your books will wind up as an unsorted pile on your desk. Then when you search for a book, you essentially have to go through your pile book by book (a sequential, or linear, search), which takes about n 2 time, and thus is not very efficient for n large. The question we are interested in here is: when is the optimal time to clean up? That is, over the long run, what is the optimal value m opt (n) of m (1 m n) at which you should put all the books in the pile back, in order, on the shelf, in the sense that the the average search plus cleanup cost (per search) is 1 General Setup

The Statement of the Problem
We now make a general formulation of our problem, which we call a search with cleanup optimization problem.
Let X = {X 1 , . . . , X n } be a finite set of distinct well-ordered objects, which we view as a probability space with probability measure µ. We consider the following discrete-time Markov chain, depending on a parameter 1 m n.
1. At time t = 0 each X i is in a sorted list (i) L, and there is an unsorted pile P which is empty.
2. At any time t ∈ {0, 1, 2, . . .}, each X ∈ X is in exactly one of L and P, i.e., X is a disjoint union X = L P.
3. At any time t 1 with |P| < m, exactly one object X = X i is selected, and X is selected with probability µ(X). If the selected X ∈ P, nothing changes. Otherwise, then X is removed from L and added to P.
(i) By list, we mean an indexed list, rather than a linked list. 4. At any time t, if |P| = m, we stop the process.
This process has finite stopping time with probability 1 provided at least m elements of X have nonzero probabilities, which we will assume.
Note the state of the process at time t is described simply by a subset P of X, together with a marked element X it (the object selected at time t 1). The set of possible states is then simply all subsets P of X, together with a marked point X it , of cardinality at most m.
Associated to this process are two (nonnegative) cost functions, S(X; P) and C(P), which do not depend upon t. Here X ∈ X and P ⊂ X. The functions S(X; P) and C(P) are called the search and cleanup costs.
Let X m = X m,n denote the set of finite sequences χ = (X i1 , . . . , X i ) in X such that (i) the underlying set {X i1 , . . . , X i } has cardinality m, and (ii) X ij = X i for j < . We extend the measure µ to a probability measure on X m by µ(χ) = j=1 µ(X ij ). (1.1) Note the sequences χ ∈ X m are in 1-1 correspondence with the possible paths of finite length for the Markov process from the initial state up to the stopping state described in Step 4. Namely, for t = 0, . . . , , let P χ (t) denote the set of elements {X i1 , . . . , X it } (here P χ (0) = ∅). Thus P χ (t) represents the "unmarked" state of the process from time t = 0 until the stopping time t = . Furthermore each µ(χ) is the probability of that path for the process. For example, suppose n 3, m = 3 and χ = (X 1 , X 2 , X 1 , X 3 ). This corresponds to selecting X 1 at time 1, X 2 at time 2, X 1 at time 3, and X 3 at time 4, after which the process stops, since we have selected m = 3 distinct objects. Specifically, at t = 1 we have P = P χ (1) = {X 1 }; at time t = 2 we have P = P χ (2) = {X 1 , X 2 }; at time t = 3, we have P = P χ (3) = {X 1 , X 2 }; (unchanged); and at the stopping time t = 4, we have P = P χ (4) = {X 1 , X 2 , X 3 }. If µ is the uniform distribution on X, the probability of this path is µ(χ) = 1 n 4 . Given χ ∈ X m , we let (χ) be its length, i.e., the corresponding stopping time, and write χ = (χ 1 , χ 2 , . . . , χ ) where = (χ). Now we extend S(X; P) and C(P) to χ = (X i1 , . . . , X i ) ∈ X m by and These values are called the total search and total cleanup costs for the path χ.
We want to optimize the average total cost function Assume µ(X) = 0 for each X ∈ X.
In the event that there is more than one such minimizing m-which we do not typically expect-we may take, say, m opt (n) to be the smallest such m, so that m opt (n) is a well-defined function.
Here we will study the asymptotic behavior in the simple case of µ being a uniform distribution on X as n = |X| → ∞ for certain cost functions S and C specified below. We note the Markov process we consider arises in the coupon collector's (or birthday) problem, and more generally, sequential occupancy problems (see, e.g., [6] or [4]). The cost functions, however, make the analysis of this problem much more delicate than occupancy problems typically studied in the literature. It turns out that the expected value of the reciprocal of the waiting time in a sequential occupancy problem plays a key role in our analysis. Several results for the expected value of the waiting time itself are known (e.g., see [1]), but not, to our knowledge, for its reciprocal.

Models and Cost Functions
From now on, we assume µ is the uniform distribution on X unless explicitly stated otherwise. There are four reasonable, simple search models to consider, all based on doing a binary search on L and a sequential search on P. Here we view P as an unordered set. The models depend upon whether the positions of L (the "shelves") are numbered or not and whether the process is memoryless or not. These models correspond to the following search algorithms A for an element X of L P.
For a memoryless process, at any time t, we assume we do not know what elements are in P, i.e., we do not remember the state of the system. Thus it is typically worthwhile to search L first, as searching L is much more efficient than searching P. Hence for a memoryless process, we will always first search L for X. If this search is unsuccessful (i.e., X ∈ P), then we search P.
At the other extreme, one can consider the process where one has complete memory, i.e., at any time t, we know the state P of the system. Thus if X ∈ L, we simply search L, and if X ∈ P, we only search P.
The other option in the model depends on the data structure for L. Imagine X 1 , . . . , X n are books with a total ordering. The X i 's in L are the books that are ordered on bookshelves, whereas the X i 's in P lie in an unorganized pile on a desk. If there is a marking on each shelf for the corresponding book, so each book has a well defined position on the shelf, we say the shelves are numbered. In this case, we think of L as a list of size n indexed by keys k 1 < k 2 < · · · < k n , where k i points to X i if X i ∈ L, and k i points to null if X i ∈ P, and a search on L, amounts to a search on n keys, regardless of how many objects X actually remain in L. Otherwise, the shelves are unnumbered, so only the relative position of the books on the shelves is important (akin to books shelved in a library stack). Here we simply view L as a sorted binary tree, and a search on L is really a search on the |L| objects in L.
While shelf positions are not typically numbered for books, this situation of "numbered shelves" commonly occurs in other situations, such as a collection of files each in their own labelled folder jacket. Namely, you may take out a file to look at, but leave the folder jacket in place so there is a placeholder for where the file goes when you put it back.
With these models in mind, the four search algorithms A for an object X in L P can be described as follows.
• M 1 (No memory, unnumbered shelves) A: do a binary search on the |L| objects in L; if this fails, then do a sequential search on P • M 2 (No memory, numbered shelves) A: do a binary search on the n keys to find the correct position for X in L; if it is not there, do a sequential search on P • M 3 (Complete memory, unnumbered shelves) A: if X ∈ L, do a binary search on the |L| objects in L; if X ∈ P, do a sequential search on P • M 4 (Complete memory, numbered shelves) A: if X ∈ L, do a binary search on the n keys for L; if X ∈ P, do a sequential search on P Each of these algorithms naturally gives rise to a search cost function S(X; P) where X ∈ L P, namely the number of comparisons needed in this algorithm. However, it is not necessary for us to write down these functions explicitly. Rather, it suffices to explicate the following average search cost functions. (In fact, one could replace the exact search cost S(X; P) by a certain average search cost and be left with the same optimization problem-see Section 5.) Let s L (j) denote the average cost of a search for an object in L when L contains n − j elements (we average over both the n choose n − j possibilities for L and the n − j possibilities for the object). Similarly, let s P (j) denote the average cost of a search for an object in P given P contains j objects (again averaging over all possibilities for P and the object).
We define the following average search cost functions for successful binary, failed binary and sequential searches on j objects: The formula for s(j) is of course exactly the expected number of steps required for a successful sequential search. It is easily seen that when j + 1 is a power of 2, b(j) (resp. b f (j)) is the exact expected number of steps required for a successful (resp. failed) binary search on j objects. These functions are not quite the exact average number of steps for binary searches for all j (they are not generally rational), but as we are primarily interested in asymptotic behavior, we will work with the functions given above for simplicity. (ii) Note that b(2 r+1 − 1) − b(2 r − 1) < 2 and is in fact close to 1 for large r. So b(n) in general is a reasonable approximation of the expected cost for a successful binary search.
Then, for the above four algorithms A, the functions s L (j) and s P (j) are given as follows.
• M 1 (No memory, unnumbered shelves) • M 3 (Complete memory, unnumbered shelves) • M 4 (Complete memory, numbered shelves) (1.9) s P (j) = s(j) Remark 1.1 If µ were a highly skewed distribution, then it might be more efficient in the no memory models to do the pile search before a list search (see Section 5).
We now define our cleanup cost functions, based on the simple algorithm of doing a binary search for each object in P to find the appropriate position to insert it into L. (Even if one remembers the general area where the object should go, there is still the time needed to identify/arrange the exact spot and the time to physically place it there, and a logarithmic cost seems like a reasonable model for this.) This leads to two different possible cleanup cost functions, corresponding to the cases of numbered and unnumbered shelves.
If the shelves are numbered, then the cleanup cost should just be the search cost to find the correct position for each object in P, and it makes sense to set where S(X; ∅) denotes the search cost to find the position in L for X. Note that there is no dependence upon what order we replace the objects. However, we can make things a little easier on ourselves if we wish. Since we will just be considering an average of C(P) over χ (weighted by 1 (χ) ), it will suffice to consider an average cleanup cost Hence we have If the shelves are unnumbered, then the cleanup cost in fact depends upon the order we replace the objects. Let us write P = {X i1 , . . . , X im } and suppose we place them back in order X i1 , . . . , X im . Write S L (X) for the cost of a (failed if X ∈ L) binary search on L for the object X. Then the orderdependent cleanup cost is Since P is unordered, we consider all cleanup orderings to occur with the same probability. Hence it suffices to consider an average over all possible orderings: where (X i1 , . . . , X im ) runs through all possible orderings of P.
As before, since we will be taking an average of our cleanup costs over χ (weighted by 1 (χ) ), we can consider the simpler quantities as in the numbered case. By additivity of the expected value, one sees As with S(X; P), we could replace the exact cleanup cost C(P) with its average over all subsets of size P (cf. (5.2)).

Remarks
1.2 This is not the only reasonable way to clean up. One could first sort the objects in P, which can be done in O(m log m) time, though the way humans naturally sort is perhaps better modeled by insertion sort, which takes O(m 2 ) time. Then one can merge L and P in O(n) steps, as in a linear merge sort. This is more efficient than our above algorithm if m is relative large and one efficiently sorts P. Since our optimization problem is one in which m should be at most logarithmic in n (cf. Proposition 2 and Remark 1.6), our cleanup algorithm above is more efficient.

1.3
Alternatively, one could do a binary-search-based merge sort after sorting P as follows. Say the ordering on X is X 1 < X 2 < · · · < X n . Let X j1 , . . . , X jm be the elements in P in sorted order, i.e., j 1 < j 2 < · · · < j m . First do a binary search to insert X j1 in L. Then do a binary search on L ∩ {X j1+1 , X j1+2 , . . . , X n } to find the position for X j2 . Continue in this manner of binary searches on smaller and smaller subsets of L, to replace all m objects. This may more be efficient than the cleanup algorithm we are using, depending on how we sort P and the relative size of m and n, and it may be interesting to study our optimization problem with this type of algorithm. However, it is only slightly more efficient when m is relatively small compared to n: suppose m ≈ log n and one does an insertion sort on P; the insertion sort alone takes O(log 2 n) time, which is the same order as our original cleanup algorithm. In light of the additional complications it brings, we do not consider this type of cleanup here.

1.4
One could also consider partial cleanups, where one does not put back all objects at the same time, but only some of the items in P. We do not wish to consider such complications here. Moreover, as it typically takes time and effort for humans to switch between tasks, there seems to be extra efficiency in practice if one clean up all at once (or in a few chunks), than in many small steps.
1.5 This model assumes all objects are in relatively close proximity, as in your room. If one wanted to consider a similar problem for objects in large library or warehouse, one should include the cost of transit time for retrieving and putting back the objects in the functions s L and C(P). The transit time should be O( √ n) assuming the objects are organized in a 2-dimensional grid, or at least 3-dimensional with bounded height.

Overview
Intuitively, there are three reasons why it may be better to wait to cleanup, i.e., why m opt (n) might be greater than 1. Assume n is large.
(i) If one has complete memory and there are relatively few objects in the pile, the search cost for an object in the pile will be less than the search cost for a random object in the list.
(ii) If the shelves are not numbered and there are relatively few objects in the pile, one will almost surely be searching for objects which are in the sorted list, and this will go slightly faster if there are less than n objects in the list.
(iii) In all four of the above models, the average cleanup cost per search should decrease as m increases.
Thus in the case of complete memory, it is rather evident that we should have m opt > 1. On the other hand, in the case of no memory, if one searches for an object in the pile, one first has to do a binary search on the list, which costs more than just searching for a random element in the list. So in the case of no memory, unnumbered shelves, it is not a priori obvious whether this factor or points (ii) and (iii) will win out. This is settled by our first result, which says one should never clean up immediately.
This is not hard, and we provide two proofs: one by computing F (1) and F (2) explicitly in each model (see Section 2.3), and another by observing F (m) < F (1) whenever m < 4b(n − m) (see Lemma 2.13).
In Section 3, we restrict ourselves for simplicity to the case of complete memory and numbered shelves (model M 4 ). An upper bound for m opt is not too difficult, since after the pile is a certain size, each search will have an associated cost that is at least F (1). Specifically, we show The problem of obtaining a good lower bound seems much more difficult, and we use some bounds shown in Section 4 to construct an approximation F (m) for F (m) such that the (smallest if not unique) value m opt (n) (see Section 2 for the definition) of m which minimizes F (m) should satisfy m opt (n) m opt (n) (Conjecture 4.2). While we can compute m opt (n) for fairly large n fairly quickly, the amount of time required to compute m opt (n) is significant, so we can only compare values of these functions for relatively small n (see Table 4.1), but it appears that m opt (n) ∼ m opt (n). Given that this is the case, one would like to determine m opt (n).
and, asymptotically, We briefly touch on the amount of cost savings in this optimization problem in Remark 4.8. Finally, in Section 5, we make some comments about the problem for non-uniform distributions. In particular, we expect that, as one varies the underlying distribution, m opt (n) is maximized for the uniform distribution.
Remark 1.6 Based on the above factors, one would expect that the optimal cleanup point should be greater in the case of complete memory versus no memory, as well as in the case of unnumbered shelves versus numbered shelves. Consequently, we expect that m opt (n; M 1 ) m opt (n; M 2 ) m opt (n; M 4 ) m opt (n; M 3 ).
We verified this numerically for small n, but we do not focus on this here. In particular, we note that preliminary numerics for M 3 suggest m opt (n) ∼ 4 log 2 (n) (Remark 4.7). (In this paper, by "numerical calculations" we mean that we used high-precision floating point calculations in PARI/GP, and not to mean that our calculations were provably correct.)

Expectation costs
In this paper, m and n denote integers satisfying 1 m n. Further, unless otherwise specified, χ will denote a path in X m . If f is a function on X m , we sometimes denote E[f ] by E m [f ] to specify m, or E m,n [f ] if we want to specify both m and n.
In this section, we decompose where the terms on the right will represent average list search, average pile search and average cleanup costs. We will analyze these terms individually. (In the case of no memory, where one does a list search then a pile search for an object X ∈ P, we include both of these search costs in the function F P .) It appears that F L and F C are increasing in m, whereas F P is decreasing in m (cf. Remark 2.7 and Lemma 2.8). We also expect that F is unimodal-initially decreasing, then increasing. Thus our optimization problem is about the question of when F P begins increasing faster than F L + F C decreases.

Expected search cost
In this section, we want to find a way to calculate E S . We can reduce this to studying averages of the form where Namely, note the probability that χ ∈ X ( ) m depends only on , and is Here m denotes the Stirling number of the second kind, i.e., the number of ways to partition a set of elements into m nonempty subsets.
In other words, S L (χ) (resp. S P (χ)) is the total cost of searches along χ when the sought-after object is in L (resp. P).

Lemma 2.2 For any χ ∈ X
( ) m , we have the following average cost formulas: Proof: To see the first equality, observe that for any χ, there must be exactly one search for an object X ij which is in L when L has n − j objects for each j = 0, 1, . . . , m − 1. Fixing one such j and averaging the contribution of this search cost over the permutations σ yields s L (n − j). The second equality is similar. 2 This yields the following expected cost formulas: Consequently, one has

Expected cleanup cost
The expected cleanup cost per item is simply where C m is as in (1.10) or (1.11) according to whether the shelves are numbered or not.
With this notation, the expected search-and-cleanup cost is Note that in the case of numbered shelves, we have In the case of unnumbered shelves, and

Consequently, we have
We remark that this implies F C (m) F L (m).
In any case, we have reduced our problem to studying the expected list search cost F L (m) and F P (m).

Some simple calculations
Here, we calculate F (1; n) and F (2; n) for each of the four models discussed above, which we hope will be instructive. In all cases, these calculations, together with the observation (2.6) that E 2 1 < 1 2 , imply that F (2; n) < F (1; n) for all n 2, giving one proof of Proposition 1. We remark the proof of this inequality does not depend on the specific definitions of the functions b(j) and b f (j), just that these functions are increasing. In fact, it does not depend upon the definition of s(j) either, as long as s(1) 0.

Calculations for m = 1
First consider m = 1. Then X 1 = X

Unnumbered shelves
Suppose we have unnumbered shelves, i.e., M 1 or M 3 . Then

Numbered shelves
Suppose we are in the case of numbered shelves, i.e., M 2 or M 4 . Then where there are − 1 occurrences of X 1 . Then As an aside, we note that the equality in (2.6) yields the closed form expression

Expected list search cost
We now return to studying search costs, in particular we consider the expected list search and cleanup costs, F L (m) and F C (m). Since studying these quantities reduces to studying Note this is the expected value of the reciprocal of a waiting time for a sequential occupancy problem.
First we obtain the following finite formula, which allows us to compute E m 1 quickly.

Lemma 2.4 We have
Note the first term can be interpreted as the j = 0 term for the sum on the right.
Proof: The generating function for Stirling numbers of the second kind is given by . (2.10) We compute the integral using partial fractions.
, and so we have We can simplify this sum a little by observing that m!
Then the first part of the above summation simplifies to m n Putting all this together yields the lemma. 2 Since the above formula is an alternating sum, it not so useful in studying the behavior of E m 1 as m varies, which is our goal, though it is useful for numerics. Now we observe some elementary bounds.
Lemma 2.5 For 1 m n, (We interpret the leftmost term as 0 when m = n.) Proof: As is well known, is a sum of m independent geometric distributions with means n n , n n−1 , . . . , n n−m+1 . Thus where H j is the j-th harmonic number. This implies the first inequality. The second is Jensen's inequality. The third follows as (χ) m for any χ ∈ X m . 2 If n → ∞ and m grows slower than √ n, → E m [ ] in probability [1]. Thus we might expect 1 Em[ ] to be a good approximation for E m [ 1 ], as the following bound shows.
For 1 < m n, Proof: Let x > 0. Chebyshev's inequality tells us Consequently, for any y Var( ) Now we apply (2.14) with which is negative if m < √ 2n, and one gets the first part. The second part follows from the crude bound 2 Remark 2.7 (i) The preceding two lemmas imply that E m 1 is decreasing in m for 1 m < √ n. However, note that it follows easily that E m 1 is decreasing in m for all m 1 because of the inequality of probabilities, µ m ( X) µ m−1 ( X) for all X.  In the case of unnumbered shelves, we may take = 0, i.e., for m 0 sufficiently small relative to n, F L (m; n) and F C (m; n) are decreasing in m for 1 m < m 0 .
Proof: It is easy to see that (e.g., by Lemma 2.5 or [1]), for fixed m,

Expected pile search cost
Now we consider the expected pile search cost First we remark the following explicit formula for the expected values in the inner sum.
Proof: If = m, then τ j (χ) = 0 for all χ ∈ X ( ) m and the formula trivially holds, so suppose > m and let χ ∈ X ( ) m . If r = τ j (χ) 1, we can remove the element at position t j+1 − 1 to get an element χ ∈ X This map from χ to χ is a j-to-1 surjective map, i.e., for j, r 1 we have Summing over all r 1, we see Similarly if r k we can remove the last k elements before position t j+1 to get a j k -to-1 map into X µ{χ ∈ X ( ) m : τ j (χ) k} and apply Proposition 2.1. 2

Consequently, we have
This expression allows us to get the following upper bound.
Proposition 2.10 For 1 j m − 1, the covariance Cov(τ j , 1 ) < 0, i.e., That this is equivalent to the condition of negative covariance asserted above follows as 2 Remark 2.11 Suppose n 4 and 1 j < m < n. Then numerically it appears that This would imply that F P (m) is increasing in m, and, in the case of complete memory, Lastly we note Lemma 2.12 We have Proof: This follows from the observation that 2. In the case of numbered shelves, if m < 4b(n), then F (m) < F (1).

An upper bound
For simplicity now, we will assume we are in model M 4 (complete memory, numbered shelves), though a similar argument can be used for M 2 as well. In this case we have Recall that F (1) = 2b(n). Thus, once the pile P has more than 4b(n) elements, a single average pile search must cost more than F (m opt (n)). This idea gives the following upper bound.
We will first prove a lemma. We say a function f : X m → R is additive if, for any χ = (χ 1 , . . . , χ ) and any 1 k < , we can write f as a sum of terms where the f (χ j ; P χ (j − 1)) depends only upon χ j and what is in the pile before time j. We can naturally restrict such functions f to functions of X k for k < m. Note that all the cost functions we considered above are additive, and any linear combination of additive functions is additive.
Let m k 1. Define a restriction map R m k : X m → X k , given by We let T m k : X m → ∞ k=1 X k be the truncated tail from the restriction map, i.e., T m k χ = (χ t k (χ)+1 , . . . , χ (χ) ).
Put P χ,k = P χ (t k (χ)) to be the pile after time t k (χ).
for all χ ∈ X m and X i ∈ X, then If, further, the inequality in (3.1) is strict for some χ and X i , then the inequality in (3.2) is also strict.
Proof: Since f and are additive, we can write .
Then the above condition guarantees, for any χ, Proof Proof of Proposition 3.1: Set k = 4b(n) and let k < m n. LetS L (χ) (resp.S P (χ)) be the average of S L (χ σ ) (resp. S P (χ σ )), where σ ranges over Sym(X). Let f = 2S L +S P , so Then note that Furthermore, this inequality must be strict for some χ (in fact, one only gets equality when n + 1 is a power of 2 and t j (χ) = j for j < k).
On the other hand, for any X i ∈ X, we have f (X i ; P χ,k ) 2b(n) (with equality if X i ∈ P χ,k ). Applying the above lemma, we see F (m) > F (k). 2

An approximate problem
Here we make a conjectural lower bound and asymptotic for m opt (n) by comparing our problem with a simpler optimization problem. We continue, for simplicity, in the case of M 4 , though similar approximate problems could be considered for M 1 , M 2 and M 3 also. Based on the bounds for E m [ 1 ] and E m [ τj ] above, we consider the approximate expectation cost Specifically, Lemma 2.5 and Proposition 2.10 imply F L (m) F L (m) and We suspect that the approximation F P (m) is much closer to this upper bound for F P (m) than F P (m) itself is, and so we should have F P (m) F P (m). This is supported by numerical evidence. (See Table 4.1 for some numerical calculations.) Moreover, since conjecturally F L (m) is decreasing in m (and, empirically, faster than F L (m) is), while F P (m) is increasing in m (and, empirically, slower than F P (m)), we make the following conjecture. Let m opt (n) be the value of m ∈ {1, 2, . . . , n} which minimizes F (m). We call the problem of determining m opt (n) an approximate search with cleanup problem.
Remark 4.1 Recall that for m opt (n), we used the function b(n) from (1.5), is an exact average search cost if n is a power of 2 and approximate otherwise (since we are working with a complete memory model here, b f (n) does not arise here). It is then natural to wonder whether using an exact formula for b(n) for all n makes much of a difference. This is relevant for small n since, but should not matter for asymptotics for large n. However, choosing b(n) as in (1.5) does not make much of a difference for small n either; see the emboldened entries the third row in Table 4.1 for the differences. The optimal value in the case of exact average search cost functions is labeled m exact opt (n). In other words, for n 5, m opt (n) is either 3b(n) − 3 2 or 3b(n) − 3 2 + 1. We note that in fact both possibilities of this proposition occur: sometimes m opt (n) is 3b(n) − 3 2 and sometimes it is 3b(n) − 3 2 + 1, though numerically there seems to be a tendency for m opt (n) to be in the right half of this interval, i.e., most of the time m opt (n) > 3b(n) − 1 2 . Proof: Let 1 m < n. We want to investigate when the difference The left hand side of (4.2) is whereas the right hand side of (4.2) is Proof: All nonzero terms of the sum are positive (resp. negative) if b a (resp. b 0), so assume 0 < b < a. Now note the above sum is negative if and only if

Hence (4.2) is positive if and only if
This is certainly the case if

4.7
For M 3 , calculations also say that m opt (n) = n for n 8, but here it is only better to never clean up if n 2. Furthermore, calculations suggest that m opt (n) ∼ 4b(n) for M 3 .

4.8
To see how close F (m) is to F (m), we plotted both for n = 20 in Figure 1. Note that there is significant cost savings to be had by waiting until m opt to clean up. However, for large n, the graph of F (m) will be more lop-sided, i.e., the right endpoint F (n) will be much larger than the left endpoint F (1). It is not feasible to compute all values of F (m) for some large n, but we graph F (m) for n = 100 in Figure 2. We expect that the graph of F (m) will have a similar shape, and that for large n, the cost savings of waiting until m opt to clean up is proportionally smaller. However, the cost savings should be more pronounced for non-uniform distributions.

Non-uniform distributions
Finally we comment on the problem for general probability distributions on X. Now if one defines the cost functions S(X; P) and C(P) using algorithm A as in Section 1.2, these cost functions do not just depend upon the multiset of probabilities {µ(X i )}, but upon the specific distribution. Example 5.1 Fix 1 r n and 0 1. Now take the distribution given by µ(X r ) = 1 − and µ(X i ) = /(n − 1). Assuming is small, then most of the time one will be searching for X r . Depending on what r is, the search (as well as cleanup) cost associated to X r might be as low as 1 or as high as b f (n) ≈ log 2 (n). Hence, at least for certain values of and n, one might expect the answer to the associated search with cleanup optimization problem depends upon the choice of r.
Therefore, we define our cost functions not using the exact search costs given by algorithm A, but rather on the associated average search costs. Specifically, in the complete memory case, we set S(X; P) = s L (n − |P|) X ∈ P s P (|P|) X ∈ P (5.1) and C(P) = |P| j=1 s L (n − j).

(5.2)
In the case of the uniform distribution on X, this gives us the same optimization problem we studied above.
Note that in the case of no memory, it may be better to always search the pile first, depending on how skewed the distribution is. For instance, in Example 5.1, if is sufficiently small, then with high probability at any t 1, we will be looking for X r and it will be in the pile. Thus we should always search the pile first. Furthermore, by this reasoning (in either the complete or no memory case), for small enough, we should clean up whenever another object gets in the pile, i.e., m opt (n) = 2.
Consequently, we can decompose the average total cost as in the uniform case though now the quantities E m 1 and E m τj will be more complicated. In this case, the probability functions for the underlying Markov process will follow more general sequential occupancy distributions (see, e.g., [6] or [4]).
Note that for a nonuniform distribution, typically objects with higher probabilities will be in the pile at any given time, so the pile search costs will be higher than in the uniform case. Put another way, the expected waiting time E m [ ] until cleanup is minimized for the uniform distribution (see, e.g., [7], [5], [3] and [2] for results on E m [ ]). Therefore, the more skewed the distribution is, the faster the probabilities E m τj should be increasing in m, i.e., the smaller m opt (n) should be, as indicated in our example above. In particular, we expect m opt (n) is maximized for the uniform distribution.