Abstract
Consider the problem of finding the maximum independent set in trees:
- Category: Dynamic Programming / Graph Optimization (restricted to trees)
- Input: A tree with vertex weights
- Output: The maximum-weight independent set of
- Paradigm: Dynamic Programming, bottom-up from leaves to root
- Typical use cases: the go-to example showing that restricting a graph to a tree can turn an NP-hard general problem — see Maximal Independent Set on general graphs — into one solvable in linear time
Problem Specification
- Instance: A tree with a weight on each vertex.
- Solution Format: A subset of vertices .
- Constraints: No two vertices in are connected by an edge.
- Objective: .
- Goal: Maximize.
Candidate Strategies / Approaches
General-Graph Backtracking ✘ (correct, but wasteful here)
The Maximal Independent Set backtracking approach (MIS1 → MIS2 → MIS3) works on any graph, including trees — but even its best refinement is exponential (, or Robson’s ). Trees have far more exploitable structure than that approach uses.
Dynamic Programming ✔
Key Idea
A tree has no cycles, so removing a vertex splits it cleanly into independent subtrees that share no vertices — this is exactly the structural property that turns an exponential general-graph search into a polynomial one (the same reason Shortest Path in a DAG beats general shortest-path search). For each vertex , track two answers instead of one: the best independent set of the subtree rooted at that includes , and the best one that excludes . Two answers are necessary because whether ‘s parent can safely include itself depends on whether was included.
Dynamic Programming Solution
1. Subproblems
Let , where is the weight of the maximum independent set of the subtree hanging from including vertex , and is the weight of the maximum independent set of the subtree hanging from excluding .
2. Base Case
If is a leaf, — a leaf’s only two options are “take just itself” (weight ) or “take nothing” (weight ).
3. Recursion
To compute , we need to know for every child of :

- : if is included, none of ‘s children can be included (they’re adjacent to ), so each child subtree must use its excluding answer — sum over all children , plus ‘s own weight.
- : if is excluded, each child subtree is free to independently pick whichever of its two answers is larger, since there’s no longer any constraint coming from .
4. Ordering of the Subproblems
Order by layers — start at the bottom (leaves) and work up to the root. Equivalently, a post-order traversal: finish computing for every child before computing .
5. Output
Pseudocode (Chosen Approach)
Algorithm 18 Tree MIS
Input: Tree rooted at , vertex weights
Output: Weight of the maximum independent set of
procedure TreeMIS()
if is a leaf then
return
for all children of do
TreeMIS()
return
Final answer: where .
Variables & Data Structures
| Name | Type | Purpose |
|---|---|---|
M[k] / (IN_k, OUT_k) | Pair of numbers, per vertex | The two subproblem answers for the subtree rooted at k |
r | Root vertex | Chosen arbitrarily if the input tree is unrooted — any vertex works as root |
Helper Functions / Operations Used
- Children lookup — for a rooted tree, each vertex’s children are simply its tree-neighbors other than its parent.
- Post-order recursion — the natural way to guarantee every child’s is computed before its parent’s.
Proof of Correctness
Claim: as computed above equals the true maximum-weight independent set of the subtree rooted at , including or excluding respectively.
- Base case: a leaf has exactly two options — include itself (weight , valid since a single vertex trivially has no internal conflicts) or include nothing (weight ) — so is correct.
- Inductive Hypothesis: is correct for every child of (guaranteed by the post-order ordering).
- Inductive Step:
- : if is in the chosen set, no child of can be (they’re each adjacent to ), so every child subtree must contribute its best excluding answer. Since different children’s subtrees share no vertices (tree structure), these choices don’t interact — the total is exactly , correct by the Inductive Hypothesis.
- : if is not in the chosen set, each child subtree is unconstrained by and can independently pick whichever of its two options is better — again, no interaction between different children’s subtrees, so the total is , correct by the Inductive Hypothesis.
- Since every valid independent set of the subtree at either includes or doesn’t, correctly captures the best of each case. At the root, correctly picks the better of the two, giving the true maximum-weight independent set of the whole tree.
Time & Space Complexity Analysis
General Case
| Complexity | Notes | |
|---|---|---|
| Time | Each vertex is processed exactly once; the work done at vertex is (one constant-time step per child), and for a tree ( edges total) | |
| Space | The M table stores 2 values per vertex; the recursion stack adds up to , which is worst case (a path-shaped tree) or for a balanced tree |
Best / Worst / Average Case
- Best / Worst / Average case: all — every vertex is visited exactly once regardless of the tree’s shape or the specific weights.
Drawbacks / Constraints
- Only works on trees. The correctness argument relies entirely on subtrees sharing no vertices — the moment cycles exist (general graphs), this clean separation breaks down, which is exactly why Maximal Independent Set on general graphs stays exponential even after heavy refinement.
- Requires a rooted tree. If given an unrooted tree, pick any vertex as root first (a single traversal) — the choice of root doesn’t affect the final answer, only how the recursion is organized.
- Recursive implementation risks stack overflow on very unbalanced trees (e.g. a long path) — an explicit iterative post-order traversal (using an explicit stack) avoids this if tree height could be large.
