---
title: 'Phylo2Vec: Vector Encoding for Binary Phylogenetic Trees'
url: https://www.emergentmind.com/topics/phylo2vec
type: topic
---

# Phylo2Vec: Vector Encoding for Binary Phylogenetic Trees

Phylo2Vec is a vector representation for rooted binary phylogenetic trees in which a tree topology is encoded as an integer vector of length $n-1$. In its original formulation, the representation is a bijection from the set of rooted binary trees on $n$ labeled leaves to a constrained subset of $\mathbb{Z}^{n-1}$, and it was introduced as a unified approach for both manipulating and representing phylogenetic trees. Its stated advantages are fast tree sampling, compressed tree representation compared to a Newick string, quick and unambiguous verification of topological identity, and systematic traversal of tree space in very large or small jumps [2304.12693]. Subsequent work has treated Phylo2Vec both as a concrete combinatorial encoding and as part of a broader family of vector-based phylogenetic representations and machine-learning pipelines [2507.11254][2506.19490][2405.07110].

## 1. Formal definition and bijective structure

Let $\mathcal{T}$ be any rooted binary tree with $n$ labeled leaves $\{0,1,\dots,n-1\}$. Phylo2Vec associates to $\mathcal{T}$ a vector
$$
v=(v_1,v_2,\dots,v_{n-1}) \in \mathbb{Z}^{n-1}
$$
subject to the single constraint
$$
v_j \in \{0,1,2,\dots,2(j-1)\}\qquad \text{for } j=1,2,\dots,n-1.
$$
Writing
$$
\mathbb{V}=\{v\in\mathbb{Z}^{n-1}: v_j\in\{0\ldots 2(j-1)\}\},
$$
one has
$$
\prod_{j=1}^{n-1}(2j-1)=(2n-3)!!,
$$
and there are exactly $(2n-3)!!$ rooted binary trees on $n$ leaves. By counting together with an injectivity argument, the map
$$
\text{Phylo2Vec}:\{\text{trees}\}\to \mathbb{V}
$$
is bijective [2304.12693].

This bijection is central to the method. It means that Phylo2Vec is not merely a feature map or heuristic embedding, but a lossless topology encoding. Topological equality becomes entrywise vector equality, and admissible vectors form a directly characterizable discrete domain. A plausible implication is that the representation is particularly well suited to algorithms that need exact tree reconstruction after search or optimization steps.

## 2. Encoding and decoding algorithms

The encoding algorithm takes as input a rooted tree $\mathcal{T}$ with leaves labeled $0,\dots,n-1$ and internal nodes labeled $n,\dots,2n-2$, and returns the Phylo2Vec vector $v[1\dots n-1]$. Every branch is first labeled: the branch leading to leaf $j$ receives label $j$, while internal-branch labels run from $n$ to $2(n-1)$ in order of increasing “height” (distance to leaves), with ties broken by largest child-label. For each $j=1,\dots,n-1$, the coordinate $v_j$ is then defined as the label of the branch that “split” to create leaf $j$ in the birth-process construction [2304.12693].

Decoding proceeds in the opposite direction. One initializes the unique 2-leaf tree on $\{0,1\}$ and draws an “extra root” above it with one branch labeled $2$. Then, for $j=2,\dots,n-1$, one reads $b=v_j$, selects branch $b$ among the current $2(j-1)+1$ branch labels, splits that branch by inserting a new internal node, and attaches leaf $j$ to it. After each insertion, all branches are relabeled to restore the invariant of having labels $0,\dots,2j$. Finally, the extra root is removed and the true root relabeled as $2n-2$ [2304.12693].

A concrete $n=4$ example illustrates the construction. For the rooted tree with Newick form
$$
(((0,2)4,1)5,3)6,
$$
the extracted vector is
$$
v=[0,0,4]\in\mathbb{Z}^3.
$$
Decoding starts from the 2-leaf tree on $\{0,1\}$, then splits branch $0$ to attach leaf $2$, and after relabeling splits branch $4$ to attach leaf $3$; after removal of the extra root, one recovers exactly the original tree [2304.12693].

The encode/decode pair gives Phylo2Vec an operational interpretation: the vector records a sequence of branch-splitting events in a canonical birth-process view of rooted binary tree construction.

