Draft:Klee's algorithm

Framed as an open research problem published in 1977 in The American Mathematical Monthly, Victor Klee presented a challenge to the journal's readers to find an algorithm to compute the measure of the union of a set of intervals that would be more efficient than sorting the interval endpoints and sequentially adding the resulting lengths together. Klee then provided an algorithm in pseudocode that performs the sorting and summation as a baseline solution for this problem with a complexity of $$O(n \log n)$$. This complexity was later proven to be optimal in 1978 by Michael Freman and Bruce Weide.

As an additional challenge, Klee invited the readers to consider the problem in multiple dimensions and how efficiently someone could solve the complexity of finding a disjoint set of d-dimensional intervals, reducing that set to the minimum number of intervals possible, and computing the measure of their union. This last question was eventually labeled Klee's measure problem.

Algorithm

 * 1) Separate the endpoint pairs for each interval and tag if each endpoint was on the left or the right.
 * 2) Sort the individual endpoints in ascending order of value, with the additional condition that left endpoints are before right endpoints when two endpoint values are the same.  (This changes any partially overlapping intervals into a series of nested ones.)
 * 3) Iterate through the sorted endpoints, keeping track of the interval nesting count and recording just the outermost interval of each nesting.
 * 4) Add up the measures of each of the resulting set of disjointed intervals.

Logical error in the original
The original pseudocode had the following lines inside of the loop through the endpoints:

$$EXCESS = EXCESS + t_i$$ if $$EXCESS = 1$$ then $$m = m + 1$$ and $$c_m = e_i$$ if $$EXCESS = 0$$ then $$d_m = e_i$$

where $$EXCESS$$ is the interval nesting count, $$i$$ is the current endpoint index, $$t_i$$ = +1 for left endpoints and -1 for right endpoints, $$e_i$$ is the current endpoint value, $$m$$ is the index of the current interval for the new set, and $$c_m$$ and $$d_m$$ are the start and end of the current interval for the new set, respectively. This code incorrectly jumps ahead $$m$$ at the right endpoint of the second outermost interval and uses that endpoint as the left endpoint of the next recorded interval.

To fix this problem, the pseudocode could be rewritten as follows:

if $$EXCESS = 0$$ then $$m = m + 1$$ and $$c_m = e_i$$ $$EXCESS = EXCESS + t_i$$ if $$EXCESS = 0$$ then $$d_m = e_i$$

This way, the first line only triggers before the first endpoint of a nested set of endpoints and the second line only triggers after the end of the last endpoint of such a set.