• No results found

6 Binary Addition DTM

Complexity theory

Algorithm 2. 6 Binary Addition DTM

The set of states is= {γ0, γ1, . . . , γ24}the starting state isγ0, the only halting state isγ24. The alphabet is= {∗,0,1,+,=}.

Input: integersab≥0 in binary with+,=. (For example to compute 31+18 we would write ‘=11111+10010’ on the machine’s tape, with the symbol ‘=’ in the starting square.)

Output:a+bin binary.

(γ0,=, γ1,=,→) # move the head to the right end of the input (γ1,0/1/+, γ1,s,→) #

(γ1,∗, γ2,∗,←) # found end of input

(γ2,0, γ3,∗,←) # the least significant bit ofbis 0 (γ2,1, γ4,∗,←) # the least significant bit ofbis 1 (γ2,+, γ5,+,←) # no more bits ofb

(γ3/γ4,0/1,s,s,←) # keep moving left until we have finished readb

(γ3,+, γ5,+,←) # finished readingb

20 2 Complexity theory

(γ5,=, γ23,∗ →) # no more bits ofaerase= (γ5/γ6,∗,s,∗,←) # moving left looking fora

(γ5,0, γ7,∗,←) # sum of least significant bits ofaandbis 0 (γ5,1, γ8,∗,←) # sum of least significant bits ofaandbis 1 (γ6,0, γ8,∗,←) # sum of least significant bits ofaandbis 1 (γ6,1, γ9,∗,←) # sum of least significant bits ofaandbis 2 (γ7/γ8/γ9,0/1,s,s,←) # moving left looking for=

(γ7,=, γ10,=,←) # finished readinga, found= (γ8,=, γ11,=,←) #

(γ9,=, γ12,=,←) #

(γ10/γ11/γ12,0/1, # moving left looking for the end of the answer s,s,←)

(γ10,∗, γ13,∗,→) # finished reading answer, now find the carry bit (γ11,∗, γ14,∗,→) #

(γ12,∗, γ15,∗,→) #

(γ13,0, γ16,0,←) # carry bit and least sig bits ofaandbsum to 0 (γ13,1, γ16,1,←) # carry bit and least sig bits ofaandbsum to 1 (γ14,0, γ16,1,←) # carry bit and least sig bits ofaandbsum to 1 (γ14,1, γ17,0,←) # carry bit and least sig bits ofaandbsum to 2 (γ15,0, γ17,0,←) # carry bit and least sig bits ofaandbsum to 2 (γ15,1, γ17,1,←) # carry bit and least sig bits ofaandbsum to 3 (γ13,=, γ18,=,←) # first part of answer is 0

(γ14,=, γ19,=,←) # first part of answer is 1

(γ15,=, γ20,=,←) # first part of answer is 0 and carry bit is 1 (γ16,∗, γ21,0,→) # set carry bit to 0 and now return to start (γ17,∗, γ21,1,→) # set carry bit to 1 and now return to start (γ18,∗, γ16,0,←) # first part of answer is 0

(γ19,∗, γ16,1,←) # first part of answer is 1

(γ20,∗, γ17,0,←) # first part of answer is 0 and carry bit is 1 (γ21,0/1/=/∗, γ21, # return to start

s,→)

(γ21,+, γ22,+,→) # finished rereadinga, found+ (γ22,0/1, γ22,s,→) # now rereadingb

(γ22,∗, γ2,∗,←) # reached start of the input (γ23,∗, γ23,∗,→) # keep moving right (γ23,+, γ24,∗,→) # erase+and halt

2.2 Deterministic Turing machines 21

Inputa,b Unary machine steps Binary machine steps

10 22 <200

1000 2000 <900

106 2×106 <3000

2100 2.5×1030 <65 000

Fig. 2.3 Comparison of running times of unary and binary addition DTMs.

One obvious difference between our two DTMs is that using binary encoding for the input results in a far more complicated machine, but which is more efficient? If the binary addition DTM is given inputab≥0, wherea is a

k-bit integer, then it is reasonably easy to see that the machine takes at most 2k+3 steps before the read–write head is positioned on the rightmost non- blank symbol and the control unit is in stateγ2. The machine then takes at most 6(k+2) steps before it is again in stateγ2 and the read–write head is again scanning the rightmost non-blank symbol. The machine does thisktimes, once for each bit ina. Finally it erases the equals and plus signs. In total it takes less than 6(k+2)2 steps. For large inputs this machine is clearly much more

efficient as the table in Figure 2.3 shows.

Having compared the running time of these two machines we introduce the formal definitions of time complexity.

Time complexity

If a DTM halts on inputx0, then itsrunning timeon inputxis the number of steps the machine takes during its computation. We denote this bytM(x).

Recall that we wish to assess the efficiency of an algorithm in terms of its worst-case behaviour. For this reason we define thetime complexityof a DTM

M that halts on every inputx0∗, to be the functionTM:N→Ngiven by

TM(n)=max

t |there existsx0nsuch thattM(x)=t.

In practice we will rarely want to work directly with Turing machines. Higher level descriptions of algorithms, such as the binary addition algorithm given in Algorithm 2.3, are much easier to use. However, if our model of computation is to be robust then a high-level algorithm should have a running time (measured in terms of the number of ‘basic operations’ it performs) that is similar to the running time of a DTM implementation of the same algorithm. To make this precise we need to be clear as to what we mean by ‘similar’.

We will consider the running times of different algorithms to be similar if they differ only by a polynomial factor. Consider the example of binary addition. In our high-level version, Algorithm 2.3, the running time on inputabwas

22 2 Complexity theory

O(loga) while for our DTM the running time wasO(log2a). Thus, for this example at least, our model is robust.

Since we consider an algorithm to be practical if and only if it has polynomial running time, our assumption that the DTM model of computation is robust can be phrased as follows.