Sie sind auf Seite 1von 5

Acta Informatica16, 87-91 (1981)

9 Springer-Verlag 1981

Another Look at the


9,'Longest Ascending Subsequence" Problem

Paul Pritchard
University of Queensland,Department of Computer Science,St. Lucia, 4067, Australia

Summary. Dijkstra has given a derivation of an efficient algorithm for a


problem concerning monotonic subsequences, and extracted a proof of a re-
lated theorem from the algorithm. Here it is shown that a careful sepa-
ration of concerns can lead to a beautiful conventional proof, a very dif-
ferent derivation of Dijkstra's algorithm, a more elegant proof from the al-
gorithm, and the discovery of a duality property.

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

from proof to construction to efficient algorithm. Each step involves an in-


crease in information content. We commence with a proof of the Theorem,
which is perhaps even simpler than Seidenburg's proof.

2. A Proof

Given is a sequence A=(Ao, ..., AN_l) of integers, N > I .


Definition. to(n): the maximum length of an ascending subsequence ending
with A, (for 0 < n < N).
Lemma 1. The maximum length of an ascending subsequence =
max { to(n) lO < n < N}.
Proof. Obvious.
Lemma 2. j < i and to(j) = to(i) implies A j > A i.
Proof. Assume the left hand side (LHS). Suppose that A j < A i. Then by ap-
pending A i to a maximal ascending subsequence ending with A i, we get the
contradiction to(i)> to(j)+ l. So Ai> A i.
Proof (of the Theorem). Let N > M 2. Suppose the maximum length of an as-
cending subsequence is < M . Then by Lemma 1, (Vn: 0 < n < N : l < to (n) <_M).
So there must be more than M indices with the same to-value (by the pigeon-
hole principle). These indices determine a descending subsequence by
Lemma 2. lq

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

to(n)=l if a n d o n l y i f (Vi: 0__<i<n: Ai>A,). (1)


This means that the indices n such that to(n)= 1 are just those at which suc-
cessive minima occur in a left to right search for the minimum element of A.
(1) suggests an induction on the value of to, employing the following gen-
eralization of (1).

Lemma 3. Let to(n)>k>O. Then t o ( n ) = k + l if and only if

(Vi: 0=<i<n: t o ( i ) > k ~ A i > A , ) .

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 makes a number of left-to-right passes over A. Since on each pass


the treatment of an element A, does not depend on any A i with i > n, algo-
rithm 1 can be mechanically transformed so that all passes are managed "simul-
taneously". Thus k will equal the number of passes in operation, and m be-
comes an array obeying the invariant

MI: (Vj: 1 <=j<=k: m[j] = m i n {Aili<n and to(i)=j}).

There is no need to maintain S, since next(n) is always n + 1. We now have


90 P. Pritchard

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

Mz: (V i: l_<i<k: m [ i ] < m [ i +1])

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)

Received December 30, 1980/Revised April 19, 1981

Das könnte Ihnen auch gefallen