# Dovetailing (computer science)

Dovetailing, in algorithm design, is a technique that interweaves different computations, performing them essentially simultaneously. Algorithms that use dovetailing are sometimes referred to as dovetailers.

## Examples

Consider a tree that potentially contains a path of infinite length (but each node has only finitely many children): if a depth-first search is performed in this environment, the search may move down an infinite path and never return, potentially leaving part of the tree unexplored. However, if a breadth-first search is used, the existence of an infinite path is no longer a problem: each node is visited in a branching manner according to its distance from the root, so an infinite path will only impact the part of the search travelling down that path.

We can regard this tree as analogous to a collection of programs; in this case, the depth-first approach corresponds to running one program at a time, moving to the next only when the current program has finished running. In the case where one of the programs runs for an infinite amount of time, this transition will never happen. The breadth-first approach of visiting each child on the same level of the tree is an instance of dovetailing, where a single step is performed for every program before moving to the next. Thus, progress is made in each program, regardless of the potential existence of a non-terminating program.

Another example is simulating a non-deterministic Turing machine M by a deterministic one (e.g. by a universal Turing machine). In such case, we need to use dovetailing in case one of the computation branches of M contains an infinite loop.

## Infinitely many simultaneous computations

In the case of an infinite number of programs, all potentially infinitely long, neither the breadth-first nor depth-first would be sufficient to ensure progress on all programs. Instead, the following technique can be used: perform the first step of the first program; next, perform the second step of the first program and first step of the second program; next, perform the third step of the first program, second step of the second program and first step of the third program; and so on.

## Etymology

An analogy with the interweaving ends of a dovetail joint in woodworking.