Simple Algorithm
Machine Learning · Convergence · 28 min

Optimal & Interpretable Trees

Why the best tree is NP-hard to find, and modern ways around it.

Greedy growth picks the best split now, not the best tree. Finding the best tree is NP-hard, and a greedy tree can be far from it, yet mixed-integer programming, dynamic programming and branch-and-bound now find provably optimal sparse trees on real datasets. This lesson covers the hardness, the algorithms and their bounds, and the argument that high-stakes decisions should use such models instead of explained black boxes.

What you’ll learn

  • State the NP-completeness of optimal decision trees (Hyafil & Rivest, 1976), and explain what it does and does not rule out.
  • Construct distributions on which greedy induction is badly myopic, and compute the accuracy it loses at fixed depth.
  • Formulate optimal classification trees as a mixed-integer program, and prove the support bounds that let branch-and-bound and dynamic programming certify optimal sparse trees.
  • Explain certifiably optimal rule lists, the Rashomon set, and the case for interpretable models in high-stakes decisions.

How hard is the best tree?

Greedy induction optimizes one split at a time. The natural alternative optimizes the whole tree at once, for instance minimizing training error plus a penalty on size over all trees. The first question is how hard that is.

The variants that machine learning cares about, such as deciding whether a labeled sample can be fitted exactly by a tree of a given size or depth, are NP-hard as well. What this does not say is that practical instances are hopeless. NP-hardness is a worst-case statement as the tree size grows with the input. For a fixed depth DD and dd binary features, a complete tree chooses one of dd features at each of its 2D−12^D-1 internal nodes, and the best leaf labels follow by majority vote, so brute force over at most d 2D−1d^{\,2^D-1} trees is polynomial in dd and nn, if quickly impractical.

Think first With d=100d=100 binary features, a complete depth-2 tree has 1003=106100^3=10^6 possible feature assignments. How many does a complete depth-3 tree have?

A depth-3 tree has 23−1=72^3-1=7 internal nodes, so there are 1007=1014100^7=10^{14} assignments. Depth 2 is easy by brute force; depth 3 already needs dynamic programming, caching and bounds, which is what the methods below supply.

Greedy myopia

The classic failure is parity. Let x1,x2x_1,x_2 be independent fair bits and y=x1⊕x2y=x_1\oplus x_2. A split on either bit leaves both children at 50/50, so every concave impurity shows a decrease of exactly zero, although the depth-2 tree that splits on x1x_1 and then on x2x_2 is perfect. For kk-bit parity, no split helps until the last level of a depth-kk tree. A weak distractor turns this zero signal into a real loss at fixed depth:

DerivationA distractor captures the root
  1. ΔG(x1)=ΔG(x2)=0,ΔG(x3)=0.5−2(0.6)(0.4)=0.02\Delta G(x_1)=\Delta G(x_2)=0,\qquad\Delta G(x_3)=0.5-2(0.6)(0.4)=0.02

    Add a third bit x3x_3 that equals yy with probability 0.60.6, independently of everything else. At the root, splits on x1x_1 or x2x_2 leave yy uniform, while x3x_3 gives children with Pr⁡(y=1∣x3=1)=0.6\Pr(y=1\mid x_3=1)=0.6 and Pr⁡(y=1∣x3=0)=0.4\Pr(y=1\mid x_3=0)=0.4:

  2. Pr⁡(y=1∣x1=a, x3=1)=0.6⋅0.50.6⋅0.5+0.4⋅0.5=0.6(a=0,1)\Pr(y=1\mid x_1=a,\ x_3=1)=\frac{0.6\cdot0.5}{0.6\cdot0.5+0.4\cdot0.5}=0.6\qquad(a=0,1)

    Greedy splits on x3x_3. Inside the child x3=1x_3=1, condition on x1=ax_1=a: given x1x_1, yy is still a fair coin (because x2x_2 is), and x3x_3 depends on (x1,x2)(x_1,x_2) only through yy, so Bayes' rule gives the same posterior for both values of aa:

  3. Accgreedy(D=2)=0.6vsAccopt(D=2)=1\mathrm{Acc}_{\text{greedy}}(D=2)=0.6\qquad\text{vs}\qquad\mathrm{Acc}_{\text{opt}}(D=2)=1

    So every second-level split under the x3x_3 root has zero decrease, and the best greedy depth-2 tree predicts y=x3y=x_3, with accuracy 0.6. The depth-2 tree on x1x_1 then x2x_2 is exact.

  4. Accopt(2)−Accgreedy(2)=12−δ\mathrm{Acc}_{\text{opt}}(2)-\mathrm{Acc}_{\text{greedy}}(2)=\tfrac12-\delta

    With Pr⁡(x3=y)=12+δ\Pr(x_3=y)=\tfrac12+\delta the distractor's decrease is 2δ2>02\delta^2>0, so it captures the root however weak it is, and the depth-2 gap is 12−δ\tfrac12-\delta. Greedy recovers only by growing deeper (depth 3 here), which costs data and readability.

