While reading the reviews for Stephen Wolfram's "A New Kind of Science" on Amazon, I came across the following statement:
Every computer science (CS) student knows the dovetailer, a very simple 2 line program that systematically lists and executes all possible programs for a universal computer such as a Turing machine (TM).
Can someone give a "simple 2 line program" which illustrates "dovetaling"?
/ˈdʌvˌteɪl/ to fit together well, or to cause something to fit together well with something else: [ I ] Our plans dovetailed, and we were able to meet that evening.
When things fit this way, you can say they dovetail — they fit easily and work well together. Your plan to dress up as a Jedi knight dovetails well with your brother's Darth Vader costume, for example. Dovetails got their name from the tail feather-like shape of the joint's pieces.
Dovetail joints are made up of two parts called pins and tails. When a master craftsman wants to marry two boards together, they cut a series of pins on one board and matching tails on the other. They are trapezoidal in shape, resembling the tail feathers of a dove (hence the name dovetail).
They must be dovetailed in many directions. They are all dovetailed into one another. We shall look carefully at the scope for dovetailing the inspection role of the commission and the employment agencies standards inspectorate to ensure that the two organisations work together closely.
A CS student usually has on hand an encoding of Turing machines to integers, which they need when they write their software Turing machine emulator that takes as input a Turing machine, and writes as output the output of the specified machine. It's possible to arrange that this encoding has the property that every integer is a different, valid program.
So the naive two-liner to list and execute all programs would be:
for (int i = 0; ; ++i) printf("%d: %d\n", i, universal_turing_machine(i));
This ignoring that in C, int
is a fixed-width type.
Now, obviously that program doesn't get very far, because quite soon it hits an i
for which the corresponding Turing machine doesn't halt. So the "dovetail" trick is to run one instruction from the first machine, then an instruction from the second and another from the first, then one each from the third, second, first, and so on. As each machine halts (if it halts), of course you can stop processing it or just sit doing nothing in its "timeslices".
I don't know quite how that's a "two-liner", considering the context switch necessary between Turing machines at each step. But the dovetail program has theoretical use (and probably no use in practice). One interesting thing about it is that it has the following property:
If there exists a program
P
which solves a problem X in polynomial time (and provides the information necessary to prove the solution), then the dovetail program solves X in polynomial time.
The proof is fairly simple (it takes constant time equivalent to executing P*(P-1)/2
Turing instructions to reach the start of the correct program P
[*], and then only polynomially worse time to execute it than it would take to execute that program on its own). The re-statement of the property that I find most amusing is:
If P=NP, then we already know polynomial solutions to all NP-complete problems.
We just haven't yet proved they're polynomial. A proof of P=NP would complete that proof without actually exhibiting which of the sub-programs it is that solves the problem. The dovetail program itself can figure that out as it goes - when each machine halts, use the polynomial-time "is this a solution to X for the original input?" algorithm that's implied by X being NP. If it is a solution, output and halt. If it isn't, keep going.
[*] Well, maybe linear time, since as you create each new virtual Turing Machine, you need to give it a copy of the input to work on. Also in practice the context switches probably aren't constant time, so call it quadratic. Hand-wave-hand-wave it's polynomial OK?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With