CS 181 — Winter 2008
Formal Languages and Automata Theory
Problem Set #8 Solutions
Problem 8.1.
(10 points)
Show that P is closed under union, concatenation, and star.
Solution
Suppose
A, B
are two languages in P. Then let
M
A
be the polynomial time Turingdecider
for
A
and
M
B
the same for
B
. The
M
A
has some asymptotic runtime
O
(
n
k
) and
M
B
has
runtime
O
(
n
‘
) for some integers
k
and
‘
. Without loss of generality, we assume that
k
≥
‘
.
We first show that
A
∪
B
∈
P. We can build the following Turing decider
M
1
for
A
∪
B
.
M
1
first runs
M
A
on its input, if
M
A
accepts, then
M
1
accepts. If
M
A
rejects, then
M
1
acts as
M
B
on the same input. Clearly, this machine decides
A
∪
B
.
Let us now analyze the running time of
M
1
. Notice that for every input word, in the worst
case we run
M
A
then
M
B
.
Then this takes
O
(
n
k
+
n
‘
) =
O
(
n
max(
k,‘
)
) =
O
(
n
k
).
Since
k
is a constant, it follows that
M
1
runs in polynomial time. The existence of
M
1
proves that
A
∪
B
∈
P.
Next, we show that
AB
∈
P by building the Turing decider
M
2
for it. Notice that given an
input string of length
n
, there are
n
+ 1 ways to partition it into two substrings (e.g. 123 can
be partitioned as
ε
·
123
,
1
·
23
,
12
·
3 or 123
·
ε
).
M
2
simply tries each partitioning one at a
time. For each partitioning, it runs
M
A
on the left half of the string then
M
B
on the right
half. If both machines accept, then
M
2
halts and accepts. If one of them rejects, then
M
2
tries the next partitioning. If all partitionings fail, then
M
2
rejects.
Let us now analyze the running time of
M
2
. Notice that for each partitioning, we run
M
A
on
a string of length at most
n
and
M
B
on a string of length at most
n
. Thus, for each partioning
M
2
takes time
O
(
n
max(
k,‘
)
) =
O
(
n
k
). However, there are
n
+ 1 partitionings. Thus, in total,
M
2
takes
O
(
n
k
+1
) time. This is still polynomial and so
AB
∈
P.
Lastly, we show that
A
*
∈
P by building a Turing decider
M
3
for it.
M
3
will work by using
dynamic programming. Notice that a string
s
is in
A
*
if
s
∈
A
or if
s
can be written
s
=
s
1
s
2
where
s
1
, s
2
∈
A
*
and
s
1
, s
2
are nonempty (and so are
proper
substrings of
s
). Then for input
string
s
, we can first calculate which length 1 substrings are in
A
*
(there are
n
of them). Then
using this information, we can calculate which of the length 2 substrings are in
A
*
(all
n

1
of them) by checking to see if they are in
A
, and otherwise checking to see if we can partition
it into substrings in
A
*
. We then continue this for the
n

2 length3 substrings, the
n

3
length4 substrings, etc. This continues until we can calculate whether the length
n
substring
(namely,
s
itself) is in
A
*
.