Optimal trees by mixed-integer optimization

Bertsimas & Dunn (2017) wrote the search for an optimal depth-DD tree as a mixed-integer optimization (MIO) problem. Fix the complete tree of depth DD, with branch nodes B\mathcal B and leaves L\mathcal L. For each branch node mm, binary variables dmd_m (does it split?) and ajma_{jm} (on feature jj?) and a threshold bmb_m; for each example ii and leaf tt, a binary zitz_{it} (does ii land in tt?); for each leaf, binary class labels and a count LtL_t of misclassified examples. Features are scaled to [0,1][0,1], and ϵ>0\epsilon>0 is smaller than any gap between distinct feature values.

min⁡  1L^∑t∈LLt+α∑m∈Bdms.t.  am⊤xi ≥ bm−M(1−zit)if t lies in the right subtree of mam⊤xi+ϵ ≤ bm+M(1−zit)if t lies in the left subtree of m∑t∈Lzit=1,∑jajm=dm,zit,ajm,dm∈{0,1}\begin{aligned}\min\ \ &\frac1{\hat L}\sum_{t\in\mathcal L}L_t+\alpha\sum_{m\in\mathcal B}d_m\\ \text{s.t.}\ \ &\mathbf a_m^\top\mathbf x_i\ \ge\ b_m-M(1-z_{it})&&\text{if }t\text{ lies in the right subtree of }m\\ &\mathbf a_m^\top\mathbf x_i+\epsilon\ \le\ b_m+M(1-z_{it})&&\text{if }t\text{ lies in the left subtree of }m\\ &\textstyle\sum_{t\in\mathcal L}z_{it}=1,\qquad\sum_ja_{jm}=d_m,\qquad z_{it},a_{jm},d_m\in\{0,1\}\end{aligned}
L^\hat L is the baseline error of predicting the majority class, so the first term is relative error; α\alpha prices each split. When zit=1z_{it}=1 the "big-M" routing constraints force example ii down the path to leaf tt, and when zit=0z_{it}=0 they switch off (MM of about 1 suffices for scaled features). Further linear constraints tie LtL_t to the examples in leaf tt and its label.

A general MIO solver, warm-started with a CART tree, returns a tree together with a bound on how far it can be from optimal, and proves optimality when the bound closes. Letting am\mathbf a_m range over Rd\R^d instead of indicator vectors gives optimal trees with oblique (hyperplane) splits. On 53 UCI datasets, Bertsimas & Dunn reported average out-of-sample gains over CART of 1–2 percentage points with axis-aligned splits and 3–5 with hyperplane splits; the size of the gain varies across datasets and studies. The weakness of generic MIO is scale: the number of binary variables grows with n⋅2Dn\cdot2^D, and solvers often stop at a time limit with a gap still open.

Branch-and-bound and dynamic programming for sparse trees

A second line of work, by Rudin, Seltzer and colleagues, uses binary features (numeric ones are binarized at chosen thresholds), a regularized objective, and specialized search with strong bounds. For a tree TT with ∣T∣|T| leaves, minimize

R(T)=1n∑i=1n1{T(xi)≠yi}+λ ∣T∣R(T)=\frac1n\sum_{i=1}^n\mathbf 1\{T(\mathbf x_i)\ne y_i\}+\lambda\,|T|
Each leaf costs λ\lambda, so a split is kept only if it pays for itself in training accuracy.
DerivationProof: sibling leaves first, then any leaf
  1. au+av−ap ≥ λa_u+a_v-a_p\ \ge\ \lambda

    Let leaves u,vu,v be siblings with parent pp, and let ata_t be the fraction of all nn examples that tt classifies correctly (apa_p for pp made into a leaf). Merging uu and vv saves λ\lambda and loses au+av−apa_u+a_v-a_p of accuracy, so optimality requires

  2. ap ≥ au+1n#{i∈v: yi=y^u} ≥ aua_p\ \ge\ a_u+\frac1n\#\{i\in v:\ y_i=\hat y_u\}\ \ge\ a_u

    The merged leaf predicts the majority label of the union, which does at least as well as predicting uu's label on all of it:

  3. au, av ≥ λa_u,\ a_v\ \ge\ \lambda

    Combining the two, av≥au+av−ap≥λa_v\ge a_u+a_v-a_p\ge\lambda, and symmetrically au≥λa_u\ge\lambda.

  4. au ≥ λ for every leaf⟹supp(p) ≥ 2λ,∣T∣≤1/λa_u\ \ge\ \lambda\ \text{for every leaf}\qquad\Longrightarrow\qquad\mathrm{supp}(p)\ \ge\ 2\lambda,\qquad|T|\le1/\lambda

    A leaf uu whose sibling is a whole subtree SS obeys the same bound. Delete uu and let SS take the parent's place, so that uu's examples are classified by SS's leaves. This saves λ\lambda, leaves SS's own examples untouched and loses at most aua_u, so optimality again forces au≥λa_u\ge\lambda. Every internal node has leaves below both children, so it is reached by at least 2λ2\lambda of the examples; and since the leaves' correct fractions sum to at most 1, there are at most 1/λ1/\lambda leaves.

