Let $x = \sa{3}\;\sa{6}\;\sa{4}\;\sa{10}\;\sa{1}\;\sa{15}\;\sa{13}\;\sa{4}\; \sa{19}\;\sa{16}\;\sa{10}$. A longest increasing subsequence of $x$ is $y=\sa{3}\;\sa{4}\;\sa{10}\;\sa{13}\;\sa{16}$ of length $5$. Another such subsequence is $z=\sa{3}\;\sa{4}\;\sa{10}\;\sa{13}\;\sa{19}$. If $\sa{21}$ is appended to $x$ this lengthen $y$ and $z$ to increasing subsequences of length $6$. But if $18$ is appended to $x$ only $y\;\sa{18}$ becomes a longest subsequence, since $z\;\sa{18}$ is not increasing.
The tables display $x$ before and after a run of Algorithm Lis.
$i$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
$x[i]$ | 3 | 6 | 4 | 10 | 1 | 15 | 13 | 4 | 19 | 16 | 10 |
$i$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
$x[i]$ | 1 | 4 | 4 | 10 | 16 | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ | $\infty$ |
Problem 121: Longest Increasing Subsequence |
In this problem we consider a word $x$ on the alphabet of positive integers. An increasing subsequence of $x$ is a subsequence $x[i_0]x[i_1]\cdots x[i_{\ell-1}]$, where $0\leq i_0\lt i_1\lt \cdots \lt i_{\ell-1}\lt |x|$ and $x[i_0]\leq x[i_1]\leq \cdots \leq x[i_{\ell-1}]$.
\begin{algorithmic} \STATE $\ell\leftarrow 1$ \FOR{$i\leftarrow 1$ \TO $|x|-1$} \STATE $(a,x[i])\leftarrow (x[i],\infty)$ \STATE $k\leftarrow \min\{j \mid 0\leq j\leq \ell \mbox{ and } a\lt x|j]\}$ \STATE $x[k]\leftarrow a$ \IF{$k = \ell$} \STATE $\ell\leftarrow \ell+1$ \ENDIF \ENDFOR \RETURN{$\ell$} \end{algorithmic}
Inspecting carefully Algorithm Lis, it should be noticed that the word $x[0\dd \ell-1]$ computed when the algorithm terminates is increasing but not usually a subsequence of $x$, as shown by the example, which leads to the next question.