Full Notes
Full Notes
Full Notes
Jarkko Kari
Fall semester 2013
University of Turku
Contents
1 Preliminaries
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Alphabets, words and languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
1
2 Regular languages
2.1 Deterministic Finite Automata (DFA) .
2.2 Nondeterministic finite automata (NFA)
2.3 NFA with -moves . . . . . . . . . . . .
2.4 Regular expressions . . . . . . . . . . . .
2.5 The pumping lemma . . . . . . . . . . .
2.6 Closure properties . . . . . . . . . . . .
2.7 Decision algorithms . . . . . . . . . . . .
2.8 Myhill-Nerode theorem . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
4
8
14
19
26
30
39
40
3 Context-free languages
3.1 Context-free grammars . . . . . . .
3.2 Derivation trees . . . . . . . . . . .
3.3 Simplifying context-free grammars
3.4 Pushdown automata . . . . . . . .
3.5 Pumping lemma for CFL . . . . .
3.6 Closure properties of CFL . . . . .
3.7 Decision algorithms . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
47
48
52
56
63
77
82
90
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
94
96
100
103
106
107
113
114
115
121
121
124
127
129
132
137
142
142
146
148
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1.1
Preliminaries
Introduction
1.2
Let us start with basic notions. An alphabet is a finite, non-empty set. The elements of the
alphabet are called letters. The choice of the alphabet depends on the application in mind. In
our examples we often use letters of the English alphabet, or digits, or other characters found on
computer keyboards. But any other symbols could be used as well. Here are some examples of
alphabets:
1 = {a, b},
2 = {0, 1, 2},
3 = {, , , }
A word (or string) is a finite sequence of letters. For example, abaab, aaa and b are words over
the alphabet 1 = {a, b}. Variables with names u, v, w, x, y, z will typically be used to represent
words.
If w is a word then |w| denotes its length, i.e. the number of symbols in it. Note that the
length of a word can be 0. Such word is called the empty word, and it is denoted by . (We
cannot just write nothing: no one would know that the empty word is there!)
For example,
|abaab| = 5
|| = 3
|| = 0
The concatenation of two words is the word obtained by writing the first word followed by the
second one as a single word. For example, the concatenation of data and base is the word database.
Notation for concatenation is similar to normal multiplication: For example,
ab aab = abaab.
The multiplication sign does not need to be written if the meaning is clear, i.e., uv is the concatenation of words u and v. So, for example, if v = a and w = ab, then vw = v w = aab.
The empty word is the identity element of concatenation, much the same way as number 1 is
the identity element of multiplication: For any word w
w = w = w.
A concatenation of a word with itself is denoted the same way as the multiplication of a number
with itself: For any integer n and word w the word wn is the concatenation of n copies of w. For
example, if w = abba then
w2 = abbaabba,
ab2 a3 b = abbaaab,
a5 = aaaaa,
w0 = ,
3 = ,
0 = .
We may use parentheses to group and to indicate the order of operations, exactly as we do when
multiplying numbers:
a[(ab)3 aa]2 b = aabababaaabababaab.
An important difference between concatenation and multiplication is that concatenation is not
commutative. (For example, ab 6= ba.) However, concatenation is associative: for all words
u, v, w holds
(uv)w = u(vw).
A prefix of a word is any sequence of leading symbols of the word. For example, word abaab has
6 prefixes:
, a, ab, aba, abaa and abaab.
A suffix of a word is any sequence of trailing symbols of the word.
A subword of a word is any sequence of consecutive symbols that appears in the word. Subwords
of abaab are:
, a, b, ab, ba, aa, aba, baa, aab, abaa, baab, abaab.
Subwords of a word are also called its factors.
A prefix, suffix or subword of a word is called proper if it is not the word itself. Each word w has
|w| different proper prefixes and suffixes.
The mirror image of a word is the word obtained by reversing the order of its letters. The
mirror image of word w is denoted by wR . For example,
(abaab)R = baaba
(saippuakauppias)R = saippuakauppias
R = .
A word w whose mirror image is the word itself is called a palindrome. In other words, word w
is a palindrome iff w = wR .
A formal language is a set of words over a fixed alphabet. The language is finite if it contains
only a finite number of words. We are mainly interested in infinite languages. Here are a few
examples of languages over the alphabet 1 = {a, b}:
{a, ab, abb}
{a, aa, aaa, aaaa, . . .} = {an | n 1}
{an bn | n 0}
{w | w = wR } = {w | w is a palindrome }
{ap | p is a prime number }
{}
Note that is a word, {} is a language containing one element (the empty word), and is a
language containing no words. They are all different. (Later, in Section 2.4, we also introduce the
regular expression .)
The language of all words over alphabet is denoted by . For example,
{a, b} = {, a, b, aa, ab, ba, bb, aaa, . . .}
{} = {, , 2 , 3 , 4 , . . .}
The operation (concatenation of words) is associative and is its identity element, so ( , )
is a mathematical structure called a monoid. More precisely, it is the free monoid generated by
. Because |uv| = |u| + |v|, the length function w 7 |w| is a monoid homomorphism from ( , )
to the additive monoid (N, +) of natural numbers.
The language of all non-empty words over is denoted by + , so
+ = \ {}.
Note that (+ , ) is the free semigroup generated by .
A problem with infinite languages is how to describe them. We cannot just list the words as
we do in the case of finite languages. Formal language theory develops techniques for specifying
(infinite) languages. How simple or complex a language is depends on the simplicity of its
description.
Regular languages
Regular (or rational) languages form a simple but important family of formal languages. They can
be specified in several equivalent ways: using deterministic or non-deterministic finite automata,
left (or right) linear grammars, or regular expressions. We start with deterministic finite automata.
A linguistic note: automaton and automata are the singular and the plural form of the same word,
respectively.
3
2.1
DFA provide a simple way of describing languages. DFA are accepting devices: one gives a word
as input and after a while the DFA tells whether the input word is in the language (DFA accepts
the word) or whether it is not in the language (DFA rejects it).
To decide whether to accept or reject the input word the DFA scans the letters of the word from
left to right. The DFA has a finite internal memory available. At each input letter the state of the
internal memory is changed depending on the letter scanned. The previous memory state and the
input letter together determine what the next state of the memory is. The word is accepted if the
internal memory is in an accepting state after scanning the entire word.
a a b a b a
a b b
Input tape
Control Unit
with
finite memory
a b a a b
a b a a b
a b a a b
a b a a b
a b a a b
a b a a b
Final states are indicated as double circles, and the initial state is indicated by a short incoming
arrow.
Example 2. Heres the transition diagram for the DFA A of Example 1
a,b
a
b
To determine whether a given word is accepted by A one just follows the path labeled with the
input letters, starting from the initial state. If the state where the path ends is a final state, the
word is accepted. Otherwise it is rejected. In our example, path labeled with input word w = abaab
leads to state r so the word is accepted. Input word abba is rejected since it leads to q which is not
a final state.
A DFA A is a finite description of the language L(A). For example, the DFA in Examples 1
and 2 represents the language of all words over the alphabet {a, b} that contain aa as a subword.
Example 3. Let us draw transition diagrams for DFA that recognize the following languages over
the alphabet {a, b}. Note that the DFA has to recognize the language exactly: All words of the
language have to be accepted; all words not in the language have to be rejected.
1. Words that end in ab.
5
a
b
a,b
a,b
a,b
a
a
b
b
a
b
Not all languages can be defined by a DFA. For example, it is impossible to build a DFA that
would accept the language
{ap | p is a prime number }.
Well prove later that even simple languages such as
{an bn | n 0}
cannot be recognized by any DFA. Languages that can be recognized by DFA are called regular.
To enable exact mathematical notations, we extend the meaning of the transition function .
The basic transition function gives the new state of the machine after a single letter is read. The
gives the new state after an arbitrary string of
extended function (that we will first denote by )
wa) = ((q,
w), a).
(q,
(1)
w) is the state after reading input w then (p, a) is the new state
In other words, if p = (q,
after reading input wa.
Note that is an extension of . They give same value for input words w = a that contain only
one letter:
a) = (q, a).
(q,
Therefore there is no danger of confusion if we simplify notations by removing the hat and indicate
simply instead of from now on.
Example 4. Consider our sample three state DFA from Examples 1 and 2. In this DFA
(r, ab) = r,
(q, bb) = p,
(p, abaab) = r.
The language recognized by DFA A = (Q, , , q0 , F ) can now be precisely formulated as follows:
L(A) = {w | (q0 , w) F }.
This is a mathematical shorthand for the set of words w over the alphabet such that, if the
machine reads input w in the initial state q0 , then the state it reaches is a final state.
7
Remark: DFA according to our definition are often called a complete DFA because each state
must have an outgoing transition with each letter in the alphabet. A commonly used alternative
is to allow partial transition functions , that do not need to be defined for all pairs of states and
input letters. In such cases, if there is no transitions from the current state with the current input
letter the machine halts and the word is rejected. However, exactly the same languages can be
recognized using complete or partial transition functions, so we stick to the terminology that the
transition function of a DFA is a complete function.
2.2
Nondeterministic finite automata are generalizations of DFA. Instead of exactly one outgoing transition from each state by every input letter, NFA allow several outgoing transitions at the same
time. A word is accepted by the NFA if some choice of transitions takes the machine to a final
state. Some other choices may lead to a non-final state, but the word is accepted as long as there
exists at least one accepting computation path in the automaton.
Example 5. Here is an example of a transition diagram of an NFA:
a,b
a,b
a
Note that there are two transitions from state p with input letter a, into states p and q. Note also
that there are no transitions from state q with input letter b. The number of transitions may be
zero, one or more.
NFA may have several different computations for the same input word. Take, for example, the
input word w = abaa. Already the first input letter a gives two choices in the automaton above:
we may go either to state p or state q. Lets say we enter state q. But there is no transition with
the next letter b, so this choice does not lead to a final state.
Let us try then the other choice: after the first a the machine is in state p. The second input
letter b keeps the machine in state p. The last two letters a and a may take the machine to state q
and then state r. So there exists a computation path that takes the machine to a final state. The
input abaa is hence accepted, and
abaa L(A).
The following computation tree summarizes all possible computations with input abaa:
a
a
q
a
r
8
It is easy to see that the sample NFA accepts exactly those words that contain aa as a subword,
so the NFA is equivalent to the DFA of Example 1. Two automata are called equivalent if they
recognize the same language.
Let us give a precise definition: An NFA A = (Q, , , q0 , F ) is specified by 5 items: State set Q,
input alphabet , initial state q0 and the final state set F have the same meaning as for a DFA.
The transition function is defined differently. It gives for each state q and input letter a a set
(q, a) Q of possible next states. Using the power set notation
2Q = {S | S Q}
we can write
: Q 2Q .
For example, the transition function of the NFA in Example 5 is given by the table
a
b
p {p, q} {p}
q {r}
r {r} {r}
Let us extend the meaning of the transition function the same way we did in DFA. Let us define
: Q 2Q
w) is the set of all states the machine can reach from state q reading input word w.
such that (q,
An exact recursive definition goes as follows:
1. For every state q
) = {q}.
(q,
(r, a).
r(q,w)
r a
Therefore there is no risk of confusion if we drop the hat and write simply instead of .
(2)
=
=
=
=
{p, q},
{p},
{p, q},
{p, q, r}.
a,b
a,b
a,b
a,b
a,b
Consider the following question: How would one go about checking whether a given NFA A accepts
a given input word w ? For example, how would one test if the NFA
10
b
a
p
a
q
b
r
b
accepts the input w = abbaabb ? One alternative is to try all possible computation paths for w
and see if any of them ends in an accepting state. But the number of paths may be very large, and
grow exponentially with the length of the input!
A better way is to scan the input only once, and keep track of the set of possible states. For
example, with the NFA above and the input w = abbaabb we have
a
a,b
a,b
a
of Example 5. Initially the NFA is in state p so the corresponding DFA is initially in state {p}.
With input letter a the NFA may move to either state p or q, so after input a the DFA will be in
state {p, q}. With input b the NFA remains in state p:
b
{p}
{p,q}
Next we have to figure out the transitions from state {p, q}. If the DFA is in state {p, q} it means
that the NFA can be in either state p or q. With input letter a the NFA can move to p or q (if it
11
was in state p) or to r (if it was in state q). Therefore, with input letter a the machine can move
to any state p or q or r, and so the DFA must make a transition to state {p, q, r}. With input b
the only transition from states p and q is into p. That is why DFA has transition from {p, q} back
into {p}:
b
b
{p}
{p,q}
{p,q,r}
Consider next the state {p, q, r}. With input a the NFA can reach any state so the DFA has a loop
back to {p, q, r}. With input b the NFA can move to state p (if it was in state p) or to state r (if
it was in state r) so the DFA has a transition from {p, q, r} into {p, r}:
a
b
b
{p}
{p,q}
{p,q,r}
{p,r}
We again have a new state to be processed, state {p, r}. From p and r the NFA can go to any state
with input a, and into states p and r with input b:
a
b
a
b
{p}
{p,q}
{p,q,r}
{p,r}
No new states were introduced, and all necessary transitions are in place.
We still have to figure out which states should be final states. The NFA accepts word w if it
leads to at least one final state. So the DFA should accept w iff it ends into a state S such that S
contains at least one final state of the DFA. Our sample NFA has only one final state r, so every
state of the new DFA that represents a set containing r is final:
a
b
a
b
a
The construction is complete. The result is a DFA that accepts the same exact language as the
original NFA. The construction is called a powerset construction.
Note that there are also other DFA that accept the same language, some of which may be
simpler than ours. We do not care: we only wanted to demonstrate that there exists at least one
such DFA.
Proof. Let
A = (Q, , , q0 , F )
be any NFA. We construct a DFA
A0 = (Q0 , , 0 , q00 , F 0 )
as follows. The states of the new DFA A0 represent the subsets of the original state set Q, i.e.,
Q0 = 2Q .
The interpretation of the states is the same as in the example above: If the state of the new DFA
machine A0 after reading input word w is
{q1 , q2 , . . . , qk }
it means that after input w the original nondeterministic machine A can be in state q1 or q2 or . . .
or qk .
The initial state of A0 is
q00 = {q0 }.
Final states are the elements of Q0 that contain at least one final state of A:
F 0 = {S Q | S F 6= }.
The deterministic transition function 0 is formally defined as follows: For all S Q and all a
[
0 (S, a) =
(q, a).
(3)
qS
Interpretation: if the nondeterministic machine can be in any state q S then after reading the
next input letter a it can be in any state that belongs to any of the sets (q, a).
Let us prove that the construction works. Using mathematical induction on the length of the
input word w we show that in DFA A0
0 (q00 , w) = (q0 , w).
Note that (q0 , w) on the right is the set of states reachable in the NFA A by input w, while 0 (q00 , w)
is the unique state in the DFA A0 reached after reading w. Both are subsets of Q.
1 Case |w| = 0, that is, w = . This is trivial since
0 (q00 , ) = q00 = {q0 } and (q0 , ) = {q0 }.
2 Suppose the claim is true for all inputs of length l. Let |w| = l + 1. Then w = ua for some
word u of length l and letter a . The claim is true for input u, so
0 (q00 , u) = (q0 , u).
Denote S = 0 (q00 , u) = (q0 , u). We have
0 (q00 , ua) = 0 (S, a) =
qS
Here the three equalities follow from (1), (3) and (2), respectively.
13
Our proof is almost done. Word w is accepted by A0 if an only if 0 (q00 , w) contains an element
of F . But (q0 , w) is the same set, and A accepts w if and only if there is a final state in (q0 , w).
So L(A) = L(A0 ).
Remark: In the powerset automaton one only needs to include as states those subsets of Q that
are reachable from the initial set {q0 }. So in practice one adds new states to the DFA during the
construction only when they are needed, as we did in the Example 8.
Example 11. Let us use the powerset construction to construct a DFA that is equivalent to
a,b
2.3
In forthcoming constructions it turns out to be useful to extend the notion of NFA by allowing
spontaneous transitions. When an NFA executes a spontaneous transition, known as an -move, it
changes its state without reading any input letter. Any number of -moves are allowed.
Example 12. Heres an example of an NFA with -moves:
a
2
a
b
3
For example, word w = aabbb is accepted as follows: The first a keeps the machine in state 1. Then
an -move to state 2 is executed, without reading any input. Next, ab is consumed through states
3 and 2, followed by another -move to state 4. The last bb keeps the automaton in the accepting
state 4.
The automaton of this example accepts any sequence of as followed by any repetition of abs
followed by any number of bs:
L(A) = {ai (ab)j bk | i, j, k 0}.
14
Formally, an NFA with -moves (or -NFA for short) is A = (Q, , , q0 , F ) where Q, , q0 and F
are as before, and is a function
Q ( {}) 2Q
that specifies for each state q the transitions with all input letters a and the empty word .
(q, a) is the set of all states p such that there is a move from q to p with input a, and
(q, ) is the set of states p such that there is a spontaneous move from q to p.
Example 13. The transition function for the -NFA of Example 12 is
1
2
3
4
a
b
{1} {2}
{3} {4}
{2}
{4}
As in the previous sections, we extend into a function that gives possible states for all input
words:
: Q 2Q ,
w) is the set of all possible states after the automaton reads input word w, starting at
where (q,
state q. When processing w any number of -moves may be used.
Before the recursive definition of we need to know all states the machine can enter from q
without reading any input letters, i.e. using only -moves. Let us call this set the -CLOSURE
of state q.
Example 14. In the -NFA of Example 12 we have
-CLOSURE
-CLOSURE
-CLOSURE
-CLOSURE
(1)
(2)
(3)
(4)
=
=
=
=
{1, 2, 4},
{2, 4},
{3},
{4}.
The -CLOSURE(q) can be easily found by analyzing which nodes can be reached from state q
using only -moves. This is a simple reachability condition in the directed graph formed by the
-edges, and can be solved effectively by a standard graph algorithm:
1
15
If S is any set of states let us denote by -CLOSURE (S) the set of states reachable from any
element of S using only -moves, i.e.
[
-CLOSURE (S) =
-CLOSURE (q).
qS
w):
Now we are ready to give a recursive definition of (q,
1. For every state q
) = -CLOSURE (q).
(q,
(By definition, the -CLOSURE consists of all states reachable without consuming any input
letter.)
2. For every state q, word w and letter a
wa) = -CLOSURE
(q,
(r, a) .
r(q,w)
r a
e
p
a) for single letters a are of special interest to us. Note, however, that they are
The values of (q,
not necessarily identical to (q, a), so we cannot remove the hat as we did with DFA and NFA:
(q, a) contains only states you can reach with one transition labeled by a. It does not allow
using -moves.
a) contains all states you can reach from q by doing any number of -moves
In contrast, (q,
and one a-transition, in any order:
e
q
Proof. Let
A = (Q, , , q0 , F )
be any -NFA. We construct the NFA
A0 = (Q, , 0 , q0 , F 0 )
that has the same state set Q and the initial state q0 as A, and whose transition function 0
anticipates all possible -moves in A before and after each letter transition: In A0 , there is a
transition from q to p with letter a if and only if in A there is a computation path from q to p that
reads a from the input. This means that 0 is the same as we discussed earlier:
a)
0 (q, a) = (q,
for all q Q and a . Now any computation in A corresponds to a leaner computation in A0
that skips over all -moves:
e
0 (p, a) =
pS
a) :
(p,
pS
u
q
e
p
d(p,a)
17
e
x
In the -NFA A
ua) = CLOSURE
(q,
(p, a) :
pS
u
q
x
e -CLOSURE[d (p,a) ]
These two sets are identical: Any x that can be reached from p through an a path is trivially
reachable through an a path. (Just use p0 = p.)
On the other hand, state p0 along the a path is an element of S. State x that can be reached
from p using a, can be reached from p0 using a.
w) for all non-empty words w. Next we have to determine
We have shown that 0 (q, w) = (q,
which states should be made final states in A0 . Assume first that 6 L(A). Then we can simply
set
F 0 = F.
0 , w), exactly same non-empty inputs w are accepted. And the empty word
Because 0 (q0 , w) = (q
is not accepted by either automaton.
Assume then that L(A). Then we have to include the initial state q0 among the final states
in A0 :
F 0 = F {q0 }.
Now both automata accept . If A accepts a non-empty w then A0 accepts it with a computation
to the same final state. If A0 accepts non-empty w then it either has a computation leading to
some element of F (in which case A accepts it) or it has a computation leading to q0 . But there is
an -path in A from q0 to some final state so A accepts w as well.
We have proved that L(A) = L(A0 ).
Example 18. If we apply the conversion process to the -NFA of Example 12 we obtain the
equivalent NFA
b
a
a,b
a
1
a
2
a
b
3
Now we know how to convert any -NFA into an equivalent NFA, and how to convert any NFA
into an equivalent DFA, so we can convert any -NFA into a DFA. All three automata models
accept same languages.
18
2.4
Regular expressions
Regular expressions provide a completely different technique to define languages. Instead of being
accepting devices such as finite automata they are descriptions of how a language is build from
simple atomic languages using some basic language operations. As we will see later, the important
Kleene theorem states that regular expressions define exactly the same family of languages as finite
automata.
Let us first define some operations on languages. Let be an alphabet, and let L, L1 and L2
be languages over .
The concatenation L1 L2 of languages L1 and L2 is defined to be the language containing
all words obtained by concatenating a word from L1 and a word from L2 . In other words,
L1 L2 = {uv | u L1 and v L2 }.
For example,
{ab, b}{aa, ba} = {abaa, abba, baa, bba}.
For every n 0 we define Ln to be the set of words obtained by concatenating n words from
language L. In other words,
L0 = {}
Ln = Ln1 L for every n 1.
For example,
{ab, b}3 = {ababab, ababb, abbab, abbb, babab, babb, bbab, bbb}.
The Kleene closure L of language L is defined to be the set of words obtained by concatenating any number of words from L together:
L =
Li .
i=0
For example,
{ab, b} = {, ab, b, abab, abb, bab, bb, ababab, ababb, . . .}.
Note that this notation is consistent with our use of to denote the set of all words over
the alphabet .
The positive closure L+ of L is
L+ =
Li ,
i=1
i.e. words that are concatenations of one or more words from L. For example,
{ab, b}+ = {ab, b, abab, abb, bab, bb, ababab, ababb, . . .}.
We have always
L = L+ {}.
Also, we have
L+ = LL .
19
3. ( + b)(ab) ( + a).
Next we want to show that regular expressions can define exactly the same languages as DFA,
NFA and -NFA recognize, i.e., regular languages.
Theorem 20 (Kleene 1956) Language L is regular if and only if there exists a regular expression
for it.
Proof. To prove the theorem we need to show two directions:
(A) We show how to construct for any given regular expression an equivalent -NFA.
(B) We show how to construct for any given DFA an equivalent regular expression.
Part(A): Let r be any regular expression. Then r can be , or a, or it can be formed from two
smaller regular expression s and t by union r = s + t, concatenation r = st or the Kleene closure
r = s . The construction is done inductively: we show how to construct an -NFA for , and a.
Then we show how to construct an -NFA for s + t, st and s if we already have -NFA for s and
t. Using these constructions one can build an -NFA for any regular expression. We prove even
more: we prove inductively that everything can be build using only one final state.
1. Below are diagrams for automata that recognize , {} and {a}. Every machine has exactly
one final state:
a
r=
r= e
r=a
2. Assume we have -NFA A1 and A2 for regular expressions s and t, respectively. Assume A1
and A2 have only one final state each:
q1
q2
f1
f2
The following -NFA recognizes the union s + t. Two new states are added that become the
new initial and final states, and four -transitions allow spontaneous moves to either A1 or
A2 .
21
q1
A1
f1
e
q2
e
f2
q1
f1
q2
A2
f2
q1
A1
f1
All constructions above work, and the resulting -NFA have exactly one final state. Using these
constructions we can build an -NFA for any given regular expression. This completes the proof of
(A).
Part (B): Let
A = (Q, , , q0 , F )
be any given DFA. We want to construct a regular expression for language L(A). Let us number
the states from 1 to n:
Q = {1, 2, . . . , n}.
k:
In this construction we build regular expressions for the following languages Rij
k = {w |
Rij
Going through a state means that the state is along the computation path, excluding the starting
and ending of the computation. For example, in the DFA
22
a,b
2
b
3 1 1 2 3
2 since its
does not go through any state greater than 2, but word baaab does not belong to R33
computation path goes through state 3:
3 1 2 3 2 3,
2 since its computation path does not end in state 3.
and word bbaba is not in R33
k for the languages Rk . We start with k = 0 and then
Let us construct regular expressions rij
ij
move up to larger values of k.
k is the language of all words that take the automaton from state i into state j
1. k = 0. Now Rij
without going through any states at all. Only possible input words are (if i = j) and single
letters a (if (i, a) = j). If a1 , a2 , . . . , ap are the input letters with transitions from state i to
0 has regular expression
j then Rij
a1 + a2 + . . . + ap
(in the case i 6= j) or
+ a1 + a2 + . . . + ap
(in the case i = j). If there are no transitions between the two states then regular expression
is used.
k1
2. Let k > 0, and assume we have constructed regular expressions for all Rij
. Consider an
arbitrary computation path from state i into state j that only goes through states {1, 2, . . . , k}.
Let us cut the path into segments at points where it goes through state k:
k1
All segments only go through states {1, 2, . . . , k 1}. The first segment is in the set Rik
,
k1
and the last segment belongs to Rkj . All middle segments start and end in state k, so they
k1
belong to Rkk
. Number of middle segments can be arbitrary. So all words that visit k at
least once are in the set
k1
k1
k1
Rik
Rkk
Rkj
.
23
k1
We also must include words that do not visit state k even once: They belong to set Rij
.
These are all words that take the machine from the initial state to some final state, using any states
whatsoever on the way.
This completes the proof of (B): The construction provides an equivalent regular expression for
any given DFA.
In practice the construction in part (B) provides huge regular expressions even for small auk as you go along. The following simtomata. It is a good idea to simplify the expressions rij
plifications are especially useful: For any regular expressions r, s and t, both sides of following
simplification rules describe the same language:
r, r
(r + )
r, r
+ r, r +
r(s + t)
(s + t)r
(r + )r , r (r + )
r
r
r
rs + rt
sr + tr
r
Example 21. As an example of the construction in part (A) of the proof, let us build an -NFA
for the regular expression
(abb + a) .
As an example of part (B), let us form a regular expression for the language recognized by the DFA
b
a,b
2
b
24
We finish this section by showing another, language equation based method for constructing a
regular expression from a DFA. As in the part (B) above, we number the states from 1 to n:
Q = {1, 2, . . . , n},
and assume that 1 is the initial state. For every i, j Q, let us denote
Li = {w | (1, w) = i}
and
Kij = {a | (i, a) = j}.
Then the following equalities of languages hold:
L1
L2
= L1 K11
= L1 K12
..
.
L2 K21
L2 K22
. . . Ln Kn1 {},
. . . Ln Kn2 ,
(4)
Using the Lemma on the equations in (4) one-by-one (and substituting the solutions obtained)
allows one to uniquely solve L1 , L2 , . . . , Ln .
Example 23. Let us use the method of language equations to find a regular expression for the
language recognized by
25
a,b
2
b
2.5
We have learned several types of devices to define formal languages, and we have proved that all
of them are able to describe exactly same languages, called regular languages. We presented
procedures for converting any device into an equivalent device of any other type. The conversion
procedures are mechanical algorithms in the sense that one can write a computer program that
performs any conversion. We say the devices are effectively equivalent.
Let us consider the following question: We are given a language L (described in English, for
example) and we want to prove that L is regular. This is fairly straightforward: all we have to do
is to design a finite automaton (DFA, NFA or -NFA) or a regular expression, and to show that
our design defines language L.
Consider then the converse situation: If language L is not regular how do we prove it ? How
can we show that there does not exist any finite state device that defines L ? We cannot try all
26
DFA one-by-one because there are infinitely many of them. In general, proving that something
cannot be done is harder than proving that it can be done, and that makes the negative question
more interesting.
Example 24. Consider the following example: Language
L = {ai bi | i 0}
contains all words that begin with any number of as, followed by equally many bs. Let us show
that there does not exist any DFA that recognizes L.
A DFA that recognizes L should first count the number of as in the beginning of the word.
The problem is that a DFA has only finitely many states, so it is bound to get confused: Some ai
and aj take the machine to the same state, and the machine can no longer remember whether it
saw i or j letters a. Since the machine accepts input word ai bi , it also accepts input word aj bi ,
which is not in the language. So the machine works incorrectly.
Let us be more precise: Assume that there exists a DFA
A = (Q, , , q0 , F )
such that L(A) = L = {ai bi | i 0}. Let n be the number of states in Q. Consider the accepting
computation path for the input word an bn .
q
0
j
a
0
an-k
qj=qk
bn
n
Input akj loops the automaton at state qj = qk . The loop can be repeated arbitrarily many times,
always getting an accepting computation. For example, if we repeat the loop twice we have an
accepting computation for the word
aj akj akj ank bn = an+(kj) bn .
This word is not in language L because k j 6= 0. Therefore automaton A is not correct: the
language it recognizes is not L.
Similar confused in counting argument works with many other languages as well. Instead of
always repeating the above argument in each case, we formulate the argumentation as a theorem
27
known as the pumping lemma. The word pumping refers to the fact that the loop can be repeated,
or pumped, arbitrarily many times. The pumping lemma states a property that every regular
language satisfies. If a language does not have this property then we know that the language is not
regular.
Theorem 25 (Pumping lemma) Let L be a regular language. Then there exists some positive
constant n such that, every word z L of length at least n can be divided into three segments
z = uvw
in such a way that
|uv| n, and
v 6= ,
(5)
u
0
qj=qk
We divide the input word z into three segments in a natural way: u consists of the first j letters,
v of the following k j letters, and w is the remaining tail. This division satisfies (5):
|uv| = k n, and
v 6= because |v| = k j > 0.
The loop reading input v can be repeated any number of times, which implies that uv i w L for
all i 0.
The pumping lemma gives a property satisfied by every regular language. Therefore, if language
L does not satisfy the pumping lemma then L is not regular. We can use the pumping lemma to
show that certain languages are not regular. But note the following: One can not use the pumping
lemma to prove that some language L is regular. The property is not if and only if: There are
some non-regular languages that satisfy the pumping lemma.
28
All languages
Languages that satisfy
the pumping lemma
Regular languages
Hence we apply the negated formulation of the pumping lemma. The pumping lemma says that
if L is regular then
(n)(z . . .)(u, v, w . . .)(i)uv i w L.
Therefore if L satisfies the opposite statement
(n)(z . . .)(u, v, w . . .)(i)
uv i w 6 L
then L is not regular. So to use the pumping lemma to show that L is not regular
(1) for every n, select a suitable word z L, |z| n, and
(2) show that for every division z = uvw where |uv| n and v 6= there exists a number i
such that
uv i w
is not in the language L.
If it is possible to do (1) and (2) then L does not satisfy the pumping lemma, and L is not regular.
Example 26. Consider the language
L = {ai bi | i 0}
from Example 24.
(1) For any given n, select z = an bn . The choice is good since z L and |z| n.
(2) Consider an arbitrary division of z into three part z = uvw where
|uv| n and v 6= .
Then necessarily u = aj and uv = ak for some j and k, and j < k. Choosing i = 2 gives
uv i w = uvvw = an+(kj) bn
which does not belong to L.
29
Example 27. Let us prove that
2
z = an .
The choice is good since z L and |z| = n2 n.
(2) Consider an arbitrary division z = uvw that satisfies (5). Let k denote the length of v. It
follows from (5) that 0 < k n. Let us choose i = 2. Then
2 +k
uv i w = uvvw = an
Because
n2 + k > n 2 + 0 = n2
and
n2 + k n2 + n < (n + 1)2 ,
number n2 + k is not a square of any integer. (It falls between two consecutive squares.)
Therefore uv 2 w does not belong to L.
Example 28. Let us use the pumping lemma to show that the following two languages are not
regular:
L = {ww | w {a, b} },
L = {ap | p is a prime number}.
2.6
Closure properties
It follows from the definition of regular expressions that the union L1 L2 of any two regular
languages L1 and L2 is regular. Namely, if r1 and r2 are regular expressions for L1 and L2 then
r1 + r2 is a regular expression for the union L1 L2 . In the same way, the concatenation of two
regular languages is always regular: r1 r2 is a regular expression for the concatenation L1 L2 . We
say that the family of regular languages is closed under union and concatenation.
In general, let Op denote some language operation, that is, an operation whose operands
and result are all formal languages. We say that the family of regular languages is closed under
operation Op if the result after applying Op on regular languages is always a regular language.
30
In this section we investigate the closure properties of regular languages under some common
operations. Let us first look into the familiar boolean operations: union, intersection and complement. We already know the closure under union. Consider then the complement. Let L be
a regular language over the alphabet . To prove that the complement
L = \ L = {w | w 6 L}
is also regular, consider a DFA A that recognizes L. Without loss of generality we may assume
that A uses alphabet , the same alphabet relative to which the complement is defined. (We can
remove all transitions, if any, that use letters not in , and we can add transitions to a sink state
with all letters of that are not in the original alphabet of the automaton.)
Every input word w takes the automaton to a unique state q. The word w is accepted if and
only if state q is among the final states of the machine. Let us change A by making every final
state a non-final state, and vice versa. The new machine accepts word w if and only if the original
machine did not accept w. In other words, the new machine recognizes the complement of language
L.
Example 29. For example, the languages recognized by the deterministic automata
b
a,b
a,b
a
Consider then the intersection of two languages. Elementary set theory (de Morgans laws) tells us
how to use union and complement to do intersection:
L1 L2 = L1 L2
for any sets L1 and L2 . If L1 and L2 are regular, so are L1 and L2 . Therefore also L1 L2 and
L1 L2 are regular. This proves that the family of regular languages is closed under intersection
as well.
Here is an important point to note: All closures above are effective in the sense that we can
mechanically construct the result of the operation on any given regular languages. For example the
union can be constructed by just inserting a plus sign between the regular expressions of the input
languages. It is irrelevant which description we use for the operands because DFA, NFA, -NFA
and regular expressions are effectively equivalent with each other.
We say that the family of regular languages is effectively closed under operation Op if there
exists an algorithm (=mechanical procedure) that produces the result of the operation for any given
regular input languages. The format of the inputs and outputs can be any of the discussed devices
(finite automaton or regular expression) since we have algorithms for converting from one format
to any other.
We have proved the following theorem:
31
Theorem 30 The family of regular languages is effectively closed under union, concatenation,
Kleene star, complementation and intersection.
Operations union, concatenation and Kleene star are called the rational operations.
The closure properties can be used to prove languages regular. It is enough to show how to
build the language from known regular languages using above operations.
Example 31. The language L containing all words over the English alphabet
= {a, b, c, . . . , z}
that do not contain the word matematiikka as a subword is regular. Namely, it is the complement
of the language
matematiikka .
We can also use closure properties to show that some languages are not regular:
Example 32. Let
L = {an cm bn | n, m 0}.
We could use pumping lemma to prove that L is not regular. But we can also reason as follows:
Assume that L is regular. Then also the language
L a b
is regular because the family of regular languages is closed under intersection and a b is regular.
But we know from the previous section that the language
L a b = {an bn | n 0}.
is not regular. Therefore our assumption that L is regular has to be false.
So if you start with L, apply operations to L and known regular languages, and end up with a
language that is known to be non-regular, then L is non-regular. (Be careful not do it in the wrong
direction: If you start with L, apply operations on L and regular languages and end up with a
known regular language, then you can not conclude anything about the regularity of L.)
32
Next we introduce some new language operations. Let and be two alphabets. A homomorphism from to is a function
h : ,
that assigns to each letter a a word h(a) . For example,
h(a) = 0,
h(b) = 01.
gives a homomorphism from = {a, b} to = {0, 1}.
Homomorphisms are applied to words by coding each letter separately and concatenating the
results together:
h(a1 a2 . . . an ) = h(a1 )h(a2 ) . . . h(an ).
For example, using the homomorphism h above we have
h(ba)
= 010,
h(babba) = 01001010,
h()
= .
Homomorphisms are applied to languages as well: The homomorphic image h(L) of a language L
consists of all homomorphic images of all Ls words:
[
h(L) = {h(w) | w L} =
{h(w)}
wL
33
Substitution s is also applied to words by applying it to each letter separately and concatenating
the results (which are languages) together:
s(a1 a2 . . . an ) = s(a1 )s(a2 ) . . . s(an ).
For example, using our sample substitutions s and f we have
f (ba) = (01 + )(000 + 11) = 01000 + 0111 + 000 + 11,
s(aba) = (0 + 11)0 10 (0 + 11),
s()
= {}.
The result of applying substitution s on a language L is the language consisting of all words that
can be obtained by applying the substitution on Ls words:
[
s(L) = {u | u s(w) for some w L } =
s(w).
wL
For example,
s(ba + aba) = s(ba) s(aba) = 0 10 (0 + 11) + (0 + 11)0 10 (0 + 11),
s(ba )
= 0 10 (0 + 11) .
Theorem 34 If L is a regular language and s a regular substitution then s(L) is a regular language.
The family of regular languages is (effectively) closed under regular substitutions.
Proof. Let L be a regular language and s a regular substitution. Let r be a regular expression for
L, and let ra be a regular expression for s(a) for every letter a .
To get a regular expression r0 for s(L) one simply replaces every letter in the expression r by
the corresponding regular expression ra . To prove formally that the resulting regular expression
r0 represents the language s(L), one uses mathematical induction on the size of expression r. The
construction works because
s({}) = {},
s({a}) = ra ,
s(L1 L2 ) = s(L1 ) s(L2 ),
s(L1 L2 ) = s(L1 )s(L2 ),
s(L ) = s(L)
for all a , all languages L1 , L2 , L and all substitutions s.
Example 35.
s(a b + bb) = s(a b) + s(bb) = s(a )s(b) + s(b)s(b) = s(a) s(b) + s(b)s(b) = ra rb + rb rb .
34
The closure under homomorphisms and regular substitutions can now be used to prove nonregularity of languages.
Example 38. Let us prove that
L = {(ab)p | p is a prime number }
is not regular. We already know that
Lp = {ap | p is a prime number }
is not regular, so let us reduce L into Lp . Assume that L is regular. Then also h(L) is regular
where h is the homomorphism
h(a) = a,
h(b) = .
But h(L) = Lp , which is not regular. Therefore L is not regular either.
Let us move on to our next language operation, the inverse homomorphism. Let h be a
homomorphism from to . The inverse homomorphism h1 is defined on languages of alphabet
. If L then language h1 (L) consists of all words over that are mapped by homomorphism
h into L, i.e.,
h1 (L) = {w | h(w) L}.
If L contains only one word we may simply write h1 (w) instead of h1 ({w}).
Example 39. For example, using homomorphism
g(a) = 01,
g(b) = 011,
g(c) = 101,
we have
g 1 (011101)
g 1 (01101)
g 1 (010)
g 1 ((10) 1)
=
=
=
=
{bc},
{ac, ba},
,
ca .
Theorem 40 The family of regular languages is (effectively) closed under inverse homomorphism.
35
p
a
q
In A0
h(a)
In A
b
a
Constructs of type
h1 (L) R
are very useful, where h is a homomorphism and R is a regular language. They contain all words
of R that are mapped to L by homomorphism h.
36
Our next language operation is called quotient. Let L1 and L2 be two languages. Their quotient
L1 /L2 is the language containing all words obtained by removing from the end of L1 s words a
suffix that belongs to L2 .
L1 /L2 = {w | wu L1 for some u L2 }.
Example 43. Let
L1 = abaa + aaa,
L2 = a + baa,
L3 = a ba .
Then
L1 /L2
L2 /L2
L3 /L1
L3 /L3
=
=
=
=
aba + aa + a,
+ ba,
a + a ba ,
a .
Theorem 44 The family of regular languages is closed under quotient with arbitrary languages.
In other words, if L1 is a regular language, and L2 any language (not necessarily even regular) then
L1 /L2 is regular.
37
Proof. Let L1 be a regular language, and A a DFA recognizing it. Let L2 be an arbitrary language.
We show that there is a DFA A0 that recognizes the quotient L0 = L1 /L2 .
DFA A0 has exactly same states and transitions as A has. Also the initial state is the same.
Only the final state sets F 0 and F are different:
F 0 = {q | u L2 :
(q, u) F },
i.e., state q is made final in A0 if and only if some word u L2 takes A from state q to some original
final state.
some u from L2
w
q
Clearly word w is accepted by A0 if and only if some wu is accepted by A for some u L2 . But
this is equivalent to saying that w belongs to L1 /L2 : it is obtained from word wu L1 by deleting
word u L2 from the end. So
L(A0 ) = L1 /L2 .
b
b
and let
L2 = {an bn | n 0}.
Language L1 /L2 is recognized by a DFA that has the same transitions as A above. We only have
to figure out which states are final:
a
b
b
Remark: closure under quotient is not effective. If L2 is some complicated language we may not
have any way of determining which states to make final. Well see later that quotient can be
effectively constructed if both L1 and L2 are regular.
38
2.7
Decision algorithms
In this section we present algorithms for determining whether a given regular language is empty,
finite or infinite. We also present an algorithm for determining whether two given regular languages
are identical, i.e. whether they contain exactly the same words. First we make the same observation as before: it does not matter in which form the input language is represented: as a regular
expression, DFA, NFA or -NFA. All representations are effectively equivalent.
Theorem 46 There is an algorithm to determine if a given regular language is empty.
Proof. Consider any regular expression r. We want to determine whether the language L(r) it
represents is empty or not. Observe that
If r = or r = a then L(r) is not empty.
If r = then L(r) is empty.
If r = r1 + r2 then L(r) is empty if and only if both L(r1 ) and L(r2 ) are empty.
If r = r1 r2 then L(r) is empty if and only if L(r1 ) or L(r2 ) is empty.
If r = (r1 ) then L(r) is not empty.
Based on these facts one can easily write a recursive algorithm for determining whether L(r) is
empty:
Empty(r)
Begin
if r = or r = a for some letter a then return( False )
if r = then return( True )
if r = r1 + r2 then return( Empty(r1 ) and Empty(r2 ) )
if r = r1 r2 then return( Empty(r1 ) or Empty(r2 ) )
if r = r1 then return( False )
End
Remark: Emptiness can be also easily determined directly from a finite automaton representation
of a regular language. One just determines (using basic graph algorithms) whether there is a path
in the transition diagram from the initial state to some final state.
Consider then the equivalence problem. Some regular expressions stand for the same language.
For example, a(a + ba) and (ab + a) a both define the same language. How can we determine for
any given two regular expressions correctly if their languages are identical ?
Theorem 47 There is an algorithm to determine if two regular languages are the same.
39
Proof. Let L1 and L2 be two regular languages (represented by finite automata or regular expressions.) Using the effective closure properties proved earlier we can construct a regular expression
for their symmetric difference
L = (L1 \ L2 ) (L2 \ L1 ) = (L1 L2 ) (L2 L1 ).
Since L1 = L2 if and only if L is empty, we can use the algorithm for emptyness to find out whether
L1 and L2 are the same language.
Our last decision algorithm determines whether a given regular language contains finitely or infinitely many words. We know that every finite language is regular:
{w1 , w2 , . . . , wn } = L(w1 + w2 + . . . + wn ).
Some regular languages are infinite, for example a .
Theorem 48 There is an algorithm to determine if a given regular language is infinite.
Proof. Let r be a regular expression.
If r = , r = a or r = then L(r) is not infinite.
If r = r1 + r2 then L(r) is infinite if and only if L(r1 ) or L(r2 ) is infinite.
If r = r1 r2 then L(r) is infinite if and only if L(r1 ) and L(r2 ) are both non-empty, and at
least one of them is infinite.
If r = (r1 ) then L(r) is infinite unless L(r1 ) is or {}.
Based on these facts one can easily write a recursive algorithm for determining whether L(r) is
infinite. The programs for emptyness and equivalence are used as subroutines.
Note that all effective constructions presented earlier may be used as subroutines in new algorithms.
Example 49. To test whether L1 L2 for given regular languages L1 and L2 , we simply test
whether L1 \ L2 = L1 L2 = .
2.8
Myhill-Nerode theorem
40
An equivalence relation R partitions S into disjoint equivalence classes such that aRb if and only if
a and b are in the same equivalence class. Let us denote the equivalence class containing a by [a]:
[a] = {b S | aRb}.
The index of an equivalence relation is the number of equivalence classes. The index can be finite
or infinite.
Every positive integer n defines the equivalence relation a b (mod n) on the set of integers.
In an (somewhat) analogous way every language L defines an equivalence relation RL on
words of :
w RL u
(x ) wx L if and only if ux L
Words w and u are in the relation RL iff exactly same extensions take them to L.
To find out whether given words w and u are in relation RL for given language L, we have to
find out for which extensions x words wx and ux belong to L. Let us denote the language of good
extensions x by
Ext(w, L) = {x | wx L}.
Then w RL u if and only if
Ext(w, L) = Ext(u, L).
Example 50. Let L = aa + baa. Then
Ext(a, L)
Ext(b, L)
Ext(ba, L)
Ext(, L)
Ext(aa, L)
Ext(aba, L)
=
=
=
=
=
=
a,
aa,
a,
aa + baa,
,
.
We see that words a and ba are in relation RL with each other. In fact,
[a] = a + ba.
The relation RL has five equivalence classes
,
b,
a + ba,
aa + baa
and
+ a + b + aa + ba + baa,
aa,
a,
and
respectively.
for all w a ,
for all w a b+ ,
for all w
6 a b .
Example 52. Let us find the equivalence classes of RL for the language
L = {w {a, b} | w contains equally many as and bs } .
Let us denote
|w|a = number of letters a in word w.
Consider languages
Ln = {w {a, b} | |w|a |w|b = n}
for different n Z. Clearly L = L0 . Every word belongs to exactly one Ln . Observe also that
Ln Lm Ln+m .
It follows from the considerations above that if w Ln then
Ext(w, L) = Ln .
Therefore Ext(w, L) and Ext(u, L) are identical if and only if w and u belong to the same set Ln .
In other words, the equivalence classes of RL are languages Ln for all n Z.
Note that in this case there are infinitely many equivalence classes.
u
This means that wRL u.
Since all words leading to the same state are in relation RL , the number of equivalence classes
is at most the number of states, and therefore finite.
Note that we have in fact shown that the number of states in any DFA recognizing L is at least
the index of RL . In fact, we can make the following observations: Let us assign to every DFA A
its own equivalence relation RA by
We still need to prove the other direction of the if and only if statement:
(=) Assume that RL has finite index. Let L1 , L2 , . . . , Ln be its equivalence classes. We
construct a DFA A with n states that recognizes L. The state set of A is labeled with the equivalence
classes [w]:
Q = {L1 , L2 , . . . , Ln }.
The idea of the construction is that every input word w will lead to state [w], the equivalence class
containing w. The transitions are defined as follows:
([w], a) = [wa].
The transitions are well defined, that is, the definition does not depend on which word w we selected
from the equivalence class: If [w] = [u] then [wa] = [ua]. (If we would have [wa] 6= [ua] then for
some x, wax L and uax 6 L or vice versa. But then also [w] 6= [u] since the extension ax
separates w and u.)
Let the initial state q0 be []. Then
(q0 , w) = [w]
for every w. The transitions were designed that way:
([], a1 a2 . . . an ) = ([a1 ], a2 a3 . . . an ) = ([a1 a2 ], a3 . . . an )
= . . . = [a1 a2 . . . an ].
Finally, the final states of our automaton are
{[w] | w L}.
Again, the choice of w does not matter since if [w] = [u] and w L then also u L. (Otherwise
extension would separate them.)
The automaton we constructed recognizes language L because
(q0 , w) = [w]
and [w] is a final state if and only if w L. This completes the proof.
Example 54. Let us construct a DFA for L = a b based on our knowledge about its equivalence
relation RL :
43
a*
a,b
+
a* b
a* b*
Note that the number of states of the DFA constructed in the proof is the same as the index of
RL . On the other hand, we observed before that every DFA recognizing L has at least that many
states, so the DFA we constructed has the minimal number of states among all DFA recognizing
L. This number of states is the index of the equivalence relation RL .
The DFA we constructed is the minimum state DFA for language L. No other DFA for L has
fewer states. In fact, the minimum state DFA is unique: every other DFA for L has more states:
Theorem 55 The minimum state DFA of language L is unique (up to renaming states).
Proof. Let A be a DFA for language L with as few states as possible, and let A0 be the automaton
constructed in the proof of Theorem 53. We want to prove that A is the same automaton as A0 ,
except that its states may be named differently.
The number of states in both machines is the same as the index of RL , and therefore the
automata relations RA and RA0 are identical to RL . (They are refinements of RL with the same
number of equivalence classes as RL .) Since the relations are the same we do not need to specify
which relation RL , RA or RA0 we mean when we talk about the equivalence class [w] of word w:
all three relations define identical equivalence classes.
Recall the definition of RA : two words belong to the same equivalence class if and only if they
take machine A to the same state:
w RA u (q0 , w) = (q0 , u)
Let us rename the states of A using the equivalence classes: For every w , rename the state
(q0 , w) as [w]. The naming is well defined: if (q0 , w) = (q0 , u) then [w] = [u], so the name is
independent of the choice of w.
Then trivially
([w], a) = [wa]
for every w and a . But this exactly how the transitions of A0 were defined in the proof
of Theorem 53. This means the transitions of A are identical to the transitions of A0 , and the
machines are identical.
In the rest of this section we discuss the minimization process, i.e., we give an algorithm to
construct the minimum state DFA that is equivalent to a given DFA.
Theorem 56 There is an algorithm to construct the minimum state DFA for given regular language.
44
Let A be a DFA that recognizes language L. In the following we construct a minimum state DFA
that is equivalent to A.
1. The first step is to remove all non-reachable states from A. State q is non-reachable if there
does not exist any input word that takes automaton A to state q.
Reachable states can be found by a simple marking procedure: First mark the initial state only.
Then mark all states that there is a transition into from some marked state. Continue this until
no new states can be marked. All non-marked states are non-reachable and can be deleted. For
example, the shaded states are reachable in the following automaton:
a
a
a b
b
a
a b
b
b
a
a
a
a
b a
b
b
b
a
a
b
2. Now assume DFA A only contains reachable states. Every state q defines an equivalence class
Eq of the automaton relation RA , consisting of all words that take the machine from initial state
q0 to state q:
Eq = {w | (q0 , w) = q}.
We know that RA is a refinement of the relation RL :
Eq
Ep
Er
In the minimum state DFA the equivalence classes of the automaton are the same as the equivalence
classes of RL . We can combine states q and p if their classes Eq and Ep belong to the same equivalence class of RL . We call such states indistinguishable. If states q and p are indistinguishable
it does not make any difference whether the machine is in state q or p: Exactly same words lead
to a final state from q and p. If we find all pairs of indistinguishable states and combine them we
end up with a DFA A0 whose equivalence relation RA0 is identical to RL . Such A0 is the minimum
state DFA for language L.
The problem is to find out if two states are indistinguishable. It is easier to find states that are
distinguishable: To show that p and q can not be combined you only need to find one word x such
that one of the states (q, x) and (p, x) is final and the other one is not.
45
Let us start classifying the states. We put states that we know are distinguishable into separate
classes. Initially we have two classes of states: one contains all final states and the other one all
non-final states. The empty word distinguishes final and non-final states.
Our sample DFA
a
a
a b
b
1
a b
b
a
b
b a
b
7
a
a
a b
b
1
a b
b
a
b
b a
b
7
We repeat the process until no class be split any more. Then we can construct an automaton by
combining all states that belong to same class. The transitions are uniquely defined: otherwise we
could split the classes further.
In our example the three classes D1 , D2 and D3 cannot be split any further, so we know that
the equivalent minimum state DFA has three states:
46
a
b
1,4
a
2,3,6,7
Classes that contain final states are made final; class that contains the original initial state is the
initial state of the new DFA.
Example 57. Let us construct the minimum state DFA that is equivalent to
a
2
a
1
b
a
b
5
Remark: The minimization process and all theorems of this section only apply to deterministic
automata. There can be several different minimal state non-deterministic automata for the same
language.
Context-free languages
Superficially thinking, one might view modern computers as deterministic finite automata: Computers have finite memory, so there are only finitely many states the computer can be at, and the
previous state of the computer determines the next state, so the machine has deterministic state
transitions. This approach is, however, not reasonable. First of all, the number of states is indeed
finite but that finite number is astronomical. And even worse: the memory of the computer is
extendible, so the approach is not even theoretically correct. A more accurate mathematical model
is needed.
Our next step into the direction of correcting the notion of computation is to overcome the
limitations of finite memory by introducing new models that allow arbitrarily large memory. The
memory can be organized and accessed in different ways. Our next model, context-free languages,
assumes an infinite memory that is organized as a stack (=last-in-first-out memory). As we are
going to see, this does not yet capture the full power of computation, but it provides a natural
generalization of regular languages. Every regular language is a context-free language, but also some
non-regular languages are included. For example {an bn | n 0} is a context-free language. On the
47
other hand there are many simple languages which are not context-free, for example {an bn cn | n
0}.
Context-free languages have applications in compiler design (parsers). The syntax of programming languages is often given in the form of context-free grammar, or equivalent Backus-Naur form
(BN-form).
3.1
Context-free grammars
AbA
B
aBa
b
In a word containing variables, and possibly terminals, one can replace any variable with the word
on the right hand side of a production for that variable. For example, in the word
aBabAbA
one can, for example, replace the first occurrence of variable A by AbA, obtaining the word
aBab AbA bA.
Then one can decide, for example, to replace the variable B by b, which gives the word
a b abAbAbA.
Rewriting one variable is called a derivation step and we denote
aBabAbA aBabAbAbA ababAbAbA.
Derivation is non-deterministic: usually one has many choices of how to proceed. We can continue
the derivation as long as there exist variables in the word. Once the word contains only terminal
symbols the derivation terminates.
One variable is called the start symbol. In the case of this example, let A be the start symbol.
All derivations start with the start symbol A, i.e., initially the word is A. The language defined by
the grammar contains all strings of terminal symbols that can be obtained from the start symbol
by applying the productions on the variables. For example, word aabaababa is in the language
defined by our grammar since it is derived by
A AbA BbA aBabA aaBaabA aabaabA aabaabB aabaabaBa aabaababa.
On the other hand, word aaa is not in the language because the only way to produce letter a is to
use the production B aBa, and it creates two as, so the number of as has to be even.
two disjoint finite alphabets, V and T , containing variables (=nonterminals) and terminals,
respectively,
a finite set P of productions of the form
A
where A V is a variable, and (V T ) is a word of terminals and variables,
a start symbol S V .
For any , (T V ) we denote
if is obtained from by rewriting one variable in using some production from P , that is, if
= uAv and = uwv for some u, v, w (V T ) and A V , and A w is in P . This is called
a derivation step. The reflective, transitive closure of is denoted by , so iff there is a
sequence of derivation steps
= 0 1 2 . . . n =
that starts with and leads to . The number n of derivation steps can be zero, so always .
We say that derives . If we want to emphasize that a derivation takes n steps, we may use the
notation n .
A word (V T ) is called a sentential form if it can be derived from the start symbol S,
i.e., if
S .
The language L(G) generated by grammar G consists of all sentential forms that contain only
terminals. In other words,
L(G) = {w T | S w}.
A language is called a context-free language if it is L(G) for some context-free grammar G.
Example 59. Consider the grammar G = (V, T, P, S) where V = {S}, T = {a, b}, and P contains
the productions
S aSb,
S .
To make notations shorter we may use the following convention: productions of the same variable
may be combined on one line, separated by symbol |. So we may write
S aSb |
We easily see that L(G) = {an bn | n 0}.
Example 60. Consider the grammar G = (V, T, P, E) where V = {E, N }, T = {+, , (, ), 0, 1},
and P contains the following productions:
E E + E | E E | (E) | N
N 0N | 1N | 0 | 1
49
For example, all the following words are in the language L(G):
0
0 1 + 111
(1 + 1) 0
(1 1) + (((0000)) 1111)
For instance, (1 + 1) 0 is derived by
E E E (E) E (E + E) E (N + N ) N (1 + 1) 0.
From the variable N all nonempty strings of symbols 0 and 1 can be derived.
From the variable E one can derive all well-formed arithmetic expressions containing operators
* and +, parentheses, and strings of 0s and 1s derived from variable N .
Here are some words that are not in the language L(G):
1(1
()
1 1 1 1
Example 61. Grammar G = (V, T, P, S) where V = {S}, T = {a, b} and P contains productions
S aSbS | bSaS | .
Let us determine L(G). Clearly every word that belongs to L(G) must contain equally many as
and bs. (Every production adds the same number of as and bs to the sentential form.) Let us
next prove that L(G) contains all words w {a, b} with equally many as and bs. Let
L = {w {a, b} | w has equally many as and bs }.
We observe first that any non-empty word w L can be written as
w=aubv
or
w=buav
for some u, v L. Let us prove this fact. Assume w L and w starts with letter a (if it start with
letter b the proof is symmetric.) Let au be the longest prefix of w with more as than bs. The next
letter coming after au has to be b because aua has more as than bs and is longer than au. So w
starts a u b . . .. In the word au the number of as is exactly one greater than the number of bs: If
the difference would be more than one then also prefix a u b would contain more as than bs, and
it is longer than au. So u has equally many as and bs. The suffix v that follows a u b has also
equally many as and bs. So w = a u b v and u, v L.
Now we are ready to prove that every word w L is in L(G). We use induction on the length
of w.
1 (basis) If |w| = 0 then w = L(G) because S is in P .
50
2 (inductive step) Assume |w| > 0, and we know that every shorter word with equally many
as and bs can be derived from S. We demonstrated above that
w=aubv
or
w=buav
where u and v are words that contain equally many as and bs. According to the inductive
hypothesis,
S u
and
S v.
If w = a u b v then we can derive w as follows:
S a S b S a u b v.
The case w = b u a v is similar. In either case, we have a derivation for w so w L(G).
For example, ababba has the following derivation:
S aSbS abSaSbS abaSbS ababS ababbSaS ababbaS ababba.
Let us prove next that every regular language can be generated by a context-free grammar.
Example 62. Let L be the regular language recognized by the NFA A
b
a
Let us construct a grammar G that generates the same language L. The variables of G are the
states {Q, P } and the terminals are {a, b}. The start symbol is the initial state Q. Every transition
of A is simulated by a production:
Q bQ
Q bP
P aQ
Whenever the automaton reads a letter and goes to state q = Q or P , the grammar produces the
same letter and changes the variable to indicate the current state q.
Each sentential form consists of a terminal word w followed by one variable. Clearly, word wq
is a sentential form if and only if the automaton can go to state q reading input w. For example,
bbaQ is a sentential form because the machine can read bba ending up in state Q:
Q bQ bbP bbaQ.
To terminate the simulation we add the transition
Q .
51
This is correct because Q is a final state: the generation is terminated iff the corresponding computation by A is accepting. For example, when accepting the word bab, the automaton is in states
Q, P, Q, Q, in this order. The corresponding derivation by the grammar is
Q bP baQ babQ bab.
It is easy to prove (using induction on the length of the terminal word) that L(G) = L.
The analogous construction can be done on any NFA, proving the following theorem.
Theorem 63 Every regular language is (effectively) context-free.
Proof. Let L be a regular language and let A = (Q, , , q0 , F ) be an NFA such that L = L(A). An
equivalent grammar is G = (V, T, P, S) where
V = Q,
T = ,
S = q0 ,
and P contains a production q ap for all q, p Q and a such that p (q, a), and a
production q for every q F . This grammar only derives sentential forms that contain at
most one variable, and the variable must appear as the last symbol of the form. Moreover, using a
straightforward induction on the length of w one sees that S wq for w and q Q if and
only if q (q0 , w). Hence S w for w if and only if w L(A).
3.2
Derivation trees
Derivations by context-free grammars can be visualized using derivation trees, also called parse
trees. Here is a derivation tree for the grammar of Example 60:
52
!!
!!
!!
aa
aa
aa
E
#
c
c
SS
SS
All the interior nodes of the tree are labeled using variables. The label of the root is the start
symbol E of the grammar. The children of a node are the symbols on the right hand side of some
production for the parents variable, in the correct order. The tree is ordered: the order of the
children matters. For example, the labels of the children from left to right of the root are E, +
and E, corresponding to the production
E E + E
in the grammar. Note that if you read the symbols on the leaves from left to right you get the
word 01 + (1 0) which is a valid sentential form for the grammar. This is called the yield of the
tree.
More generally, a derivation tree for a grammar G = (V, T, P, S) is a directed, ordered tree
whose nodes are labeled by symbols from the set
V T {}
in such a way that
the interior nodes are labeled by variables, i.e. elements of V ,
the root is labeled by the start symbol S,
if X1 , X2 , . . . , Xn are the labels of the children of a node labeled by variable A, ordered from
left to right, then
A X1 X2 . . . Xn
is a production belonging to P . If n 2 then no Xi may be .
Note that the label is needed to allow productions A in the derivation tree.
If one reads the labels of the leaves from left to right, one obtains a word over (V T ) . This
word is called the yield of the derivation tree. More precisely, the yield is obtained by traversing
the tree in the depth-first-order, always processing the children of each node from left-to-right, and
recording the leaves encountered during the traversal.
53
A subtree of a derivation tree is the tree formed by some node and all its descendants. A
subtree is like a derivation tree except that its root may have a different label than the start
symbol. In general, a tree that is like a derivation tree except that the root is labeled by a variable
A instead of the start symbol S, is called an A-tree.
In our example,
N
SS
1
is a subtree, an N -tree.
It is clear that any derivation S in the grammar has a corresponding derivation tree whose
yield is the sentential form . Conversely, every derivation tree represents derivations according to
the grammar. Note, however, that the tree does not specify in which order parallel variables are
rewritten, so the derivation obtained from a given tree is not unique. For example, the derivation
tree in the beginning of the section represents the derivation
E E + E N + E 0N + E 01 + E 01 + (E) 01 + (E E)
01 + (N E) 01 + (1 E) 01 + (1 N ) 01 + (1 0)
where we have underlined in every sentential form the variable being rewritten in the next derivation
step. But the same tree also represents the derivation
E E + E E + (E) E + (E E) E + (E N ) E + (E 0) E + (N 0)
E + (1 0) N + (1 0) 0N + (1 0) 01 + (1 0).
Different derivations that correspond to the same tree only differ in the order in which variables are
rewritten. A derivation is called leftmost if at every derivation step the leftmost variable of the
sentential form is rewritten. The first derivation above is leftmost. The second derivation above is
rightmost: One always rewrites the rightmost variable.
Every derivation tree defines a unique leftmost derivation, and a unique rightmost derivation.
The leftmost derivation rewrites the variables in the order in which the depth first traversal of the
tree from left to right encounters them. The rightmost derivation corresponds to the depth first
traversal from right to left. So we have a one-to-one correspondence between derivation trees and
leftmost (and rightmost) derivations.
Finding a derivation for w L(G) (or equivalently, finding a derivation tree with yield ) is
called parsing. Later, in Section 3.7, we discuss an efficient parsing algorithm.
Note that even though every derivation tree defines a unique leftmost derivation, some words
may have several leftmost or rightmost derivations. This happens if a word is the yield of more
than one derivation tree. For example, in our sample grammar the word 1 + 0 + 1 has the following
two leftmost derivations
E E + E E + E + E 1 + E + E 1 + 0 + E 1 + 0 + 1, and
E E+E
1+E
1 + E + E 1 + 0 + E 1 + 0 + 1,
corresponding to the derivation trees
54
E
1
E
1
A context-free grammar G is called ambiguous if some word has more than one leftmost
derivation (equivalently: more than one derivation tree). Otherwise the grammar is unambiguous.
In unambiguous grammars there is essentially just one way to parse each word in L(G).
Our sample grammar is ambiguous. However, one can find an equivalent grammar that is not
ambiguous:
E P | E + P
P A | P A
A N | (E)
N 0N | 1N | 0 | 1
(Both non-ambiguity and equivalence to the original grammar can be proved using mathematical
induction on the length of the word.)
For some context-free languages there exist only ambiguous grammars. Such languages are
called inherently ambiguous. Note that ambiguity is a property of a grammar, while inherent
ambiguity is a property of a language.
There does not exist an algorithm that would determine if a given context-free grammar G is
ambiguous or unambiguous. (This will be proved in the last part of these notes.) For individual
grammars one can come up with ad hoc proofs of unambiguity.
Example 64. The grammar G1 = ({S}, {a, b}, P1 , S) where P1 contains the productions
S aSb | aaS |
is ambiguous because the word aaab has two different leftmost derivations
S aaS aaaSb aaab
and
Example 65. The grammar G2 = ({S}, {a, b}, P2 , S) with the productions
S aSSb | ab
is unambiguous. Consider two different leftmost derivations D1 and D2 . Let us prove that the
words they derive must be different.
55
Let wS be the sentential form right before the first derivation step where D1 and D2 differ.
Here, w is a terminal word and may contain both terminal and non-terminal symbols.
Derivations D1 and D2 apply different productions to S so after the next derivation step we
obtain the sentential forms waSSb and wab. The words that can be derived from these two
sentential forms begin waa . . . and wab . . ., respectively, and are therefore different.
Example 66. Let us determine whether the grammar G3 = ({S}, {a, b}, P3 , S) with productions
S aSb | bS |
is ambiguous or unambiguous.
Also inherent ambiguity is an undecidable property. In other words, there does not exist an
algorithm for determining if the language L(G) generated by given context-free grammar G is
inherently ambiguous or not. Here is an example of an inherently ambiguous CF language (we skip
the proof):
{an bn cm em | n, m 1} {an bm cm en | n, m 1}.
3.3
Grammars G and G0 are called equivalent if L(G) = L(G0 ). Certain types of productions are
undesirable in a grammar, and we would like to find an equivalent grammar that does not contain
such productions. In the following we want to remove productions of the forms
A
A B
(-production), and
(unit production),
where A and B are variables. We also want to remove symbols that are unnecessary in the sense
that they are not used in any terminating derivations. The simplification is done in the following
order:
1. Remove -productions.
56
57
If several Xi are nullable, all combinations of i = and Xi are used. One original production
can result in up to 2k new productions if the righthand-side contains k nullable variables. In
our example, the new productions will be
S
A
B
C
D
E
AC | C | A | aB | AD | D
ab | S
Aa | a | AB | B
AAa | Aa | a
EbD
bb
There are no -productions. The language generated by the new grammar is L(G) \ {}.
The construction works because the empty string is directly plugged in the righthandside of the production, whenever there is a variable that can produce . When we use that
production during a derivation, we decide at that point whether the nullable variable on the
right hand side will be used to produce an empty string, or a non-empty string. For example,
an original derivation
S AC abC ab
is replaced by the more direct derivation
S A ab.
Since C derived in the old derivation, we used S A instead of S AC.
Step 2.
Remove unit productions. Assume we have removed all -productions from P . The righthand side
of every production in P is some non-empty word. Let us find an equivalent grammar that does
not contain -productions or unit productions. The idea is similar to Step 1: We anticipate all
possible unit derivations, and plug them in directly into the productions.
(a) For every variable A, find all variables B, such that
A B.
All such variable-to-variable derivations A B can be found using a reachability algorithm
in the directed graph whose vertices are the variables and there is an edge from A to B iff
A B is in P . Clearly, A B iff there is a path in the graph from A to B. In our
example, after the simplification step 1, the unit productions are
S C | A | D
A S
B B
58
S|C |A|D
A|S|C |D
B
C
D
E
(b) For every pair of variables X and Y , X 6= Y , such that X Y and Y X, we can remove
one of the variables: They both derive exactly the same words. So we trim P by replacing Y
by X everywhere, both left- and righthand sides of all productions.
In our example we can remove A since it derives same words as S. This leaves us with a
smaller number of variables:
S
B
C
D
E
SC | C | S | aB | SD | D | ab
Sa | a | SB | B
SSa | Sa | a
EbD
bb
(c) Now we construct P 0 from the trimmed set P . For every non-unit production
Y
in P and every variable X such that X Y , we include in P 0 the non-unit production
X .
We obtain an equivalent grammar whose derivations may be shorter: An original derivation
X Y
that used unit productions X Y is replaced by a single derivation step
X .
In our example we end up with the following productions:
S
B
C
D
E
SC | aB | SD | ab | SSa | Sa | a | EbD
Sa | a | SB
SSa | Sa | a
EbD
bb
Such variables can be simply removed together with all productions containing the variable on
either side of the production. This does not effect the generated language since the variable is not
used in any terminating derivation.
To find variables that do generate some terminal string, we apply similar marking procedure as
with -productions: First we mark all variables X such that there is a production
X w
in P , where w T contains only terminals. Then we mark variables Y such that there is a
production
Y
in P where contains only terminals, and variables that have already been marked. This is
repeated until no more variables can be marked. The variables that have not been marked at the
end can be removed.
In our sample grammar, variable D does not derive any terminal string and can be removed.
We obtain the following grammar
S
B
C
E
SC | aB | ab | SSa | Sa | a
Sa | a | SB
SSa | Sa | a
bb
Step 4. Remove symbols (variables and terminals) that are not reachable from the start symbol
S.
We use a similar marking procedure: Initially mark the start symbol S. Then mark all symbols
appearing on the right hand side of a production for a variable that has been already marked.
Repeat until no new symbols can be marked. All symbols that have not been marked can not be
reached from the initial symbol, and they can be removed.
Note that it is important that step 3 is executed before step 4, not the other way around: Step
3 may introduce new unreachable symbols!
In our example, variable E is unreachable and can be removed. (But it became unreachable
only after we removed D!) We end up with the grammar
S SC | aB | ab | SSa | Sa | a
B Sa | a | SB
C SSa | Sa | a
After steps 1,2,3 and 4 we have found an equivalent (except the possible loss of the the empty word
) grammar that does not have -productions, unit productions, or useless symbols.
If the empty word was in the original language and we want to include it in the new grammar
we may introduce a nonrecursive start symbol. That is, we add a new variable S 0 , make it the
start symbol, and add the productions
S 0 , for every production S from the original start symbol S,
S 0
60
where S is the old start symbol. The new grammar is equivalent to the original one, and it has
only one -production that can be used only once.
Example 68. Let us simplify the following grammar by removing -productions, unit productions
and useless symbols:
S Aa | CbDS
A | BA
B AA
C a
D aAD
In many proofs it is useful to be able to assume that the productions in the grammar are of
some restricted, simple form. Several such simplified types of grammars are known to be equivalent
to general context-free grammars. They are called normal forms of grammars.
A grammar G is said to be in the Chomsky normal form (or CNF) if all productions are of
the forms
A BC, or
A a
where A, B and C are variables, and a is a terminal. Righthand-sides of productions consist of two
variables, or one terminal.
Theorem 69 Every context-free language L without is generated by a grammar that is in the
Chomsky normal form. An equivalent CNF grammar can be constructed effectively.
Proof. Let G be a grammar that generates L. First we use the simplification steps 1, 2, 3 and 4
above to find an equivalent grammar G0 that does not contain -productions or unit productions.
Because there are no unit productions, all productions with one symbol on the right hand side are
terminal productions A a, i.e., they are already in the Chomsky normal form.
We only need to work on productions
A X1 X2 . . . Xn
where n 2, and Xi V T . For every terminal symbol a that appears on the right hand side we
introduce a new variable Va . If Xi = a we replace Xi by Va . After this is done for all terminals,
the right hand side of the production contains only variables. For example, the production
S cAbbS
will be replace by the production
S Vc AVb Vb S
where Vb and Vc are new variables.
Then we add new terminal productions
Va a
61
for all terminals a. The only word derivable from Va is a. Note that these productions are in the
Chomsky normal form.
Clearly the new grammar is equivalent to the original one: Every application of the original
production
S cAbbS
is replaced by a sequence of derivation steps that first uses production
S Vc AVb Vb S
and then applies productions
Vc c and Vb b
to replace all occurrences of variables Vc and Vb by terminals c and b.
So far we have constructed an equivalent grammar whose productions are of forms
A B1 B2 . . . Bn , and
A a
where n 2, and A, B1 , B2 , . . . , Bn are variables, and a denotes a terminal symbol.
The only productions that are not in CNF are
A B1 B2 . . . Bn
where n 3. For each such production we introduce n 2 new variables D1 , D2 , . . . , Dn2 , and
replace the production by the productions
A B1 D1
D1 B2 D2
D2 B3 D3
...
Dn3 Bn2 Dn2
Dn2 Bn1 Bn
(Note that the new variables Di have to be different for different productions.) One application
of the original production gives the same result as applying the new productions sequentially, one
after the other. The new grammar is clearly equivalent to the original one.
Example 70. Let us find a Chomsky normal form grammar that is equivalent to
S SSaA | bc | c
A AAA | b
Another normal form is so-called Greibach normal form (or GNF). A grammar is in Greibach
normal form if all productions are of the form
A aB1 B2 . . . Bn
where n 0 and A, B1 , B2 , . . . , Bn are variables, and a is a terminal symbol. In other words, all
productions contain exactly one terminal symbol and it is the first symbol on the right hand side
of the production. Without proof we state the following:
62
3.4
Pushdown automata
A pushdown automaton (PDA) is a non-deterministic finite state automaton that has access to an
infinite memory, organized as a stack. Well see that the family of languages recognized by PDA is
exactly the family of context-free languages. A PDA consists of the following:
Stack. The stack is a string of symbols. The PDA has access only to the leftmost symbol of
the stack. This is called the top of the stack. During one move of the PDA, the leftmost symbol
may be removed (popped from the stack) and new symbols may be added (pushed) on
the top of the stack.
Input tape. Similar to finite automata: the input string is normally scanned one symbol at
a time. But also -moves are possible.
Finite state control unit. The control unit is a non-deterministic finite state machine.
Transitions may depend on the next input symbol and the topmost stack symbol.
There are two types of moves: normal moves, and -moves.
1. Normal moves: Depending on
(a) the current state of the control unit,
(b) the next input letter, and
(c) the topmost symbol on the stack
the PDA may
(A) change the state,
(B) pop the topmost element from the stack,
(C) push new symbols to the stack, and
(D) move to the next input symbol.
2. Spontaneous -moves dont have (b) and (D), i.e. they are done without using the input tape.
Example 72. As a first example, let us consider the following PDA that recognizes the language
L = {an bn | n 1}. The PDA has two states, Sa and Sb , and the stack alphabet contains two
symbols, A and Z0 . In the beginning, the machine is in the initial state Sa , and the stack contains
only one symbol Z0 , called the start symbol of the stack. Possible moves are summarized in the
following table:
63
State
Top of
stack
Sa
Z0
Sa
Sb
Sb
InputS
a
Add one A
to the stack,
stay in state Sa
Add one A
to the stack,
stay in state Sa
Z0
ymbol
b
Remove one A
from the stack,
go to state Sb
Remove one A
from the stack,
stay in state Sb
Remove Z0
from the stack,
stay in state Sb
An input word is accepted if the PDA can reach the empty stack after reading all input symbols.
An instantaneous description (ID) records the configuration of a PDA at any given time. It is
a triple
(q, w, )
where
q is the state of the PDA,
w is the remaining input, i.e. the suffix of the original input that has not been used yet,
and
is the content of the stack.
The ID contains all relevant information that is needed in subsequent steps of the computation.
We denote
(q1 , w1 , 1 ) ` (q2 , w2 , 2 )
if there exists a move that takes the first ID into the second ID, and we denote
(q1 , w1 , 1 ) ` (q2 , w2 , 2 )
if there is a sequence of moves (possibly empty) from the first ID to the second ID. Finally, we
denote
(q1 , w1 , 1 ) `n (q2 , w2 , 2 )
if the first ID becomes the second ID in exactly n moves.
For example, when accepting input string aaabbb our sample PDA enters the following IDs:
(Sa , aaabbb, Z0 ) ` (Sa , aabbb, AZ0 ) ` (Sa , abbb, AAZ0 ) ` (Sa , bbb, AAAZ0 ) ` (Sb , bb, AAZ0 ) `
` (Sb , b, AZ0 ) ` (Sb , , Z0 ) ` (Sb , , )
64
The word is accepted iff all input letters were consumed, and in the end the stack was empty. This
is referred to as acceptance by empty stack.
There is another way of defining which words are accepted. Some states may be called final
states, and a word is accepted iff after reading all input letters the PDA is in an accepting state.
This is called acceptance by final state. We see later that the two possible modes of acceptance are
equivalent: If there is a PDA that recognizes language L using empty stack then there (effectively)
exists another PDA that recognizes L using final states, and vice versa.
Formally, a pushdown automaton M consists of
(Q, , , , q0 , Z0 , F )
where
Q is a finite state set,
is the finite input alphabet,
is the finite stack alphabet,
q0 Q is the initial state,
Z0 is the start symbol of the stack,
F Q is the set of final states,
is the transition function from
Q ( {})
to finite subsets of
Q .
Note that (q, a, Z) is a set of possible outcomes; PDA are non-deterministic.
1. The interpretation of executing a transition
(p, ) (q, a, Z)
(where p, q Q, a , Z , ) is that in state q, reading input letter a, and Z on the
top of the stack, the PDA goes to state p, moves to the next input symbol, and replaces Z
by on top of the stack. The leftmost symbol of will be the new top of the stack (if 6= ).
In terms of IDs, this transition means that the move
(q, aw, Z) ` (p, w, )
is allowed for all w and .
65
(Sa , a, Z0 )
(Sa , a, A)
(Sa , b, A)
(Sb , b, A)
(Sb , , Z0 )
=
=
=
=
=
{(Sa , AZ0 )}
{(Sa , AA)}
{(Sb , )}
{(Sb , )}
{(Sb , )}
and all other sets (q, a, Z) are empty. It does not matter which set we choose as the set of final
states, since we use acceptance by empty stack. (Choose, for example F = .) We have
N (M ) = {an bn | n 1}.
66
Example 74. Let us construct a PDA M such that
N (M ) = {w | w {a, b} and w is a palindrome }.
The PDA will read symbols from the input and push them into the stack. At some point it guesses
that it is in the middle of the input word, and starts popping letters from the stack and comparing
them against the following input letters. If all letters match, and the stack and the input string
become empty at the same time, the word was a palindrome. We define,
M = ({q1 , q2 }, {a, b}, {Z0 , A, B}, , q1 , Z0 , )
where is
(q1 , , Z0 )
(q1 , a, Z0 )
(q1 , b, Z0 )
(q1 , a, A)
(q1 , b, A)
(q1 , a, B)
(q1 , b, B)
(q2 , b, B)
(q2 , a, A)
(q2 , , Z0 )
=
=
=
=
=
=
=
=
=
=
{(q1 , )}
{(q1 , AZ0 ), (q2 , AZ0 ), (q2 , Z0 )}
{(q1 , BZ0 ), (q2 , BZ0 ), (q2 , Z0 )}
{(q1 , AA), (q2 , AA), (q2 , A)}
{(q1 , BA), (q2 , BA), (q2 , A)}
{(q1 , AB), (q2 , AB), (q2 , B)}
{(q1 , BB), (q2 , BB), (q2 , B)}
{(q2 , )}
{(q2 , )}
{(q2 , )}
State q1 is used in the first half of the input word, state q2 in the second half. The three possible
outcomes of some transitions have the following roles in accepting computations:
use the first transition, if the input letter is before the end of the first half of the input word.
use the second transition, if the input word has even length and the current input letter is
the last letter of the first half, and
use the third transition, if the input word has odd length and the current input letter is
exactly in the middle of the input word.
For example, the word bab is accepted because
(q1 , bab, Z0 ) ` (q1 , ab, BZ0 ) ` (q2 , b, BZ0 ) ` (q2 , , Z0 ) ` (q2 , , ),
and the word abba is accepted, as shown by
(q1 , abba, Z0 ) ` (q1 , bba, AZ0 ) ` (q2 , ba, BAZ0 ) ` (q2 , a, AZ0 ) ` (q2 , , Z0 ) ` (q2 , , )
A PDA is called deterministic if every ID has at most one possible move. This means that
if (q, , Z) is non-empty then (q, a, Z) is empty for every input letter a, and
67
Theorem 76 If L = N (M ) for some PDA M then there (effectively) exists a PDA M 0 such that
L = L(M 0 ).
Proof. Now M recognizes L by empty stack. We construct a PDA M 0 that simulates M and detects
when the stack is empty. When that happens the PDA enters a final state. In order to be able to
detect the empty stack, we again introduce a new bottom of the stack -symbol X0 . As soon as
X0 is revealed the new PDA enters a new final state qf .
More precisely, Let
M = (Q, , , , q0 , Z0 , ).
We construct
M 0 = (Q {q00 , qf }, , {X0 }, 0 , q00 , X0 , {qf })
where 0 contains all transitions of plus the following transitions:
0 (q00 , , X0 ) = {(q0 , Z0 X0 )},
0 (q, , X0 ) = {(qf , )}, for all states q Q.
If in the original PDA M
(q0 , w, Z0 ) ` (q, , )
then in M 0 we have the accepting computation
(q00 , w, X0 ) ` (q0 , w, Z0 X0 ) ` (q, , X0 ) ` (qf , , )
69
Example 77. Let us modify our first PDA so that it recognizes the language
L = {an bn | n 1}
using final state instead of the empty stack.
Next we prove that PDA recognize all context-free languages. In fact, all context-free languages
are recognized by PDA having only one state. We prove this by showing how any context-free
grammar can be simulated using only the stack.
Consider for example the grammar G = (V, T, P, S) where V = {S}, T = {a, b} and P contains
productions
S aSbS | bSaS | .
Let us construct a PDA M that recognizes L(G) using empty stack. The stack symbols of M are
the terminals and variables of G. At all times, the content of the stack is a suffix of a sentential
form by G. Initially the stack contains the start symbol of the grammar.
If the topmost symbol of the stack is a terminal symbol it is compared against next input
letter. If they are identical, the symbol is popped from the stack and the machine moves to
the next input letter. If they are different the simulation halts.
If the topmost symbol of the stack is a variable the machine rewrites it by a righthand side
of a production using an -move.
In our example, we get the machine
M = ({q}, {a, b}, {a, b, S}, , q, S, )
with defined as follows: Transitions for terminals on the top of the stack are
(q, a, a)
(q, a, b)
(q, b, b)
(q, b, a)
=
=
=
=
{(q, )},
,
{(q, )},
,
and the transitions for the variable S on top of the stack are
(q, , S) = {(q, aSbS), (q, bSaS), (q, )}.
There are no other transitions. The machine simulates leftmost derivations of G on the stack.
Terminals that are generated on the left end have to match the input, while a variable at the left
extreme is rewritten in the stack using a production of the grammar.
70
2. If
S w
by a leftmost derivation, where w is terminal word and starts with a variable or is empty,
then
(q, wu, S) ` (q, u, )
in M , for any word u.
This can be proved using mathematical induction on the number of derivation steps, using
the fact that a leftmost derivation step in G can be simulated by the PDA M by applying
the production to the variable that is on top of the stack. Using the terminal matching
transitions, one can then move all terminals that appear on top of the stack to the consumed
input, until the top of the stack is again a variable (or empty, in the end of an accepting
computation).
This proves that L(G) N (M ): If w L(G) then
S w
which means that in M (we have = )
(q, w, S) ` (q, , ).
Next we prove that PDA recognize only context-free languages: For a given PDA M we construct
an equivalent context-free grammar G.
Theorem 80 If L = N (M ) for some PDA M then there (effectively) exists a context-free grammar
G such that L = L(G).
This construction is made complicated by the fact that the PDA may have more than one state.
Let us divide the proof into the following two steps:
Lemma 81 If L = N (M1 ) for some PDA M1 then there effectively exists a PDA M2 that has only
one state such that L = N (M2 ).
72
Lemma 82 If L = N (M2 ) for some PDA M2 with one state, then there effectively exists a contextfree grammar G such that L = L(G).
Proof of Lemma 81. Let L = N (M1 ) for PDA
M1 = (Q, , , , q0 , Z0 , ).
Let us construct a PDA M2 with only one state that recognizes the same language. The computations of M1 will be simulated in such a way that the state of M1 will be stored in the topmost
element of the stack in M2 . So at all times the topmost element of the stack contains both the
stack symbol of M1 and the current state of M1 .
Then every move of M1 can be simulated by M2 since it knows the state and the topmost stack
symbol of M1 . After each step, the next state is written into the new topmost element.
This is fine as long as at least one new symbol is written into the stack. However, there is one
big problem: Where is the next state stored when the stack is only popped and nothing new is
written ? The new top of the stack is the symbol that used to be the second highest on the stack,
and it is not accessible for writing. The new state must have been stored in the stack already
when the second highest symbol was pushed into the stack and this was possibly long before it
becomes the topmost symbol.
How do we know long before what is going to be the state of M1 when a particular stack symbol
is revealed to the top of the stack ? Answer: we guess it using non-determinism of M2 . At the
time when the symbol becomes the topmost element of the stack we only need to verify that the
earlier guess was correct.
In order to be able to verify the guess it has to be stored also on the stack symbol above.
Therefore stack symbols of M2 need to contain two states of M1 : one indicates the state of the
machine when the symbol is the topmost element of the stack, and the other state indicates the
state of M1 when the the element below is the topmost element of the stack.
Therefore the stack alphabet of M2 is
2 = (Q Q) {X0 }
where X0 is a new bottom of the stack symbol. Symbol [q, Z, p] on top of the stack indicates that
that M1 is in state q with Z is on top of the stack and p will be the state of M1 when the element
below becomes the topmost symbol of the stack. PDA M2 only has one state # which is completely
irrelevant to computations.
At all times the stack is kept consistent in the sense that if [q, Z, p] is immediately above [q 0 , Z 0 , p0 ]
in the stack then p = q 0 . We call this the consistency requirement of the stack. Consistency means
that the content of the stack will always look like
[q1 , Z1 , q2 ][q2 , Z2 , q3 ] . . . [qm , Zm , qm+1 ],
for some qi Q and Zi . The values q2 , q3 , . . . , qm+1 are the non-deterministic guesses done
by M2 about the state of M1 when the corresponding stack elements will get exposed on top of the
stack. As the stack shrinks the correctness of the guesses gets verified.
The construction is done in such a way that M2 can reach ID
(#, u, [q1 , Z1 , q2 ][q2 , Z2 , q3 ] . . . [qm , Zm , qm+1 ])
73
respectively.
3. For M1 s transitions
(p, X1 X2 . . . Xk ) (q, a, Z)
where k 1, we add, for every q1 Q, the transitions
(#, [p, X1 , p2 ][p2 , X2 , p3 ] . . . [pk , Xk , q1 ]) 2 (#, a, [q, Z, q1 ])
to M2 , for all combinations of
p2 , p3 , . . . , pk Q.
74
Interpretation: If M1 goes from state q to p and changes Z on top of the stack into X1 X2 . . . Xk ,
then M2 can replace the topmost element of the stack by the corresponding stack elements.
The states p2 , p3 , . . . , pk inserted in the stack are non-deterministic guesses, so we must have a
transition for all choices of them. The bottom most state inserted is q1 , which was the second
state component of the stack element [q, Z, q1 ] removed. We know it matches the state stored
in the stack element below. And the topmost state is the new state p of the PDA M1 .
The corresponding computation steps by M1 and M2 are
(q, au, ZZ1 Z2 . . . Zm ) ` (p, u, X1 X2 . . . Xk Z1 Z2 . . . Zm )
and
(#, au, [q, Z, q1 ][q1 , Z1 , q2 ][q2 , Z2 , q3 ] . . . [qm , Zm , qm+1 ])
` (#, u, [p, X1 , p2 ][p2 , X2 , p3 ] . . . [pk , Xk , q1 ] [q1 , Z1 , q2 ][q2 , Z2 , q3 ] . . . [qm , Zm , qm+1 ]),
respectively.
We have now constructed all transitions in 2 of M2 . The PDA M2 simulates M1 step-by-step. The
transitions are such that at all times the stack of M2 is consistent: the states on the consecutive
stack elements match. An ID can be reached in M2 if and only if the corresponding ID can be
reached in M1 . Therefore M2 empties the stack exactly when M1 empties the stack, i.e. they
recognize the same languages.
Example 83. Let us construct a one state PDA that is equivalent to the PDA
M = ({Sa , Sb }, {a, b}, {Z0 , A}, , Sa , Z0 , )
where
(Sa , a, Z0 )
(Sa , a, A)
(Sa , b, A)
(Sb , b, A)
(Sb , , Z0 )
=
=
=
=
=
{(Sa , AZ0 )}
{(Sa , AA)}
{(Sb , )}
{(Sb , )}
{(Sb , )}
The variables of G are the stack symbols of M . Let us assume that the stack alphabet and
the input alphabet are disjoint. (If they are not we can simply rename stack symbols.) We have
V = ,
S = Z0 .
For every transition
(#, Z1 Z2 . . . Zm ) (#, a, Z)
where a {} we introduce a production
Z aZ1 Z2 . . . Zm
in P . All sentential forms derived by leftmost derivations are then of the special form
w,
where w and . In the corresponding ID of M the string is the content of the stack
and w input that has been consumed so far.
The next leftmost derivation step applies a production on the leftmost symbol of . This
corresponds to one move by PDA M : The corresponding transition changes the topmost symbol
of the stack and possibly consumes one more input letter. The derivation step
wZ waZ1 Z2 . . . Zm
hence corresponds to the move
(#, au, Z) ` (#, u, Z1 Z2 . . . Zm ).
Using mathematical induction on the number of derivation steps, one easily proves that = w is
a sentential form derivable by a leftmost derivation in G if and only if is a possible stack content
after consuming w from the input. If is terminal, it means that the corresponding calculation in
M has reached the empty stack, and = w (=the consumed input) was accepted by M .
The computation
(#, abba, Z) ` (#, abba, AZZA) ` (#, bba, ZZA)
` (#, bba, BZA) ` (#, ba, ZA)
` (#, ba, BA) ` (#, a, A) ` (#, , )
of M has the corresponding derivation
Z AZZA aZZA aBZA abZA abBA abbA abba
in G.
We have proved the equivalence of three devices: context-free grammars and PDA using empty stack
and final states acceptance modes. The constructions were all effective which means that when
investigating closure properties of context-free languages or decision algorithms we may assume
that the input is given in any of the three forms.
3.5
Not all languages are context-free. Our first technique for proving that some language is not
context-free is a pumping lemma. Just as in the case of regular languages, pumping lemma states
a property that every context-free language satisfies. If a language does not satisfy the pumping
lemma then the language is not context-free.
Theorem 85 (Pumping lemma for CFL) Let L be a context-free language. Then there exists
a positive number n such that every word z of length n or greater that belongs to language L can
be divided into five segments
z = uvwxy
in such a way that
|vwx| n, and
v 6= or x 6= ,
(6)
This division satisfies (6) since vx = ab 6= and |vwx| = |ab| 2. Subwords v and x can be
pumped arbitrarily many times: for every i 0
uv i wxi y = am1 ai bi bm1 = am+i1 bm+i1
is in language L.
77
Let us analyze the reason why our L satisfies the pumping lemma: Language L is generated by
a grammar with two productions
S aSb | .
Word am bm , m 1, has the derivation
S = am1 Sbm1 = am1 aSbbm1 = am1 abbm1
corresponding to the derivation tree
S
a m-1
b m-1
a S b
S = am1 Sbm1 ,
S = aSb,
S = .
The middle subderivation S = aSb derives from the variable S the variable itself. It means that
the subderivation can be iterated arbitrarily many times. Repeating it i times gives
S = am1 Sbm1 =i am1 ai Sbi bm1 = am1 ai bi bm1
corresponding to the derivation tree
S
a m-1
b m-1
a S b
a S b
i times
S
a S b
In other words, pumping i times the words v = a and x = b corresponds to iterating the middle
subderivaton i times.
Let us prove that the argument above can be repeated for any context-free language.
Proof of the pumping lemma. The proof is based on the fact that during derivations of
sufficiently long words, some variable derives a sentential form containing the variable itself. In
other words, we have derivations
S uAy
A vAx
A w
78
for some terminal words u, v, w, x, y and variable A. But the derivation A vAx may be repeated
arbitrarily many times, say i times:
S uAy uvAxy uvvAxxy . . . uv i Axi y uv i wxi y
This shows that uv i wxi y is in language L for every i 0.
To make a precise proof including the bounds for the lengths of the subwords, we have to be more
careful: Let G be a grammar in the Chomsky normal form such that L = L(G) (or L = L(G) {}
if L). Let us define the depth of a derivation tree as the length of the longest path from its
root to any leaf. We use the following simple lemma:
Lemma 86 If a derivation tree by a CNF grammar for word w has depth d then |w| 2d1 .
Proof. We use mathematical induction on d:
1 (basis) If d = 1 then the tree must be the trivial one:
A
a
and the word it derives has length 2d1 .
2 (inductive step) Let d > 1. The tree is
A
AA
T1 A
| {z }
AA
T2 A
| {z }
and w = uv. Since the depths of trees T1 and T2 are at most d 1, it follows from the
inductive hypothesis that |u| 2d2 and |v| 2d2 . Therefore
|w| = |u| + |v| 2d2 + 2d2 = 2d1 .
Let k be the number of variables in G, a CNF grammar for language L. Let us choose the
number n in the pumping lemma as n = 2k . Let z L be such that |z| n, and let T be a
derivation tree for z.
The depth of T has to be at least k + 1, since we just proved that a tree of smaller depth can
yield only shorter words. Pick one maximum length path from the root to a leaf in T . Reading
from the leaf up, let
a, V1 , V2 , V3 , . . . , Vk+1
be the first k + 2 labels along path :
79
S
Vk+1
Vk
V2
V
1
a
z
Since the grammar has only k variables, it follows from the pigeon hole principle that two of
the variables V1 , V2 , . . ., Vk+1 must be identical, say
Vs = Vt = V, for s < t.
So the derivation tree for z looks like:
S
v Vx
w
S uV y
V vV x
V w.
This means there are derivations for words uv i wxi y for all i 0:
S uV y uvV xy uv 2 V x2 y . . . . . . uv i V xi y uv i wxi y
Clearly vx 6= : Since the grammar G is in CNF, node Vs must have a sibling, and the yield of the
sibling is part of either v or x.
To complete the proof we have to show that the length of the word vwx is at most n = 2k : But
it is the yield of the subtree rooted at Vt , and the depth of that subtree is at most k + 1. (If there
would be longer path from Vt to a leaf, then that path combined with the path from the root S to
Vt would be longer than the longest path , a contradiction.) We know how long can the yields
of trees of depth k + 1 be: they can have at most 2k letters. This completes the proof of the
pumping lemma.
We use the pumping lemma to prove that certain languages are not context-free. It works the
same way as the pumping lemma for regular languages. To show that a language is not a CFL we
show that it does not satisfy the pumping lemma. So we are more interested in the negation of the
pumping lemma.
80
Example 88. Another example:
n
o
L = am bk cm dk | m, k 1 .
3.6
Recall: We say that the family of context-free languages is closed under language operation Op if
L1 , L2 , . . . are CFLs = Op(L1 , L2 , . . .) is a CFL,
that is, if operation Op is applied to context-free languages, the result is also context-free. We
say that the closure is effective if there is a mechanical procedure (=algorithm) that constructs
the result Op(L1 , L2 , . . .) for any context-free input languages L1 , L2 , . . .. Inputs and outputs are
given in the form of PDA or context-free grammar it does not matter which format is used since
both devices can be algorithmically converted into each other.
Theorem 89 The family of CFL is effectively closed under the following operations:
Union (if L1 , L2 are CFL, so is L1 L2 ),
Concatenation (if L1 , L2 are CFL, so is L1 L2 ),
Kleene closure (if L is CFL, so is L ),
Substitutions with CFL (if L is CFL, and f is a substitution such that for every letter a
the language f (a) is CFL, then f (L) is CFL),
Homomorphisms (if L is CFL, and h is a homomorphism then h(L) is CFL),
Inverse homomorphisms (if L is CFL, and h is a homomorphism then h1 (L) is CFL),
Intersections with regular languages (if L is CFL and R is a regular language, then L R is
CFL).
The family of CFL is not closed under the following operations:
Intersection (for some CFL L1 and L2 the language L1 L2 is not a CFL),
Complementation (for some CFL L the language L is not a CFL),
Quotient with arbitrary languages (for some CFL L and language K the language L/K is
not a CFL).
82
Assume that all variable sets V and Va are disjoint. The following grammar generates f (L):
G0 = (V 0 , T 0 , P 0 , S),
where
V0 =V
!
Va
Ta
contains all terminals of grammars Ga . Productions P 0 include all productions that are in the
sets Pa and, in addition, for every production in P there is a production obtained by replacing all
occurrences of terminals a by the start symbol Sa of the corresponding grammar Ga .
Example 90. If L = L(G) is generated by the grammar
G = ({X}, {a, b}, P, X)
with productions
X aXb | bXa |
and if f is the substitution such that the language f (a) is generated by the grammar
Ga = ({Y }, {0, 1}, Pa , Y )
with
Y 0Y Y 0 | 1
and f (b) is the language generated by the grammar
Gb = ({Z}, {0, 1}, Pb , Z)
with
Z 0Z1Z | ,
then f (L) is generated by the grammar G0 = (V 0 , T 0 , P 0 , S 0 ) where
V 0 = {X, Y, Z},
T 0 = {0, 1},
S 0 = X,
and P 0 contains the productions
X Y XZ | ZXY | ,
Y 0Y Y 0 | 1,
Z 0Z1Z | .
84
4. Homomorphism. This follows from the previous proof because homomorphism is a special
type of substitution. We can also make a direct construction where we replace in the productions
all occurrences of terminal letters by their homomorphic images.
Example 91. If L is generated by the grammar
E
N
E + E | N
0N | 1N | 0 | 1
and the homomorphism h is given by h(+) = , h(0) = ab, h(1) = ba then h(L) is generated by
E
N
E E | N
abN | baN | ab | ba
5. Inverse homomorphism. In this case it is easier to use PDA instead of grammars. Let L be
recognized by the PDA
M = (Q, , , , q0 , Z0 , F )
using final states, and let
h :
be a homomorphism. Let us construct a PDA M 0 that recognizes the language h1 (L) using final
states, that is, it accepts all words w such that h(w) L.
On the input w, machine M 0 computes h(w) and simulates M with input h(w). If h(w) is
accepted by M then w is accepted by M 0 . The result of h on w is stored on a buffer, or virtual
input tape, inside the control unit. That is where the simulation of M reads its input from. h(w)
is computed from w letter-by-letter as needed: as soon as the virtual tape becomes empty the next
real input letter a is scanned, and h(a) is added on the virtual tape. Let
B = {u | a u is a suffix of h(a) }.
Set B consists of all possible contents of the virtual input tape. Set B is of course a finite set.
Let us construct the PDA
M 0 = (Q0 , , , 0 , S 0 , Z0 , F 0 ),
where
Q0 = Q B,
S 0 = [q0 , ], and
F 0 = F {}.
When the virtual tape becomes empty, we have a transition for reading the next real input
letter a, and loading its homomorphic image h(a) to the virtual tape: For all a , and
Z we have the transition
([q, h(a)], Z) 0 ([q, ], a, Z).
Initially the virtual tape is empty, so the initial state of M 0 is
[q0 , ].
The input word is accepted if in the end the virtual input tape is empty (M has consumed the
whole input word), and the state of M is a final state. Therefore the final states of M 0 are the
elements of
F {}.
Example 92. Consider the PDA
M = ({q}, {a, b}, {A}, , q, A, {q})
where
and let h be the homomorphism h(0) = b, h(1) = aa. Let us construct a PDA that recognizes
h1 (L(M )). Possible contents of the virtual tape are
B = {b, , aa, a}
so the state set of the new PDA will be
Q0 = {[q, b], [q, ], [q, aa], [q, a]}.
The initial state is
S 0 = [q, ],
and there is only one final state
F 0 = {[q, ]}.
From the original transition (q, AA) (q, a, A) we get two simulating transitions:
([q, a], AA) 0 ([q, aa], , A),
([q, ], AA) 0 ([q, a], , A),
and from (q, ) (q, b, A) one transition:
([q, ], ) 0 ([q, b], , A).
Finally, we have the following transitions for loading the virtual tape:
([q, aa], A) 0 ([q, ], 1, A),
([q, b], A) 0 ([q, ], 0, A).
86
Example 93. Let us show that the language
L = {an b2n c3n | n 1}
is not context-free. Define the homomorphism h(a) = a, h(b) = bb, h(c) = ccc. Then
h1 (L) = {an bn cn | n 1}
which we know is not context-free (Example 87). So L cannot be CFL either: if it were, then
h1 (L) would be CFL as well.
6. Not closed under Intersection. It is enough to find one counter example. Let
L1 = {an bn cm | n, m 1}
and
L2 = {am bn cn | n, m 1}.
Both L1 and L2 are context-free: L1 is the concatenation of context-free languages
{an bn | n 1} and c+ ,
and L2 is the concatenation of
(One can easily also construct grammars for both L1 and L2 .) But even though L1 and L2 are
context-free, their intersection
L1 L2 = {an bn cn | n 1}
is not. So the family of CFL is not closed under intersection.
7. Not closed under complementation. If they were, then the family of CFL would be closed
under intersection as well:
L1 L2 = L1 L2 ,
a contradiction with 6 above.
8. Not closed under quotient with arbitrary language. Homework.
9. Intersection with regular languages. Even though the intersection of two context-free
languages may be non-context-free, as proved in 6 above, the intersection of any context-free
language with a regular language is always context-free.
Let L be recognized by the PDA
M = (QM , , , M , qM , Z0 , FM )
using final states, and let R be recognized by the DFA
A = (QA , , A , qA , FA ).
87
Let us construct a PDA M 0 that runs both M and A in parallel. The state set of M 0 is
Q0 = QM QA .
The states are used to store the states of both M and A for the input that has been read so far.
All -transitions of M are simulated as such, without changing the QA -component of the state.
But whenever M reads next input letter, the QA -component is changed according to DFA A.
Clearly, if in the end of the input word both QM - and QA -components are final states, the word is
accepted by both M and A.
So we have the PDA
M 0 = (QM QA , , , 0 , [qM , qA ], Z0 , FM FA )
where 0 is the following: Each transition
(q 0 , ) M (q, a, Z)
by the original PDA, and each state p QA of the DFA provide the transition
([q 0 , A (p, a)], ) 0 ([q, p], a, Z)
into the new machine M 0 . The first state component and the stack follow the instruction of the
PDA M , while the the second state component simulates the DFA A: On input a, state p is changed
to A (p, a). Note that if a = then A (p, a) = p.
Example 94. Consider the PDA
M = ({q}, {a, b}, {A}, , q, A, {q})
where
=
=
=
=
We have covered all the operations stated in Theorem 89, so the proof of the theorem is complete.
Example 97. Let us show that family of context-free languages is closed under quotient with
regular languages. Let L be a context-free language, and let R be a regular language.
Let be the union of the alphabets of L and R, and let
0 = {a0 | a }
89
be a new alphabet obtained by marking symbols of . We may assume and 0 are disjoint. If
w is a word over then w0 will denote the corresponding word over 0 obtained by marking every
letter of w.
Consider the finite substitution s defined on alphabet as follows
s(a) = {a, a0 }
for every a . It follows from the closure properties that the language
L1 = s(L) (0 R)
is context-free.
Let us analyze L1 . It consists of all words w0 u where wu L and u R. If we delete nonmarked letters from L1 and remove marks, then we obtain words of the quotient L/R. So let h be
the homomorphism h : 0 defined by
h(a) =
h(a0 ) = a
if a , and
if a0 0 .
Then
L/R = h(L1 ),
which proves that L/R is context-free.
We proved in the example above that if L is context-free and R is regular then L/R is contextfree. We know (see the homework) that there is some (unrestricted) language K such that L/K
is not context-free. This raises the following natural question: If L and K are context-free is L/K
guaranteed to be context-free ? (Answer is no but can you come up with a counter example ?)
3.7
Decision algorithms
Now we give algorithms for deciding if a given CFL is empty, finite or infinite. Also a polynomial
time parsing algorithm for deciding if a given word belongs to a given CFL is presented.
Later on, well see that there are many questions concerning CFL that do not have algorithms.
For example, there do not exist any algorithms for deciding if a given CFG is ambiguous, if given
two CFGs are equivalent (i.e. define the same language), if a given CFG generates , if the
intersection of two given CFLs is empty, etc.
Note that a CFL can be represented in different ways: as a grammar, or as a PDA that
recognizes the language. It does not matter which representation we use since we have algorithms
for converting a CFG into equivalent PDA and vice versa.
Theorem 98 There are algorithms to determine if a given CFL is (a) empty, (b) finite, (c) infinite.
Proof. (a) In Section 3.3 we used a marking procedure to find all variables that derive a terminal
word. The language L(G) is non-empty if and only if the start symbol S of the grammar G gets
marked.
90
(b) and (c). To test wether a given grammar generates finitely or infinitely many words, we start
by simplifying the grammar, and converting it into Chomsky normal form. After this we only have
productions of types
A BC,
A a,
and we dont have useless symbols, i.e., symbols that do not derive terminal words and/or are not
reachable from the start symbol. (We may lose but it does not matter since L is finite if and only
if L {} is finite.)
So we can assume grammar G is in CNF, and all variables are useful. Well use the fact that
grammar G generates an infinite language if and only if some variable can derive a word containing
the variable itself. Let us call a variable A self-embedded if
A + A
for some words and . Since the grammar is in CNF, and are not both empty words. Let
us prove that L(G) is infinite if and only if G contains a self-embedded variable:
(1) Assume A + A. Then (as in the pumping lemma)
A + i A i
for every i 1. Since the grammar does not contain useless symbols every word of terminals
and variables derives some terminal word. If x, y, w are terminal words derivable from ,
and A, respectively, then
A + i A i xi wy i
for every i 1. So A derives infinitely many words (because x or y is non-empty), and
L(G) must be infinite. (G does not contain useless symbols, so A is reachable from the start
symbol.)
(2) Assume that the language L(G) is infinite. Then it contains a word w which is longer than
the number n in the pumping lemma. As in the proof of the pumping lemma we know that
the derivation of w contains a self-embedded variable. So G has at least one self-embedded
variable.
To determine if L(G) is infinite we have to find out whether any variable is self-embedded. We can
use a marking procedure to determine if variable A is self-embedded:
Self-embedded(G, A)
1.
2.
3.
4.
5.
6.
7.
8.
9.
Next we turn to the parsing problem.
Theorem 99 There is an algorithm to determine whether a given word w is in a given CFL L.
Proof. If w = then we simply check whether the start symbol S is nullable in the grammar for
L, as done in Section 3.3. Assume then that |w| 1. Let G be a grammar in the Chomsky normal
form for language L, or L \ {}. Grammar G can be effectively constructed.
In the following we describe (an inefficient) recursive algorithm to test whether a given variable
A derives a given non-empty word w. The recursion is based on the fact that A w if and only
if either
|w| = 1 and A w is a production in G, or
|w| 2 and there exists a production A BC in the grammar, and a division w = uv of w
into two non-empty words u and v, such that variables B and C derive u and v, respectively.
Derives(G, A, a1 a2 . . . an )
% Does A a1 a2 . . . an (where n 1) in the given CNF grammar G?
1. If n = 1 then if A a1 in G then return TRUE else return FALSE
2. For i 2 to n do
3.
For all productions A BC in G do
4.
If Derives(G, B, a1 . . . ai1 ) and Derives(G, C, ai . . . an ) then return TRUE
5. return FALSE
The call Derives(G, S, w) with the start symbol S returns TRUE if and only if w is in L. The
algorithm clearly always halts and returns the correct answer.
The algorithm presented in the proof is very inefficient: it may require an amount of time which
is exponential with respect to the length of w. In this course we are usually only interested in the
existence of algorithms not so much their efficiency but since parsing context-free grammars
is an important problem with applications in compiler design, let us discuss a way to make the
algorithm faster.
The standard technique of dynamic programming can be used to convert the exponential
time recursive algorithm above into an iterative polynomial time algorithm. The idea is based on
the observation that during the recursive computation of Derives(G, S, w) the same recursive calls
get repeated over-and-over again, with the same exact call parameters. So rather than recomputing
the results of the recursive calls over-and-over again, let us compute them only once and memorize
the result in an array. For this purpose we introduce for each variable A of the CNF grammar an
n n Boolean matrix Ann , where n is the length of the word w = a1 a2 . . . an we are parsing. The
element Aij of the matrix will store value TRUE iff variable A derives the subword ai ai+1 . . . ai+j1
of length j that begins in position i, for all j = 1, 2, . . . , n and all i = 1, 2, . . . , n j + 1. The matrix
can be filled up incrementally for larger and larger values of j:
1 (j = 1) We set Ai1 to TRUE if and only if A ai is a production in the grammar, and
92
2 (j > 1) We set Aij to TRUE if and only if for some k < j there is a production A BC
such that the entries Bik and Ci+k,jk are TRUE, indicating that variables B and C derive
the subwords of length k and j k starting in positions i and i + k, respectively.
The resulting algorithm (known as the CYK-algorithm for three independent inventors Cocke,
Younger and Kasami) runs in time O(n3 ).
CYK(G, a1 a2 . . . an )
% G is in the Chomsky normal form and n 1
1. For every variable A and all i, j n initialize Ai,j FALSE
2. For i 1 to n do
3.
For all productions A ai do
4.
set Ai,1 TRUE
5. For j 2 to n do
6.
For i 1 to n j + 1 do
7.
For k 1 to j 1 do
8.
For all productions A BC do
9.
If Bi,k and Ci+k,jk are TRUE then set Ai,j TRUE
10.Return S1,n .
The following illustration shows which entries of the matrices for B and C are compared when
determining Aij on line 9 of the algorithm:
j
B B B B i,j
C
C
C
C
If the input word is generated by the grammar, we usually want to parse it: we want to find a
derivation of the word in the grammar. This can be established in the CYK algorithm simply by
recording in each TRUE matrix element also the reason why the element was set TRUE, that is, which
two TRUE matrix entries were used on line 9. In the end, if the entry S1n is TRUE, we can parse the
word by following these pointers till the single letters.
Example 100. Consider the CNF grammar G = ({S, A, B, C}, {a, b}, P, S) where P contains the
productions
S AB | BC
A BA | a
B CC | b
C AB | a
Let us use the CYK algorithm to find out that the word w = baaba is in the language L(G). The
parsing discovered by the algorithm is summarized in the derivation tree
93
S15
A12
B33
B11
A21
C32
C51
A31 B41
In this last part of the course we want to show that certain decision problems can not be
algorithmically solved. By a decision problem we mean a computational question with a well
defined yes/no-answer. For example, questions like
Does a given regular expression define an infinite language ?,
Is a given context-free grammar ambiguous ?, or
Does a given quadratic equation have a real number solution ?
are decision problems. A decision problem has an input parameter (identified by the expression
given. . . in the examples above), and values of the parameter are called instances of the
problem. An instance is positive or negative if the answer to the decision problem is yes or
no, respectively. For example, a b and ab + ba are a positive and a negative instance of the first
problem above.
We say that a decision problem is decidable if there exists an algorithm that, when given an
instance as input, returns the correct yes/no-answer. The algorithm has to work for all instances
correctly. The computation may take any finite amount of time, but for every instance the computation halts after some time and gives the correct answer. A problem is called undecidable
if no such algorithm exists. Well see that many important and natural decision problems are
undecidable.
Note that it does not make sense to talk about decision problems that have only one instance:
such problems are of course decidable since there is a trivial algorithm that simply writes out yes
or no we just may not know which of the two algorithms is the correct one. For this reason
questions like Is P = N P ? or Is the Riemann hypothesis true ? are trivial to us: they dont
have different input instances. For the same reason, decision problems with only finitely many
different instances are decidable since the correct yes/no -answers can be summarized in a finite
table. So only problems with infinitely many different instances are considered.
To connect decision problems to formal languages, we encode instances of problems as words
over some alphabet. This is reasonable: when typing an instance into a computer in order to run
an algorithm we are encoding it, for example, as a word over the binary alphabet {0, 1}. It is up to
us to determine which encoding we use: As long as two encodings can be effectively converted into
94
each other, the problem cannot be decidable in one presentation and undecidable in the other one.
It is also reasonable to require that the encoding scheme is such that one can effectively recognize
those words that are not encodings of any input instance. In the following, we denote by hIi the
word that is the encoding of instance I.
We associate to a decision problem P a language LP that contains the encodings of all positive
instances. (This language, naturally, depends also on the encoding scheme we choose to use.) Then
the decision problem P becomes the problem of determining if a given word w is in the language
LP or not. We define the membership problem of language L as the following decision problem:
Membership in L
Instance: Word w
Problem: Is w L ?
Language L whose membership problem is decidable is called recursive. As we have not yet
precisely defined the concept of an algorithm (and hence that of decidability), this definition is not
yet mathematically stated here. It is an informal description of recursive languages that is useful
to keep in mind: if one can design an algorithm (e.g. a computer program) for determining if any
given word w is in the language L then L is recursive. In the following sections we provide a precise
definition of recursive languages in terms of Turing machines.
A semi-algorithm for a decision problem is an algorithm like process that correctly returns
value yes for positive instances, but on negative instances the semi-algorithm either returns answer no or it does not return any value, i.e. it runs for ever without ever halting. Notice that
there is a non-symmetry between the positive and negative instances: A semi-algorithm for positive
instances does not imply that there would be also a semi-algorithm for the negative instances. The
decision problem obtained by swapping the negative and positive instances is called the complement of the original problem. A decision problem is called semi-decidable if its positive instances
have a semi-algorithm.
A language L is recursively enumerable (or simply r.e.) if its membership problem is
semi-decidable. So the decidability (semi-decidability) of decision problem P corresponds to the
recursiveness (recursive enumerability, respectively) of the corresponding language LP of positive
instances. For this reason, recursive languages are sometimes called decidable languages and r.e.
languages are sometimes called semi-decidable languages.
Based on this informal definition we can observe the following apparent facts:
(i) If a problem is decidable then also the complement problem is decidable. (That is, the family
of recursive languages is closed under complementation.)
(ii) Every decidable problem is also semi-decidable. (All recursive languages are also recursively
enumerable.)
(iii) If a problem and its complement are both semi-decidable then the problem is decidable. (A
language L is recursive if and only if both L and its complement are r.e.)
For observation (iii) note that the two semi-algorithms for the problem and its complement can be
executed concurrently until one of them returns the answer.
It is not very surprising that there are languages that are not recursive or even recursively
enumerable. (We can reason as follows: The number of different r.e. languages is countably
95
infinite. This follows from the fact that the number of different algorithms is countably infinite.
But the number of different languages is uncountably infinite, i.e. there exist more languages than
there are r.e. languages.) What is more surprising is that we can actually find individual languages
that correspond to very natural decision problems and are non-recursive.
4.1
Turing machines
In order to precisely define recursive and recursively enumerable languages we must have a definition
of an algorithm. We do this using Turing machines: We say a language is r.e. iff it is recognized by
a Turing machine, and it is recursive iff it is recognized by a Turing machine that halts on every
input.
At first, Turing machines may seem like very restricted models of computers, but they are in fact
as powerful as any other model people have come up with so far. For example, well see that Turing
machines can simulate register machines (a model closer to the modern computer architecture).
One cannot, of course, prove that nobody ever invents a totally different, more powerful computer architecture. But all evidence indicates that this is not possible, as long as we require
that the computer executes mechanically a finitely describable program on discrete steps. The
Church-Turing thesis states that every effectively computable function can be computed by a
Turing machine. This is an unprovable statement since the concept of effectively computable has
no precise definition the thesis asserts that Turing machines are a proper meaning of effective
computability.
A Turing machine (TM) has a finite state control similar to PDA. Instead of an input tape
and a stack, the machine has only one tape that is both the input tape and the work tape. The
machine has a read/write head that may move left or right on the tape. Initially, the input is
written on the tape. At each time step the machine reads the tape symbol under its read/write
head, and depending on the tape symbol and the state of the control unit, the machine may
1. Change its state,
2. Replace the symbol on the tape under the read/write head by another tape symbol, and
3. move the R/W head left or right on the tape.
The tape is infinite to the left and to the right.
Tape
B B a1 a2
an B B
R/W head
q
Control unit
Initially the input word is written on the tape in such a way that the read-write head scans the
first letter of the input. All other tape locations contain a special blank symbol B. The input is
accepted if and only if the machine eventually enters the final state.
Formally, a (deterministic) Turing machine is 7-tuple
M = (Q, , , , q0 , B, f )
where
96
a
q
97
BBB
So the first (last) letter of u (or v, respectively) is not B, because the leading and trailing Bs
are trimmed away from u and v, respectively. Note also that the current state q can be uniquely
identified from the word u q a v because we required alphabets Q and to be disjoint.
For example, the ID xyxqxBByy (where q Q) represents the situation
BBB x
x B B y
y BBB
y B B B
BBB
and qB represents
BBB B
BBB
On the other hand, BqB and xyBqBxBB are not valid IDs since the surrounding Bs have not
been removed. Precisely speaking, IDs are words of the set
({} ( \ {B}) ) Q ( ( \ {B}) {}).
Let us define moves of the Turing machine. Let the current ID be
= u q a v.
If is the next ID we denote ` and say that results from by one move. More precisely:
1. Assume that
(q, a) = (p, b, L).
If u = then the next ID is = pBbv, except that possible trailing Bs are removed
from bv.
If u = u0 c for some c then the next ID is = u0 pcbv, except that possible trailing
Bs are removed from bv.
2. Assume that
(q, a) = (p, b, R).
If v = then the next ID is = ubpB except that possible leading Bs are removed
from ub.
If v 6= then the next ID is u = ubpv, except that possible leading Bs are removed from
ub.
98
3. If (q, a) is undefined then no move from is possible, and is a halting ID. If q = f then
is an accepting ID.
Our TM model is deterministic, which means that for each there is at most one such that
` . As usual, we write
`
if the TM changes into in any number of moves (including 0 in which case = ), we denote
`+
if the TM changes into using at least one move, and we denote
`i
if the TM changes into in exactly i moves.
For any w we define the corresponding initial ID
q0 w, if w 6= ,
w =
q0 B, if w = .
The language recognized (or accepted) by the TM M is
L(M ) = {w | w and w ` uf v for some u, v }.
Example 101. Let us design a TM M that recognizes the language
L(M ) = {w {a, b} | w is a palindrome }.
We have
M = ({q0 , qF , qL , qa , qa0 , qb , qb0 }, {a, b}, {a, b, B}, , q0 , B, qF )
where is detailed below.
The idea is that the machine reads the first input letter from the tape, remembers it in the
control unit (using states qa and qb ), finds the last letter on the tape and compares it with the first
letter. If they are identical, they are erased (replaced by B), and the process is repeated. If the
whole input word gets erased (or if there remains only one input letter if the original word has odd
length) the word was a palindrome, and the machine accepts it.
99
= (qF , B, R)
(q0 , a)
(qa , a)
(qa , b)
(qa , B)
(qa0 , B)
(qa0 , a)
=
=
=
=
=
=
(qa , B, R)
(qa , a, R)
(qa , b, R)
(qa0 , B, L)
(qF , B, R)
(qL , B, L)
(q0 , b)
(qb , a)
(qb , b)
(qb , B)
(qb0 , B)
(qb0 , b)
=
=
=
=
=
=
(qb , B, R)
(qb , a, R)
(qb , b, R)
(qb0 , B, L)
(qF , B, R)
(qL , B, L)
(qL , a) = (qL , a, L)
(qL , b) = (qL , b, L)
(qL , B) = (q0 , B, R)
4.2
Writing down Turing machines for complicated languages can be difficult and boring. But one
can use some programming techniques. The goal of this section is to convince the reader that
100
Turing machines are indeed powerful enough to recognize any language that a computer program
can recognize.
1. Storing a tape symbol in the finite control. We can build a TM whose states are pairs [q, X]
where q is a state, and X is a tape symbol. The second component can be used in remembering a
particular tape symbol. We have already used this technique in Example 101 where states qa and
qb were used to remember the tape symbol a or b. As another example, consider the following TM
that recognizes the language
L = ab + ba
The machine reads the first symbol, remembers it in the finite control, and checks that the same
symbol does not appear anywhere else in the input word:
(q0 , a)
(q0 , b)
([q, a], b)
([q, b], a)
([q, a], B)
([q, b], B)
=
=
=
=
=
=
([q, a], a, R)
([q, b], b, R)
([q, a], b, R)
([q, b], a, R)
(qF , B, R)
(qF , B, R)
2. Multiple tracks. Sometimes it is useful to imagine that the tape consists of multiple tracks.
We can store different intermediate information on different tracks:
BBB B a b a B B a B BBB
BBB B B B B a a a a BBB
BBB a a a a B B B B BBB
q
For example, we can construct a TM with 3 track tape that recognizes the language
L = {ap | p is a prime number }
as follows. Initially the input is written on the first track and the other two tracks contain Bs.
(This means we identify a with [a, B, B] and B with [B, B, B].) The machine operates as follows.
It first checks the small cases: If the input is empty or a then the machine halts in a non-final
state; if the input is aa it halts in the final state. Otherwise, the machine starts by placing two as
on the second track. Then it repeats the following instructions:
1. Copy the content of the first track to the third track.
2. Subtract the number on the second track from the third track as many times as possible. If
the third track becomes empty, halt in a non-final state. (The number on the first track was
divisible by the number on the second track.)
3. Increment the number on the second track by one. If the number becomes the same as the
number on the first track halt in the final state (no proper factor was found). Else go back
to step 1.
101
3. Checking off symbols. This simply means that we introduce a second track where we can
place blank B or symbol . The tick mark can be conveniently used in remembering which letters
of the input have been already processed. It is useful when we have to count or compare letters.
For example, consider the language
L = {ww | w (a + b) }.
We first use the tick mark to find the center of the input word: Mark alternatively the first and last
unmarked letters, one-by-one. The last letter to be marked is in the center. So we know where the
second w should start. Using the Storing a tape symbol in the finite control -technique,
one can check one-by-one the letters to verify that the letters in the first half and the second half
are identical.
4. Shifting over. This means adding an new cell at the current location of the tape. This can be
established by shifting all symbols one position to the right by scanning the tape from the current
position to the right, remembering the content of the previous cell in the finite control, and writing
it to the next cell on the right. Once the rightmost non-blank symbol is reached the machine can
return to the new vacant cell that was introduced. (In order to recognize the rightmost non-blank
symbol, it is convenient to introduce an end-of-tape symbol that is written in the first cell after the
last non-blank symbol.)
5. Subroutines. We can use subroutines in TM in an analogous way as they are used in normal
programming languages. A subroutine uses its own set of states, including its own initial state
q00 and a return state qr . To call a subroutine, the calling TM simply changes the state to q00 , and
makes sure the read-write head is positioned on the leftmost symbol of the parameter list to the
subroutine.
Constructing TM to perform specific tasks can be quite complicated. Even to recognize some simple
languages may require many states and complicated constructions. However, TM are powerful
enough to be able to simulate any computer program. The claim that Turing machines can compute
everything that is computable using any model of computation is known as Church-Turing thesis.
Since the thesis talks about any model of computation, it can never be proved. But so far TM
have been able to simulate all other models of computation that have been proposed.
As an example, let us see how a Turing machine would simulate a register machine, a realistic
model of a conventional computer. The tape contains all data the computer has in its memory.
The data can be organized for example in such a way that word vi in memory location i is stored
on the tape as the word
#0i vi #
where # and are special marker symbols. The contents of the registers of the CPU are stored on
their own tracks on the tape.
To execute the next instruction, the TM finds the memory location addressed by the specific
Program Counter register. In order to do that the TM goes through all memory locations one by
one and using the tick marks counts if the address i is the same as the content of the Program
Counter register. When it finds the correct memory location i, it reads the instruction vi and
memorizes it in the finite control. There are only finitely many different instructions.
To each instruction corresponds its own subroutine. To simulate the instruction, the TM can
use the same tick marking to find any required memory locations, and then execute the particular
102
task. The task may be adding the content of a register to another register, for example. Adding
two numbers can be easily implemented (especially if we decide to represent all number in the
unary format so that number n is represented as the word an ). Loading a word from the memory
to a register is simple as well. To write a word to the memory may require shifting all cells on the
right hand side of the memory location, but we know how to do that.
4.3
In this section two modifications to our TM model are briefly described. The variations are equivalent: They recognize exactly the same family of r.e. languages as the basic model.
1. Multiple tape TM. We can allow the TM to have more than one tape. Each tape has its
own independent R/W head. This is different from the one tape TM with multiple tracks since
the R/W heads of different tapes can now be at different positions. (For the sake of clarity, in the
following illustrations we represent the blank tape symbol by leaving the cell empty):
X X a b
X a
a b a
Depending on the state of the finite control and the current tape symbols on all tapes the machine
can
change the state,
overwrite the currently scanned symbols on all tapes, and
move each R/W head to left or right independently of each other.
Formally, the transition function is now a (partial) function from
(Q \ {f }) n
to
Q n {L, R}n
where n is the number of tapes. The transition
(q, X1 , X2 , . . . , Xn ) = (p, Y1 , Y2 , . . . , Yn , d1 , d2 , . . . , dn )
(where d1 , d2 , . . . , dn {L, R}) means that the machine, in state q, reading symbols X1 , X2 , . . . Xn
on the n tapes, changes its state to p, writes symbols Y1 ,Y2 , . . . Yn on the tapes, and moves the first,
second, third, etc. R/W head to the directions indicated by d1 , d2 , . . . dn , respectively. Initially,
103
the input is written on tape number one, and all other tapes are blank. A word is accepted iff the
machine eventually enters the final state f .
Let us see how a one tape TM can simulate an n-tape TM M . The single tape will have 2n
tracks two tracks for every tape of M : One of the tracks contains the data of the corresponding
tape in M ; The other one contains a single symbol # indicating the position of the R/W head on
that tape. The single R/W-head of the one-tape machine is located on the leftmost indicator #.
For example, the 3-tape configuration illustrated above would be represented by the following ID
with six tracks:
#
X X a b
#
X a
X
#
a b a
q
To simulate one move of the multitape machine M , the one-tape machine scans the tape from left
to right, remembering in the finite control the tape symbols indicated by the symbols #. Once all
#s have been encountered, the machine can figure out the new state p and the action taken on
each tape. During another sweep over the tape, the machine can execute the instruction by writing
the required symbols and moving the #s on the tape left or right.
If, for example, we have
(q, X, B, B) = (p, Y, Y, B, L, L, R),
after one simulation round the one tape machine will be in the ID
#
X Y a b
#
X a
Y
#
a b a
p
Note that simulating one step of the multitape machine requires scanning through the input twice,
so the one-tape machine will be much slower. But all that matters is that the machines accept
exactly the same words. It is clear that multitape TM recognize exactly the family of r.e. languages,
and multitape TM that halt with all inputs recognize exactly the family of recursive languages.
2. Non-deterministic Turing machines. We can also introduce non-determinism. Now is a
function from
(Q \ {f })
104
to subsets of
Q {L, R}.
When scanning tape symbol X in state q, the machine can execute any instruction from the set
(q, X). If (q, X) is empty, the machine halts.
A word w is accepted if and only if there exists a computation that takes the initial ID w into
an ID where the state is the final state. Note that there may be other computations that halt in a
non-final state, or do not halt at all, but the word is accepted as long as there exists at least one
accepting computation.
For example, recognizing the language
L = {ww | w (a + b) }
is easy using non-determinism. One does not need to find the center point of the input word first:
one may guess where the center point is, and start matching letters one-by-one assuming this center
point. If in the end all letters have been matched we know that the guess was correct and the word
is in the language.
In an analogous way, it is easy to construct a non-deterministic TM that recognizes the language
{w#u | w, u (a + b) and w is a subword of u }.
The machine first makes a non-deterministic guess of the position in u where the subword w begins.
Then it verifies the correctness of the guess by erasing symbols in w and u, one by one. If the process
ends successfully and the whole w gets erased, the word is in the language.
Non-determinism does not increase the recognizing power of Turing machines. A non-deterministic
TM can be simulated by a deterministic one as follows. Let r be the maximum size of the sets
(q, X) for any q and X. In other words, there are always at most r possible choices in the
non-deterministic machine.
The deterministic TM will use three tapes (which we know can be converted into a one-tape
version.) The first tape contains the input, and that tape never changes.
On the second tape we generate words over the alphabet
{1, 2, . . . , r}
in some predetermined order. We may, for example, start with shortest words, and move up to
longer and longer words. Words of equal length are generated in the lexicographical order. (In
other words, we are counting integers 1,2,3,. . . and representing them in r-ary base.)
We use the word generated on the second tape to make a selection among non-deterministic
choices for each move. Let x be the word generated on the second tape. We copy the input from
the first tape to the third tape, and simulate the non-deterministic TM on the third tape. At each
step, we use the next symbol on the second tape to decide which alternative to use among the
non-deterministic choices. In any of the following cases we give up, generate the next word on the
second tape, and repeat the process:
If the next r-ary digit on the second tape is greater than the number of choices in the current
ID of the machine we are simulating, or
if we consumed all letters from the second tape.
105
If on the other hand the machine halts in the final state, the simulating machine accepts the word.
The machine we constructed is deterministic. It is also equivalent to the original non-deterministic
machine M because it tries all possible non-deterministic choices one after the other until (if ever)
it finds an accepting computation:
If a word w is accepted by M , there exists a sequence of choices that leads to an accepting
calculation. Sooner or later that sequence of choices gets generated on the second tape. Once
that happens, we get an accepting computation on the third tape, and the word w is accepted.
If on the other hand w 6 L(M ), there does not exist a sequence of choices that leads to a
final state. Therefore, our deterministic machine never halts: it keeps generating longer and
longer words on the second tape, never halting.
We conclude that non-deterministic TM recognize exactly the family of recursively enumerable
languages. Note that the simulating deterministic machine M 0 is much slower than the original
non-deterministic machine M . If M accepts word w in n moves, it may take more than rn moves
by M 0 to accept the same word.
It is a famous open problem whether the deterministic machine needs to be so much slower. Let
us denote by P the family of languages that are recognized by some deterministic TM in polynomial
time. In other words, L is in P if there exists a Turing machine M and a polynomial p such that
M recognizes L, and for every w L the accepting calculation for w has at most p(|w|) moves.
Analogously, let us denote by N P the family of languages that are recognized by some nondeterministic TM in polynomial time. That means, for every w L there exists an accepting
computation for w that uses at most p(|w|) moves. Equivalently, every w L has a polynomial
size certificate string s (=the content of the second tape in the simulation above) such that a
deterministic TM can verify in polynomial time that w is in L, when given w and s as input.
It is a celebrated question whether P = N P , i.e., whether there are languages that are recognized non-deterministically in polynomial time, but recognizing them deterministically requires
super-polynomial time. It is generally assumed that P 6= N P . This problem has enormous practical importance because there are many important computational problems that are known to be
in NP.
4.4
Closure properties
We can easily prove the following closure properties. The closures are effective when the languages
are represented as Turing machines recognizing them.
Theorem 102 The following hold:
1. The family of recursive languages is effectively closed under complementation.
2. The families of recursive and recursively enumerable languages are effectively closed under
union and intersection.
3. A language L is recursive if and only if both L and its complement \ L are recursively
enumerable.
106
Later after we learn how to prove that a language is not recursive or r.e. we see that the family
of r.e. languages is not closed under complementation.
Proof.
1. Complementation of recursive languages. Let L be a recursive language, recognized by
the Turing machine
M = (Q, , , , q0 , B, f )
that halts on every input. Let us build a Turing machine
M 0 = (Q {f 0 }, , , 0 , q0 , B, f 0 )
that recognizes the complement of L.
M 0 is identical to M except that M 0 has a new final state f 0 , and whenever (q, X) is not
defined for q 6= f we add the transition 0 (q, X) = (f 0 , X, R).
Now, if w L(M ) then both M and M 0 halt in state f . That state is not the final state of M 0 ,
so M 0 does not accept w, and w 6 L(M 0 ). Conversely: If w 6 L(M ) then M halts in a non-final
state q. Therefore M 0 continues one more move and enters its final state f 0 . So w L(M 0 ).
2. Union and intersection of recursive and r.e. languages Let L1 and L2 be languages
recognized by Turing machines M1 and M2 , respectively. Let us describe a new TM M for the
intersection L1 L2 . The machine M simply executes M1 and M2 one after the other on the
same input w: It first simulates M1 on w. If M1 halts in the final state, M clears the tape, copies
the input word w on the tape and starts simulating M2 . If also M2 accepts w then M accepts.
Clearly, M recognizes L1 L2 , and if M1 and M2 halt on all inputs then also M halts on all
inputs.
Consider then the union L1 L2 . Note that the approach we used for the intersection does not
work here: simulating first M1 and then M2 may not halt on input w even if M2 would accept w.
This happens if M1 does not halt.
To determine if M1 or M2 accepts w we execute both M1 and M2 simultaneously, using a twotape Turing machine M . Machine M simulates M1 on the first tape and M2 on the second tape.
If either one enters the final state, the input is accepted.
3. If L is recursive then its complement is recursive by the case 1 of the theorem. Hence both L
and its complement are r.e.
Conversely, assume that both L and \ L are r.e. languages. Let M1 and M2 be Turing
machines that recognize L and \ L, respectively. We want to construct a TM M that recognizes
L and halts on every input. This is done as in the proof of case 2 above: Machine M uses two tapes
and simulates M1 and M2 simultaneously on the input word w. Machine M halts when either M1
or M2 halts in its final state. It is clear that M eventually halts because every word is accepted by
M1 or M2 . The input is accepted if and only if it was M1 that halted in the final state.
4.5
Recall that for each decision problem we have the corresponding language that contains the encodings of positive instances, and that we use the notation hIi for the encoding of instance I. Without
loss of generality we can assume that encodings are words over the binary alphabet {a, b}. This
107
is because letters of any larger alphabet can be coded into binary words, and such coding does
not change the (semi)decidability of the problem. For this reason, it is enough to consider Turing
machines whose input alphabet is = {a, b}.
In this section, we consider decision problems that concern Turing machines, so we need to
choose how we encode Turing machines as words. The choice is quite arbitrary; let us fix the
following encoding scheme. By renaming the states and tape letters we may assume that the state
set is
Q = {q1 , q2 , . . . , qn }
and the tape alphabet is
= {X1 , X2 , . . . , Xm }.
The input alphabet {a, b} and the blank symbol B must be part of the tape alphabet, so we assume
X1 = a, X2 = b, X3 = B.
We may also assume that q1 is the initial state and qn is the final state of the machine. So we
restrict our instances to Turing machines M = (Q, , , , q1 , B, qn ) where
Q = {q1 , q2 , . . . , qn },
= {a, b},
= {X1 , X2 , . . . , Xm } with
X1 = a,
X2 = b,
X3 = B.
It is clear that such Turing machines recognize all recursively enumerable languages over the alphabet {a, b}, and that halting machines of this kind recognize all recursive languages over {a, b}.
We also use the notation
D1 = L,
D2 = R
for the direction of movement.
An arbitrary transition
(qi , Xj ) = (qk , Xl , Ds )
by the machine M is encoded as the word
ai b aj b ak b al b as .
Let us encode the machine M as the word
hM i = bbb an bb am bb code1 bb code2 bb . . . bb coder bbb
where code1 , code2 , . . . ,coder are encodings of all the defined transitions (qi , Xj ), listed in the
lexicographic order of i, j.
Example 103. Consider the Turing machine
M = ({q1 , q2 , q3 }, {a, b}, {a, b, B, X4 }, , q1 , B, q3 )
108
with transitions
(q1 , a)
(q1 , B)
(q2 , b)
(q2 , B)
(q2 , X4 )
=
=
=
=
=
(q1 , b, R),
(q2 , B, L),
(q1 , X4 , R),
(q3 , a, R),
(q1 , B, L).
Then
hM i = bbbaaabbaaaabb abababaabaa bb abaaabaabaaaba bb aabaababaaaabaa
bb aabaaabaaababaa bb aabaaaababaaaba bbb.
It is easy to see that the set of valid encodings of Turing machines is recursive: there is an algorithm
that checks whether a given word w is an encoding of some Turing machine.
Lemma 104 The language
Lenc = { w {a, b} | w = hM i for some Turing machine M }
of valid encodings of Turing machines is recursive.
Proof. We can construct a TM that verifies the following facts about the input word w:
Word w has the correct form
bbb an bb am bb code1 bb code2 bb . . . bb coder bbb,
for some n 2 and m 3, where each codet is a word from a+ ba+ ba+ ba+ ba+ . Such words
form even a regular language.
Each codet = ai b aj b ak b al b as satisfies the conditions
1
1
1
1
i
k
j, l
s
n 1,
n,
m,
2.
This means each codet is a valid encoding of some transition (qi , Xj ) = (qk , Xl , Ds ).
0
Now we are ready to prove that a specific language over the alphabet {a, b} is not recursively
enumerable. The language in question is
Ld = {hM i | M does not accept word hM i },
the encodings of those Turing machines that do not accept their own encoding.
109
The proof above (due to A.Turing in 1936) is, in fact, the powerful diagonal (or self-reference)
argument used in many contexts in mathematics. A similar argumentation was used by G.Cantor
in 1891 to show that the set of reals is not countable, and by K.Godel in 1931 in his first incompleteness theorem. In fact, such self-reference argument was already present in the liars paradox
by Epimenides the Cretan (all Cretans are liars).
The diagonal aspect of Turings proof can be visualized as follows: Consider an arbitrary
enumeration M1 , M2 , . . . of Turing machines, and an an infinite 0/1-matrix whose rows are indexed
by Turing machines Mi and whose columns are indexed by their encodings hMi i. The entry
(Mi , hMj i) of the table is 1 iff Mi accepts word hMj i, so that the row indexed by Mi is the
characteristic sequence identifying which words hMj i are accepted by Mi :
+M1, +M2, +M3, +M4,
+M j ,
M1
M2
M3
M4
1, if Mi accepts +Mj ,
0, if Mi does not accept +Mj ,
Mi
The diagonal entries of the table are indexed by (Mi , hMi i). Consider the sequence of bits along
the diagonal, and complement this sequence by swapping each bit.
110
+M j ,
0
0
1
0
Mi
It is clear that the complemented diagonal cannot be identical to any row of the table. So a
language whose characteristic sequence is the complemented diagonal is not recognized by any
Turing machine. Our Ld is this language.
The theorem means that there is no semi-algorithm that would be able to detect whether a
given Turing machine M does not accept the input word hM i. This decision problem seems rather
artificial, but it serves as a seed from which we can conclude many other more natural
problems to be undecidable. The method we use to obtain undecidability results is reduction.
The following result is the first example of this method. We show that there is no algorithm to
determine if a given Turing machine accepts a given input word. More precisely, we show that
there is no semi-algorithm to detect the negative instances:
Corollary 106 The language
Lu = { hM i#w | M accepts input word w }
is not recursive. More specifically, its complement is not recursively enumerable.
Proof. Suppose the contrary: There is a Turing machine Mu that recognizes the complement of
Lu . Using Mu as a subroutine we can then build a Turing machine Md that recognizes Ld ,
contradicting Theorem 105. Machine Md works as follows: On input w it
1. Checks whether w is a valid encoding of some Turing machine. This can be done effectively,
as by Lemma 104 the language Lenc is recursive. If w is not a valid encoding, machine Md
halts in a non-final state.
2. If w = hM i is a valid encoding then Md writes w#w = hM i#hM i on the tape and starts Mu
on this input. The final state is entered if and only if Mu accepts this word.
Such Turing machine Md clearly exists if Mu exists. Now, word w is accepted by Md if and only if
w is a valid encoding of a Turing machine that does not accept its own encoding. In other words,
Md recognizes Ld , a contradiction. Hence Mu cannot exist.
transition function of Md , but a precise enough construction of it so that it is clear that it exists if
Mu exists.
The same reduction can be expressed at a higher level by relying on the fact that any semialgorithm (in our informal, intuitive sense) can be implemented on a Turing machine: Suppose
there is a semi-algorithm for recognizing the 0 entries in the infinite table discussed above (i.e., a
semi-algorithm for testing whether a given Turing machine does not accept a given word). This
semi-algorithm then also identifies entries 1 on the inverted diagonal, contradicting Theorem 105.
The following undecidability result is another example of the reduction technique. This reduction is more complicated as it requires an effective modification of a given Turing machine. The
reduction is described at the high level using an informal description of an algorithm rather than
constructing the corresponding Turing machine. The result is that there is no algorithm to tell if
a given Turing machine eventually halts when it is started on the blank tape:
Corollary 107 The language
Lhalt = { hM i | M halts when started on the blank tape }
is not recursive.
Proof. Suppose the contrary: There is an algorithm A to determine if a given Turing machine
halts when started on the blank tape. Then the following algorithm Au determines whether a given
Turing machine M accepts a given word w, contradicting Corollary 106. On input M and w:
1. Au builds a new Turing machine M 0 that halts from the blank initial tape if and only if M
accepts w. Such M 0 can be effectively constructed for any given M and w: When started,
M 0 writes w on its tape. (This may be done using |w| states.) Then M 0 moves back to the
first letter of w and enters the initial state of M . From there on, transitions of machine M
are used. For each halting but non-final state of M the machine M 0 goes into a new looping
state that makes the machine move to the right indefinitely without ever halting. Clearly, on
the initially empty tape this M 0 halts if and only if M accepts input w.
2. Au does not execute M 0 . Instead, it just gives M 0 as an input to the hypothetical algorithm
A, and returns yes if A returns yes, and returns no if A returns no.
Clearly, Au returns yes on input M and w if and only if M accepts w. But such algorithm cannot
exist by Corollary 106. Hence, the algorithm A cannot exist either.
With a more careful analysis of the reduction above we could conclude that the complement of
Lhalt is not recursively enumerable, i.e., non-halting is not semi-decidable.
So, the reduction method is used as follows: To prove that a decision problem P is undecidable
we assume that there would exist an algorithm A that solves P . Then we describe an algorithm
A0 that solves some known undecidable problem P 0 , using A as a subroutine. Since such algorithm
A0 cannot exist we conclude that algorithm A cannot exist either. Notice that such undecidability
proof involves designing an algorithm! but the algorithm is for a known undecidable problem P 0
and it uses a hypothetical subroutine A that solves P .
A reduction to show that problem P is not semi-decidable works analogously: Assume that a
semi-algorithm for P exists. Build a semi-algorithm A0 (using A as a subroutine) for a problem P 0
112
that is known not to be semi-decidable. As such A0 cannot exist, semi-algorithm A cannot exist
either.
Example 108. Yet another example of a reduction: Let us show that there is no algorithm to
determine if a given r.e. language is empty, that is, whether a given Turing machine accepts any
words. Suppose the contrary: algorithm A determines if a given Turing machine accepts some
word. Then we have the following algorithm A0 to determine if a given Turing machine M accepts
a given word w, contradicting Corollary 106:
1. First A0 builds a new Turing machine M 0 that erases its input, writes w on the tape, moves
to the first letter of w and enters the initial state of M . From there on, the transitions of M
are used. Such machine M 0 can be effectively constructed for any given M and w.
2. Next A0 gives the machine M 0 it constructed as an input to the hypothetical algorithm A,
and returns the answer that A provides.
Note that if M accepts w then M 0 accepts all words, and if M does not accept w then M 0 does
not accept any word. So A returns yes on input M 0 if and only if M accepts w. Algorithm A0
described above cannot exist (Corollary 106) so the hypothetical algorithm A does not exist.
4.6
Language Lu provides now the following two corollaries, based on Corollary 106 and Theorem 109:
Corollary 110 There are recursively enumerable languages that are not recursive.
Corollary 111 The family of recursively enumerable languages is not closed under complementation.
113
By Theorem 109 we know that some Turing machine Mu recognizes Lu . Such machine Mu is called
a universal Turing machine since it can simulate any given Turing machine on any given input,
when given the description of the machine to be simulated. So Mu is a programmable computer:
rather than building a new TM for each new language, one can use the same TM Mu and only
change the program hM i that describes which Turing machine should be simulated. This is a
very important concept just imagine if we had to build a new computer for each algorithm we
want to execute!
4.7
Rices theorem
We have seen that many questions concerning Turing machines are undecidable. Some questions
are clearly decidable (e.g., Does a given Turing machine have 5 states ?). But it turns out that
any non-trivial question that only concerns the language that a TM recognizes, rather than the
machine itself, is undecidable. By a non-trivial question we mean any question that is not always
true or always false.
More precisely, let P be any family of languages. We call P a non-trivial property if there
exist Turing machines M1 and M2 such that L(M1 ) P and L(M2 ) 6 P. For any such fixed P
it is undecidable for a given Turing machine M whether L(M ) P. This result is known as the
Rices theorem.
Theorem 112 Let P be a non-trivial property. There is no algorithm to determine if a given
Turing machine M has L(M ) P.
Proof. We use a reduction from Lu . The reduction is very similar to the one in Example 108.
Without loss of generality, assume that 6 P. (If this does not hold, we simply consider the
complement of P instead of P.) Because P is non-trivial, there exists a Turing machine MP such
that L(MP ) P.
Suppose there is an algorithm A to determine if a given Turing machine M has L(M ) P.
Then we have the following algorithm A0 to determine if a given Turing machine M accepts a given
word w, contradicting Corollary 106:
1. First A0 builds a new Turing machine M 0 that
Stores its input u on a separate track for later use.
Writes w on the tape and moves to the first letter of w.
Enters the initial state of M . From there on, the transitions of M are used, ignoring
word u, until M enters its final state f .
If M enters state f then word u is written on an otherwise blank tape, and machine MP
is simulated on u. Final state is entered if MP accepts u.
2. Next A0 gives the machine M 0 it constructed as an input to the hypothetical algorithm A,
and returns the answer that A provides.
Note that the machine M 0 can be effectively constructed for any given M and w, so algorithm A0
performing the steps above exists, assuming A exists.
The correctness of algorithm A0 follows from the following simple observations:
114
+ If M accepts w then M 0 will accept exactly the same words u that MP accepts. Hence, in
this case L(M 0 ) = L(MP ) P.
- If M does not accept w then M 0 does not accept any input words u, so in this case L(M 0 ) =
6 P.
Algorithm A0 described above cannot exist (Corollary 106) so the hypothetical algorithm A does
not exist.
Remarks: (1) Rices theorem covers decision problems covered individually in the previous section.
It also shows that, for example, the following questions are undecidable: Does a given TM accept
all input words ?, Is L(M ) regular for given TM M ?, Does a given Turing machine accept all
palindromes ?, and so on.
(2) A more careful analysis of the proof of Rices theorem above shows that, for a non-trivial
property P such that P, it is not semi-decidable for a given Turing machine M whether
L(M ) P.
4.8
The word problem of semi-Thue systems T asks for given words x and y whether x y using
the rewrite rules of T . As usual, x y means that there is a derivation x x1 x2 . . . y
of arbitrary length. We prove in the following that the word problem of some semi-Thue systems is
undecidable. In fact, we even show the undecidability of the so-called individual word problem
in some T : In this problem, word y is fixed, and we ask whether a given word x derives y.
115
First we associate to each Turing machine M a semi-Thue system TM that simulates M . This
is not difficult to do since the operations of a Turing machine can be viewed as rewriting its
instantaneous descriptions. All rewriting is done locally and can hence be expressed as rewrite
rules. The only slight complication is caused by the left and right ends of the ID where the tape
may need to be extended of shrank by adding or removing blank tape symbols. Note that a rewrite
rule cannot recognize the end of a word so no specific rules can be defined for the left and right
end. For this reason we add to the ends of the ID new symbols [ and ]. A Turing machine ID
will then be represented as the word [].
The following effective construction provides a semi-Thue system such that for any ID and
of M there is a derivation step [] [] if and only if ` is a valid move by the TM. Note that
our simulation of M by TM is tight in the sense that we make sure to add or erase blanks at both
ends of the ID.
Let M = (Q, , , , q0 , B, f ) be an arbitrary Turing machine where Q = . The corresponding semi-Thue system is TM = (, R) where
= Q {[, ]},
and R contains the following productions:
1. (Left moves) For every q Q and a such that (q, a) = (p, b, L) for some p Q and
b ,
if b 6= B we have in R the productions
xqa pxb
[qa [pBb
for all x
pxby
px]
[pBby
[pB]
for all x, y
for all x
for all y
2. (Right moves) For every q Q and a such that (q, a) = (p, b, R) for some p Q and
b ,
if b 6= B we have in R the productions
qay bpy
qa] bpB]
for all y
xbpy
xbpB]
[py
[pB]
116
for all x, y
for all x
for all y
In each case we correctly simulate the action of adding or removing blanks when the TM acts at
the left or right end of the ID. It is easy to see that if ` in M then [] [] in TM , and this is
the only possible derivation step from []. If is a halting ID then there are no possible derivation
steps from []. The following Lemma summarizes these facts:
Lemma 114 Let be an ID of the Turing machine M and let TM be the semi-Thue system
constructed above. Then, for w ,
[] w
if and only if
w = [] and ` .
To prove the undecidability of the word problem in semi-Thue systems we want the simulating
Turing machine to erase its tape when the computation is finished. This can be assumed:
Lemma 115 For every Turing machine M one can effectively construct a Turing machine M 0
such that L(M 0 ) = L(M ) and every accepting computation in M 0 ends in the ID f B.
Proof. We modify M in the following ways:
In the beginning of the computation, end-markers are added at the beginning and end of the
input word.
During the computation by M , if the machine encounters an end-marker the end-marker is
moved so that the Turing machine always remains between the markers.
If M accepts the input word, the computation continues as follows: the machines moves to
the left end-marker, and starting from there moves to the right, erasing all symbols along the
way until meeting the right end-marker. Then the final state f is entered.
It is clear that such TM can be effectively constructed and that it has the required properties.
Theorem 116 There exists a semi-Thue system T and a fixed word u such that the individual
word problem Does a given word x derive u in T ? is undecidable
Proof. Let M be a Turing machine such that L(M ) is not recursive. Such M exists by Corollary 110.
By Lemma 115 we may assume that every accepting computation ends in the ID f B. Let TM be
the corresponding semi-Thue system constructed above. By Lemma 114, for any input w
[w ] [f B]
if and only if w L(M ). Here w is the initial ID corresponding to input word w. If there were
an algorithm to determine if a given word x derives u = [f B] in T = TM , then there would be an
algorithm to determine if M accepts a given word w, contradicting the non-recursiveness of L(M ).
Remarks: (1) By adding [f B] to the production set, we can choose u = in the previous
theorem. We obtain a semi-Thue system in which it is undecidable whether a given word derives
the empty word.
117
(2) It is known that there exists a semi-Thue system (, R) with || = 2 and |R| = 3 whose
individual word problem is undecidable. The decidability status for semi-Thue systems with two
productions is not known.
A Thue system is a special kind of semi-Thue system where u v is a production if and only if
v u is a production. In other words, all rewrite-rules may be applied in both directions. For this
reason we write the productions of a Thue system as u v, and we denote one step derivations
using instead of . It is easy to see that is an equivalence relation, and moreover, it is a
congruence of the monoid :
Lemma 117 Let T = (, R) be a Thue system. Then is an equivalence relation, and u1 v1 ,
u2 v2 implies that u1 v1 u2 v2 .
The Thue system T is a finite presentation of the quotient monoid / whose elements are
the equivalence classes. The word problem of T is then the question of whether two given words
represent the same element in the quotient monoid.
The following theorem strengthens Theorem 116 for the case of Thue systems:
Theorem 118 There exists a Thue system T and a fixed word u such that the individual word
problem Does a given word x derive u in T ? is undecidable
Proof. We use the following two facts concerning the semi-Thue system TM associated to TM M :
(i) The rewriting is deterministic: If w contains a single occurrence of a letter from Q then
there is at most one z such that w z.
(ii) In each production u v both u and v contain a single occurrence of a letter from Q.
Let us interpret TM as a Thue system, i.e., let us allow the productions to applied in either direction.
Let us denote the derivation relation of the Thue system by , while we denote u v and u v
if v is obtained from u by an application of a production in the forward or backward direction,
respectively.
As in the proof of Theorem 116, let M be a Turing machine that recognizes a non-recursive
language, and assume that every accepting computation ends in the unique ID [f B]. Let us prove
that [w ] [f B] in TM if and only if w L(M ).
One direction is clear: If w L(M ) then [w ] [f B], so also [w ] [f B]. Consider then the
other direction: Suppose that w is such that [w ] [f B]. Then there exists a sequence of
words w0 , w1 , w2 , . . . wn such that
[w ] = w0 w1 w2 . . . wn = [f B].
Let n be as small as possible. If all derivation steps use the productions in the forward direction,
that is, if wi1 wi for all i = 1, 2, . . . , n, then the derivation simulates the computation of the
TM from initial ID w , so w L(M ).
Suppose then that for some i we have wi1 wi . Let i be the largest such index. Because no
production can be applied in the forward direction to [f B] we must have i < n. By the maximality
of i we then have wi wi+1 . But the property (ii) above guarantees that all wi contain exactly
one occurrence of a letter from Q, and property (i) then implies that the forward rewriting is
118
deterministic. We conclude that wi1 = wi+1 , and the derivation can be made shorter by removing
the unnecessary loop wi1 wi wi+1 . This contradicts the minimality of n.
Remark: (1) The theorem means that the word problem is undecidable among finitely presented
monoids. The result can be strengthened further: It is known that the word problem is undecidable
even among finitely presented groups. On the other hand, the word problem is decidable among
finitely presented abelian (i.e. commutative) monoids and groups.
(2) By adding [f B] to the production set, we can choose u = in the theorem. In this case,
it is undecidable if a given word represents the identity element of the monoid. (A small proof is
needed to show that shortest derivations [w ] [f B] do not use the new production [f B] .
This can be done because [ and ] isolate the rewriting into disjoint regions of the word.)
(3) There exists a Thue system (, R) with || = 2 and |R| = 3 whose word problem is undecidable.
A grammar is a quadruple G = (V, T, P, S) where V and T are disjoint finite alphabets of variables
(=non-terminals) and terminals, respectively, S V is the start symbol, and P is a finite set of
productions u v where u and v are words over V T and u contains at least one variable.
The pair (V T, P ) acts as a semi-Thue system: we denote w w0 if w0 is obtained from w by
replacing an occurrence of u by v for some u v P . Note that if w T is terminal then no
derivation is possible from w. The language
L(G) = {w T | S w}
generated by the grammar consists of all the terminal words that can be derived from the start
symbol S. Such grammars are called type-0 grammars.
Example 119. The grammar G = ({S, X, Y, Z}, {a, b, c}, P, S) with productions
S
X
Yb
Yc
bZ
aZ
aXbc |
| aY b
bY
Zcc
Zb
aX
In the following we show that type-0 grammars generate exactly the family of recursively enumerable
languages. While Turing machines are accepting devices, grammars are generative. To convert a
Turing machine into an equivalent grammar we therefore need to reverse the computation so
that we start generating from the accepting ID backwards in time, until an initial ID is reached.
119
Theorem 120 Turing machines and type-0 grammars are effectively equivalent.
Proof. It is clear that the language generated by any grammar is recursively enumerable: There is
a semi-algorithm that enumerates all derivations in the order of increasing length. If any of them
derives the input word then the word is accepted. A Turing machine executing such semi-algorithm
can be effectively constructed.
For the converse direction we show how to construct for any given Turing machine M =
(Q, , , , q0 , B, f ) a type-0 grammar G = (V, , P, S) such that L(G) = L(M ). Let TM = (, R)
be the semi-Thue system associated to machine M . Recall that = Q {[, ]} and that in each
production u v in R both u and v contains exactly one occurrence of a letter from Q. Let RR
be the set of reversed rules: for every u v in R there is v u in RR . From Lemma 114 we
R = (, RR ) if and only if w L(M ). Here = q w if w 6= and
have that [f B] [w ] in TM
w
0
= q0 B. Notice also that all words that can be derived from [f B] are of the form [zqy] where
q Q and z, y with |y| 1. This follows from the facts that the productions do not change
the boundary symbols [ and ], and between the boundary symbols exactly one occurrence of
a letter from Q is maintained, followed by at least one other symbol before the right boundary
marker.
We add to two new symbols # and S so the variable set of the grammar is
V = Q ( \ ) {[, ], #, S}.
We add to the production set RR the following new productions:
S
[q0
#a
#]
[q0 B]
[f B]
#
a#
for all a
The first production is used to initialize the sentential form to [f B]. The last production erases
[q0 B] if the empty word is accepted. The other productions erase from any [q0 w] symbols [, q0 and
], leaving w. In any case, S [f B] and [w ] w using these productions.
Let P be RR extended by the productions above. Now the grammar G = (V, , P, S) is fully
defined. Let us prove that L(G) = L(M ).
If w L(M ) then w ` f B in M . Hence, G admits the derivation
S [f B] [w ] w,
so w L(G).
Conversely, let w L(G). All derivations begin S [f B]. Using productions in RR one
can only reach sentential forms of types [zqy] where q Q. In order to derive a terminal word,
production [q0 B] or [q0 # needs to be used. The first production can only be applied on
[q0 B], which can be reached only if L(M ), as required. The second one can be applied only on
[zqy] = [q0 y], deriving #y] in one step. This derives only the terminal string y so y = w. In any
case, if S w then [f B] [w ] using productions of RR only. We conclude that w ` f B in
M , so w L(M ).
120
4.9
In this section we see undecidable problems encountered in different contexts. Problems considered
include the Post correspondence problem, problems associated to context-free grammars, a problem
concerning products of integer matrices, and a variant of the tiling problem.
4.9.1
Our next undecidable problem is the Post correspondence problem (PCP). An instance to
PCP consists of two lists of words over some alphabet :
L1 : w1 , w2 , . . . wk
L2 : x1 , x2 , . . . xk
Both lists contain equally many words. We say that each pair (wi , xi ) forms a pair of corresponding
words. A solution to the instance is any non-empty string i1 i2 . . . im of indices from {1, 2, . . . , k}
such that
wi1 wi2 . . . wim = xi1 xi2 . . . xim .
In other words, we concatenate corresponding words wi and xi to form two words. We have a
solution if the concatenated wi s form the same word as the corresponding concatenated xi s. The
PCP asks whether a given instance has a solution or not. It turns out that PCP is undecidable.
PCP can also be expressed compactly in terms of homomorphisms: The problem asks for two
given homomorphisms h1 , h2 : whether there exists a non-empty word u such that
h1 (u) = h2 (u). The connection to the formulation using lists is seen if we choose = {1, 2, . . . , k}
and h1 (i) = wi and h2 (i) = xi for all i . Then h1 (u) and h2 (u) are the concatenated words of
wi s and xi s given by the index sequence u.
Example 121. Consider the following two lists:
L1 : a2 ,
L2 : a2 b,
b2 ,
ba,
ab2
b
a
ba2
does not have a solution: If it would have a solution, the solution would need to start with index
1. Since w1 = a2 b and x1 = a2 , the second list has to catch up the missing b: The second index
121
has to be 2. Because w1 w2 = a2 ba and x1 x2 = a2 ba2 the first list has to catch up. The next index
cannot be 1 because w1 w2 w1 = a2 baa2 b and x1 x2 x1 = a2 ba2 a2 differ in the 7th letter. So the third
index is 2, yielding w1 w2 w2 = a2 baa and x1 x2 x2 = a2 ba2 ba2 . Now the first list has to catch up ba2
which is not possible since neither w1 nor w2 starts with letter b.
ba
ab
Now a solution has to start with index 1. Since w1 = a, x1 = ab we need a word from the first list
that starts with a b. So the second index has to be a 2. Since w1 w2 = aba, x1 x2 = abab the first list
has to catch up a missing b again, so the third index has to be a 2 as well. This never ends: The
first list will always be behind by one b. Therefore this PCP instance does not have a solution.
Theorem 124 The Post correspondence problem is undecidable.
Proof. We reduce the word problem of semi-Thue systems to PCP. Assume there exists an algorithm
A that solves PCP. Let us see how A can be used to solve the word problem of semi-Thue systems.
Let T = (, R) be a given semi-Thue system, and let x and y be given words over alphabet .
We may assume that in every rewrite rule u v both u and v are non-empty words, and also
that x and y are both non-empty. We can make this assumption because the semi-Thue systems
constructed in the proof of Theorem 116 has this property, so the word problem is undecidable in
such restricted cases.
First we construct an equivalent PCP instance. Let 0 be a new alphabet containing letters a0
for all a . For every word w over we will denote by w0 the corresponding word obtained by
priming all letters. (More precisely, w0 = h(w) where h is the homomorphism that maps a 7 a0 ,
for all a .) Let # be a new marker symbol.
The following corresponding pairs of words are placed into the lists L1 and L2 :
L1
#x
#
a
a0
v0
L2
#
y#
a0
a
u
for every a
for every a
for every u v R
The only pair in which one of the words is the prefix of the corresponding word in the other list
is (#x, #). Therefore a solution to the PCP instance must start with this pair. Correspondingly,
the only pair in which one word is the suffix of the other one is (#, y#) so any solution must end
in this pair.
Consider the situation after the first pair:
#x
#
122
The second word has to catch-up x. The only pairs that can match letters of x are (a0 , a) for a
and (v 0 , u) for u v R. Such pairs produce a new primed word after x in the first catenated
word:
# x x01
#x
Note that x1 can be obtained from x in the semi-Thue system in some number of derivation
steps, using non-overlapping applications of productions. (In other words, words x and x1 can be
expressed as concatenations x = w0 u1 w1 u2 . . . ui wi and x1 = w0 v1 w1 v2 . . . vi wi of words such that
all (ui , vi ) are pairs of corresponding words in the PCP instance. Notice that the priming of the
letters in x01 prevent the ui from extending over the boundary of x and x01 .) So we have x x1
in T .
Next the second list has to catch-up the primed word x01 . The only pairs that contain primed
letters in the second components are (a, a0 ) for a . Such pairs create a new unprimed copy of
x1 in the first word:
# x x01 x1
# x x01
Now the process is repeated on x1 instead of x, and so on. We are forced to create matching words
of type
# x x01 x1 x02 x2 x03 x3 . . . x0n xn
# x x01 x1 x02 x2 x03 x3 . . . x0n
such that the semi-Thue system has a derivation
x x1 x2 x3 . . . xn .
The second list can catch-up the first list only if for some n we have xn = y. Then the pair (#, y#)
can be used to close the words:
# x x01 x1 x02 x2 x03 x3 . . . x0n xn #
# x x01 x1 x02 x2 x03 x3 . . . x0n y #
From the discussion above it is obvious that the PCP instance we constructed has a solution if and
only if x y in the given semi-Thue system T .
Example 125. Consider the semi-Thue system of Example 113 with productions
bb
b,
aba bab,
a
aa,
and let x = ababa and y = bab. The corresponding PCP instance contains 9 pairs of words:
L1 :
L2 :
#ababa
#
#
bab#
a
a0
b
b0
a0
a
b0
b
b0
bb
b0 a0 b0
aba
a0 a0
a
The word problem instance has a positive solution, so the PCP has one too:
#ababa b0 a0 b0 b0 a0 b a b b a b0 a0 b0 a0 b a b a b0 b0 a0 b0 b b a b b0 a0 b0 b a b #
#
aba b a b0 a0 b0 b0 a0 b a bb a b0 a0 b0 a0 b aba b0 b0 a0 b0 bb a b b0 a0 b0 bab#
123
Remarks: (1) The PCP is undecidable even if the words wi and xi are over the binary alphabet
{a, b}. This is because any alphabet can be encoded in the binary alphabet using an injective
homomorphism h : {a, b} . (For example, we can use distinct binary words of length
dlog2 ||e to encode the letters of .) Because of the injectivity of h, the instance h1 , h2 :
is equivalent to the binary instance h h1 , h h2 : {a, b} .
(2) Our construction converts a semi-Thue system (, R) into a PCP instance containing 2|| +
|R|+2 pairs of words. Over the binary alphabet || = 2 this means |R|+6 pairs. There is a smarter
reduction that provides only |R| + 4 pairs of words. Since there is a semi-Thue system with |R| = 3
rules whose individual word problem is undecidable, we obtain that PCP is undecidable among
lists with 7 pairs of words. On the other hand, PCP is known to be decidable among instances
with two pairs of words. The exact boundary where P CP becomes undecidable is not known.
4.9.2
Next we learn how PCP can be reduced to some questions concerning context-free grammars and
languages, proving that those questions are undecidable. Let w1 , w2 , . . . , wk be a finite list of words
over an alphabet , and assume that {1, 2, . . . k, #, $} = . We associate to this list the
context-free language
L(w1 , . . . , wk ) = { i1 i2 . . . in #wiRn wiRn1 . . . wiR1 $ | n 1}
over the alphabet = {1, 2, . . . , k} {#, $}. Using the homomorphism notation with h(i) = wi
for all i {1, 2, . . . , k}, the language consists of words u#h(u)R $ for all non-empty u {1, 2, . . . k}+ .
The language is indeed context-free because it is derived by the CFG G = ({S, A}, , P, S) with
productions
S A$
to create the end marker $,
A 1Aw1R | 2Aw2R | . . . | kAwkR
to create matching index/word pairs, and
A 1#w1R | 2#w2R | . . . | k#wkR
to terminate. In fact, we have the following:
Lemma 126 Language L(w1 , . . . , wk ) is (effectively) recognized by a deterministic PDA without
-transitions, using the final state acceptance mode.
Proof. We construct such a PDA
A = (Q, , , , q0 , Z0 , {qF }),
where Q = {q0 , q1 , q2 , qF } and = {Z0 }. The idea is that A reads in state q1 indices i
{1, 2, . . . , k} and pushes, for each index i, the corresponding word wiR into the stack. Once the
124
marker # is encountered the machine changes into state q2 and starts matching input letters with
the symbols in the stack. The word is accepted if and only if the stack contains Z0 when the last
input letter is $ is being read. The precise transition function is given by
(q0 , i, Z0 )
(q1 , i, Z)
(q1 , #, Z)
(q2 , a, a)
(q2 , $, Z0 )
=
=
=
=
=
{(q1 , wiR Z0 )}
{(q1 , wiR Z)}
{(q2 , Z)}
{(q2 , )}
{(qF , Z0 )}
for
for
for
for
all
all
all
all
i {1, 2, . . . , k}
Z and i {1, 2, . . . , k}
Z
a
The reason for proving the lemma above is the following corollary
Corollary 127 The complement of language L(w1 , . . . , wk ) is (effectively) context-free.
Proof. Let us add into the PDA A of Lemma 126 a new state f and transitions (q, a, Z) = (f, Z)
for all q Q {f }, a and Z such that (q, a, Z) was undefined in A. The new PDA is still
deterministic without -transitions, its transition function is complete, and the symbol Z0 is never
removed from the stack so the stack never becomes empty. These properties imply that the PDA
does not halt until the entire input word has been read, and that for every input word w
there corresponds a unique state q and stack content such that (q0 , w, Z0 ) ` (q, , ). Here,
q = qF if and only if w L(w1 , . . . , wk ). If we swap the final states so that the new final states are
all states except qF then the new PDA recognizes the complement of L(w1 , . . . , wk ).
In fact, the family of deterministic context-free languages is closed under complementation. However, possible -transitions in a deterministic PDA cause technical difficulties in the proof. In our
case, complementation is easy because the PDA has no -transitions: it is enough simply to swap
the accepting and non-accepting states.
The complement is not needed in our first result:
Theorem 128 It is undecidable for given context-free languages L1 and L2 whether L1 L2 = .
Proof. We reduce the Post correspondence problem. Let
L1 : w1 , w2 , . . . wk
L2 : x1 , x2 , . . . xk
be a given instance to PCP. We effectively construct the context-free languages L(w1 , . . . wk ) and
L(x1 , . . . xk ) corresponding to the two lists, as discussed above. We clearly have
L(w1 , . . . wk ) L(x1 , . . . xk ) 6=
if and only if the PCP instance has a solution. Indeed, word i1 i2 . . . in #wR $ is in L(w1 , . . . wk ) and
L(x1 , . . . xk ) if and only if n 1 and
wi1 wi2 . . . win = w = xi1 xi2 . . . xin .
125
The grammar G constructed in the beginning of the section for the language L(w1 , . . . , wn ) is of
very restricted form: it is a linear grammar (righthand-sides of all productions contain at most
one variable) and it is unambiguous. Therefore we have the undecidability of the question Is
L(G1 ) L(G2 ) = for given linear, unambiguous context-free grammars G1 and G2 ? From this
remark we immediately obtain the next undecidability result:
Theorem 129 It is undecidable whether a given linear context-free grammar G is unambiguous.
Proof. Let G1 = (V1 , T1 , P1 , S1 ) and G2 = (V2 , T2 , P2 , S2 ) be two given linear, unambiguous grammars. Rename the variables so that V1 and V2 are disjoint and do not contain variable S. Construct
a new linear grammar
G = (V1 V2 {S}, T1 T2 , P, S)
where P contains all productions from P1 and P2 , and the additional start-up productions
S S1 | S2 .
Then L(G1 ) L(G2 ) = if and only if G is unambiguous. Indeed, every u L(G1 ) L(G2 ) has
two left-most derivations in G starting with the steps S S1 and S S2 , and continuing with
derivations according to G1 and G2 , respectively. Conversely, if L(G1 ) L(G2 ) = then every
generated word has only one leftmost derivation because G1 and G2 are unambiguous.
In the next result we use the fact that the complements of L(w1 , . . . , wk ) and L(x1 , . . . , xk ) are
effectively context-free.
Theorem 130 It is undecidable if a given context-free language L satisfies L = .
Proof. We reduce Post correspondence problem. For any given instance
L1 : w1 , w2 , . . . wk
L2 : x1 , x2 , . . . xk
to PCP we effectively construct the complements L1 and L2 of L(w1 , . . . , wk ) and L(x1 , . . . , xk ),
respectively. Then L1 L2 = if and only if L(w1 , . . . , wk ) L(x1 , . . . , xk ) = , that is, if and
only if the PCP instance has no solution. We can effectively construct the union L1 L2 , so the
result follows from the undecidability of PCP.
Corollary 131 Let L1 and L2 be given context-free languages and let R be a given regular language.
The following questions are undecidable:
(a) Is L1 = L2 ?
(b) Is L2 L1 ?
(c) Is L1 = R ?
126
(c) Is R L1 ?
The question whether L1 R is, however, decidable.
Proof. Undecidability of (a)-(d) follows from Theorem 130 by fixing L2 = R = . To decide
whether L1 R one can effectively construct the intersection L1 R and test it for emptyness.
4.9.3
Next we prove an undecidability result that concerns product of square matrices with integer entries.
Let {M1 , M2 , . . . , Mk } be a finite set of n n matrices over integers. We say that the matrix set is
mortal if some product of the matrices from the set is the zero matrix.
Example 132. Consider the following two 2 2 matrices:
0 1
2 0
M1 =
M2 =
1
0
0 1
Matrix set {M1 , M2 } is not mortal: Because det(M1 ) and det(M2 ) are non-zero, the determinant
of every product is non-zero.
M1 =
0 1
1
0
M2 =
0 0
0 1
The matrix mortality problem asks whether a given set of n n integer matrices is mortal. In the
following we show that this problem is undecidable, even among 3 3 integer matrices. We start
by associating to each word w = a1 a2 . . . am over the alphabet {1, 2, 3} the integer
(w) = am + 4am1 + . . . + 4m1 a1 ,
that is, the number that w represents in base four. It is clear that is injective, and for any words
u and v we have
(uv) = (v) + 4|v| (u).
Next we associate to any pair u, v of words over the alphabet {1, 2, 3} the following 3 3 integer
matrix:
|u|
4
0
0
Mu,v = 0
4|v| 0 .
(u) (v) 1
The following lemma shows that the mapping (u, v) 7 Mu,v is a monoid homomorphism from
{1, 2, 3} {1, 2, 3} to the multiplicative monoid of 3 3 integer matrices:
Lemma 134 For all u, v, x, y {1, 2, 3} holds that Mu,v Mx,y = Mux,vy .
127
4|u| 4|x|
0
0
4
0
0
4
0
0
0
0
4|v| 4|y|
0
4|v| 0 0
4|y| 0 =
(u) (v) 1
(x) (y) 1
4|x| (u) + (x) 4|y| (v) + (y) 1
4|ux|
0
0
= 0
4|vy| 0
(ux) (vy) 1
Let
1 0
1
A = 1 0 1 .
0 0
0
(7)
and
for all i = 1, 2, . . . , k. Let us show that this set is mortal if and only if the PCP instance has a
solution.
Suppose first that the PCP instance has a solution i1 i2 . . . im , so that wi1 wi2 . . . wim = xi1 xi2 . . . xim .
Then
Mi01 Mi2 Mi3 . . . Mim = Mu,v
where u = wi1 wi2 . . . wim and v = 1xi1 xi2 . . . xim . We have v = 1u, so by (7) holds AMu,v A = 0.
We see that the matrix set is mortal.
128
Conversely, suppose that the matrix set is mortal. This means that
AW1 AW2 A . . . AWm A = 0
where each Wj is a product of some matrices Mi and Mi0 . Each Wj is of the form Mu,v for some
words u, v over the alphabet {1, 2, 3}, so by (7) the matrices AWj A are scalar multiples of A, say
AWj A = aj A. Then
0 = AW1 AW2 A . . . AWm A = a1 AW2 A . . . AWm A = a1 a2 AW3 A . . . AWm A . . . = a1 a2 . . . am A.
We conclude that some aj = 0, that is, AWj A = 0. Let Wj = Mu,v . By (7) we have that v = 1u.
Because word u is over the alphabet {2, 3} we must have
Wj = Mi01 Mi2 Mi3 . . . Mim
for some indices i1 , i2 , . . . im , with m 1. We see that
u = wi1 wi2 . . . wim
v = 1xi1 xi2 . . . xim
so v = 1u implies that the PCP instance has a solution i1 i2 . . . im .
Remarks: (1) Our proof converted a PCP instance of size k into a set of 2k + 1 matrices. Since the
PCP is undecidable among seven pairs of words, we see that the mortality problem is undecidable
among sets of 15 integer matrices of size 3 3. A more careful analysis of the proof above, in fact,
yields the undecidability of the mortality problem for sets of k + 1 = 8 matrices of size 3 3.
(2) Mortality is undecidable among sets of two 21 21 matrices, and among sets of three 12 12
matrices.
(3) It is not known whether the mortality problem is decidable among sets of 2 2 matrices. For
a set of two 2 2 matrices the problem is decidable.
An interesting decision problem concerning a given single square matrix is the Skolem-Pisot
problem: Given an n n integer matrix M , does there exists k 1 such that the element in the
upper right corner of M k is zero ? This is known to be decidable for matrices of size n 5, but
the decidability status is unknown for larger values of n.
4.9.4
Tiling problems
Wang tiles are unit square tiles with colored edges. Each tile can be represented as a 4-tuple
(N, E, S, W ) where N, E, S and W are the colors of the north, east, south and west sides of the
square. Let T be a finite set of such tiles. A tiling is an assignment t : Z2 T of tiles on the
plane in such a way that the adjacent edges of neighboring tiles have the same color.
Example 136. The tile set
T = {(Green, Green, Red, Red), (Red, Red, Green, Green)}
consists of two tiles
129
Red
Green
Red
Green
Green
Red
Red
Green
Note that the tiles may not be rotated. The tiling problem asks whether a given finite set of tiles
admits at least one valid tiling. This question turns out to be undecidable. Here we, however, only
prove the undecidability of the following variant, called the tiling problem with a seed tile:
Given a finite set T of Wang tiles and a seed tile s T , does there exist a valid tiling of the plane
that contains at least one copy s ?
To prove the undecidability of tiling problems we associate to each Turing machine M a set of
Wang tiles such that valid tilings draw computations according to M . Horizontal rows represent
consecutive IDs.
Theorem 137 The tiling problem with a seed is undecidable.
Proof. For any given Turing machine M = (Q, , , , q0 , B, f ) we effectively construct a Wang tile
set T with s T such that T admits a tiling containing s if and only M does not halt when started
on the blank tape. The undecidability then follows from Corollary 107.
In the following description of the tile set T the colors on the edges are drawn as labeled arrows.
The matching rule is that each arrow head must meet an arrow tail with the same label in the
neighboring tile. Such presentation can easily be converted into colors by identifying each arrow
direction/label -pair by a unique color. Tile set T contains the following three initialization tiles:
B
q0B
The second initialization tile is chosen as the seed tile s. For each tape symbol a there is the
following alphabet tile:
130
For each q, p Q and a, b such that (q, a) = (p, b, L) we include the left action tile
b
p
qa
and for each q, p Q and a, b such that (q, a) = (p, b, R) we have the right action tile
b
p
qa
qa
q
Consider a valid tiling using T that contains a copy of the seed tile s. The horizontal row that
contains the seed is necessarily tiled with the initialization tiles:
B
q0B
The labels on the arrows represent the initial configuration of the Turing machine on the initial
blank tape. Above the seed tile fits only the action tile corresponding to the transition (q0 , B).
The tile next to the action tile is forced to be the merging tile, and the other tiles on this row are
necessarily the alphabet tiles. For instance, if (q0 , B) = (p, a, R) then the following row is forced:
B
pB
p
B
q0B
As a result, the labels on the arrows on the second row represent the configuration of the TM after
one step. Continuing likewise, we see that the rows above the seed row are forced to simulate the
TM step-by-step. Because there is no action tile corresponding to qa if (q, a) is undefined, we
see that the tiling can be completed to the whole plane if and only if the TM does not halt. The
bottom half of the plane can always be filled with the blank tile.
131
4.10
In this section we show that it is undecidable whether a given first-order mathematical statement
concerning natural numbers is true. As a direct consequence we obtain the incompleteness result
of Godel: there are such statements that are true but cannot be proved to be true.
Recall that N = {0, 1, 2, . . .} denotes the set of natural numbers (including 0). Arithmetic
formula we consider may contain variables, natural number constants, operators + and , relation
symbol =, logical connectives , , and quantifiers , . All these items have the usual, well-known
interpretation in N. An occurrence of variable x in a formula is free if it is not within the scope
of a quantifier x or x. A formula that has no free variables is a statement. Each statement is
either true or false in N, using the usual interpretation of the quantifiers and connectives. Formula
that contains free variables becomes true or false when all free variables are given a value from N.
Other symbols (such as relations 6=, < and , and connectives and ) can be used as
shorthand notations as they can be expressed using the basic symbols. For example, x y is a
shorthand notation for z (x + z = y), and represents .
We skip the formal, precise definitions of formulas and their truth values, as these are well
known from other courses. Note that the formulas considered are first-order, meaning that all
variables refer to single natural numbers. No second-order variables representing sets of numbers
are used. We employ the following commonly used precedence rules: the evaluation is done in the
order
1.
2. and
3. and
4. .
In the following, we build complicated formulas step-by-step, using simpler formulas as building
blocks. So we may introduce new relation symbols as shorthand notations. For example, we could
define formula Prime(z) to be
(z > 1) x, y (x, y > 1 xy 6= z),
132
and using this formula we can express the twin prime conjecture as the statement
m z(z > m) Prime(z) Prime(z + 2).
It would be very useful if there would exist an algorithm to determine if a given first-order statement
over N is true or false. But it turns out that such an algorithm does not exist. We prove this fact
using a reduction from the Post correspondence problem. In the proof we use the notations from
Section 4.9.3 on matrix product mortality: We assume a PCP instance over the binary alphabet
= {2, 3}, and for any word w = a1 a2 . . . am over the alphabet {2, 3} we define the natural number
(w) = am + 4am1 + . . . + 4m1 a1 .
(Note: we could equally well use the alphabet {1, 2} and base 3 instead of {2, 3} and base 4, but
for the sake of consistency with Section 4.9.3 we stick to the prior notation.)
Theorem 139 It is undecidable whether a given first-order arithmetic statement is true.
Proof. We reduce the Post correspondence problem. Let
L1 : w1 , w2 , . . . wk
L2 : x1 , x2 , . . . xk
be a given instance of PCP, over the binary alphabet = {2, 3}. In the following, we construct an
arithmetic statement that is true if and only if the PCP instance has a solution. Let us denote, for
all i = 1, . . . , k,
Ai = (wi ),
Bi = 4|wi | ,
Ci = (xi ), and
Di = 4|xi | .
These are natural number constants that can be computed
that these are the elements in the 3 3 matrices
Bi 0
Mi = Mwi ,xi = 0 Di
Ai Ci
0
0
1
we defined in the proof of Theorem 135 on matrix product mortality. Let i1 i1 . . . im be a sequence
of indices, ij {1, 2, . . . , k}. For every j = 0, 1, . . . , m we define the natural numbers
aj
bj
cj
dj
=
=
=
=
(wi1 . . . wij ),
4|wi1 ...wij | ,
(xi1 . . . xij ), and
4|xi1 ...xij | .
bj 0
0 dj
Mi1 Mi1 . . . Mij =
aj cj
133
the product
0
0 .
1
Because
bj1
0
0
Bij
0
dj1 0 0
aj1 cj1 1
Aij
0
Dij
Cij
0
Bij bj1
0 =
0
1
Bij aj1 + Aij
0
Dij dj1
Dij cj1 + Cij
0
0
1
(Start),
(Follow),
Because is injective, the sequence i1 i2 . . . im is a solution to the PCP if and only if the condition
am = cm
(End).
holds. We see that the PCP instance has a solution if and only if numbers aj , bj , cj and dj satisfying
the conditions (First), (Follow) and (End) exist for some choice of m 1 and i1 i2 . . . im . In other
words, the PCP instance has a solution if and only if the statement
m (m 1) a0 , . . . , am b0 , . . . , bm c0 , . . . , cm d0 , . . . , dm
Start(a0 , b0 , c0 , d0 )
j [(j < m) Follow(aj , bj , cj , dj , aj+1 , bj+1 , cj+1 , dj+1 )]
End(am , bm , cm , dm )
(8)
where x mod y denotes the smallest natural number that is congruent to x modulo y. Note that
n = (a, b, i) if and only if
(n < 1 + (i + 1)b) [x a = n + x(1 + (i + 1)b)]
is true. Let us denote this formula by Beta(a, b, i, n).
The idea of the -function is that it can reproduce sequences of numbers from fixed a and b by
varying i. Any desired sequence can be obtained, as stated by the following lemma:
Lemma 140 Let m 1, and let n0 , n1 , . . . nm be arbitrary natural numbers. Then there exist
a, b N such that ni = (a, b, i) for all i = 0, 1, . . . , m.
Proof of the lemma. Let us choose b = m! max{n0 , n1 , . . . , nm }. Let us first show that numbers
1+(i+1)b are pairwise coprime: Let p be a prime number that divides both 1+(i+1)b and 1+(j+1)b
where 0 i < j m. Then p also divides the difference (1 + (j + 1)b) (1 + (i + 1)b) = (j i)b.
Because j i divides b we see that p necessarily divides b, which contradicts the fact that it also
divides 1 + (i + 1)b.
As numbers 1 + (i + 1)b are pairwise coprime, the Chinese remainder theorem provides a natural
number a such that a ni (mod 1 + (i + 1)b), for all i = 0, 1, . . . , m. Because ni b < 1 + (i + 1)b,
we have that ni = (a, b, i).
Now each of the variable sequences in (8) can be replaced by two variables. As we need four
sequences of numbers, we encode them in four pairs: 1 , 2 provide ai s, 1 , 2 provide bi s, 1 , 2
provide ci s, and 1 , 2 provide di s. We obtain the statement
m (m 1) 1 , 2 , 1 , 2 , 1 , 2 , 1 , 2
Beta(1 , 2 , 0, 0) Beta(1 , 2 , 0, 1) Beta(1 , 2 , 0, 0) Beta(1 , 2 , 0, 1)
j [(j < m) a, b, c, d, a0 , b0 , c0 , d0 Follow(a, b, c, d, a0 , b0 , c0 , d0 )
Beta(1 , 2 , j, a) Beta(1 , 2 , j + 1, a0 )
Beta(1 , 2 , j, b) Beta(1 , 2 , j + 1, b0 )
Beta(1 , 2 , j, c) Beta(1 , 2 , j + 1, c0 )
Beta(1 , 2 , j, d) Beta(1 , 2 , j + 1, d0 )]
r Beta(1 , 2 , m, r) Beta(1 , 2 , m, r),
which is true if and only if the PCP instance has a solution. Note that the (Start) condition
Beta(1 , 2 , 0, 0) Beta(1 , 2 , 0, 1) Beta(1 , 2 , 0, 0) Beta(1 , 2 , 0, 1)
just states that
(a0 = 0) (b0 = 1) (c0 = 0) (d0 = 1)
and the (End) condition
r Beta(1 , 2 , m, r) Beta(1 , 2 , m, r)
135
(9)
states that there exists number r that is equal to both am and cm , that is, am = cm . The middle
part of the formula extracts aj , bj , cj , dj , aj+1 , bj+1 , cj+1 and dj+1 into variables a, b, c, d, a0 , b0 , c0 and
d0 , and verifies that the (Follow) condition is satisfied at step j.
The undecidability of determining the truth value of a first order statement now follows from
the undecidability of PCP.
As an immediate corollary we see that that the same problem is not even semi-decidable:
Corollary 141 There is no semi-algorithm to determine if a given first-order arithmetic statement
is true.
Proof. For any statement either is true or is true. If there is a semi-algorithm for true
statements then we can execute this semi-algorithm on inputs and in parallel until we receive
an answer. This provides an algorithm to determine whether is true, contradicting Theorem 139.
The previous corollary has far reaching consequences on the (un)axiomatizability of arithmetics.
The statements that can be deduced from any finite axiom set form a semi-decidable set. Indeed,
to check whether a given statement follows from the axioms, one can enumerate longer and longer
deductions until (if ever) the given statement is obtained. Hence, by the previous corollary, the
sets of provable statements and true statements cannot coincide: One is semi-decidable while the
other one is not. There always remain true statements that have no proof from the axioms. This
is Godels celebrated incompleteness theorem.
More generally, consider any proof system for arithmetics. A reasonable request for such a
system is that proofs are finite sequences of symbols, and that there is an algorithm to verify
that a given proof is a correct proof for a given statement . For any such proof system, there
is a semi-algorithm to determine if a given statement has a correct proof in the system. (Indeed,
enumerate longer and longer strings until a valid proof for is found.) Hence, by the previous
corollary, the sets of true statements and provable statements cannot be identical.
Remark: An important subfamily of first-order arithmetic formulas are diophantine equations.
These are polynomial equations with integer coefficients, and one is interested in finding integer
solutions. For example, x3 +y 3 = z 3 is a diophantine equation with three variables. It has solutions
x = 0, y = z and y = 0, x = z among natural numbers, so the statement x y z x3 + y 3 = z 3 is
true. On the other hand, statement x x2 + 1 = 0 is clearly false in N.
In 1900, D.Hilbert proposed a list of 23 open mathematical problems, that have greatly influenced the mathematical research in the 20th century. The 10th problem in the list asked to device
a process (=algorithm, in modern terms) to determine if a given diophantine equation has an integer solution. The problem is algorithmically equivalent if natural number solutions are required
rather than integer solutions. In 1970, the problem was proved undecidable by Y.Matiyasevich.
This means that there is no algorithm to determine if the first-order formula
xy . . . P (x, y, . . .) = Q(x, y, . . .)
over N is true, where P and Q are given polynomials with natural number coefficients, that is, P
and Q are given terms formed using +, , variables and constants.
136
4.11
So far we have considered decision problems and associated languages. In many setups it would be
useful to have a more complex output from an algorithm than just yes or no. Informally, we
say that a (total) function f : is total computable (or a total recursive function)
if there is an algorithm that produces from any input w the output f (w) . A partial
function f : is partial computable (or a partial recursive function) if there is
a semi-algorithm-like process that produces from input w the output f (w) if f (w) is
defined, and returns no answer if f (w) is not defined.
In applications, algorithms are applied on objects other than words. However, computation is
defined on words and therefore we encode inputs and outputs as words, exactly as we did when
we connected decision problems to languages. Using some fixed encodings, we say that a total (or
partial) function f : D R from domain D to range R is total (partial, respectively) computable
if there is a total (partial, respectively) word function that maps hdi 7 hf (d)i for all d D such
that f (d) is defined, and is undefined on hdi if f is not defined on d. The behavior of the word
function on input words that are not valid encodings is irrelevant. (As we request that the set of
valid encodings is a recursive language, the algorithm that computes the word function hdi 7 hf (d)i
can first recognize whether the input word is a valid encoding or not.)
All reasonable encodings can be effectively converted into each other, so the choice of the
encoding is rather arbitrary. (The fact that encodings hi1 and hi2 of set X can be effectively
converted into each other is simply stating that there are total computable word functions f and
g that map f : hxi1 7 hxi2 and g : hxi2 7 hxi1 for all x X.)
Example 142. If D = R = N we may choose to use the unary encoding hni = an into the alphabet
{a}. In this case, a function f : N N is total computable iff the function an 7 af (n) is a total
computable function a a . If we decided to use the usual binary encoding bin(n) of numbers in
the binary alphabet {0, 1} then exactly same functions f : N N would be computable because
there are total computable conversions bin(n) an between the encodings.
Example 143. Let us define the Busy Beaver -function BB : N N as follows. For any
positive integer n, let T M (n) denote the set of all Turing machines that
(i) have n non-final states (and one final state),
(ii) have two tape symbols (one of which is the blank symbol), and
(iii) eventually halt when started on the blank tape.
In other words, Turing machines in T M (n) are of the form ({q1 , q2 , . . . , qn , f }, {a}, {a, B}, , q1 , B, f )
and they halt when started with the empty input .
The value BB(n) is defined as the maximum number of moves that any machine in T M (n)
makes before halting when started on the blank tape. (And let us define BB(0) = 0 to make the
function total.) It is known that BB(1) = 1, BB(2) = 6, BB(3) = 21 and BB(4) = 107. The
value of BB(n) is not known for larger values of n, but it is known that BB(5) 47176870 and
BB(6) 102879 . The values of the function grow very rapidly.
Let us show that BB : N N is not total computable and that, in fact, for any total
computable g : N N there exists n such that BB(n) > g(n). So the function BB is not
137
bounded by any total computable function g. This is based on the fact that if there were a total
computable g such that BB(n) g(n) for all n N, then one could effectively determine if a given
TM M that satisfies conditions (i) and (ii) above halts when started on the blank tape: One first
computes g(n) and then simulates M for up to g(n) steps. If M does not halt by the g(n)th step,
the machine never halts.
This contradicts the undecidability of the halting problem of Turing machines from the empty
tape, proved in Corollary 107. Actually, we need to know that this undecidability holds even when
the machine considered has only two tape letters:
Lemma 144 It is undecidable whether a given TM with two tape symbols halts when started on
the blank tape.
Proof. We reduce the halting problem of TM with arbitrary tape alphabet, shown undecidable in
Corollary 107. Let M = (Q, , , , q0 , X, f ) be an arbitrary Turing machine, with blank symbol
X. Let us choose m = dlog2 ||e so that there is an injective function g : {a, B}m . We can
choose g in such a way that g(X) = B m . Let us extend g into a homomorphism {a, B} .
We can effectively construct a TM M 0 with tape alphabet {a, B} that simulates M on any
input w as follows. The corresponding input to M 0 is g(w), so all tape symbols of M are encoded
as blocks of length m. To simulate the next move of M , the simulating machine M 0 is located on
the first letter of the block that encodes the symbol currently scanned by M :
M:
X X y
z X X
q
m
M :
BBB
g(y)
g(s)
g(z)
BBB
More precisely, we use Turing machines to define total and partial computable functions f :
. Let M = (Q, , , , q0 , B, qF ) be a Turing machine, with final state qF . We define the
halting ID w corresponding to word w analogously to the initial ID w :
qF w, if w 6= ,
w =
qF B, if w = .
138
u,
if w ` u and u ,
fM (w) =
undefined, otherwise.
In other words, we require the Turing machine to write the output fM (w) on the tape, move on
the first letter of the output and halt in state qF . In all other cases (if M does not halt or halts in
a different ID) the value fM (w) is undefined.
A function f is a partial computable function if there exists a Turing machine M such that
f = fM . If, moreover, f is defined for all w then f is a total computable function.
Example 145. The function an #am 7 an+m and w 7 for w 6 a #a is a total recursive
function {a, #} a . It is computed by the TM described by the diagram
a/a (R)
a/a (R)
#/# (R)
a/a (L)
B/B (L)
a/B (L)
#/B (L)
B/B (L)
#/# (R)
#/a (L)
B/B (L)
a/B (L)
#/B (L)
a/a (L)
a/a (R)
#/# (R)
B/B (R)
B/B (R)
where vertices are the states, and for each transition (q, a) = (p, b, D) there is an edge in the
diagram from vertex q into vertex p labeled by a/b (D). The initial state q0 is indicated by a single
incoming arrow and the final state qF is the one with a double circle.
The idea of the machine is very simple: it first scans the input from left-to-right to check that
the input contains one occurrence of letter # (if not, the machine erases the input and halts), and
then returns to the beginning, replacing the symbol # by the last a of the input.
This example shows that the function (n, m) 7 n + m from N N to N is total computable
when we use the natural encodings hn, mi = an #am and hki = ak for all n, m, k N.
The following observation is easy: Language L is recursive if and only if its characteristic
function L : {0, 1} defined by
0, if w 6 L,
L (w) =
1, if w L
is total recursive. Analogously, L is recursively enumerable iff the partial function obtained
by changing output 0 of L (w) to undefined is partial recursive.
As we know, Turing machines are powerful enough devices to compute anything computable by
other types of algorithms (e.g. arbitrary computer programs). Taking advantage of this observation,
in order to show that a function f is total (or partial) computable, we simply describe from now on
informal algorithms (or semi-algorithms) with outputs, instead of providing exact Turing machines
that compute f .
139
Example 146. Our earlier Example 108 describes a function f : hM i#w 7 hM 0 i that associates
to a Turing machine M and word w a Turing machine M 0 such that L(M 0 ) = {a, b} if w L(M ),
and L(M 0 ) = if w 6 L(M ). The construction of M 0 from given M and w is effective, so the
function f is total computable.
D*
S*
L
f
f
Transitivity of m is obvious:
Lemma 147 If L1 m L2 and L2 m L3 then L1 m L3 .
Proof. Let f1 and f2 be the reductions of L1 to L2 , and L2 to L3 , respectively. Then the composition
w 7 f2 (f1 (w)) is a reduction of L1 to L3 .
Because a reduction is a computable function, if L m K then any algorithm solving the membership problem of K can be used to solve the membership problem of L. This justifies the reduction
method we have used in the undecidability proofs.
Theorem 148 Let L and K be languages such that L m K. Then also \ L m
\ K. Moreover,
if K is recursive then L is recursive,
if K is recursively enumerable then L is recursively enumerable,
if \ K is recursively enumerable then \ L is recursively enumerable.
Proof. The same reduction f that reduces L to K also reduces the complement \ L to the
complement \ K. If K is recursive (or r.e.) then the following algorithm (semi-algorithm,
respectively) determines if given w is in L:
1. Compute u = f (w) where f is the reduction from L to K.
140
More informally, we say that a decision problem A is many-one reducible to a decision problem B if
LA m LB where LA and LB are the languages containing the encodings of the positive instances of
A and B, respectively. In other words, there is an effective conversion that takes positive instances
of A into positive instances of B, and negative instances of A into negative instances of B. Then,
as shown by the previous theorem, if problem A is known not to be decidable (or semi-decidable)
we can conclude that B is not decidable (or semi-decidable) either. All our reductions in prior
sections were of this type.
Example 149. Example 108 provides a many-one reduction from Lu = {hM i#w | w L(M )}
to K = {hM i | L(M ) 6= }. Because the complement of Lu is not r.e., we concluded that the
complement of K is not r.e. either.
We call an r.e language K r.e.-complete if for every r.e. language L we have L m K. In other
words, K is an r.e. language such that all r.e. languages can be many-one reduced to K. We
already know examples of r.e.-complete languages:
Theorem 150 The language Lu = {hM i#w | w L(M )} is r.e.-complete.
Proof. Language Lu is recursively enumerable (Theorem 109). We just need to show that all
r.e. languages can be many-one reduced to Lu . We first observe that any r.e. language L0
over an arbitrary alphabet is many-one reducible to an r.e. language L {a, b} over the
alphabet {a, b}. Such reduction is provided, for example, by choosing any injective homomorphism
h : {a, b} and L = h(L0 ). Function h is clearly total computable and hence a reduction of
L0 into L. Language L is r.e. because the family of r.e. languages is closed under homomorphisms
(homework).
By transitivity of m , is it enough to show that L m Lu . Because L is r.e. there is a TM M
such that L = L(M ). Define the function f that maps w {a, b} into the word hM i#w. Clearly
such f is total computable, and it reduces L into Lu .
Once language Lu is known to be r.e.-complete, many-one reductions can be used to find other
r.e.-complete languages:
Theorem 151 Let L be an r.e.-complete language. If K is an r.e. language such that L m K
then also K is r.e.-complete
Proof. Let M be an arbitrary r.e. language. Then M m L, so by transitivity of m we have
M m K.
We say that a decision problem is r.e.-complete if the encodings of its positive instances form an
r.e.-complete language, that is, if the decision problem is semi-decidable and every semi-decidable
problem can be many-one reduced into it. By the previous theorem, all semi-decidable problems
that we proved undecidable using a many-one reduction from Lu are r.e.-complete. Hence we can
immediately conclude the following:
141
Does a given TM halt when started on the blank initial tape ? is an r.e.-complete decision
problem.
There is a semi-Thue system T and a word u such that individual word problem Does a
given x derive u in T ? is r.e.-complete. There is a Thue system with the same property.
Does a given PCP instance have a solution ? is r.e.-complete.
Questions Is L1 L2 6= for given CFL L1 and L2 ?, Is given CFG G ambiguous ? and
Is L 6= for a given CFL L ? are r.e.-complete.
Does a given Wang tile set not admit a valid tiling that contains a given seed tile ? is
r.e.-complete.
If (not necessarily r.e.) language K has the property that L m K for all r.e. languages L then
we say that K is r.e.-hard. Hence a language is r.e.-complete if and only if it is r.e.-hard and it
is r.e. Clearly a language is r.e.-hard if some r.e.-complete language K can be many-one reduced
into it. An analogous terminology is used on decision problems. The proof of the Rices theorem
shows that all non-trivial questions about r.e. languages are r.e.-hard. We have also shown the
r.e.-hardness of determining whether a given first-order arithmetic statement over N is true.
Remark: Many-one reducibility is only one form of reducibility that can be used to show that
a language is not r.e. Language L is said to be Turing reducible to language K, denoted by
L T K, if there is an algorithm for the membership problem of L that may use as a subroutine
a hypothetical algorithm (=oracle) that solves the membership problem of K. The oracle may
be invoked several times and the input to the oracle may depend on the answers the oracle has
provided on previous queries. Many-one reductions are special types of Turing reductions where the
oracle is invoked only once, and this happens at the end of the algorithm and the answer from the
oracle is directly relayed as the final answer of the algorithm. Still, if L T K and K is known to
be recursive (or r.e) then L has to be recursive (or r.e., respectively) as well. So Turing reductions
can also be used to prove undecidability results.
4.12
We have used Turing machines as the mathematical model to precisely define the concept of decidability/undecidability. In this last section we briefly describe (without detailed proofs) some other
simple devices that are computationally as powerful as Turing machines.
4.12.1
Counter machines
Z, if n = 0,
sign(n) =
P, if n > 0.
142
=
=
=
=
=
=
(q1 , 1, 0)
(q2 , 0, 0)
(f, 0, 0)
(q0 , 1, +1)
(q2 , +1, 1)
(q0 , 0, 0)
In the transitions, symbol is used to indicate that both Z and P are allowed. This machine
recognizes the set L(M ) = {2k | k 0}. The idea is that the first counter value is divided by two
(using a loop in which counter 1 is decremented twice and counter 2 is incremented once), and this
is repeated until an odd value is reached. The number is accepted iff this odd value is one. For
example,
(q0 , 4, 0) ` (q1 , 3, 0) ` (q0 , 2, 1) ` (q1 , 1, 1) ` (q0 , 0, 2) ` (q2 , 0, 2) ` (q2 , 1, 1) ` (q2 , 2, 0)
` (q0 , 2, 0) ` (q1 , 1, 0) ` (q0 , 0, 1) ` (q2 , 0, 1) ` (q2 , 1, 0)
` (q0 , 1, 0) ` (q1 , 0, 0) ` (f, 0, 0).
143
Example 153. The following 4-counter machine recognizes the prime numbers: M = (Q, 4, , q0 , f )
where Q = {q0 , . . . , g7 , f } and the transition function is
(q0 , P, Z, Z, Z)
(q1 , P, P, Z, Z)
(q2 , P, , , )
(q2 , Z, , , )
(q3 , , , , P )
(q3 , , , , Z)
(q4 , , P, P, )
=
=
=
=
=
=
=
(q1 , 1, +1, 0, 0)
(q2 , +1, +1, 0, 0)
(q2 , 1, 0, +1, +1)
(q3 , 0, 0, 0, 0)
(q3 , +1, 0, 0, 1)
(q4 , 0, 0, 0, 0)
(q4 , 0, 1, 1, +1)
(q4 , , Z, P, )
(q5 , , , , P )
(q5 , , , , Z)
= (q5 , 0, 0, 0, 0)
= (q5 , 0, +1, 0, 1)
= (q4 , 0, 0, 0, 0)
(q4 , , P, Z, )
(q6 , , , , P )
(q6 , , , , Z)
= (q6 , 0, 0, 0, 0)
= (q6 , 0, +1, 0, 1)
= (q2 , 0, +1, 0, 0)
(q4 , , Z, Z, ) = (q7 , 0, 0, 0, 0)
(q7 , P, Z, Z, P ) = (q7 , 1, 0, 0, 1)
(q7 , Z, Z, Z, Z) = (f, 0, 0, 0, 0)
First, the machine verifies that the input n is at least 2, and it initializes the second counter to
value 2:
(q0 , n, 0, 0, 0) ` (q1 , n 1, 1, 0, 0) ` (q2 , n, 2, 0, 0).
In state q2 , the value of counter 1 is copied to counter 3:
(q2 , n, m, 0, 0) ` (q2 , 0, m, n, n) ` (q3 , 0, m, n, n) ` (q3 , n, m, n, 0) ` (q4 , n, m, n, 0).
In state q4 , the content of counter 2 is repeatedly subtracted from counter 3 as many times as
possible:
(q4 , n, m, k, 0) ` (q4 , n, 0, km, m) ` (q5 , n, 0, km, m) ` (q5 , n, m, km, 0) ` (q4 , n, m, km, 0).
In the last round, when counter 3 becomes zero we have reached some ID (q4 , n, m x, 0, x). The
next action depends on whether counter 2 became zero at the same time:
If counter 3 becomes zero first, then m does not divide evenly n. The machine increases m
by one and repeats everything from state q2 :
(q4 , n, m x, 0, x) ` (q6 , n, m x, 0, x) ` (q6 , n, m, 0, 0) ` (q2 , n, m + 1, 0, 0).
If counters 2 and 3 become zero at the same time then m divides n. Number n is a prime
number if and only if m = n:
(q4 , n, 0, 0, m) ` (q7 , n, 0, 0, m) ` (q7 , n m, 0, 0, 0).
This leads to (f, 0, 0, 0) iff n m = 0.
Counter machines recognize sets of numbers rather than words. But words can be encoded as
numbers (in various ways). The following definition is robust to changes in the encoding used,
as long as they remain effectively equivalent: We say that A N is recursive (or recursively
enumerable or r.e.-complete) if the language {an | n A} is recursive (recursively enumerable or
r.e.-complete, respectively). It is clear that all sets recognized by counter machines are recursively
enumerable. It turns out that there are counter machines with just two counters that recognize
r.e.-complete sets. Two counters are sufficient to simulate any Turing machine.
144
r0 r1 r2
r0 r1 r2
In the counter machine, the move of the read/write-head to the left corresponds to dividing and
multiplying by two the numbers n and m, respectively. The least significant bit l0 of n is the new
tape symbol to be scanned, and the newly written tape symbol b should be written as the least
least significant bit of the new value m. So, the new ID after the move should be
((p, l0 ), n0 , m0 , 0)
145
Remarks. The simulation of a Turing machine by a 3-counter machine presented above is quite
efficient: The time to simulate one move of the TM is proportional to the length of the non-blank
part of the tape. In contrast, the simulation by a 2-counter machine is exponentially slower.
4.12.2
Fractran
J.Conways Fractran is a fun variant of counter machines. Fractran programs consist of finite lists
n1
,
m1
n2
,
m2
n3
,
m3
...,
nk
mk
of rational numbers. Let n N be any natural number. In one step, the number gets multiplied
ni
ni
by the first fraction m
in the list such that n m
is an integer. If no such fraction exists then the
i
i
system halts.
146
847
,
45
143
,
6
7
,
3
10
,
91
3
,
7
36
,
325
1
,
2
36
5
Starting from n = 10, the first multiplication is by 12 , giving 5. This is then multiplied by
36. Continuing likewise, we get the iteration
1/2
36/5
143/6
3/11
143/6
3/11
7/3
7/3
10/91
36
5 ,
giving
10/91
It is relatively easy to simulate any counter machine using Fractran. The idea is the same that
we used when converting a 3-counter machine into a 2-counter machine: We associate separate
prime numbers to different counters, and the counter values are represented as powers of those
primes. Yet one more prime is used to represent the state of the counter machine. More precisely,
to simulate a 2-counter machine with state set Q = {s0 , s1 , . . . , sk }, we choose 6 distinct prime
numbers p, q, r and p0 , q 0 , r0 . We assume that s0 = f is the final state, and that the counter machine
halts if and only if its state is s0 . (To have this condition satisfied we can simply add a dummy
state that is entered from all halting IDs that are not accepting.)
The ID (si , n, m) of the 2-counter machine is represented by the natural number ri pn q m . Four
Fractran instructions are associated to each non-final state si : These have denominators ri pq,
ri p, ri q, and ri , in this order, corresponding to transitions (si , P, P ), (si , P, Z), (si , Z, P ) and
(si , Z, Z). To make notations simpler, let us use the inverse sign-function and define
(Z) = 0,
(P ) = 1.
Now we can write that the denominator of the Fractran-instruction corresponding to (si , x, y) is
ri p(x) q (y) .
The instructions for different non-final states si are ordered from the largest i = k to the smallest
i = 1. So we get 4k instructions whose denominators are
rk pq, rk p, rk q, rk ;
...
It is easy to see that the first denominator in this list that divides ri pn q m is the one that corresponds
to transition (si , sign(n), sign(m)).
The numerators are designed so that they give the result of the transition, but we have to use
marked versions of the primes because otherwise identical primes would be canceled in the fraction.
The fraction that corresponds to an arbitrary transition (si , x, y) = (sj , d, e) is
r0j p0(x)+d q 0(y)+e
.
ri p(x) q (y)
An application of this instruction changes the state into (the marked version) r0j and changes the
number of ps and qs as indicated by the values d and e in the counter machine instruction.
147
Remark: The simulation of a k-counter machine by Fractran is analogous, for any k. One just
uses two additional primes pc and p0c for each counter c. Because 3-counter machines admit an
efficient simulation of Turing machine, we also obtain an efficient simulation by Fractran: The time
to simulate one Turing machine move is proportional to the size of the non-blank part of the tape.
Example 157. Consider the 2-counter machine M = ({s0 , s1 , s2 }, 2, , s1 , s0 ) with transitions
(s2 , P, ) = (s1 , 1, +1)
(s2 , Z, ) = (s2 , 0, 0)
(s1 , P, ) = (s2 , 1, 0)
(s1 , Z, ) = (s0 , 0, 0)
5
,
7
11
,
13
3 132
,
22 5 11
3 13
,
22 5
32 13
,
22 11
32
,
22
32 13
,
2 5 11
32
,
25
13
,
2 11
1
.
2
The five step computation from the initial ID (s1 , 4, 0) into (s0 , 0, 2) in the counter machine corresponds to the Fractran computation
2 54 32 53 2 3 53
22 53 3 13 52 2 13 52
2 11 52 32 13 5 2 3 13 5 22 13 5
22 11 5 3 132 2 132 2 11 13
2 112 11 13
112
4.12.3
Tag systems
Posts tag systems are yet another class of simple systems capable of simulating arbitrary Turing
machines. A tag system is a triple T = (, k, g) where is a finite alphabet, k 1 is the deletion
number and g : is a function assigning a word to each letter in . Such system is called
148
a k-tag system. This is a deterministic rewrite system. Any word u whose length is at least
k gets rewritten as follows: Let u = avw where a and |av| = k. Then
u wg(a).
In other words, the first k letters of u are erased and the word g(a) is appended to the end where
a is the first letter of u. The rewriting terminates if a word is reached whose length is less than k.
Example 158. Consider the 3-tag system ({0, 1}, 3, g) where g(0) = 00 and g(1) = 1101. This
is a system studied (and found intractable) by E.Post. For example, starting from word 10010 we
obtain
10010
101101
1011101
11011101
111011101
0111011101
101110100
1101001101
10011011101
110111011101
1110111011101
01110111011101
1011101110100
11011101001101
111010011011101
0100110111011101
011011101110100
01110111010000
1011101000000
11010000001101
100000011011101
0000110111011101
011011101110100
The last word in the sequence already appeared 6 steps before, so we enter a loop of length 6. On
the other hand, starting from 100100100000 one reaches in 419 steps the halting word 00.
Let us denote by L(T ) the set of words that eventually halt (i.e., evolve into a word of length less
than k) in the k-tag system T . Clearly L(T ) is recursively enumerable. Without a proof we state
the following result:
Theorem 159 There is a 2-tag system T such that the language L(T ) is r.e.-complete.
Remarks: (1) Sometimes tag systems are defined slightly differently, as follows: the successor of a
non-empty word u is obtained by first appending g(a) to the end, corresponding to the first letter
149
a of u. If the result ug(a) is shorter than the deletion number k the process halts; otherwise the
first k letters of ug(a) are erased. In this way, even words shorter than k may have a successor.
(2) Another natural decision problem associated to a tag-system is the word problem, asking
whether a given word x derives another given word y. Clearly, the halting problem of any tagsystem can be Turing reduced to its word problem.
(3) The tag system of Example 158 is still a mystery. It is not known whether its word problem is
decidable or not. It is not even known whether there exists any word that neither eventually halts
nor enters a cycle. (If one these always happens then the word problem is of course decidable.)
150