Computing nearest neighbour interchange distances between ranked phylogenetic trees

Many popular algorithms for searching the space of leaf-labelled (phylogenetic) trees are based on tree rearrangement operations. Under any such operation, the problem is reduced to searching a graph where vertices are trees and (undirected) edges are given by pairs of trees connected by one rearrangement operation (sometimes called a move). Most popular are the classical nearest neighbour interchange, subtree prune and regraft, and tree bisection and reconnection moves. The problem of computing distances, however, is \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$${\mathbf {N}}{\mathbf {P}}$$\end{document}NP-hard in each of these graphs, making tree inference and comparison algorithms challenging to design in practice. Although ranked phylogenetic trees are one of the central objects of interest in applications such as cancer research, immunology, and epidemiology, the computational complexity of the shortest path problem for these trees remained unsolved for decades. In this paper, we settle this problem for the ranked nearest neighbour interchange operation by establishing that the complexity depends on the weight difference between the two types of tree rearrangements (rank moves and edge moves), and varies from quadratic, which is the lowest possible complexity for this problem, to \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$${\mathbf {N}}{\mathbf {P}}$$\end{document}NP-hard, which is the highest. In particular, our result provides the first example of a phylogenetic tree rearrangement operation for which shortest paths, and hence the distance, can be computed efficiently. Specifically, our algorithm scales to trees with tens of thousands of leaves (and likely hundreds of thousands if implemented efficiently).

The problem of reconstructing evolutionary histories from sequence data is central for many popular methods in computational biology.Most commonly trees are inferred from sequences via maximum likelihood [21,10], MCMC [18,22,4], distance-, or parsimony-based approaches [23].All these methods rely on various tree rearrangement operations [20], the most popular of which are nearest neighbour interchange (NNI), subtree prune and regraft (SPR), and tree bisection and reconnection (TBR).Under any such operation, the tree inference problem can be formulated as a graph search, where vertices are trees and edges are given by tree rearrangement operations.For search algorithms to be efficient, it is important to understand the geometry of these graphs.For example, basic geometric properties of the NNI graph have been successfully leveraged to speed up the maximum likelihood method [16].The most basic geometric characteristic that frequently arises in applications is the minimum number of rearrangements necessary to transform one tree into another [20].The problem then amounts to computing the length of a shortest path between trees in the three graphs.This can also be seen as computing the distance between trees in the corresponding metric space.
Classical results in mathematical phylogenetics imply that these distances are NP-hard to compute for all three rearrangement operations NNI, SPR, and TBR [5,3,12,1].Intuitively, the difference between the three operations is how much change can be done to a tree by a single operation, with NNI being the most local type of rearrangement and TBR the most global one.Remarkably, it took over 25 years and a number of published erroneous attempts, as discussed in detail by DasGupta et al. [5], to prove that computing distances is NP-hard in NNI [5].Similarly, incorrect proofs for SPR have been discussed in the literature [11,1], before Bordewich and Semple [3] proved the NP-hardness result for rooted trees and Hickey et al. [12] utilised this proof to establish the result for unrooted trees.To facilitate practical applications, fixed parameter tractable algorithms [7] for computing the SPR distance have been developed over the years [24,3,25].Computing the NNI distance is also known to be fixed parameter tractable [6].Although important, these algorithms remain impractical for large distances and are only applied to trees with a moderate number of leaves or those with small distances [25].
Another area where algorithms for computing shortest paths and distances between trees play a central role, is calculating consensus or summary trees [15,2,27].A popular tree distance measure used in such methods is the Robinson-Foulds distance [17], as it can be computed in linear time.Lack of biological motivation however is a downside of this approach, which often results in poor summaries and hence is not used for summarising samples of trees obtained in a full Bayesian tree inference approach [8].In general, distance measures that are easy to compute typically have this problem, whereas measures that are biologically relevant, including rearrangement-based distances, are often hard to compute [25].
In this paper, we establish that using a generalisation of the NNI operation introduced by Gavryushkin, Whidden, and Matsen [9] called RNNI (for Ranked Nearest Neighbour Interchange), the shortest path problem is computable in O(n 2 ), where n is the number of tree leaves.This makes RNNI the first tree rearrangement operation under which shortest paths and distances between trees are polynomial-time computable.Our proof of this result (Theorem 1) is constructive -we provide an algorithm called FindPath that computes shortest paths in the RNNI graph in O(n 2 ) time.Our algorithm is optimal as shortest paths often have length quadratic in the number of leaves n.The algorithm is practical as it takes seconds on a laptop to compute the distance between trees with thousands of leaves, while in the closely related NNI graph the tractable number of leaves is well below twenty [13,26].
Because NNI can be seen as a special case of RNNI, we investigate whether there exists a threshold at which the complexity shifts from NP-hard to polynomial.Specifically, we introduce an edge weight parameter ρ in the RNNI graph and consider a parametrised graph RNNI(ρ).We show that the shortest path problem is NP-hard in RNNI(0) and quadratic in RNNI (1), so the complexity changes with ρ.We hence propose to characterise the complexity classes of the problem RNNI(ρ) for values of ρ ≥ 0. This problem is similar in spirit to the beyond worst-case analysis (including parametrised complexity [7]) framework [19].Just like in our result, a γ-perturbation stable instance of the maximum cut problem is known [19] to be NP-hard for small values of γ and polynomial for larger values of γ.Since the problem of identifying the value of γ where the complexity switches has largely been resolved [14], we hope that the approaches reviewed by Roughgarden [19] will be helpful for our proposed study as well.

