Sub topic 10
The Size-Bounded Merge Argument
This chapter assumes you have read the tree knapsack chapter and its best_rooted_subtree code. It also assumes you can do Big-O analysis by charging cost to elements, and that you know how a post-order traversal visits children before parents.
The tree knapsack merge has two nested loops over table indices, so it looks like O(k²) per edge and O(n·k²) in total. The real bound is O(n·k). This chapter explains why and shows which implementation details the bound depends on.
What one merge actually costs
When child u is merged into parent p, the parent table a already covers the part of p's subtree built so far. Call that part's size A. The child table b covers u's whole subtree, of size B. Both tables are capped at k, and the inner loop stops once i + j > k. The work for one merge is therefore at most:
cost(A, B) = min(A, k) · min(B, k)
Two details make this true. First, a table never grows longer than its subtree: merged has length min(len(a) + len(b) - 1, k + 1). Second, the loops run over len(a) and len(b), not over range(k + 1).
The uncapped case: O(n²)
Start with no cap (k ≥ n). A merge costs A · B, which equals the number of node pairs (x, y) with x on the parent side and y on the child side. After the merge, x and y are in the same table, so that pair is never counted again. In fact, each pair is counted exactly once, at their lowest common ancestor. There are fewer than n² pairs, so the total is O(n²). With the cap, the argument splits into cases.
The capped case: three kinds of merge
Call a side small if its size is below k and big otherwise.
| Case | Cost per merge | Charging rule | Total |
|---|---|---|---|
| Both small | A · B | Each node x pays for its partners while x's side is small | ≤ 2nk |
| One small, one big | k · (small size) | Each node on the small side pays k | ≤ nk |
| Both big | k² | Each such merge reduces the number of disjoint big pieces by one | ≤ nk |
Both small. Charge the cost to node pairs as in the uncapped case. A node x pays one unit for each partner it meets while its own side is small. The merged side has size below 2k, so every partner x meets in such merges ends up on x's side, and that side holds fewer than 2k nodes. After that, x's side is big and x never pays in this case again. So x pays fewer than 2k units, and the total is under 2nk.
One small, one big. The cost is k times the small side's size. Give k units to each node on the small side. After the merge, that node belongs to a big side and can never be on a small side again. Each node pays this at most once, so the total is at most nk.
Both big. Each big side contains at least k nodes, and the big sides are disjoint. So at most n/k big pieces ever exist independently. Each big-big merge joins two of them into one, so there are fewer than n/k such merges. Each costs k², for a total below nk.
Adding the cases gives fewer than 4nk operations, which is O(n·k).
Measuring it
The script below simulates only the sizes and adds up min(A, k) · min(B, k) for each merge. Every node's parent has a smaller index, so processing indices in decreasing order merges children before parents.
import random
def merge_work(parent, k):
n = len(parent)
size = [1] * n
work = 0
for u in range(n - 1, 0, -1):
p = parent[u]
work += min(size[p], k) * min(size[u], k)
size[p] += size[u]
return work
n, k = 20000, 50
trees = {
"path": [max(i - 1, 0) for i in range(n)],
"star": [0] * n,
"random": [0] + [random.randrange(i) for i in range(1, n)],
}
for name, parent in trees.items():
w = merge_work(parent, k)
print(f"{name:7s} work={w:>10d} work/(n*k)={w / (n * k):.2f}")In every run, the ratio work/(n*k) stays below 4, whatever the tree's shape. Try raising k toward n: the work approaches the O(n²) pair count, as the uncapped argument predicts.
Common errors
- Looping to k instead of to table length. This gives O(n·k²) work, as the warning above explains.
- Forgetting the cap on the merged table. Tables then grow to full subtree size, and the cost becomes O(n²) even for small k.
- Thinking order matters here. Unlike small-to-large merging, this bound holds for any order of children. No heavy-child trick is needed.
Exercise
On a star with center 0 and n − 1 leaves, merged one leaf at a time, what is the total merge work as a function of n and k, and how does it compare with the n·k bound?
Show answer
The merge of the j-th leaf costs min(j, k) · 1, because the center's side has size j and the leaf has size 1. The sum is k(k+1)/2 + (n − 1 − k)·k when n − 1 ≥ k. That is roughly n·k, inside the bound. Each leaf is a small side merged into the center once, which is the "one small, one big" case.
Exercise
A programmer stores dp[u] as a list of length k + 1 filled with NEG and loops i and j over range(k + 1). The answers are correct. What is the running time, and what one change restores O(n·k)?
Show answer
Each of the n − 1 merges costs about k² operations, so the total is O(n·k²). Make each list's length min(subtree size, k) + 1 and loop over len(a) and len(b). Each merge then costs at most min(A, k) · min(B, k).