Longest alternating subsequence

In combinatorial mathematics, probability, and computer science, in the longest alternating subsequence problem, one wants to find a subsequence of a given sequence in which the elements are in alternating order, and in which the sequence is as long as possible.

Formally, if $$\mathbf{x} = \{x_1, x_2, \ldots, x_n\}$$ is a sequence of distinct real numbers, then the subsequence $$\{x_{i_1}, x_{i_2}, \ldots, x_{i_k}\}$$ is alternating (or zigzag or down-up) if


 * $$x_{i_1} > x_{i_2} < x_{i_3} > \cdots x_{i_k}\qquad \text{and} \qquad 1\leq i_1 < i_2 < \cdots < i_k \leq n.$$

Similarly, $$\mathbf{x}$$ is reverse alternating (or up-down) if
 * $$x_{i_1} < x_{i_2} > x_{i_3} < \cdots x_{i_k}\qquad \text{and} \qquad 1\leq i_1 < i_2 < \cdots < i_k \leq n.$$

Let $${\rm as}_n(\mathbf{x})$$ denote the length (number of terms) of the longest alternating subsequence of $$\mathbf{x}$$. For example, if we consider some of the permutations of the integers 1,2,3,4,5, we have that
 * $${\rm as}_5(1,2,3,4,5) = 2 $$; because any sequence of 2 distinct digits are (by definition) alternating. (for example 1,2 or 1,4 or 3,5);
 * $${\rm as}_5(1,5,3,2,4) = 4, $$ because 1,5,3,4 and 1,5,2,4 and 1,3,2,4 are all alternating, and there is no alternating subsequence with more elements;
 * $${\rm as}_5(5,3,4,1,2) = 5, $$ because 5,3,4,1,2 is itself alternating.

Efficient algorithms
In a sequence of distinct elements, the subsequence of local extrema (elements larger than both adjacent elements, or smaller than both adjacent elements) forms a canonical longest alternating sequence. As a consequence, the longest alternating subsequence of a sequence of $$n$$ elements can be found in time $$O(n)$$. In sequences that allow repetitions, the same method can be applied after first replacing each run of repeated elements by a single copy of that element.

Distributional results
If $$\mathbf{x}$$ is a random permutation of the integers $$1,2,\ldots,n$$ and $$A_n \equiv {\rm as}_n(\mathbf{x})$$, then it is possible to show   that


 * $$ E[A_n] = \frac{2 n}{3} + \frac{1}{6} \qquad \text{and} \qquad \operatorname{Var}[A_n] = \frac{8 n}{45} - \frac{13}{180}.  $$

Moreover, as $$n \rightarrow \infty$$, the random variable $$A_n$$, appropriately centered and scaled, converges to a standard normal distribution.

Online algorithms
The longest alternating subsequence problem has also been studied in the setting of online algorithms, in which the elements of $$\mathbf{x}$$ are presented in an online fashion, and a decision maker needs to decide whether to include or exclude each element at the time it is first presented, without any knowledge of the elements that will be presented in the future, and without the possibility of recalling on preceding observations.

Given a sequence $$X_1, X_2, \ldots, X_n$$ of independent random variables with common continuous distribution $$F$$, it is possible to construct a selection procedure that maximizes the expected number of alternating selections. Such expected values can be tightly estimated, and it equals $$(2-\sqrt{2})n + O(1)$$.

As $$n \rightarrow \infty$$, the optimal number of online alternating selections appropriately centered and scaled converges to a normal distribution.