Definitions and background results
Unless stated otherwise, by a tree in this paper we mean a ranked phylogenetic tree, which is a binary tree where leaves are uniquely labelled by elements of the set {a 1 , . . ., a n } for a fixed integer n, and all internal (non-leaf) nodes are uniquely ranked by elements of the set {1, . . ., n − 1} so that each child has a strictly smaller rank than its parent.All leaves are assumed to have rank 0 but we only refer to the ranks of internal nodes throughout.In total there are (n−1)!n! 2 n−1 such trees on n leaves [9].Two trees are considered to be identical if there exists an isomorphism between them which preserves edges, leaf labels, and node rankings.For example, trees in Figure 1 Trees in the RNNI graph with three NNI moves on the left and a rank move on the right.
Because internal nodes of a tree T are ranked uniquely, we can address the node of rank t ∈ {1, . . ., n − 1}, and we write (T ) t to denote this node.An interval [(T ) t , (T ) t+1 ] is defined by two nodes of consecutive ranks.A cluster C ⊆ {a 1 , . . ., a n } in a tree T is a subset of leaves that contains all leaves descending from one internal node of T .We then say that this internal node induces the cluster C, and that the subtree rooted at this node is induced by C. Trees can uniquely be specified using the cluster representation, that is a list of all clusters induced by internal nodes of that tree ordered according to the ranks of internal nodes.For example, the cluster representation of tree T in Figure 1 is [{a 1 , a 2 }, {a 1 , a 2 , a 3 }, {a 4 , a 5 }, {a 1 , a 2 , a 3 , a 4 , a 5 }].For a set S ⊆ {a 1 , . . ., a n } and tree T we denote the most recent common ancestor of S in T , that is the node of the lowest rank in T that induces a cluster containing all elements of S, by (S) T .Note that (C) T = (T ) t if the cluster C is induced by the node of rank t in T .
Our main object of study is the following class of graphs RNNI(ρ) indexed by a real-valued parameter ρ ≥ 0. Vertices of the RNNI(ρ) graph are trees as defined above.Two trees are connected by an edge (also called an RNNI move) if one results from the other by performing one of the following two types of tree rearrangement operation (see Figure 1): (i) A rank move on a tree T exchanges the ranks of two internal nodes (T ) t and (T ) t+1 with consecutive ranks, provided the two nodes are not connected by an edge in T .(ii) Trees T and R are connected by an NNI move if there are edges e in T and f in R both connecting nodes of consecutive ranks in the corresponding trees, such that the (non-binary) trees obtained by shrinking e and f into internal nodes are identical.The parameter ρ ≥ 0 is the weight of the rank move operation, an NNI move weighs 1.
The weight of a path in RNNI(ρ) is the sum of the weights of all moves along the path.The distance between two trees in RNNI(ρ) is the weight of a path with the minimal weight, which we will call a shortest path.When ρ = 1 we assume that the graph is unweighted.
We consider the following class of problems parametrised by a real number ρ ≥ 0.