Predict before you look

With λ=0.01\lambda=0.01 and n=10,000n=10{,}000 training examples, at least how many examples must each leaf of an optimal tree classify correctly?

Show the answer

100 examples

λn=100\lambda n=100. Each internal node must be reached by at least 2λn=2002\lambda n=200 examples, and an optimal tree has at most 1/λ=1001/\lambda=100 leaves. Any partial tree that violates these bounds can be discarded without exploring it.

Such bounds, plus a lower bound for every partial tree (the errors already committed by its finished leaves plus λ\lambda per leaf), let a search discard every partial tree whose bound exceeds the best objective found so far. OSDT (Hu, Rudin & Seltzer, 2019) organized this as branch-and-bound. GOSDT (Lin, Zhong, Hu, Rudin & Seltzer, 2020) recast it as dynamic programming over subproblems, the subsets of examples that can reach a node, stored as bit vectors and cached, and extended it to objectives such as balanced accuracy and F-score. DL8 (Nijssen & Fromont, 2007) and MurTree (Demirović et al., 2022) use dynamic programming over itemsets with caching, MurTree with a specialized solver for depth-2 subtrees. As of the mid-2020s these methods certify optimal shallow trees on many real datasets with thousands of examples; depth and the binarization of continuous features remain the main limits.

Rule lists, the Rashomon set and interpretability

A rule list is a one-sided tree: "if p1p_1 then y^1\hat y_1, else if p2p_2 then y^2\hat y_2, …, else y^0\hat y_0", each pjp_j a conjunction of binary features. CORELS (Angelino, Larus-Stone, Alabi, Seltzer & Rudin, 2018) minimizes misclassification plus λ\lambda times the list length by branch-and-bound over prefixes of the list, with a prefix tree, a symmetry-aware map that merges equivalent prefixes, and support bounds like those above, and returns a certificate of optimality.

text
if (age = 18-20) and (sex = male) then predict yes
else if (age = 21-23) and (priors = 2-3) then predict yes
else if (priors > 3) then predict yes
else predict no

A rule list found by CORELS for predicting two-year recidivism on ProPublica's Broward County data. Angelino et al. (2018) report that such optimal sparse rule lists are about as accurate as the proprietary COMPAS risk tool, while every prediction can be checked by hand.

Rudin (2019) drew the policy conclusion. A post-hoc explanation of a black box is a second model that approximates the first, so it can be unfaithful exactly where it matters, and for high-stakes decisions on structured data with meaningful features she argues that interpretable models often match black-box accuracy and should be used instead.

