9 Springer-Verlag 1981
Paul Pritchard
University of Queensland,Department of Computer Science,St. Lucia, 4067, Australia
1. Introduction
Dijkstra [1] has given "some beautiful arguments using mathematical induc-
tion." The third (and last) of these is unusual in that it begins with a derivation
of an efficient algorithm to solve the following
Problem. Given a sequence A = ( A o..... AN_l) of integers, N > 1, design an al-
gorithm to determine the maximum length of an ascending subsequence. ("As-
cending" is used with respect to the numerical ordering =<.)
Then, by reasoning about his algorithm, Dijkstra extracts a proof of the fol-
lowing
Theorem. A sequence of length > M 2 contains a monotonic subsequence of
length > M.
("Monotonic" means ascending or descending.)
Seidenburg has given a nice proof of this theorem in [4], which is undoubt-
edly considerably more elegant than Dijkstra's. However, this is not to say
that the former gives more insight than the latter, because it is non-construc-
tive, whereas Dijkstra provides not only an algorithm but an optimal one [-3].
Neither proof is uniformly preferable since the two proofs address different
concerns, viz. elegance of proof versus constructibility and efficiency.
We proceed to show that it is both possible and worthwhile to separate
these concerns - to structure our knowledge so as to give a smooth transition
0001-5903/81/0016/0087/$01.00
88 P. Pritchard
2. A Proof
3. A Construction
In view of Lemma 1, we can solve the Problem by calculating to(n) for each n.
Rather than launch immediately into an algorithm, let us first establish some
simple properties of to. Clearly, to(O) = 1. A little more thought reveals that
Proof. Suppose the LHS is false - i.e. to(n)> k + 1. Let i' be the index of the
penultimate element in an ascending, maximal-length subsequence of
(Ao ..... A,). Then O< i' <n, to(i')>k and AI,<A 0, so that the RHS is false. On
The Longest Ascending Subsequence Problem 89
the other hand, suppose the RHS is false. Then there is an i, 0 < i < n , such that
to(i) > k and A i < A,. So to(n) > to(i), hence to(n) > k + 1 and the LHS is false.
Lemma 3 shows how to(n) can be computed for all n, because if those ele-
ments A i with to(i)<k are removed from A, then the indices n with to(n)=k+ 1
in the original sequence are those with to-values of 1 in the new sequence. The
algorithm below implements this induction. It employs a subset S of
{i[O<i<N} to keep track of the elements "removed" from A, using the func-
tion next:
next(i)=min {jlj>i and jr
IS[ denotes the size of S.
Algorithm 1.
k, S.'=0, r
{invariant: k = max ({to(i)[i e S} w {0})}
doISl#:N ~n, k : = n e x t ( - 1), k + l ;
"collect the indices n with to(n)=k":
m, n, S := A,, next(n), S u {n};
{invariant: S= {ilto(i)<k} u {i[i<n and to(i)=k}
and O<=n<N and m=min{Aili<n and to(i)=k}}
do n ~ N-~if A. < m ~ m , n,S:=A., next(n), Su{n}
Hm<=A.-~n:=next(n)
fi
od
od
{ k = m a x {to(n)[O<=n<N}}
It is easy to extract a proof of the Theorem from this algorithm. For let
N > M 2. If there is an ascending subsequence of length > M we are done.
Otherwise, O<_k<_M is invariantly true, so for some k > 0 , m takes on > M dif-
ferent values, which clearly form a descending subsequence. But note that this
proof is predicated on a correctness proof of the algorithm, and thus must be
regarded as being more complex than our earlier conventional proof.
4. An Efficient Algorithm
Algorithm 1'.
n, k.'=0, 0;
{invariant: k=max({to(i)rO<i<n}~{O}) and O<n<N and M1}
do n+N--,"set j=to(n)":
j:=l;
{invariant: (V i: 1 <=i<j: m[i] <=A,) and l<j<=k+ 1}
do j=#k+l cand m[j]<=A.-->j:=j+l od;
m[j], k:=A,, max {k,j};
n:=n+l
od
Now it can be deduced that
is invariantly true - M z is true initially since k = 0, and remains true after each
update since
m 2 and (Vi: l < i < j : m[iJ<A.) and l < j < k + l and ( j = k + l cor m[j]>A.)
implies wp("m[j], k: = A,, max {k,j}", Mz).
Alternatively, a simple conventional proof can be given as follows.
Lemma 4. Let O< n < N, and m be defined as in M 1 above. Then M z holds.
Proof. Let l<_i<k, i l < n , t o ( i l ) = i + l and m [ i + l ] = A i . Then there must be
an i2<i 1 such that Ai2<:Ai~ and to(i2)=i. So m [ i ] = m i n { A j l j < n and to(j)
=i} <Ai2<Ail = m [ i + l ] , and hence M z holds.
In any case the inner loop of algorithm 1' can be recast as a binary search,
giving the algorithm derived by Dijkstra Eli.
Dijkstra proves the Theorem with some subtle reasoning about his algo-
rithm running "in parallel" with an analogous algorithm for descending sub-
sequences. A much simpler proof can be given by adapting the proof from al-
gorithm 1 in the obvious way.
5. Remarks
The choice of the function to was somewhat arbitrary, in that it is equally rea-
sonable to investigate the "dual" function with the
Definition. from(n): the maximum length of an ascending subsequence starting
with A, (for 0 < n < N).
Such an investigation would be equally fruitful, because our lemmas, Proof
of the Theorem and algorithms all have corresponding dual forms, as guaran-
teed by the
Duality Lemma. Let A ' = ( A ' o .... ,A'n_I) be the sequence with A ' i = - - A N _ l _ i, for
0 < i < N. Then from (n) for A' is equal to to (N - 1 - n) for A.
The Longest Ascending Subsequence Problem 91
The dual of algorithm 1 makes a number of passes from right to left, each
pass deleting successive maxima. It can be transformed to give a dual of
Dijkstra's algorithm.
We note that the transformation relating a multiple-pass algorithm to a sin-
gle-pass algorithm is of independent interest. F o r example, it can be used in
the opposite direction to that in the above exercise, to derive Eratosthenes'
sieve from Dijkstra's well-known algorithm for finding prime numbers.
Finally, we draw some conclusions concerning the relationship between
programming and mathematics. Firstly, conventional proofs can always be
found that are more elegant than proofs based on algorithms, but it is nev-
ertheless true that algorithmic considerations can further our understanding.
Secondly, programmers can employ (at least) three kinds of knowledge, viz. (i)
mathematical knowledge, (ii) knowledge leading from mathematical facts to al-
gorithms, and (iii) knowledge about algorithms themselves (program transfor-
mations). Thirdly, it is a good general policy to investigate the mathematics of
the problem domain before designing an algorithm, or at least to maintain a
clear separation between (i) and (ii). Indeed, we chose to emphasise the former
in this exercise, and would refer the reader to Dijkstra's [2] for the latter.
Acknowledgements. I am indebted to E.W.Dijkstra for posing the problem and suggesting that I
seek a transformation relating our respective solutions, and to David Gries for his polished ver-
sion of Algorithm 1'. The referees were also very helpful.
References
1. Dijkstra, E.W.: Some beautiful arguments using mathematical induction. Acta Informat. 13, 1-8
(1980)
2. Dijkstra, E.W.: A discipline of programming. Englewood Cliffs, N.J.: Prentice-Hall 1976
3. Fredman, M.L.: On computing the length of longest increasing subsequences. Discrete Math. 11,
29-35 (1975)
4. Seidenburg, A.: A simple proof of a theorem of Erd6s and Szekeres. J. London Math. Soc. 34,
352 (1959)