## 3. Complexity, storage, and comparison properties

The decoding map $v\mapsto \text{tree}$ runs in $O(n)$ time: each of the $n-1$ insertions and relabelings can be done in amortized $O(1)$ with a suitable data structure. Encoding requires locating, for $j=1,\dots,n-1$, the branch that split to create $j$; with internal nodes pre-labeled this is $O(n\log n)$, and without pre-labeling it is $O(n^2)$. In practice, using just-in-time compilation with Numba, near-linear behavior was observed up to $n\approx 10^4$ [2304.12693].

The representation also supports direct uniform random sampling of rooted binary trees. If one samples
$$
v_j \sim \mathrm{Uniform}\{0\ldots 2(j-1)\}
$$
independently for $j=1,\dots,n-1$, then decoding yields a uniformly distributed binary tree in $O(n)$ time [2304.12693].

Compression is another explicit design goal. The vector $v$ needs only $\lceil \tfrac12 (n-1)\rceil$ bytes when stored as 16-bit integers, versus $O(n\log n)$ bytes for a Newick string, and up to $6\times$ reduction in memory was reported for $n$ up to $10^4$. Equality testing is unambiguous and linear: two trees are topologically identical if and only if their Phylo2Vec vectors coincide entrywise, so equality testing is $O(n)$, whereas canonizing Newick strings is stated as $O(n\log n)$. Duplicate removal over $M$ trees is described as trivial to parallelize [2304.12693].

These properties place Phylo2Vec in a regime that is simultaneously exact and algorithmically tractable. It provides a discrete coordinate system for tree topologies that can be sampled, stored, and compared without intermediate parsing or canonicalization.

## 4. Tree-space traversal and phylogenetic inference

By viewing trees as points $v\in\mathbb{V}$, Phylo2Vec induces explicit moves in vector space that correspond to topological rearrangements. “Small jumps” are defined by incrementing or decrementing a single coordinate $v_i$ by $\pm 1$, clamped to $[0,2(i-1)]$; this typically corresponds to a one-SPR move, and there are $\sum 2(i-1)=O(n^2)$ such neighbors. “Large jumps” are obtained by modifying multiple coordinates simultaneously or by switching root-equivalence class, noting that there are $2n-1$ rootings of any unrooted tree. These larger moves can jump across SPR distances saturating Robinson–Foulds or KF distances. Additional “shuffling” of leaf labels removes bias introduced by the initial labeling [2304.12693].

As a proof of concept for likelihood-based inference, Phylo2Vec was used in a simple hill-climbing scheme on five real datasets: Yeast (8 taxa, 127 k bp), H3N2 (19 taxa, 1.4 kb), M501 (29 taxa, 2.5 kb), FluA (69 taxa, 1 kb), and Zika (86 taxa, 10.8 kb). At each iteration, one randomly picks $i\in\{1,\dots,n-1\}$, computes
$$
\Delta \ell_{ij}=\ell(v[i\leftarrow j])-\ell(v)
$$
for all feasible $j$, takes the best-improving $j$, stops if no $\Delta \ell_{ij}>0$ for any $i$, and re-labels or shuffles $v$ every epoch to avoid label bias. Branch lengths and substitution parameters are re-optimized at each step via RAxML-NG [2304.12693].

Reported results indicate that the procedure usually converges in 1–3 epochs and $\ll 10^4$ likelihood evaluations despite tree-space size $\sim (2n-3)!!$. All runs reached the same or near-same ML tree as state-of-the-art tools such as RAxML-NG and IQ-TREE from random starts. For the Yeast dataset, exhaustive enumeration of approximately 105 trees showed that Phylo2Vec hill-climbing needed only 96 log-likelihood evaluations to reach the global optimum [2304.12693].

This suggests that the representation is not only storage-efficient but also search-compatible: coordinate updates in vector space can serve as a practical proposal mechanism for phylogenetic optimization.

## 5. Software ecosystem and relation to other vector encodings

A later software release, `phylo2vec` or `Phylo2Vec`, presents a high-performance package for encoding, manipulating, and analysing binary phylogenetic trees. Its core implementation is in Rust, with dedicated Python and R wrappers; the release is described as superseding the original Python release and as providing significant performance improvements and memory efficiency while remaining accessible to a broad audience in the bioinformatics community [2506.19490].