Why would a simple model be nearly as accurate? Consider the Rashomon set: all models in a class whose loss is within ϵ\epsilon of the best (the name is Breiman's, after the film in which witnesses give different but equally plausible accounts). Semenova, Rudin & Parr (2022) argue that when this set is large relative to the hypothesis space, as measured by their Rashomon ratio, it is likely to contain simple models too; a practical sign is that very different methods reach similar accuracy on the same data. Xin et al. (2022) enumerated the entire Rashomon set of sparse decision trees on real datasets, so that users can choose among near-optimal trees by criteria the training objective did not encode, and can study variable importance across the whole set rather than for one tree.

Check your understanding

On a tabular dataset, a boosted ensemble, a random forest and a sparse depth-4 tree all reach about the same test accuracy. What does the Rashomon-set view suggest?

  1. The sparse tree must be overfitting, since it matches far larger models
  2. The near-optimal set is probably large, so a simple, readable model may lose little accuracy, and conclusions drawn from any single model's structure are fragile
  3. The ensemble is underfitting and should be grown larger
  4. The three models must make the same prediction on every example
Show the answer

B. The near-optimal set is probably large, so a simple, readable model may lose little accuracy, and conclusions drawn from any single model's structure are fragile

Similar accuracy from very different model families is the practical signature of a large Rashomon set. It means interpretable models are worth looking for, and also that "the" model is a choice: many models fit equally well while relying on different features.

Key takeaways

  • Building trees of minimum expected test count is NP-complete (Hyafil & Rivest, 1976), a worst-case statement as size grows; fixed-depth search is polynomial, if large (d 2D−1d^{\,2^D-1} assignments).
  • Greedy induction is myopic: parity gives every first split zero decrease, and a weak distractor that agrees with yy with probability 12+δ\tfrac12+\delta captures the root and costs 12−δ\tfrac12-\delta in accuracy at depth 2.
  • Optimal depth-DD trees can be written as mixed-integer programs with big-M routing constraints (Bertsimas & Dunn, 2017), including oblique splits; generic solvers struggle with scale.
  • For R(T)=error+λ∣T∣R(T)=\text{error}+\lambda|T|, every leaf of an optimal tree classifies correctly at least a fraction λ\lambda of the data and every internal node is reached by at least 2λ2\lambda; such bounds power branch-and-bound and dynamic programming (OSDT, GOSDT, DL8, MurTree) and CORELS for rule lists.
  • Large Rashomon sets explain why simple models are often nearly as accurate as complex ones; Rudin argues that in high-stakes settings they should replace explained black boxes.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What does the NP-completeness result of Hyafil & Rivest (1976) imply?

  1. No algorithm can find an optimal depth-3 tree on a real dataset
  2. Greedy trees are always within a constant factor of optimal
  3. Unless P = NP, no polynomial-time algorithm builds trees of minimum expected test count for every instance as the instances grow
  4. Decision trees cannot be learned from data efficiently in practice
Show the answer

C. Unless P = NP, no polynomial-time algorithm builds trees of minimum expected test count for every instance as the instances grow

NP-completeness is a worst-case statement about a family of instances of growing size. It is compatible with exact solutions for fixed depth (polynomial, if large) and with specialized solvers that certify optimal shallow trees on real data.

Question 2

Let y=x1⊕x2y=x_1\oplus x_2 for fair bits, and let x3x_3 equal yy with probability 12+δ\tfrac12+\delta for some small δ>0\delta>0. What does greedy Gini induction do at depth 2?

  1. Splits on x1x_1, then x2x_2, and classifies perfectly
  2. Splits on x3x_3 first, and its depth-2 accuracy is only 12+δ\tfrac12+\delta
  3. Picks the root at random, since all three splits tie
  4. Splits on x3x_3 first, then on x1x_1, and classifies perfectly
Show the answer

B. Splits on x3x_3 first, and its depth-2 accuracy is only 12+δ\tfrac12+\delta

x3x_3 is the only root split with a positive Gini decrease (2δ22\delta^2). Under it, x1x_1 and x2x_2 remain uninformative one at a time, so no second-level split helps and the tree predicts y=x3y=x_3. The optimal depth-2 tree is exact.

Question 3

With objective R(T)=training error+λ ∣T∣R(T)=\text{training error}+\lambda\,|T| and λ=0.02\lambda=0.02, at most how many leaves can an optimal tree have?

  1. 25
  2. 50
  3. 100
  4. There is no bound
Show the answer

B. 50

Each leaf of an optimal tree classifies correctly at least a fraction λ\lambda of the data, and these fractions sum to at most 1, so ∣T∣≤1/λ=50|T|\le1/\lambda=50.

Question 4

What is Rudin's (2019) main objection to explaining black-box models in high-stakes decisions?

  1. Explanations are too slow to compute
  2. Black boxes are always less accurate than interpretable models
  3. Interpretable models cannot be trained on tabular data
  4. A post-hoc explanation is a second, approximate model that can be unfaithful to the first, so interpretable models should be used where they perform comparably
Show the answer

D. A post-hoc explanation is a second, approximate model that can be unfaithful to the first, so interpretable models should be used where they perform comparably

An explanation that is right most of the time can still be wrong on the very cases that matter. Rudin argues that on structured data with meaningful features, interpretable models often match black-box accuracy, so the black box and its explanation are unnecessary there.

Further reading

  • L. Hyafil & R. L. Rivest, "Constructing optimal binary decision trees is NP-complete," Information Processing Letters 5(1), 15–17 (1976).
  • D. Bertsimas & J. Dunn, "Optimal classification trees," Machine Learning 106(7), 1039–1082 (2017).
  • X. Hu, C. Rudin & M. Seltzer, "Optimal sparse decision trees," Advances in Neural Information Processing Systems 32 (2019).
  • E. Angelino, N. Larus-Stone, D. Alabi, M. Seltzer & C. Rudin, "Learning certifiably optimal rule lists for categorical data," Journal of Machine Learning Research 18(234), 1–78 (2018).
  • C. Rudin, "Stop explaining black box machine learning models for high stakes decisions and use interpretable models instead," Nature Machine Intelligence 1(5), 206–215 (2019).