CS 70
Discrete Mathematics for CS
Spring 2000
Wagner
Sample Final
You will have three hours for the final itself. This sample final is similar in style and scope to the actual
final; however, it is rather longer than the actual final. The exam is openbook, opennotes. We have not
assigned points to the questions (but we will do this for the final itself).
1. (?? pts.)
The longest common subsequence problem
In this question, we will need an induction principle for
pairs of strings
over an alphabet
Σ
. We say that
a pair of strings
(
a
0
,
b
0
)
is “less than” another pair
(
a
,
b
)
, written
(
a
0
,
b
0
)
≺
(
a
,
b
)
, if

a
0

+

b
0

<

a

+

b

,
where

a

denotes the length of string
a
∈
Σ
*
. Our induction principle is the following, where
P
(
·
,
·
)
is
a proposition that applies to pairs of strings:
• if
2
a
,
b
∈
Σ
*
[
2
a
0
,
b
0
∈
Σ
*
,
(
a
0
,
b
0
)
≺
(
a
,
b
)
⇒
P
(
a
0
,
b
0
)]
⇒
P
(
a
,
b
)
then
2
a
,
b
∈
Σ
*
P
(
a
,
b
)
.
Now consider the following problem. A
subsequence
of a string
a
is any sequence that can be obtained
by deleting one or more (not necessarily consecutive) letters from
a
, leaving the order of the remaining
letters unchanged. A
longest common subsequence
(written
lcs
) of two strings
a
and
b
is a sequence of
maximum length that is a subsequence of both
a
and
b
. Thus for example a lcs of
a
=
GACTTACCCAGT
and
b
=
GTTATGTACA
is
GATTACA
; a lcs of
a
=
ATT
and
b
=
GCG
is the empty string
λ
.
Suppose we want to compute a lcs of two given strings,
a
and
b
. [This problem arises, for example,
in DNA analysis and in file comparison utilities.] To make the notation simpler, we’ll actually just
compute the
length
of a lcs (which is essentially the same problem). Here is a suggested algorithm:
algorithm lcs
length(a,b)
if
a
=
or
b
=
then return(0)
else if
head
(
a
) =
head
(
b
)
then return(
max
{
1
+
lcs
length
(
tail
(
a
)
,
tail
(
b
))
,
lcs
length