RNNI(ρ)-SP INSTANCE:
A pair of trees T and R on n leaves FIND: A path of minimal weight between T and R in RNNI(ρ) Since RNNI(ρ) is a connected graph, there always exists a solution to RNNI(ρ)-SP.Furthermore, the size of every solution to an instance of RNNI(ρ)-SP is bounded by a polynomial in n, despite the search space being super-exponential.This is because the diameter of the RNNI(1) graph is bounded from above [9] by n 2 − 3n − 5/8.
Our main goal is to prove that RNNI(1)-SP can be solved in polynomial time.We will see later in the paper that it follows from a classical result [5] that RNNI(0)-SP is NP-hard.To be consistent with notations used in the literature [9], we will denote the graph RNNI(1) by RNNI.

FindPath algorithm
In this section we introduce an algorithm called FindPath that computes paths between trees and is quadratic in the number of leaves.
An input of the FindPath algorithm is two trees T and R in their cluster representation.We denote the representation of R by [C 1 , . . ., C n−1 ].The algorithm considers the clusters C 1 , . . ., C n−2 iteratively in their order and produces a sequence p of trees which becomes a shortest path from T to R after the algorithm terminates.During each iteration k = 1, . . ., n − 2 new trees are added to p if necessary, and we will refer to the last added tree as T 1 .In iteration k, the rank of (C k ) T 1 is decreased by RNNI moves until C k is induced by the node of rank k in T 1 .In Proposition 1 we show that FindPath is a deterministic algorithm with running time quadratic in the number of leaves n.In particular, there always exists a unique move that decreases the rank of (C k ) T 1 as described above.
Algorithm 1 FindPath(T, R) if (C k ) T 1 and node u preceding (C k ) T 1 in T 1 are connected by an edge then 5: T 2 is T 1 with the rank of (C k ) T 1 decreased by an NNI move 6: else 7: T 2 is T 1 with ranks of u and (C k ) T 1 swapped 8: Proof.To show that FindPath is a deterministic algorithm (see the pseudocode above), we have to prove that tree T 2 constructed in the while loop (line 3) of the algorithm always exists and is uniquely defined.If T 2 is obtained in line 7 from T 1 by a rank move, the tree exists and is unique because there always exists exactly one rank move on any particular interval that is not an edge.It remains to show that an NNI move that decreases the rank of (C k ) T 1 always exists and is unique.
To prove this we consider cases k = 1 and k > 1 separately.Case k = 1.In this case C k consists of two leaves {x, y}.Since we assumed that the while condition is satisfied, the node v = ({x, y}) T 1 has rank r > 1.Consider the node u preceding v in T 1 , so the rank of u is r − 1. Assume without loss of generality that x is in the cluster induced by u, so y has to be outside this cluster.Consider the following three disjoint subtrees of T 1 : the subtree T 11 induced by a child of u and containing x, the subtree T 12 induced by the other child of u, the subtree T 13 induced by a child of v and containing y. Now observe that out of two NNI moves possible on the edge [u, v] in T 1 , only the one that swaps T 12 and T 13 does decrease the rank of the most recent common ancestor of {x, y}.Hence T 2 exists and is unique in this case.Case k > 1.In this case C k = C i ∪ C j for i, j < k.In this case the subtree of T 1 induced by (C i ) T 1 is identical to the subtree of R induced by (C i ) R , and the same is true for (C j ) T 1 and (C j ) R .Hence, we can reduce this case to k = 1 by suppressing C i and C j in both T 1 and R to new leaves c i and c j (of rank zero) respectively.As in Case k = 1, exactly one of two possible NNI moves deceases the rank of the most recent common ancestor of c i , c j in T 1 , so the same is true for the most recent common ancestor (C k ) T 1 , and T 2 is unambiguously defined.Thus, FindPath is a deterministic algorithm.
To prove correctness, note that the algorithm starts by adding T to the output path, and every new tree added to the output path is an RNNI neighbour of the previously added one (see line 5 and 7).To see that the output path terminates in R, observe that after k iteration of the for loop (line 2) of the algorithm, the first k clusters of T 1 and R must coincide, and so after n − 2 iterations a path between T and R is constructed.
The worst-case time complexity of FindPath is quadratic in the number of leaves, as there can be at most n − 2 executions of the for loop (line 2) and in every iteration of the for loop at most n − 2 while loops (line 3) are executed.Here and throughout the paper we assume that the output of FindPath is encoded by a list of RNNI moves rather than an actual list of trees.This is because writing out a tree on n leaves takes time linear in n and the complexity of FindPath becomes cubic.

