The Hong Kong University of Science & Technology
COMP 271: Design and Analysis of Algorithms
Fall 2007
Solution to Assignment 7
Question 1
: Give an
O
(
n
2
) time dynamic programming algorithm to find the
longest monotonically decreasing subsequence of
n
numbers. For example,
if the input sequence is
<
5
,
23
,
4
,
17
,
12
,
45
>
, then the output should be
<
23
,
17
,
12
>
.
Solution:
Input:
A sequence of
n
numbers
X
=
< x
1
,
· · ·
,x
n
>
.
Output:
The longest decreasing subsequence in
X
.
Idea:
We first give an algorithm which finds the length of longest decreasing
subsequence; later we will modify it to report a subsequence with this length.
Step 1: Space of Subproblems
For 1
≤
i
≤
n
, define
c
[
i
] = The length of the longest decreasing subsequence that ends with
x
i
.
Note that the length of the longest decreasing subsequence in
X
is given by
max
1
≤
i
≤
n
c
[
i
].
Step 2: Recursive Formulation
The longest decreasing subsequence that ends with
x
i
has the form
< Z,x
i
>
where
Z
is the longest decreasing subsequence that ends with
x
r
for some
r < i
and
x
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '07
 ARYA
 Algorithms, Dynamic Programming, C Programming, Recursion, Recurrence relation

Click to edit the document details