Complexity Theory - Chapter 1 - Introduction
Complexity Theory - Chapter 1 - Introduction
Turing Machine(TM)
1 Introduction to Computational Complexity
Throughout history people had a notion of a process of producing an output from a set of
inputs in a finite number of steps, and they thought of “computation” as “a person writing numbers
on a scratch pad following certain rules.”
After their success in defining computation, researchers focused on understanding what
problems are computable. They showed that several interesting tasks are inherently uncomputable:
No computer can solve them without going into infinite loops (i.e., never halting) on certain inputs.
Computational complexity theory focuses on issues of computational efficiency — quantifying the
amount of computational resources required to solve a given task. We will quantify the efficiency of
an algorithm by studying how its number of basic operations scales as we increase the size of the
input.
Consider the following hypothetical question: First, Is it harder to multiply than to add?
Second, why? We can see that there is no algorithm for multiplication computationaly as simple as
that of addition, and this proves of something a stumbling block. For multiplication of two n-digit
numbers there are two methods (algorithms): Repeated addition and Grade-School algorithm. For
this discussion, let the basic operations be addition and multiplication of single digits. (In other
settings, we may wish to throw in division as a basic operation.)
Using repeated addition: to compute a · b, just add a to itself b − 1 times. The grade-school
algorithm for computing 577 · 423 is illustrated below:
577
423
1731
1154
2308
244071
Though the repeated addition algorithm is perhaps simpler than the grade-school algorithm,
we somehow feel that the latter is better. Indeed, it is much more efficient. For example,
multiplying 577 by 423 using repeated addition requires 422 additions, whereas doing it with the
grade-school algorithm takes 3 multiplications of a number by a single digit and 3 additions.
In the case of multiplication and addition the size of the input is the number of digits in the
numbers. The number of basic operations used to multiply two n-digit numbers (i.e., numbers
between 10n−1and 10n) is at most 2n2 for the grade-school algorithm and at least n10n−1for repeated
addition.
The huge difference between the two algorithms is apparent: Even for 11-digit numbers, a
simple pocket calculator running the grade-school algorithm would beat the best supercomputer
running the repeated addition algorithm. We see that the efficiency of an algorithm is to a
considerable extent much more important than the technology used to execute it.
Computation is a mathematically precise notion. We will typically measure the
computational efficiency of an algorithm as the number of basic operations it performs as a function
of its input length. That is, the efficiency of an algorithm can be captured by a function T from the
1
set N of natural numbers to itself, i.e., T: N→N, such that T(n) is equal to the maximum number of
basic operations that the algorithm performs on inputs of length n. For the grade-school algorithm
we have at most T(n) = 2n2and for repeated addition at least T(n) = n10n-1.
Throughout history people have been solving computational tasks using a wide variety of
methods, ranging from intuition and “eureka” moments to mechanical devices such as abacus or
sliderules to modern computers. How can we find a simple mathematical model that captures all of
these ways to compute?
Surprisingly enough, it turns out that there is a simple mathematical model that suffices for
studying many questions about computation and its efficiency—the Turing machine. It suffices to
restrict attention to this single model since it seems able to simulate all physically realizable
computational methods with little loss of efficiency. Thus the set of “efficiently solvable”
computational tasks is at least as large for the Turing machine as for any other method of
computation. One possible exception is the quantum computer model, but we do not currently
know if it is physically realizable. In the following sections we discuss about the theoretical
computational model of computing – the Turing Machine(TM).
.... □ □ a b a a □ □ .....
Tape
Read/Write head
(Moves in both directions)
q0
h
Finite Control
q1
q3
q2
Figure 1: A Turing Machine consisting of a Tape, R/W head and a finite control.
3
is a finite nonempty set of tape symbols;
□ is the blank symbol; □
is a nonempty set of input symbols and is subset of and □ ;
s is the initial state; s Q
H is the set of halting/final states; H ⊆
δ is the transition function, is a function from Q to Q {L, R}, that is
a mapping (q, x) onto (q', y, D) where D denotes the direction of movement
of R/W head: D = L or R according as the movement is to the left or right.
Note: (1) The acceptability of a string is decided by the reach ability from the initial state to some
final state. So the final states are also called the accepting states.
(2) may not be defined for some elements of Q .
Example-1: Consider the following Turing Machine
M = (Q, , , δ, □, s, H), where
Q = {q0, q1, h},
a, □
∑ = {a},
s = q0,
H = {h},
And δ (the transition function) is given by the following table.
q, σ δ(q, σ)
q0 a (q1, □, R)
q0 □ (h, □)
q1 a (q0, □, R )
q1 □ (q0, □,L)
Table 1: for the transition table
When M is started in its initial state q0, it scans its head to the right, changing all a's to □'s as it
goes, until it finds a tape square already containing □; then it halts. (Changing a nonblank symbol to
the blank symbol will be called erasing the nonblank symbol.) To be specific, suppose that M is
started with its head scanning the first of four a's, the last of which is followed by a □. Then M will
go back and forth between states q0 and q1 four times, alternately changing an a to a □ and moving
the head right; the first and fourth lines of the table for δ are the relevant ones during this sequence
of moves. At this point, M will find itself in state q0 scanning □ and, according to the second line of
the table, will halt.
Example2: Consider the Turing machine defined by
4
And
If this Turing machine is started in state q0 with the symbol a under the read-write head, the
applicable transition rule is δ (q0,a)= (q0,b,R). Therefore, the read-write head will replace a with b,
then move right on the tape. The machine will remain in state q0. Any subsequent a will also be
replaced with a b, but b's will not be modified. When the machine encounters the first blank, it will
move left one cell, and then halt in final state q1.
The figure 2 below shows several stages of the process for a simple initial configuration.
5
Figure 3: Transition graph
We now formalize the operation of a Turing machine. As we have the “productions” as the
control theme of a grammar, the “transitions” or “moves” are the central theme of a Turing
machine. These transitions are given as a table or list of 5-tuples, where each tuple has the form:
(current-state, symbol-read, symbol-written, direction, next-state)
Creating such a list is called “implementation description” or “programming” of a Turing machine.
A Turing machine is often defined to start with the read/write head positioned over the first
(leftmost) input symbol.
To specify the status of a Turing machine computation, we need to specify the state, the
contents of the tape, and the position of the head. Since all but a finite initial portion of the tape will
be blank, the contents of the tape can be specified by a finite string. These considerations lead us to
the following ways of describing Turing Machines.
Since one can make several different definitions of a Turing machine, it is worthwhile to summarize
the main features of our model, which we will call a standard Turing machine:
1. The Turing machine has a tape that is unbounded in both directions, allowing any number of
left and right moves.
2. The Turing machine is deterministic in the sense that δ defines at most one move for each
configuration.
3. There is no special input file. We assume that at the initial time the tape has some specified
content. Some of this may be considered input. Similarly, there is no special output device.
Whenever the machine halts, some or all of the contents of the tape may be viewed as
output.
6
the entire input string is split as x1x2 , the first symbol of x2 is the current symbol a under the R/W
head and x2 has all the subsequent symbols of the input string, and the string x2 is the substring of
the input string formed by all the symbols to the left of a.
Example: A snapshot of Turing machine is shown in figure below. Obtain the instantaneous
description.
x1 x2
..... □ □ a4 a1 a2 a1 a2 a2 a a4 a2 □ □ .......
Tape
R/W head
State
q
a4a1a2a1a2a2 q a a4a2
Figure 5: Representation of ID
Note: (1) For constructing the ID, we simply insert the current state in the input string to the left of
the symbol under the R/W head.
(2) We observe that the blank symbol may occur as part of the left or right substring.
The instantaneous description gives only a finite amount of information to the right and left of the
read-write head. The unspecified part of the tape is assumed to contain all blanks; normally such
blanks are irrelevant and are not shown explicitly in the instantaneous description. If the position of
blanks is relevant to the discussion, however, the blank symbol may appear in the instantaneous
description. For example, the instantaneous description q ω indicates that the read-write head is on
the cell to the immediate left of the first symbol of w and that this cell contains a blank.
Moves in Turing Machine
As in the case of pushdown automata, δ(q, x) induces a change in ID of the Turing Machine.
We call this change in ID a move.
Suppose δ(q, xj) = (p, y, L). The input string to be processed is x1x2 . . . xn, and the present
symbol under the R/W head is xi. So the ID before processing xiis
x1 x2 . . . xi-1 q xi . . . xn
After processing xi, the resulting ID is
7
x1 x2 . . . xi-2 p xi-1 y xi+1. . . xn
This change of ID is represented by
x1 x2 . . . xi-1 q xi . . . xn ├ x1 x2 . . . xi-2 p xi-1 y xi+1. . . xn
If i = 1, the resulting ID is py x2 x3. . . xn .
Suppose δ(q, xj) = (p, y, L), then the change of ID is represented by
x1 x2 . . . xi-1 q xi . . . xn ├ x1 x2 . . . xi-1 y p xi+1. . . xn
If i = n, the resulting ID is x1 x2 . . . xn-1 y p □ .
We can denote an ID by Ij for some j. Ij├ Ik defines a relation among IDs. So the symbol ├*
denotes the reflexive – transitive closure of the relation ├. In particular, Ij├* Ij. Also, if I1├* In,
then we can split this as I1 ├ I2 ├ I3 ├ . . . . ├ In for some IDs, I2, . . . , In-1.
Note: The description of moves by IDs is very much useful to represent the processing of input
strings.
Example 3: Consider the TM description given in Table 1. Draw the computation sequence of
the input string 00.
Solution:
8
We describe the computation sequence in terms of the contents of the tape and the current
state. If the string in the tape is a1a2 . . . aj aj+1 . . . am and the TM in state q is to read aj+ 1, then we
write a1a2 . . . aj q aj+1 . . . am
For the input string 00□, we get the following sequence:
q100□├ 0q10□├ 00q1□├ 0q201├ q2001
├ q2□001├□q3001 ├□□q401├ □□0q41├ □□01q4□
├ □□010q5├□□01q200├□□0q2100├ □□q20100
├ □q2□0100 ├ □□q30100 ├ □□□q4100 ├ □□□1q400
├ □□□10q40 ├ □□□100q4□ ├ □□□1000q5□
├ □□□100q200 ├ □□□10q2000 ├ □□□1q20000
├ □□□q210000 ├ □□q2□10000 ├ □□□q310000 ├ □□□□q50000
(□, □, R)
(y, y, R)
(y, y, R) (y, y, L)
(x, x, R) (□,□,R)
(0, x, R)
q2 q3 q5 q6
q1
(1, y, L)
9
Solution: The initial tape input is □0011□. Let us assume that M is in state qj and the R/W head
scans 0 (the first 0). We can represent this as in Figure 5.
□ 0 0 1 1 □
Tape
R/W head
State
q
Figure 5: TM processing 0011
The figure can be represented by
□0011□
q1
From Figure 4 we see that there is a directed edge from q1 to q2 with the label (0. x, R). So
the current symbol 0 is replaced by x and the head moves right. The new state is q2. Thus. we get
□x 011□
q2
The change brought about by processing the symbol 0 can be represented as
(0, x, R)
□0011□ □x011□
q1 q2
The entire computation sequence reads as follows:
(0, x, R) (0, 0, R)
□0011□ □x 011□ □x 011□
q1 q2 q2
(y, y, R) (□, □,
□xxyy□ □xxyy□□
q5 q6
10
3 Construction of Turing Machine(TM)
Designing a Turing machine to solve a problem is an interesting task. It is somewhat similar
to programming. Given a problem, different Turing machines can be constructed to solve it. But we
would like to have a Turing machine which does it in a simple and efficient manner. Like we learn
some techniques of programming to deal with alternatives, loops etc, it is helpful to understand
some techniques in Turing machine construction, which will help in designing simple and efficient
Turing machines. It should be noted that we are using the word ‘efficient’ in an intuitive manner
here.
11
Finite
Control
..... .....
..... .....
...... ......
Figure 6: Multitape Turing machine
In a typical move:
(i) M enters a new state.
(ii) On each tape, a new symbol is written in the cell under the head.
(iii) Each tape head moves to the left or right or remains stationary. The heads move
independently: some move to the left, some to the right and the remaining heads
do not move.
The initial ID has the initial state q0, the input string w in the first tape (input tape), empty
strings of b's in the remaining k - 1 tapes. An accepting ID has a final state, some strings in each of
the k tapes.
“Multi-tape Turing Machines are equivalent to Standard Turing Machines”.
Excercise: Prove that, Every language accepted by a multitape TM is acceptable by some
single-tape TM (that is, the standard TM).
Excercise: Prove that, If M1 is the single-tape TM simulating multitape TM M, then the time
taken by M1 to simulate n moves of M is (n2).
12
Note: If q Q and x and (q, x) = {(ql, y1, D1), (q2, y2, D2), . . ., (qn, yn, Dn)} then the NDTM
can chose anyone of the actions defined by (qi, yi, Di) for i = 1, 2, . . ., n.
We can also express this in terms of ├ (move) relation. If (q, x) = {(qi, yi, Di) i = 1, 2, . . .,
n} then the ID zqxw can change to anyone of the n IDs specified by the n-element set (q, x).
Suppose (q, x) = {(q1, y1, L), (q2, y2, R), (q3, y3, L)}. Then
z1z2 . . . zkqxzk+1 . . . zn├z1z2 . . . zk-1q1zky1zk+1. . . zn
or z1z2 . . . zkqxzk+1 . . . zn├ z1z2 . . . zky2q2zk+1. . . zn
or z1z2 . . . zkqxzk+1 . . . zn├ z1z2 . . . zk-1q3zky3zk+1. . . zn
So on reading the input symbol, the NDTM M whose current ID is z1z2 . . . zkqxzk+1 . . . zn
can change to any one of the three IDs given earlier.
Remark: When (q, x) = {(qi, yi, Di) i = 1, 2, . . ., n} the NDTM chooses any one of the n
triples totally (that is, it cannot take a state from one triple, another tape symbol from a second triple
and a third D(L or R) from a third triple, etc.)
Definition: w is accepted by a nondeterministic TM M if q0w├* xqfy for some final
state qf.
Note: As in the case of NDFA, an ID of the fonn xqy (for some q F) may be reached as the result
of applying the input string w. But w is accepted by M as long as there is some sequence of
moves leading to an ID with an accepting state. It does not matter that there are other
sequences of moves leading to an ID with a nonfinal state or TM halts without processing
the entire input string.
Excercise: Prove that, If M is a nondeterministic TM, there is a deterministic TM M1 such that
T(M) = T(M1).
Definition: Let M be a TM and w an input string. The running time of M on input w, is
the number of steps that M takes before halting. If M does not halt on an input string w, then the
running time of M on w is infinite.
Note: Some TMs may not halt on all inputs of length n. But we are interested in computing the
running time, only when the TM halts.
Definition: The time complexity of TM M is the function T(n), n being the input size,
where T(n) is defined as the maximum of the running time of M over all inputs w of size n.
Techniques in Construction of Turing Machines
The basic guidelines for designing a Turing machine are:
i) The fundamental objective in scanning a symbol by the R/W head is to know what
to do in the future. The machine must remember the past symbols scanned. The
Turing machine can remember this by going to the next unique state.
ii) The number of states must be minimized. This can be achieved by changing the
states only when there is a change in the written symbol or when there is a change
in the movement of the R/W head.
13
Exercises
1. Design a Turing machine to recognize all strings consisting of an even number of 1's.
2. Design a Turing machine over {1, □}which can compute a concatenation function over Σ =
{1}. If a pair of words (w1, w2) is the input. the output has to be w1w2.
3. Design a TM that accepts {0n1n | n ≥ 1}.
4. Design a Turing machine M to recognize the language {1n2n3n | n ≥ 1}.
5. Design a Turing machine that accepts the set of all even palindromes over {0,1}.
6. Design a Turing machine to add two given integers.
7. Design a TM that copies strings of l's? Find the computation sequence for the input w = 111.
8. Design a TM that reads a string in {0, 1}* and erases the rightmost symbol.
14