In computer algorithms, there is this thing called ‘**soft heap**‘, due to Princeton computer scientist Bernard Chazelle (2000), which is basically corrupting a certain percentage of data when building the data structure. When first heard of that, I can’t imagine how on earth could such a thing be anywhere useful.

Indeed it is, and the reason it works is quite interesting to me. Hence I’m writing this post about finding the (real) -th smallest element in a set via this corrupted data structure. By the way, this was presented to me in a very interesting lecture by Robert Tarjan. For you computer scientists: be warned, I really don’t know much about the subject.

**Part 1: Heap**

Naively sorting a set (i.e. compare the first two elements, put the smaller first; compare the 2nd and 3rd, move the third past the 2nd if it’s smaller; then compare the 3rd and the first, etc.) could take many comparisons. One can of course do better, the current known fastest sort takes operations.

One way to achieve is the so-called **heap sort**:

A **heap** is a binary tree with a number (key) in each node and the child of a node always has key larger than that of the node. (i.e. the minimum element is at the root)

Now the problem of sorting breaks into two parts:

i) build a heap with our given keys

ii) sort elements in a heap

As we can see, if we manage to build a heap with all branches having roughly the same length (computer scientist call that a **balanced binary tree**), there will be elements at level . The height of the heap is . Main idea of the sort is to go through all elements and aim for doing a constant times the height many operations at each element (then we would get our ).

**Building the heap:** Suppose we already have the first element put in a heap, occupying all nodes in the first levels and some nodes in the -th level. We put the -th element at the first empty node on the -th level; compare it to its parent, swap if it’s smaller than it’s parent; compare to its parent again… Do this until it’s larger than it’s parent. Note that 1) swap does not change the shape of the tree. 2) when the process stops, we end up with a new heap. Hence inserting this new element takes at most twice the height many operations (compare and swap once at each level).

In total, building the heap takes no more than operations. (in fact one can do this in time, but for our purpose it’s not needed)

**Sort from the heap:** Take the root as the minimum element, delete it from the heap; now compare the two elements in level 1, put the smaller into the root, now compare the two children of the empty node, etc. (if at some stage the empty node has only one child then just move it, stop if the empty node has no child.) At the end, delete the leaf node that’s empty. Again this process takes no more than

twice the height of the tree many operations.

Combining the above two steps, we have operations.

As one can guess heaps does much more than sorting stuff, once we spent our time to build data into a heap, we can save such structure and update it once the data set changes:

**Inserting element:** (put the element at the next available leaf and move upwards)

**Find minimum: ** (well, the min is at the root)

**Delete minimum:** (Delete, move up the smaller level-2 element, etc.)

OK~ so much for that, let’s move to the more interesting business:

**Part 2: Error**

The idea is: given a set of data with keys, instead of putting them into a heap, we put them into another structure (i.e. ‘soft heap’) such that the structure assigns a fake key every element and treat it as the real key. It does so in a way that:

i) All elements have fake keys larger than or equal to their real key.

ii) Only a certain percentage () of elements have fake key different (larger than) real key.

The advantage of doing that is, unlike heap, it now takes only (amortized) time to delete the minimum element, while building it still takes time. (Explaining exactly how this is done requires another post which I might write…for now, check out Chazelle ’00.)

Meanwhile, I guess the most curious thing is, how can we get any uncorrupted results with a corrupted data structure? As an example, let’s see how to find the (genuine) medium with data structure having precent of corrupted keys:

The mean observation is that, since all fake keys are larger than real keys, by deleting the smallest of elements (according to fake keys), the genuine medium will be larger than all deleted elements (in real key) and hence surely remains in the set.

Now we have reduced the problem to finding the -th smallest element in a set of cardinality . Of course we can solve this by putting the elements in a reversed soft heap (i.e. all real keys are changed to their negative value), delete the smallest (fake key) elements, etc.

At each step, we reduce the size of the data set by at least a factor of , and the amortized running time at each step is (building the soft heap) (deleting about half of the elements) which is, in total .

Since the size shrinks in constant factor, the total running time is simply a geometric series, hence .

**Remarks:**

1. In the above argument, can be anything less than hence in fact quite a lot of data are allowed to be corrupted!

2. It doesn’t matter whether we are finding the medium or the -th smallest element, indeed, after the first step in the above argument we are literally finding the -th smallest element. The general rule being simply taking the negative of the keys before building the soft heap whenever .

3. The problem of finding the -th smallest element is very well studied and algorithms that do this are called selection algorithms. Obviously our running time is optimum since it takes that much time to read off the data set! However, there are many other ways to achieve this linear running time, some are not even amortized, but as far as I know, one cannot do this with a real heap.