Felsenstein's Tree-Pruning Algorithm
Felsenstein's tree-pruning algorithm is a post-order dynamic programming recursion that computes the phylogenetic likelihood of a sequence alignment in linear time with respect to taxon count.
Felsenstein's tree-pruning algorithm is a foundational dynamic-programming algorithm introduced by Joseph Felsenstein in 1973 and popularized for nucleotide sequences in 1981 that computes the exact likelihood of a molecular sequence alignment on a fixed phylogenetic tree topology and branch-length vector. In maximum-likelihood phylogenetics and Bayesian inference, the characters observed at the tips (leaves) of an evolutionary tree are known from DNA sequencing, whereas the ancestral character states at the N โ 1 internal nodes of a rooted binary tree of N taxa are unobserved latent variables.
Calculating the marginal probability P(xh | ฯ, t) of observing alignment column xh requires summing the joint probability over all ANโ1 possible combinations of ancestral character states at the internal nodes, where A is the alphabet size (A = 4 for nucleotides, A = 20 for amino acids, or A = 61 for sense triplets in codon substitution models). Naive brute-force summation over all ANโ1 ancestral state histories grows according to an exponential function exp((N โ 1) ln A), reaching 499 โ 4.0 ร 1059 terms for a modest 100-taxon DNA tree. Felsenstein's pruning algorithm exploits the conditional independence of evolutionary lineages under a Markov chain model to move summation signs inward across the tree hierarchy, reducing the computational complexity per site from O(AN) to linear time O(N ยท A2).
Mathematical Formulation of the Pruning Recursion
Conditional Subtree Likelihood Vectors Lv(s)
Consider a rooted binary phylogenetic tree ฯ with branch lengths t and a continuous-time Markov substitution model defined by equilibrium state frequencies ฯs and branch transition probability matrices P(t) = exp(Qt), where Psx(t) is the probability that state s โ {1, ..., A} at a parent node evolves into state x โ {1, ..., A} at a child node separated by branch length t. Assume that sites h = 1, ..., L evolve independently and identically, so that the total log-likelihood of the alignment X is the sum over unique site patterns:
โ(ฯ, t) = ln P(X | ฯ, t) = โh=1L ln P(xh | ฯ, t)
For a single site h and any node v in the tree, Felsenstein defined the conditional subtree likelihood Lv(h)(s) as the probability of observing all tip character states at or below node v (in the clade rooted at v), given that node v itself is in character state s โ {1, ..., A}.
Initialization at Tip Nodes and Post-Order Internal Recursion
Felsenstein's algorithm evaluates the conditional likelihood vector Lv(h) = [Lv(h)(1), ..., Lv(h)(A)]T from the tips down to the root via a bottom-up post-order tree traversal (visiting both child nodes u and w before visiting their parent node v):
- Initialization at Leaf (Tip) Nodes: For any external tip node k โ {1, ..., N} where the observed character in alignment column h is xk:
- Unambiguous Character: Lk(h)(s) = 1 if s = xk, and Lk(h)(s) = 0 for all s โ xk. (For example, if tip k has nucleotide
Ain state order{A, C, G, T}, its tip vector is[1, 0, 0, 0]). - Missing Data, Gap (
-), or IUPAC Ambiguity Code: Because missing data or ambiguity codes represent uncertainty about which state is observed at tip k, Lk(h)(s) = 1 for every state s compatible with the symbol (e.g.,[1, 1, 1, 1]forNor-, and[1, 0, 1, 0]for purineR).
- Post-Order Recursion at Internal Nodes: For an internal node v with left child u (connected by branch length tu) and right child w (connected by branch length tw), evolution along branch tu and its descendant clade is conditionally independent of evolution along branch tw and its descendant clade once the ancestral state s at parent node v is specified. Therefore, for each possible parent state s โ {1, ..., A}:
Lv(h)(s) = [ โx=1A Psx(tu) Lu(h)(x) ] ร [ โy=1A Psy(tw) Lw(h)(y) ]
Once Lv(h) is computed from Lu(h) and Lw(h), the descendant subtrees below u and w have been algebraically "pruned" into the A-element vector at node v.
- Marginal Termination at the Root Node (r): When the post-order traversal reaches the root node r, the unconditional site likelihood P(xh | ฯ, t) is obtained by weighting each possible root state s by its prior equilibrium frequency ฯs:
P(xh | ฯ, t) = โs=1A ฯs Lr(h)(s)
| Sequence Alphabet | Alphabet Size (A) | Matrix-Vector Cost per Node (2A2) | Brute-Force States for N = 50 Taxa (A49) | Felsenstein Pruning Operations per Site for N = 50 |
|---|---|---|---|---|
| Binary / Restriction Sites | A = 2 | 8 FLOPs | 249 โ 5.63 ร 1014 | 49 ร 8 = 392 FLOPs |
| DNA / RNA Nucleotides | A = 4 | 32 FLOPs | 449 โ 3.17 ร 1029 | 49 ร 32 = 1,568 FLOPs |
| Translated Amino Acids | A = 20 | 800 FLOPs | 2049 โ 5.63 ร 1063 | 49 ร 800 = 39,200 FLOPs |
| Universal Sense Codons | A = 61 | 7,442 FLOPs | 6149 โ 4.90 ร 1087 | 49 ร 7,442 = 364,658 FLOPs |
Pulley Principle, Rate Heterogeneity, and Numerical Optimizations
Felsenstein's Pulley Principle for Unrooted Trees
When the underlying substitution model Q is time-reversibleโsatisfying the detailed balance condition ฯi qij = ฯj qji, which in turn ensures ฯi Pij(t) = ฯj Pji(t)โ combined with the Chapman-Kolmogorov semigroup property P(t1 + t2) = P(t1)P(t2), the site likelihood P(xh | ฯ, t) is completely invariant to the placement of the root node r anywhere along any branch of the tree. Felsenstein termed this invariance the Pulley Principle.
Because of the Pulley Principle, maximum-likelihood phylogenetics searches operate on unrooted trees (which have 2N โ 3 branches for N taxa) and place a virtual root on any internal branch being optimized during Nearest-Neighbor Interchange or Subtree Pruning and Regrafting. By caching the partial likelihood vectors pointing into both ends of an edge, the branch length te of any single edge e can be optimized by Newton-Raphson iteration in O(L ยท A2) time without recomputing the conditional likelihoods of the rest of the tree.
Discrete Gamma (+ฮ4) Rate Heterogeneity Across Sites
In real biological sequences, functional constraints cause substitution rates to vary substantially across alignment positions (for example, between first, second, and third codon positions, or between catalytic active sites and solvent-exposed loops). Following Ziheng Yang (1994), rate heterogeneity across sites is modeled by multiplying all branch lengths at site h by a relative rate factor rk drawn from a continuous Gamma(ฮฑ, ฮฑ) distribution of mean 1 and variance 1/ฮฑ, approximated by K = 4 equal-probability discrete categories (+ฮ4) with rates r1, r2, r3, r4 (each having prior weight 1/K = 0.25).
To evaluate the likelihood under a +ฮK model, Felsenstein's pruning algorithm is executed independently for each rate multiplier rk (replacing P(t) with P(rk t)), and the site likelihood is averaged across the K rate categories before taking the logarithm:
P(xh | ฯ, t, ฮฑ) = โk=1K (1 / K) [ โs=1A ฯs Lr(h,k)(s) ]
Site-Pattern Compression and Floating-Point Underflow Scaling
Modern phylogenetic engines (such as RAxML, IQ-TREE, and PhyML) accelerate Felsenstein's pruning algorithm via two essential engineering techniques:
- Site-Pattern Compression (Aliasing): Any two alignment columns xh and xm that display the exact same pattern of characters across all N tips have identical conditional likelihood vectors at every node of the tree. Compressing L total alignment columns into Lu โค L unique site patterns with integer multiplicity weights wp reduces the total runtime to O(N ยท Lu ยท K ยท A2).
- Floating-Point Underflow Rescaling: On trees containing hundreds or thousands of taxa, multiplying probabilities Psx(t) < 1 across hundreds of internal nodes causes the conditional likelihoods Lv(s) to decay exponentially toward zero as exp(โcN), eventually underflowing double-precision IEEE 754 floating-point representation (~2.2 ร 10โ308). Whenever maxs Lv(s) falls below a safety threshold (such as 2โ256), the vector Lv is divided by its maximum element Mv = maxs Lv(s) (or scaled by a power of 2), and ln(Mv) is accumulated into a per-site log-scaling ledger added back to the final log-likelihood at the root.
Frequently Asked Questions
Why is Felsenstein's pruning algorithm O(N ยท A2) per site instead of O(N ยท A)?
At each of the N โ 1 internal nodes of a binary tree, the algorithm must evaluate the conditional likelihood Lv(s) for each of the A possible parent states s โ {1, ..., A}. For each parent state s, it computes the inner product โx=1A Psx(t) Lu(x) over all A possible child states x on both the left and right branches, requiring 2A2 floating-point multiplications per node and O(N ยท A2) operations across the entire tree.
How does Felsenstein's pruning algorithm handle gaps (-) and missing data (N)?
Under standard time-reversible Markov substitution models that do not explicitly model insertion-deletion events as an extra state, a gap (-) or unknown nucleotide (N) at tip k is treated as missing data (complete uncertainty about which character state is present). Its tip conditional likelihood vector is initialized to all ones, Lk(s) = 1 for all s โ {1, ..., A}, which algebraically prunes away a terminal branch Containing only missing data without biasing the remaining tree likelihood.
What is Felsenstein's Pulley Principle and when does it break down?
Felsenstein's Pulley Principle states that the likelihood of an alignment on a tree is unchanged when the root is moved along any branch, so long as the sum of the two sub-branch lengths attached to the root stays constant. It holds whenever the substitution model satisfies time-reversibility (ฯi qij = ฯj qji) and stationarity; it breaks down under non-reversible substitution models (such as general non-reversible 12-rate DNA matrices) or non-stationary base-composition models across lineages.
References
- Felsenstein, J. (1981). "Evolutionary trees from DNA sequences: A maximum likelihood approach." Journal of Molecular Evolution, 17(6), 368โ376. https://doi.org/10.1007/BF01734359
- Felsenstein, J. (1973). "Maximum likelihood and minimum-steps methods for estimating evolutionary trees from data on discrete characters." Systematic Zoology, 22(3), 240โ249. https://doi.org/10.2307/2412304
- Yang, Z. (1994). "Maximum likelihood phylogenetic estimation from DNA sequences with variable rates over sites: Approximate methods." Journal of Molecular Evolution, 39(3), 306โ314. https://doi.org/10.1007/BF00160154
- Felsenstein, J. (2004). Inferring Phylogenies. Sinauer Associates. https://doi.org/10.1086/424804