# Impact on the order of elements on the cost of searching in a linked list

I have the following homework question that I am struggling with. I have read the corresponding chapter from the book, but no guidance there.

Consider a linked list $X: X_1 \to X_2 \to X_3 \ldots$. Assume that the cost of examining a particular element $X_i$ is $C_i$. Note that to examine $X_i$, one needs to scan through all elements in front of $X_i$. Let $P_i$ be the probability of searching for element $X_i$, so the total cost for all searches is $$\sum_{j=1}^{n} \left( P_j \cdot \sum_{i=1}^{j} C_i \right)$$

1. Show that storing elements in non-increasing order of $P_i/C_i$ does not necessarily minimize the total cost.

2. Show that storing elements in non-decreasing order of $P_i$ does not necessarily minimize the total cost.

Any help and direction how to approach the problem will be highly appreciated.

• You need to try a few examples until you hit one in which the orders in 1 and 2 are not optimal. Keep trying. – Yuval Filmus Mar 4 '14 at 14:35
• Brute force sounds wrong approach to me! – setlio Mar 4 '14 at 16:25
• I never saw that problem, and I have no a priori idea about the proof. But the use of the words "non-increasing" or "non-decreasing" is somehow attracting my attention. So, for each question, I would try with two elements that have the same ranking, but different values for $C_i$, and check what happen for the 2 possible orders. – babou Mar 4 '14 at 18:19
• @setlio It's a general approach for finding counterexamples. You try a few examples, see why they don't work, and then try to compose an example that does work. Alternatively, try to prove that what they ask is impossible, see where the proof fails, and use this to guide your construction. – Yuval Filmus Mar 4 '14 at 18:53
• try to find out about the following: what would be the motivation for the suggested ordering? what could cause that motivation to fail (general idea: there are other factors beyond those that motivated the ordering which affect optimality) ? identify degrees of freedom in the construction of samples - how do different realizations thereof affect optimality? furthermore, identify any special cases the assumptions comprise ( general idea: try to re-phrase the problem in simpler terms. what cases will be excluded? investigate them. do the formulae involved simplify for the special cases ? ). – collapsar Mar 6 '14 at 14:04

I'm assuming costs are strictly positive. Otherwise it doesn't make much sense to consider the quantity $P_i/C_i$.
Regarding 1: Consider any order, and suppose that for some $i$, $P_i/C_i < P_{i+1}/C_{i+1}$. Suppose that we switch the elements $i$ and $i+1$. Say that we switched between $\ldots,A,B,\ldots$ and $\ldots,B,A\ldots$. Let $\Delta$ be the difference in average search cost (new cost minus old cost). When looking for $A$, which happens with probability $P_i$, we have to pay $C_{i+1}$ more. When looking for $B$, which happens with probability $P_{i+1}$, we have to pay $C_i$ less. So $$\Delta = P_i C_{i+1} - P_{i+1} C_i = (P_i/C_i - P_{i+1}/C_{i+1}) C_i C_{i+1} < 0.$$ In other words, the switch was beneficial. We conclude that the optimal arrangement is to store the elements in non-increasing order.