Summarizing Felsenstein pruning
The pruning algorithm computes conditional likelihood vectors at each node via post-order traversal, combining children's likelihoods with transition probabilities, yielding overall tree likelihood efficiently as a product across sites. This dynamic programming approach avoids the exponential blowup of enumerating ancestral states by summing partial likelihoods per state per node, achieving O(n·s²) complexity instead of exponential.
What maximum‑likelihood programmes such as PhyML, RAxML or MrBayes need in the inner loop is, for a fixed tree topology, set of branch lengths and a chosen substitution model, the probability of the observed characters at the tips.
Under the usual models of sequence evolution each site evolves as a continuous–time Markov chain on a finite state space (A,C,G,T for DNA, 20 amino acids for protein, …), and sites are independent. For a single site with pattern (D) at the leaves, the likelihood of the tree (T) can be written as
[
L(T\mid D)=\sum_{x_{\text{root}}}\pi_{x_{\text{root}}}!!\sum_{x_1}\cdots\sum_{x_m}
\prod_{(u,v)\in E} P_{x_u x_v}(t_{uv}),
]
where (x_1,\dots,x_m) range over the unobserved states at the internal nodes, (P_{ij}(t)) is the transition probability from (i) to (j) along a branch of length (t), and (\pi) is the equilibrium distribution at the root. A naïve evaluation of this sum by enumerating all (|\mathcal{S}|^{m}) assignments of states to the (m) internal nodes very quickly becomes infeasible.
Felsenstein’s 1981 “pruning” or “peeling” algorithm avoids that exponential blow‑up by doing the summations in a dynamic‑programming fashion. For each node (u) and for each possible state (i) define a partial likelihood
[
\ell_u(i) = \Pr\bigl(\text{data in the subtree below }u \mid \text{state at }u=i\bigr).
]
These (\ell)s can be computed in a single post‑order (tip‑to‑root) traversal:
-
At a tip (u) with observed state (d), (\ell_u(i)=1) if (i=d) (or an appropriate emission probability if ambiguous), otherwise (\ell_u(i)=0).
-
At an internal node (u) with children (v) and (w),
[
\ell_u(i) = \Bigl[\sum_j P_{ij}(t_{uv}),\ell_v(j)\Bigr];\Bigl[\sum_k P_{ik}(t_{uw}),\ell_w(k)\Bigr].
]
The terms in square brackets are the contributions of the two subtrees. For multifurcating nodes you take the product over all children.
Once the vector (\ell_{\text{root}}(i)) has been computed, the site likelihood is obtained by summing over the root state,
[
L(T\mid D) = \sum_i \pi_i,\ell_{\text{root}}(i).
]
For a whole alignment of independent sites, the total log–likelihood is the sum over sites of (\log L(T\mid D_s)).
Because each branch is visited once and each (\ell_u(i)) is obtained by summing over (|\mathcal{S}|) states of its children, the cost is (O(n,|\mathcal{S}|^2)) per site for a binary tree with (n) tips. This linear‑in‑(n) dynamic programme is vastly better than the brute‑force approach, which scales exponentially with the number of internal nodes, and is what makes maximum‑likelihood and Bayesian tree inference computationally tractable on realistic data sets.