Tree Dynamic Programming
Process nodes postorder and combine selected versus unselected child states for maximum independent set.
Core idea
Root the tree and define a small state for each parent-child constraint. For maximum independent set, one state selects the node and forces children out; the other leaves it out and frees each child.
Read the visualization
Rows follow postorder so child states are ready before their parent. The two columns expose the selected and unselected recurrences and their final root comparison.
| select | not selected | |
|---|---|---|
| root 4 | ||
| L 1 | ||
| R 5 | ||
| LL 3 | ||
| LR 6 |
Root the tree and prepare selected and unselected states for every node.
Complexity and tradeoffs
Time: O(n). Space: O(n). Each edge contributes once to a constant number of states; recursion depth follows tree height.
Where it fits
Tree DP solves independent sets, vertex covers, subtree allocation, and many hierarchical selections. The key is choosing a state that makes child subproblems independent.