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 x = { x 1 , x 2 , … , x n } is a sequence of distinct real numbers, then the subsequence { x i 1 , x i 2 , … , x i k } is alternating (or zigzag or down-up)if
x i 1 > x i 2 < x i 3 > ⋯ x i k and 1 ≤ i 1 < i 2 < ⋯ < i k ≤ n . Similarly, x is reverse alternating (or up-down) if
x i 1 < x i 2 > x i 3 < ⋯ x i k and 1 ≤ i 1 < i 2 < ⋯ < i k ≤ n . Let a s n ( x ) denote the length (number of terms) of the longest alternating subsequence of x . For example, if we consider some of the permutations of the integers 1,2,3,4,5, we have that
a s 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) a s 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; a s 5 ( 5 , 3 , 4 , 1 , 2 ) = 5 , because 5,3,4,1,2 is itself alternating.The longest alternating subsequence problem is solvable in time O ( n ) , where n is the length of the original sequence.
If x is a random permutation of the integers 1 , 2 , … , n and A n ≡ a s n ( x ) , then it is possible to show that
E [ A n ] = 2 n 3 + 1 6 and Var [ A n ] = 8 n 45 − 13 180 . Moreover, as n → ∞ , the random variable A n , appropriately centered and scaled, converges to a standard normal distribution.
The longest alternating subsequence problem has also been studied in the setting of online algorithms, in which the elements of 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 , … , 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 − 2 ) n + O ( 1 ) .
As n → ∞ , the optimal number of online alternating selections appropriately centered and scaled converges to a normal distribution.