Строительный блокнот  Automata methods and madness 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 [ 161 ] 162 163 164 165 166 167 168 169 170 171 172 173 174 175

randomization included is O(nlogn). However, since by the tiniest of chances each of the pivot choices could take the largest or smallest element, the worst-case running time of Quicksort is still O(n). Nevertheless, Quicksort is still the method of choice in many applications (it is used in the UNIX sort command, for example), since its expected running time is really quite good compared with other approaches, even with methods that are 0{n log n) in the worst case.

11.4.2 A Turing-Machine Model Using Randomization

To represent abstractly the ability of a Turing machine to make random choices, much like a program that calls a random-number generator one or more times, we shall use the variant of a multitape TM suggested in Fig. 11.6. The first tape holds the input, as is conventional for a multitape TM. The second tape also begins with nonblanks in its cells. In fact, in principle, its entire tape is covered with Os and Is, each chosen randomly and independently with probability 1/2 of a 0 and the same probability of a 1. We shall refer to the second tape as the random tape. The third and subsequent tapes, if used, are initially blank and are used as scratch tapes by the TM if needed. We call this TM model a randomized Turing machine.


Randombits ... 00101000101001000010091111

Scratch tape(s)

Figure 11.6: A Turing machine with the capability of using randomly generated numbers

Since it may not be realistic to imagine that we initialize the randomized TM by covering an infinite tape with random Os and Is, an equivalent view of this TM is that the second tape is initially blank. However, when the second head is scanning a blank, an internal coin flip occurs, and the randomized



TM immediately writes either a 0 or a 1 on the tape cell scanned and leaves it there forever without change. In that way, there is no work - certainly not infinite work - done prior to starting the randomized TM. Yet the second tape appears to bo covered with random Os and Is, since those random bits appear wherever the randomized TMs second tape head actually looks.

Example 11.12: We can implement the randomized version of Quicksort on a randomized TM. The important step is the recursive process of taking a sublist, which we assume is stored consecutively on the input tape and delineated by markers at both ends, picking a pivot at random, and dividing the sublist into low and high sub-sublists. The randomized TM does as follows:

1. Suppose the sublist to be divided is of length m. Use about О (logm) new random bits on the second list to pick a random number between 1 and m; the mth element of the sublist becomes the pivot. Note that we may not be able to choose every integer between 1 and m with absolutely equal probability, since m not be a power of 2. However, if we take, say [2 log2 ml bits from tape 2, think of it as a number in the range 0 to about m, take its remainder when divided by m, and add 1, then we shall get all numbers between 1 and m with probability that is close enough to 1/m to make Quicksort work properly.

2. Put the pivot on tape 3.

3. Scan the sublist delhieated on tape 1, copying those that are no greater than the pivot to tape 4.

4. Again scan the sublist on tape 1, copying those elements greater than the pivot to tape 5.

5. Copy tape 4 and then tape 5 to the space on tape 1 that formerly held the delineated sublist. Place a marker between the two lists,

6. If either or both of the sub-sublists have more than one element, recursively sort them by the same algorithm.

Notice that this implementation of Quicksort takes 0{n logn) time, even though the computing device is a multitape TM, rather than a conventional computer. However, the point of this example is not the running time but rather the use of the random bits on the second tape to cause random behavior of the Turing machine. □

11.4.3 The Language of a Randomized Turing Machine

We are used to a situation where every Turing machine (or FA or PDA for that matter) accepts some language, even if that language is the empty set or the set of all strings over the input alphabet. When we deal with randomized Turing machines, we need to be more careful about what it means for the TM



to accept an input, and it becomes possible that a randomized TM accepts no language at all. The problem is that when we consider what a randomized TM M does in response to an input w, we need to consider M with all possible contents for the random tape. It is entirely possible that M accepts with some random strings and rejects with others; in fact, if the randomized TM is to do anything more efficiently than a deterministic TM, it is essential that different contents of the randomized tape lead to different behaviors.

К we think of a randomized TM as accepting by entering a final state, as for a conventional TM, then each input w to the randomized TM M has some probability of acceptance, which is the fraction of the possible contents of the random tape that lead to acceptance. Since there are an infinite number of possible tape contents, we have to be somewhat careful computing this proba-bihty. However, any sequence of moves leading to acceptance looks at only a finite portion of the random tape, so whatever is seen there occurs with a finite probability equal to 2 * if m is the number of cells of the random tape that have been scanned and influenced at least one move of the TM. An example will illustrate the calculation in a very simple case.

Example 11.13 : Our randomized TM M has the transition function displayed m Fig. 11.7. M uses only an input tape and the random tape. It behaves in a very simple manner, never changing a symbol on either tape, and moving its heads only to the right (direction R) or keeping them stationary (direction S). Although we have not defined a formal notation for the transitions of a randomized TM, the entries in Fig. 11.7 should be understandable; each row-corresponds to a state, and each column corresponds to a pair of symbols XY, where X is the symbol scanned on the input tape, and У is the symbol scanned on the random tape. The entry in the table gUVDE means that the TM enters state g, writes U on the Input tape, writes V on the random tape, moves the input head in direction D, and moves the head of the random tape ш duection

-+ go

qiOORS

qOlSR

qilORS

дзПЗВ

qiOORS

QiBOSS

q2lORS

qiBOSS

qaOQRR

qsllRR

giBOSS

qBlSS

Figure 11.7: The transition function of a randomized Turing machine

You should be aware that the randomized TM described in Example 11.12 Is not a language-recognizing TM. Rather, it performs a transformation on its input, and the running time of the transformation, although not the outcome, depends on what was on the random tape.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 [ 161 ] 162 163 164 165 166 167 168 169 170 171 172 173 174 175