---
title: Nearest-Neighbor Interchange (NNI) Search
url: https://www.emergentmind.com/topics/nearest-neighbor-interchange-nni-search
type: topic
---

# Nearest-Neighbor Interchange (NNI) Search

Searching arXiv for papers on nearest-neighbor interchange search and related phylogenetic tree search.
I’m checking whether the arXiv search interface is available in this environment.
Nearest-Neighbor Interchange (NNI) search denotes the exploration of phylogenetic tree space by sequences of local rearrangements in which two adjacent subtrees are swapped across an operating edge. In the literature represented by "Efficient Parallel Computation of Nearest Neighbor Interchange Distances" [1205.3402] and "Walks on SPR Neighborhoods" [1110.1114], NNI search appears in two closely related forms: as the computation of an NNI sequence that transforms one weighted phylogeny into another, and as an NNI walk that traverses a prescribed neighborhood of trees. The topic therefore spans distance measures, approximation algorithms, parallel tree transformation, and the combinatorics of local-search trajectories.

## 1. Formal setting and elementary operations

Let \(T\) be an unrooted binary phylogenetic tree on leaf set \(S=\{1,\dots,n\}\). An internal edge \(e\) of \(T\) has four incident subtrees \(T_A,T_B,T_C,T_D\) whose leaf-sets partition \(S\) as \(A\mid B\mid C\mid D\). A single nearest-neighbor-interchange (NNI) move at \(e\) replaces the split \(A\cup B\mid C\cup D\) by one of the two alternative splits, say \(B\cup C\mid A\cup D\), yielding a new tree \(T'\) [1110.1114].

The weighted formulation specializes this local move to 3-regular phylogenetic trees. Let \(e_1\text{--}e_2\text{--}e_3\) be three consecutive edges forming a path of length 3 in \(T\). The NNI-operation on the triplet \((e_1,e_2,e_3)\) swaps the two subtrees hanging off \(e_1\) and \(e_3\) while keeping the rest of the tree fixed; the central edge \(e_2\) is called the operating edge. In a weighted phylogeny each edge \(e\) has a nonnegative weight \(w(e)\), and the cost of \(\mathrm{nni}(e_1,e_2,e_3)\) is \(w(e_2)\) [1205.3402].

These definitions induce two standard search objects. The NNI distance \(d_{NNI}(T,T')\) is the minimum number of NNI moves transforming \(T\) into \(T'\). In the weighted setting, the weighted NNI-distance between two weighted phylogenies \(T_1,T_2\) on the same leaf-set and the same multiset of edge-weights is
\[
d_{nni}(T_1,T_2)
=\min\Bigl\{\sum_{k=1}^m w(e_k)\;:\;
T_1\stackrel{nni(e_1)}{\longrightarrow}
T^{(1)}\stackrel{nni(e_2)}{\longrightarrow}\cdots
\stackrel{nni(e_m)}{\longrightarrow}T_2\Bigr\}.
\]
One often writes
\[
\mathrm{opt}
=\min\{\text{total weight of an NNI-sequence }T_1\to T_2\}.
\]

An NNI-walk is a sequence of trees
\[
T_0,\;T_1,\;T_2,\;\dots,\;T_L
\]
where each consecutive pair \(T_i,T_{i+1}\) differ by exactly one NNI move; its length is \(L\), the total number of NNI steps [1110.1114]. This distinction between optimizing transformation cost and organizing a walk through a neighborhood is central to the theory of NNI search.

## 2. Shared edges, bad edges, and decomposability

A principal structural notion for weighted NNI search is the good edge-pair. Let \(T_1,T_2\) be two weighted phylogenies. An internal edge \(e_1\in E(T_1)\) and \(e_2\in E(T_2)\) form a good edge-pair if and only if:

- \(w_1(e_1)=w_2(e_2)\),
- the partition of the leaf-labels induced by removing \(e_1\) equals that induced by removing \(e_2\),
- the partition of the remaining internal-edge-weights induced by removing \(e_1\) equals that induced by removing \(e_2\) [1205.3402].

Any edge with no matching partner in the other tree is called a bad edge. Good edge-pairs can be used to split the big instance into independent smaller subinstances. This is the first step of the parallel approximation strategy: detect good edge-pairs in \(T_1,T_2\), split \(T_1,T_2\) along good edges into sub-problems, and then assume without loss of generality that no good edges remain [1205.3402].

The reduction is algorithmically significant because it isolates the part of the search space in which all internal edge weights must effectively be rearranged. In this reduced case, if \(T_1,T_2\) have no good edge-pairs and the sum of internal-edge-weights is \(W\), then
\[
d_{nni}(T_1,T_2)\ge W.
\]
The stated proof idea is that each internal edge weight must be moved at least once [1205.3402]. This lower bound is the basis for the approximation-ratio analysis of parallel NNI search.

## 3. Parallel approximation framework for weighted NNI-distance

The parallel algorithm of [1205.3402] works in the standard Concurrent-Read, Concurrent-Write PRAM model, assuming \(O(1)\)-time access to any shared memory cell and arbitrary but consistent resolution of simultaneous writes. The complexity goal is an overall running time \(O(\log n)\) using either \(O(n)\) or \(O(n\log n)\) processors, with work near-linear.

At a high level, the algorithm follows the 4-phase structure of the DasGupta-He-Jiang-Li-Tromp-Zhang (2000) sequential \(4(1+\log n)\)-approximation, with each subphase replaced by a parallel subroutine [1205.3402].

| Step | Operation | Stated role |
|---|---|---|
| 0 | Detect good edge-pairs | Split into sub-problems; assume no good edges remain |
| 1 | Auxiliary trees and linearization | Build balanced auxiliary trees and transform trees into linear trees |
| 2 | Sort the internal-edge sequence | Reorder internal edges by parallel block-pair merge-sort |
| 3 | Sort the leaf-permutation | Reconcile balanced trees that differ by a permutation \(\pi\) of their leaves |
| 4 | Concatenate all sequences | Obtain an \(O(\log n)\)-approximation |

The auxiliary-tree step constructs for each \(T_i\) a balanced auxiliary tree \(T_i'\) of the same leaf-labels and weights, but fully height-balanced and nondecreasing from root to leaves. Both \(T_i\) and \(T_i'\) are then transformed into linear trees \(L_i,L_i'\) in \(O(\log n)\) time using \(O(n)\) processors. A linear tree is one in which every internal node is adjacent to at least one leaf [1205.3402].

The subsequent steps sort two different kinds of disorder. First, the internal-edge sequence of a linear tree is sorted to match a target sorted sequence. Second, once the auxiliary trees have identical internal-edge order, the residual difference is a permutation \(\pi\) of leaf labels, which is implemented by transporting leaves along paths in a balanced tree. The total weight-sum is \(O(\log n)\cdot W\), and by the lower bound \(W\le \mathrm{opt}\), the algorithm achieves an \(O(\log n)\) approximation [1205.3402].

The main theorem is stated as follows: if \(T_1,T_2\) are two weighted phylogenies on \(n\) leaves with the same leaf-set and edge-weight multiset, then one can compute in \(O(\log n)\) time on \(O(n)\) (or \(O(n\log n)\)) CRCW-PRAM processors an NNI-sequence of total weight
\[
\le O(\log n)\,\mathrm{opt},
\]
where \(\mathrm{opt}=d_{nni}(T_1,T_2)\) [1205.3402].

## 4. Core parallel subroutines

The linearization subroutine transforms a 3-regular tree into a linear tree. It first precomputes, for each end-node \(v\), the path \(P(v)\) upward to its first junction-node via parallel pointer-jumping in \(O(\log n)\). It then repeats, until no junction-nodes remain, a constant-time parallel phase in which end-paths activate junction-nodes, activated junctions generate the NNI moves that insert end-paths, and pointers are updated by contraction and re-splicing. Each of the \(O(\log n)\) iterations takes \(O(1)\) time with \(O(n)\) processors [1205.3402].

The edge-reordering subroutine, denoted Procedure ParallelEdgeMergeSort\((L,L')\), performs an \(O(\log n)\)-round parallel merge-sort of block-pairs. In round \(k\), the internal-edge sequence is partitioned into blocks of length \(2^k\), block-pairs are paired from the two ends inwards, and the \(2\cdot 2^k\) edges within each block-pair are sorted or merged by computing, for each edge, its rank in the opposite block via \(O(1)\)-time comparisons done in parallel. This produces a merged block of length \(2^{k+1}\) plus \(O(1)\) NNI-moves per edge. The final block spans all edges in sorted order [1205.3402].

The leaf-permutation subroutine, Procedure ParallelLeafSort\((T_1',T_2')\), assumes balanced binary trees \(T_1',T_2'\) of depth \(O(\log n)\) with the same multiset of leaf-labels permuted by \(\pi\). It decomposes \(\pi\) into disjoint cycles in \(O(1)\) time on \(n\) processors. In parallel for each cycle, and for each element in the cycle, it transports the corresponding leaf from its current position to its target position along the unique path via \(O(\text{tree-depth})\) moves. Since the depth is \(O(\log n)\), each cycle takes \(O(\log n)\) time, and the total time is \(O(\log n)\) on \(O(n)\) processors [1205.3402].

The good-edge-pair detection subroutine reduces the problem to the partition-labeling problem. Each internal edge \(e\) is converted into a leaf of unique new label \(\rho(e)\) encoding \(w(e)\), both trees are rooted arbitrarily, and partition-labeling is solved in \(O(\log n)\) time on \(O(n\log n)\) processors using the method of Hon-Kao-Lam-Sung-Yiu (2004). A final scan of partition-labels in both trees identifies any label \(\ell\) occurring on exactly one internal node in \(T_1\) and one in \(T_2\) as a good edge-pair [1205.3402].

The approximation-ratio proof is additive across these subroutines. Linearization moves each internal edge at most once, so its total is at most \(W\). In each of the \(O(\log n)\) merge-sort rounds, every internal edge participates in exactly one block-merge and contributes its weight once, so the total is at most \(W\cdot O(\log n)\). In leaf-sorting, each leaf transport visits \(O(\log n)\) edges, so the total weight is also at most \(W\cdot O(\log n)\). Hence the overall cost is at most \(C\cdot W\cdot \log n\) for some constant \(C\), and therefore at most \(C\,\mathrm{opt}\,\log n\) [1205.3402].

## 5. NNI walks on SPR neighborhoods

A second line of work studies NNI search not as a shortest transformation between two fixed trees, but as a walk that visits every tree in a prescribed neighborhood. In this setting, an SPR move on \(T\) consists of pruning some subtree \(t\) by cutting an edge \(e\), and then regrafting \(t\) onto a possibly different edge of the remaining tree, followed by suppressing any resulting degree-2 node to restore binarity. The SPR-neighborhood is
\[
N_{SPR}(T)=\{\,T'\mid d_{SPR}(T,T')=1\}.
\]
Allen and Steel show that for any unrooted binary tree on \(n\) leaves,
\[
|N_{SPR}(T)|=2(n-3)(2n-7)=(2n-6)(2n-7)
\]
[1110.1114].

The main theorem of [1110.1114] states that for every unrooted binary tree \(T\) on \(n\) leaves, any NNI-walk that visits all trees in \(N_{SPR}(T)\) has length
\[
L=M+\Theta(n^2),
\]
where \(M=|N_{SPR}(T)|=(2n-6)(2n-7)\). Equivalently,
\[
(2n-6)(2n-7)+c_1n^2\le L\le (2n-6)(2n-7)+c_2n^2
\]
for some absolute constants \(c_1,c_2>0\) [1110.1114].

The proof decomposes the SPR-neighborhood into orbits. For each edge \(e\) of \(T\), the orbit \(O_e\subseteq N_{SPR}(T)\) is the set of all trees obtained by pruning exactly \(e\). One checks that \(|O_e|=2n-5\), and there are \(2n-3\) edges in \(T\). Orbits overlap only on the original tree \(T\) and on those SPR-neighbors obtainable by an NNI move of \(T\), exactly \(2n-6\) such overlaps. Within each orbit \(O_e\), there is an NNI-circuit visiting every member of \(O_e\) with at most \(2(2n-5)\) steps, which yields the upper bound when circuits are spliced back-to-back via the base tree \(T\). The lower bound follows because, in each orbit, all but \(O(1)\) trees form a subtree of the NNI-graph that has no edges leading out of \(O_e\), forcing \(\Omega(n)\) back-and-forth steps beyond a Hamiltonian pass, and therefore \(\Omega(n^2)\) extra steps overall [1110.1114].

This result settles Bryant’s Second Combinatorial Conjecture. In the language of search, it shows that if the objective is to examine every SPR-neighbor by moving only through NNI-adjacent trees, then no Hamiltonian path exists and the shortest walk necessarily incurs quadratically many revisits beyond the number of distinct neighbors [1110.1114].

## 6. Algorithmic significance, heuristics, and open problems

The two lines of work describe complementary aspects of NNI search. The weighted-distance results formalize how to construct an NNI-sequence between two trees under explicit complexity and approximation guarantees. The neighborhood-walk results formalize the unavoidable overhead of restricting traversal to NNI moves while trying to cover a larger SPR neighborhood. Taken together, they indicate that local-search behavior depends not only on the cost of individual NNI operations but also on the global geometry of the target set.

Several practical implications are stated explicitly in [1205.3402]. Good-edge-pair splitting suggests a divide-and-conquer NNI-search: identify cheap shared edges first, solve subproblems in parallel. The linearization and block-merge steps give a structured way to propose NNI moves in \(O(\log n)\) phases, which can seed multithreaded hill-climbing or simulated-annealing on large trees. A stated trade-off is parallelism versus local quality: the \(O(\log n)\)-approximate solution may be far from optimal on some instances, and mixing with local descent on bad edges may improve accuracy [1205.3402].

The neighborhood results supply a complementary worst-case warning. If a search strategy must examine every SPR-neighbor by hopping via NNI moves, it will make \(\Theta(n^2)\) more local moves than there are distinct neighbors. The same work notes that organizing SPR neighbors into orbits and precomputing DFS-like circuits per orbit provides a systematic way to visit all one-SPR neighbors with only \(O(n^2)\) overhead, rather than naively random-walking through NNI space [1110.1114].

Several open questions are stated in the weighted-distance work. The APX-hardness status of weighted NNI-distance remains open. It is also asked whether one can reduce the \(\log n\) factor in the approximation ratio or improve parallel time and work, and whether the same ideas can be adapted to SPR or TBR distances [1205.3402]. These questions situate NNI search at the intersection of combinatorial phylogenetics, approximation algorithms, and parallel computation.

Source: https://www.emergentmind.com/topics/nearest-neighbor-interchange-nni-search