FindPath computes shortest paths in optimal time
In this section we prove the main result of this paper, that RNNI(1)-SP is polynomial.Specifically we prove that paths returned by FindPath are always shortest.We also show that FindPath is an optimal algorithm, that is, no sub-quadratic algorithm can solve RNNI(1)-SP.
The main ingredient of our proof is to show that a local property (see (1) in the proof) of the FindPath algorithm is enough to establish that the output paths are shortest.The property can intuitively be understood as FindPath always choosing the best tree possible to go to.Importantly, this result can be used for an arbitrary vertex proposal algorithm in an arbitrary graph to establish that the algorithm always follows a shortest path between vertices in the graph, hence our proof technique is of general interest.
Theorem 1.The worst-case time complexity of the shortest path problem in the RNNI graph on trees with n leaves is O(n 2 ).Hence RNNI(1)-SP is polynomial time solvable.
Proof.We prove this theorem by showing that for every pair of trees T and R, the path computed by the FindPath algorithm is a shortest RNNI path.We denote this path by FP(T, R) and its length by |FP(T, R)|.By d(T, R) we denote the length of a shortest path between T and R, that is, the RNNI distance between trees.We hence want to show that |FP(T, R)| = d(T, R) for all trees.
Assume to the contrary that T and R are two trees with a minimum distance Let T ′ be the first tree on a shortest RNNI path from T to R. Then d(T ′ , R) = d(T, R) − 1, implying that the distance between T ′ and R is strictly smaller than that between T and R. Hence We finish the proof by showing that no trees satisfy this inequality.
Specifically, we will show that for all trees T , R, and T ′ such that T ′ is one RNNI move away from T , We will use Figure 2 to demonstrate our argument.Assume to the contrary that T and R are trees for which there exists T ′ violating inequality (1).Out of all such pairs T, R choose one with the minimal and let [(T ) t , (T ) t+1 ] be the interval in T on which the RNNI move connecting T and T ′ is performed.Let C k be the cluster of R such that the node (C k ) T is moved down by the first move on FP(T, R).If the rank of (C k ) T is not in {t, t + 1} then (C k ) T and (C k ) T ′ induce the same cluster, so FindPath would make the same rearrangement in both trees T and T ′ in the first move along FP(T, R) and FP(T ′ , R) resulting in trees T 1 and T ′ 1 which are RNNI neighbours, as in Figure 2. In this case, paths FP(T 1 , R) and FP(T ′ 1 , R) violate inequality (1) but FP(T 1 , R) is strictly shorter than FP(T, R), contradicting our minimality assumption.Hence, the first move on FP(T, R) has to involve an interval incident to at least one of the nodes (T ) t , (T ) t+1 .
Moreover, because C k is the first cluster satisfying the while condition of FindPath applied to T and R, all clusters C j with j < k have to be present in T .And since the first move on FP(T, R), which decreases the rank of (C k ) T , involves nodes with ranks not higher than t + 2, the most recent common ancestor of C k has rank not higher than t + 1 after this move.Hence k ≤ t + 1.Furthermore, clusters C j for all j ≤ k − 2 have to be present in T ′ as well as T , because all clusters induced by nodes of rank t − 1 or lower coincide in these two trees.Cluster C k−1 , however, might not be induced by a node in T ′ if k − 1 = t.Therefore, the first move on FP(T ′ , R) can decrease the rank of the most recent common ancestor of either C k−1 or C k .
We will distinguish two cases depending on whether T and T ′ are connected by an NNI or a rank move.For each of these we will further distinguish all possible moves between T and T 1 .Note that in all figures illustrating possible moves on FP(T, R) and FP(T ′ , R) below, the position of the tree root is irrelevant, so we have positioned roots to simplify our figures.
Figure 3. NNI move between T and T ′ on the edge [(T ) t , (T ) t+1 ] indicated in bold, and the third RNNI neighbour resulting from a move on this edge.
Case 1. T and T ′ are connected by an NNI move.So [(T ) t , (T ) t+1 ] is an edge in T -see Figure 3. Denote the clusters induced by the children of (T ) t by A and B and the cluster induced by the child of (T ) t+1 that is not (T ) t by C, and assume that the NNI move between T and T ′ exchanges the subtrees induced by clusters B and C. Additionally, denote the cluster induced by the child of (T ) t+2 that is not (T ) t+1 by D -see Figure 3.Note that [(T ) t+1 , (T ) t+2 ] does not have to be an edge in tree T (see Case 1.4).
We now consider all possible moves FindPath can perform to go from T to T 1 that involve a node of rank t or t + 1, that is, we will consider three intervals in total.1.1 RNNI move (either type) on interval [(T ) t , (T ) t+1 ].This move has to be the NNI move that is different from the NNI move connecting T and T ′ .In this case, the cluster B ∪ C is built in T 1 , as depicted in the bottom of Figure 3. Hence the first cluster C k that satisfies the while condition of FindPath must contain elements from both B and C but not from A, and the rank of (C k ) R has to be at most t.But then FindPath applied to T ′ and R has to decrease the rank of (C k ) T ′ in its first step implying that 1.2 NNI move on (edge) interval [(T ) t+1 , (T ) t+2 ] that swaps the subtrees induced by clusters C and D.
This move is shown in Figure 4A by an arrow from T to the leftmost tree in the middle row.In this case, the first cluster C k that satisfies the while condition of FindPath computing FP(T, R) must intersect D but not C. Additionally, C k must intersect A, or B, or both of them.Hence, we will consider each of these three cases individually, and demonstrate them in Figure 4.
The rank of (C k ) T 1 is strictly higher than that of (C k ) R .In this case, FindPath decreases the rank of (C k ) T 1 in the second step.This results in the path from T to the rightmost tree in Figure 4A.Hence, FP(T ′ , R) also has to begin with two moves that decrease the rank of (C k ) T ′ twice, resulting in the rightmost path in Figure 4B.Similarly to case 1.2.2, we arrive at a contradiction that trees T 2 , T ′ 2 , and R violate inequality (1) and ].This case is analogous to case 1.3 (see Figure 5).If the ranks of (C k ) T 1 and (C k ) R coincide then C k−1 = A ∪ B, and applying FindPath to T ′ , R we get T ′ 1 = T .If the rank of (C k ) T 1 is strictly higher than that of (C k ) R then FindPath decreases the rank of (C k ) T 1 in the second step.Recall that the interval between nodes of rank t and t + 1 is an edge in both T and T ′ .Hence, the first two moves on FP(T ′ , R) decrease the rank of (C k ) T ′ twice resulting in T ′ 2 which is an RNNI neighbour of T 2 as depicted in Figure 5.As before, this contradicts our minimality assumption.1.5 RNNI move (either type) on interval [(T ) t−1 , (T ) t ].In this case C k ⊆ A ∪ B and the rank of (C k ) R is at most t − 1.This implies that C k is the first cluster to satisfy the while condition for T ′ and the first move on FP(T ′ , R) decreases the rank of (C k ) T ′ by exchanging the subtrees induced by B and C.This results in T ′ 1 = T .
Case 2. T and T ′ are connected by a rank move.We assume that the rank move is performed on the interval [(T ) t , (T ) t+1 ].Denote the cluster induced by (T ) t by A, the clusters induced by the children of (T ) t by A 1 and A 2 , the cluster induced by (T ) t+1 by B, and the clusters induced by the children of (T ) t+1 by B 1 and B 2 -see Figure 6.
Comparison of paths FP(T, R) and FP(T ′ , R) if there is an NNI move between T and T ′ and a rank move on the interval above this edge follows on FP(T, R). and FP(T ′ , R) when [(T ) t+1 , (T ) t+2 ] is an edge.We use notations We again consider all possible moves FindPath can perform to go from T to T 1 that involve a node of rank t or t + 1.