Related work has expanded the landscape of vector encodings for rooted phylogenetic trees.

| Representation | Vector length | Stated property |
|---|---:|---|
| Phylo2Vec | $n-1$ | bijection for binary trees; fast sampling and comparison |
| HOP tree representation | $2n$ | HOP distance computable in near-linear time |
| OLA | $n-1$ | encoding and decoding take linear time |

The HOP representation encodes a rooted binary tree on $n$ taxa as a vector of length $2n$ in which each taxon appears exactly twice, introduces a rearrangement operator called a HOP, and defines the HOP distance as the minimum number of HOPs required to transform one tree into another. That distance is computable in $O(n\log n)$ time, and experiments reported that it is better correlated to the Subtree-Prune-and-Regraft distance than the Robinson-Foulds distance [2405.07110]. The ordered leaf attachment (OLA) encoding also maps rooted binary tree topologies to integer vectors of length $n-1$; its encoding and decoding are linear time, and its image is described as a simply-described subset of integer sequences [2503.10169].

Taken together, these developments indicate that Phylo2Vec belongs to a broader program of replacing pointer-based or string-based tree representations with algebraically structured vector spaces that support exact reconstruction, tractable neighborhood operations, and direct use in downstream algorithms.

## 6. Order-dependent formulations, distances, and broader usage

A later theoretical treatment studies Phylo2Vec as an order-dependent representation. Let $X$ be a set of $n$ leaves and $\sigma:X\to\{1,2,\dots,n\}$ an ordering of $X$. For each $i=2,3,\dots,n$, let
$$
T_i \cong T\mid\{\rho,\sigma^{-1}(1),\dots,\sigma^{-1}(i)\},
$$
and define an $i$-th P2V-labeling $f_{P2V}^i$. The Phylo2Vec vector of $T$ under $\sigma$ is then
$$
v_{P2V}^\sigma(T)=[v_1,v_2,\dots,v_n]
$$
with
$$
v_1=0,\qquad v_2=1,\qquad v_i=f_{P2V}^i(s_i)\ \text{for } i=3,\dots,n,
$$
where $s_i$ is the sibling of the newly adjoined leaf in $T_i$ [2507.11254].

Given two such vectors under the same ordering $\sigma$, the dissimilarity is the Hamming distance
$$
d_{P2V}^\sigma(T,T')=\bigl|\{i:v_i\neq v_i'\}\bigr|,
$$
which is computable in $O(n)$ time once both vectors are in hand. No fixed-$\sigma$ P2V distance is known to correlate directly with rooted SPR distance: examples exist with $d_{rSPR}(T,T')<d_{P2V}^*(T,T')$ and also the reverse. However, if one minimizes over all orderings,
$$
d_{P2V}^*(T,T')=\min_\sigma d_{P2V}^\sigma(T,T')\le h(T,T'),
$$
where $h(T,T')$ is the hybrid number. If $S=(x_1,\dots,x_n)$ is a common cherry-picking sequence of $T$ and $T'$ and $\sigma$ is the induced ordering $\sigma(x_i)=n-i+1$, then
$$
d_{P2V}^\sigma(T,T')=\mathrm{wt}(S),
$$
and minimizing over all such induced orderings yields the temporal tree-child hybrid number $h_t(T,T')$ [2507.11254].

The name “Phylo2Vec” is also used more broadly in later machine-learning discussion. One paper explicitly notes that “Phylo2Vec” was not the term used in the original tropical-neural-network paper, and there it refers to learning Euclidean embeddings of trees by analogy to word2vec. That work contrasts such Phylo2Vec-style autoencoders with a tropical embedding $\phi:\mathbb{T}\to\mathbb{R}^N$ whose coordinates are tropical distances to learned reference points, emphasizing that the tropical construction is distinguished by explicit use of the tropical metric rather than by a purely data-driven encoder [2309.13410]. A plausible implication is that “Phylo2Vec” now denotes both a specific bijective integer encoding and, in a looser methodological sense, a class of vector-based phylogenetic representations used in storage, optimization, comparison, and learning.

Source: https://www.emergentmind.com/topics/phylo2vec