This is a great question about computational phylogenetics. Let me break down the Felsenstein pruning algorithm comprehensively.
Overview of the Problem
In phylogenetics, we want to find the most likely evolutionary tree given sequence data. The likelihood of a tree depends on:
- The tree topology
- Branch lengths
- A model of sequence evolution
- The observed sequences at the leaves
Brute Force Approach (Why it's bad)
Without the algorithm, for each tree topology and set of branch lengths, we'd need to:
- Enumerate all possible ancestral states at internal nodes
- For each complete assignment, calculate probability of the tree given those states
- Sum over all possible assignments
If we have:
- n taxa (leaves)
- n-1 internal nodes
- k possible states (codons, nucleotides, amino acids)
Number of possible ancestral state combinations: k^(n-1)
For n=10 taxa and k=4 nucleotides: 4^9 ≈ 262,000 enumerations per tree!
This becomes intractable very quickly.
The Felsenstein Pruning Algorithm
The key insight is dynamic programming: we can compute likelihoods recursively from leaves to root, avoiding redundant calculations.
Core Concept: Likelihood at a Node
For any node j, define L_j(s) as: "the likelihood of the observed sequences in the subtree rooted at j, given that node j has state s"
Algorithm Steps
Initialization (at leaves):
For each leaf node i with observed sequence state s_i:
L_i(s) = 1 if s = s_i
L_i(s) = 0 if s ≠ s_i
Recursion (at internal nodes):
For an internal node j with two children (left, right):
L_j(s) = Σ_r Σ_l P(r|s,t_right) * L_right(r) * P(l|s,t_left) * L_left(l)
Where P(r|s, t) is the probability of observing state r at a descendant given state s at the ancestor over branch length t. We sum across all possible states r and l at the two children, weighted by their transition probabilities and the likelihoods of their respective subtrees.
Final likelihood:
Likelihood = Σ_s π(s) * L_root(s)
Where π(s) = equilibrium frequency of state s
Complexity Analysis
The algorithm reduces computation dramatically. At each node, we compute O(k²) operations for each possible state, evaluating all pairs of child states. This transforms an exponential problem into polynomial time—for n taxa, we perform roughly O(n·k³) operations instead of k^(n-1) enumerations.
Without dynamic programming, we'd need to evaluate k^(n-1) different ancestral state assignments, each requiring O(n·k²) likelihood calculations, yielding O(n·k^(n+1)) total operations. The DP approach achieves this by recognizing that subtree likelihoods follow optimal substructure—each node's likelihood depends only on its children's subtrees—and avoids recomputing the same subtree likelihoods across different ancestral state scenarios.
This transforms an exponential problem into linear scaling with the number of taxa, enabling analysis of realistic phylogenetic datasets with dozens or hundreds of species. We can also optimize branch lengths independently for each edge using this efficient calculation framework.
root
/ \
/ \
A B
/ \ / \
1 2 3 4
With sequences 1="A", 2="G", 3="C", 4="T", the leaf likelihoods are straightforward: each position has probability 1 for its observed state and 0 otherwise. At internal node A, we sum over all possible states, multiplying the transition probabilities from A to its children by their respective likelihoods. For instance, L_A(A) captures the probability of observing "A" at leaf 1 and "G" at leaf 2 given state A at node A, which simplifies to P(A|A,t₁)·P(G|A,t₂) since all other state combinations contribute zero. Similarly, L_A(G) = P(A|G,t₁)·P(G|G,t₂). Node B follows the same pattern with its leaves, and the root likelihood integrates across all possible root states weighted by their prior probabilities.
This dynamic programming approach avoids the exponential explosion of evaluating all 4⁴ = 256 possible internal state assignments. The substitution model—whether Jukes-Cantor with its symmetric transition rates or more complex models like HKY85 or GTR—determines the specific transition probability values P(r|s,t). Extensions handle multiple alignment columns independently, rate variation across sites, and larger state spaces like codons or amino acids, though the core algorithm remains unchanged for bifurcating trees.
The method has practical constraints: it requires a known tree topology, assumes sites evolve independently without covariation, and still needs numerical optimization for branch lengths. Compared to simpler approaches like parsimony that only count changes, or distance methods like neighbor-joining that sacrifice statistical rigor for speed, the Felsenstein algorithm provides a proper probabilistic framework—one that Bayesian methods like MrBayes and BEAST build upon when integrating over uncertainty in both trees and evolutionary parameters.
Felsenstein Pruning Algorithm
The Core Problem
Computing tree likelihood requires integrating over all possible ancestral states at internal nodes. Brute-force enumeration would evaluate k^(n-1) state combinations (where k=alphabet size, n=number of taxa). For 10 taxa with DNA: 4^9 ≈ 262,000 enumerations—and this explodes quickly.
Algorithm Overview
The pruning algorithm uses dynamic programming to compute likelihood bottom-up in a single tree traversal:
Define: L_j(s) = likelihood of observed sequences in subtree rooted
at node j, given node j has state s
Three Steps
1) Initialize leaves (data terminals):
For leaf i with observed state s_obs:
L_i(s) = 1 if s = s_obs
L_i(s) = 0 if s ≠ s_obs
2) Recurse at internal nodes (bottom-up):
For internal node j with children left, right:
L_j(s) = Σ_l Σ_r [P(l|s, t_left) · L_left(l)]
× [P(r|s, t_right) · L_right(r)]
Where:
P(l|s, t) = transition probability (state s → l over branch length t)
Σ_l Σ_r = marginalize over all possible child states
3) Sum at root:
Tree Likelihood = Σ_s π(s) · L_root(s)
where π(s) = equilibrium frequency of state s
Concrete Example
Simple 4-taxon tree:
Tree: MRCA
/ \
A B
/| |\
1 2 3 4
Seqs: 1=A, 2=G, 3=C, 4=T
Leaves (initialization):
L_1(A)=1, L_1(G)=0, ... [leaf 1 is A]
L_2(A)=0, L_2(G)=1, ... [leaf 2 is G]
L_3(A)=0, L_3(C)=1, ... [leaf 3 is C]
L_4(A)=0, L_4(T)=1, ... [leaf 4 is T]
Node A (internal):
L_A(A) = Σ_l Σ_r P(l|A,t₁) · L_1(l) · P(r|A,t₂) · L_2(r)
= P(A|A,t₁)·1·P(G|A,t₂)·1 + [15 zero terms]
= P(A|A,t₁) · P(G|A,t₂)
L_A(G) = P(A|G,t₁)·1·P(G|G,t₂)·1 + ...
Node B: Similar calculation, then root combines A and B likelihoods across all possible root states.
Key point: We never explicitly enumerate all 4³ = 64 internal state combinations—only compute what's necessary.
Complexity Comparison
| Approach | Operations | Scaling |
|---|
| Brute Force | k^(n-1) × O(n·k²) | O(n·k^(n+1)) — exponential! |
| Felsenstein | Sum over n nodes × k states × k² child pairs | O(n·k²) — linear! |
Speedup example (n=10, k=4):
- Brute force: ~10 × 4¹¹ ≈ 10 million operations
- Felsenstein: 10 × 16 = 160 operations
- 62,500× faster
Why This is Dynamic Programming
The algorithm satisfies the DP requirements:
-
Optimal substructure: Tree likelihood decomposes recursively
L_parent = f(L_left_subtree, L_right_subtree)
-
Overlapping subproblems: Without memoization, the same subtree likelihood would be recalculated in every brute-force enumeration path
-
Memoization via tree structure: Each node computed exactly once, bottom-up
Key Implementation Details
Substitution Model
Transition probabilities depend on the chosen model. For Jukes-Cantor:
P(i|i, t) = 1/4 + (3/4)·exp(-4μt/3) [same state]
P(i|j, t) = 1/4 - (1/4)·exp(-4μt/3) [different state]
For HKY85, GTR: more parameters, same algorithmic structure.
Numerical Stability
Working in log-space is critical to avoid underflow:
log_L_j(s) = log(Σ_l Σ_r exp[log P(l|s,t_l) + log L_left(l)
+ log P(r|s,t_r) + log L_right(r)])
Use the log-sum-exp trick to maintain numerical precision.
Extensions
| Scenario | Handling |
|---|
| Multiple sites | Apply independently to each column; sum log-likelihoods |
| Ambiguous data | Treat unknown bases as uniform likelihood over possibilities |
| Rate heterogeneity | Integrate across gamma-distributed rate categories |
| Codon sequences | Extend k from 4 to 61 (same algorithm) |
Limitations & Context
- Tree search still required: Felsenstein computes likelihood of one tree; modern methods use heuristics/MCMC to search tree space
- Branch length optimization: Still requires numerical optimization (Newton-Raphson, etc.) at each tree evaluation
- Relatedness to Bayesian methods: BEAST/MrBayes use Felsenstein inside MCMC for posterior inference
The pruning algorithm is the computational foundation for most modern probabilistic phylogenetics tools (RAxML, PAUP*, PhyML, IQ-TREE).