NNI move on (edge) interval [(T ) t+1 , (T ) t+2
].The following two sub-cases are analogous to case 1.3.2.2.1 (T ) t+2 is a parent of (T ) t .The first move on FP(T, R) builds a cluster A ∪ B 1 or A ∪ B 2 , and we assume without loss of generality that it is the former, as in Figure 6.This implies that C k intersects A and B 1 but not B 2 If the ranks of (C k ) T 1 and (C k ) R coincide then the previous cluster C k−1 of R has to be A. Therefore, the first move on FP(T ′ , R) decreases the rank of (A) T ′ , which results in T ′ 1 = T .If the rank of (C k ) T 1 is strictly higher than that of (C k ) R then FindPath decreases the rank of (C k ) T 1 in the second step.Due to the symmetry we can assume that C k ⊆ A 1 ∪ B 1 , which implies that the move between T 1 and T 2 exchanges the subtrees induced by A 2 and B 1 , as depicted on the left of Figure 6.C k ⊆ A 1 ∪ B 1 implies that the first two moves on FP(T ′ , R) result in a tree T ′ 2 that is an RNNI neighbour of T 2 -see Figure 6.This is a contradiction to the minimality assumption on |FP(T, R)|.2.2.2 (T ) t+2 is not a parent of (T ) t .In this case, there exists a cluster C induced by the child of (T ) t+2 which is different from the one that induces B -see Figure 7.We can assume without loss of Comparison of paths FP(T, R) and FP(T ′ , R) if there is a rank move between T and T ′ and an NNI move on the edge below the corresponding (rank) interval follows on FP(T, R).
generality that C k ⊆ C ∪ B 1 and the first move on FP(T, R) builds a new cluster C ∪ B 1 .If the ranks of (C k ) T 1 and (C k ) R coincide then C k−1 = A, which implies that A is induced by the node of rank t in both T and R.So T ′ 1 = T .If the rank of (C k ) T 1 is strictly higher than that of (C k ) R then FindPath decreases the rank of (C k ) T 1 in the second step -see Figure 7.The corresponding first moves on FP(T ′ , R) are shown on the right in Figure 7, and we again get that T 2 and T ′ 2 are RNNI neighbours.2.3 Rank move on interval [(T ) t+1 , (T ) t+2 ].Again, depending on whether or not the ranks of (C k ) T 1 and (C k ) R coincide, we arrive at the conclusion that either T ′ 1 = T or T 2 and T ′ 2 are RNNI neighbours, similarly to case 1.4.2.4 RNNI move (either type) on interval [(T ) t−1 , (T ) t ].In this case C k ⊆ A and the first move on FP(T ′ , R) must be a rank swap resulting in T ′ 1 = T .Since all possible cases result in a contradiction, we conclude that inequality (1) is true for all trees, which completes the proof of the theorem.
We finish this section by showing that no algorithm has strictly lower worst-case time complexity than FindPath.We again assume here that the output of an algorithm for solving RNNI(1)-SP is a list of RNNI moves.Requiring the output to be a list of trees would result in cubic complexity while maintaining the optimality of FindPath.
Proof.We prove this by establishing the lower bound on the output size to the problem, that is, the length of a shortest paths.
Hence the length of the output path of FindPath is 2 and therefore quadratic in n.Theorem 1 then implies that this path is a shortest path.It follows that the worst-case size of the output to RNNI(1)-SP is quadratic.
As we have seen in Section 2, the shortest path problem RNNI(1)-SP is solvable in polynomial time.In this section, we will show that a classical result in mathematical phylogenetics implies that RNNI(0)-SP is NP-hard.We will also discuss RNNI(ρ)-SP for other values of ρ.
Proof.Note that the length of the path required in an instance of RNNI(0)-SP is equal to the minimum number of NNI moves necessary to convert one tree into another tree where the rankings of internal nodes are ignored and NNI moves are allowed on every edge.This minimum is called the NNI distance and the corresponding problem is known to be NP-hard [5].
In the light of Theorem 1 and Theorem 2 the following problem is natural.

