# Dovetailing (computer science)

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

Consider a tree that potentially contains a path of infinite length: 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 corresponds to 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 program of infinite runtime.

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 first step of the second program and the second step of the first program; next, perform the first step of the third program, the second step of the second program, and the third step of the first program; and so on.

Note: We could dovetail the depth-first (no dovetailing) and breadth-first (full dovetailing) mechanism of combining algorithms. This recursive application of the dovetailing algorithm to itself leads to an infinite number of new algorithms, each involving slightly less total dovetailing.

## Etymology

1. The term might have come from dovetail card shuffling.
2. An analogy with the interleaving ends of a dovetail joint in woodworking.