Problem 1. Characterise the complexity of RNNI(ρ)-SP in terms of ρ.
This problem is also of applied value.For example, trees might come from an inference method with higher certainty of their branching structure and lower certainty of their nodes order.A comparison method for such trees should have higher penalty for NNI changes and lower penalty for rank changes, which in our notations requires ρ < 1.
In the rest of this section, we show that the FindPath algorithm substantially relies on the fact that the rank move and the NNI move have the same weight in the RNNI graph.This suggests that a non-trivial algorithmic insight is necessary to extend our polynomial complexity result to other values of ρ.Proposition 2. FindPath does not compute shortest paths in RNNI(ρ) for ρ = 1.

Additional open problems
The idea utilised by DasGupta et al. [5] to prove that computing distances in NNI is NP-hard stems from a result that shortest paths in NNI do not preserve clusters [13], that is, sometimes a cluster shared by two trees T and R is shared by no other tree on any shortest path between T and R.This counter-intuitive property eventually led to the computational hardness result in NNI.Moreover, this property makes little sense biologically as trees clustering the same set of sequences into a subtree should be closer to each other than to a tree that does not have that subtree.Indeed, a shared cluster means that both trees support the hypothesis that this cluster has evolved along a subtree.In light of this biological argument, the NP-hardness result can be interpreted as RNNI(ρ)-SP being hard only when the graph RNNI(ρ) is biologically irrelevant.Although in sharp contrast with the common belief in the field of computational phylogenetics [25], this interpretation resonates with the idea suggested in the beyond worst case analysis framework [19] that some problems are only computationally hard when their instances are practically irrelevant.The following question is hence natural.
(1) For which values of ρ does RNNI(ρ) have the cluster property?How do those compare to the values of ρ for which RNNI(ρ)-SP is efficient?Other natural questions that arise in the context of our results are the following.(2) The questions we have considered for ranked NNI can be studied in other rearrangement-based graphs on leaf-labelled trees, such as the ranked SPR graph and the ranked TBR graph [20].What is the complexity of the shortest path problem there?(3) Can our results be used to establish whether the problem of computing geodesics between trees with real-valued node heights is polynomial-time solvable?This geodesic metric space is called t-space and an efficient algorithm for computing geodesics in t-space would be of importance for applications [8].

Figure 4 .
Figure 4. Comparison of paths FP(T, R) and FP(T ′ , R) if T and T ′ are connected by an NNI move on edge [(T ) t , T t+1 ] in T .The bottom row displays all possibilities for T 2 and T ′ 2 , depending on the position of cluster C k that satisfies the while condition of FindPath: case C k intersects B and D is on the left, C k intersects A and D is in the middle, and C k intersects C and D is on the right.1.2.1 C k intersects A, B, and D but not C.In this case, since we assumed [(T 1 ) t , (T 1 ) t+1 ] to be an edge in the tree, no move on T 1 can decrease the rank of (C k ) T 1 .It follows from the proof of Proposition 1

Figure 6 .
Figure 6.Rank move between T and T ′ and possible initial segments of FP(T, R) and FP(T ′ , R) when [(T ) t+1 , (T ) t+2 ] is an edge.We use notations A = A 1 ∪ A 2 and B = B 1 ∪ B 2 .

Figure 8 . 4 a 1 a 2 a 3 a 4 a 1 a 2 a 3 a 4 a 3 a 4 a 1 a 2 Figure 9 .
Figure 8. Path computed by FindPath (top) and a shorter path (bottom) for ρ > 1.