Algorithm Search⌘K

Proofs: Knapsack branch and bound

These are the human proofs of Knapsack branch and bound, at full length.

Knapsack branch and bound returns an optimal packing full

This proof shows: correctness, termination.

We use the pseudocode of the page. The items are sorted so that v1/w1≥⋯≥vn/wnv_1 / w_1 \ge \cdots \ge v_n / w_n, with items of weight 0 first. For 0≤k≤n0 \le k \le n, a set Y⊆{1,…,n}Y \subseteq \{1, \ldots, n\} extends a node (k,cw,cv,X)(k, c_w, c_v, X) if Y∩{1,…,k}=XY \cap \{1, \ldots, k\} = X. A set is feasible if its weight is at most WW. Let F(k,cw,cv)F(k, c_w, c_v) be the largest value of cv+∑i∈Zvic_v + \sum_{i \in Z} v_i over sets Z⊆{k+1,…,n}Z \subseteq \{k + 1, \ldots, n\} with cw+∑i∈Zwi≤Wc_w + \sum_{i \in Z} w_i \le W.

Lemma 1 (the bound is valid). If cw≤Wc_w \le W, then U(k,cw,cv)≥F(k,cw,cv)U(k, c_w, c_v) \ge F(k, c_w, c_v), where UU is the bound rounded down.

Proof. Let r=W−cwr = W - c_w. A fractional packing of items k+1,…,nk + 1, \ldots, n is a vector y∈[0,1]n−ky \in [0, 1]^{n-k} with ∑yiwi≤r\sum y_i w_i \le r. Let gg be the greedy vector: yi=1y_i = 1 for items k+1,…,j−1k + 1, \ldots, j - 1, yj=(r−∑i<jwi)/wjy_j = (r - \sum_{i < j} w_i) / w_j for the first item jj that does not fit, and yi=0y_i = 0 after it (if all items fit, gg is all ones). We show that gg maximizes ∑yivi\sum y_i v_i. Let yy be any fractional packing, and let ρ=vj/wj\rho = v_j / w_j (or ρ=0\rho = 0 if all items fit). For i<ji < j, vi≥ρwiv_i \ge \rho w_i and gi=1≥yig_i = 1 \ge y_i; for i>ji > j, vi≤ρwiv_i \le \rho w_i and gi=0≤yig_i = 0 \le y_i. So

∑i(gi−yi)vi≥ρ∑i(gi−yi)wi=ρ(r−∑iyiwi)≥0\sum_i (g_i - y_i) v_i \ge \rho \sum_i (g_i - y_i) w_i = \rho \Big(r - \sum_i y_i w_i\Big) \ge 0

when jj exists, because then ∑igiwi=r\sum_i g_i w_i = r; when all items fit, gi=1≥yig_i = 1 \ge y_i for all ii and the sum is clearly nonnegative. Items of weight 0 are first and have gi=1g_i = 1. A 0-1 packing ZZ is a fractional packing, so cv+∑i∈Zvi≤cv+∑givic_v + \sum_{i \in Z} v_i \le c_v + \sum g_i v_i, the unrounded bound. The left side is an integer, so it is also at most the rounded-down bound UU. □\square

Lemma 2 (the take child keeps the bound). If cw+wk+1≤Wc_w + w_{k+1} \le W, then U(k+1,cw+wk+1,cv+vk+1)=U(k,cw,cv)U(k + 1, c_w + w_{k+1}, c_v + v_{k+1}) = U(k, c_w, c_v).

Proof. The greedy fill from item k+1k + 1 with room rr takes item k+1k + 1 whole, because wk+1≤rw_{k+1} \le r, and then continues on items k+2,…k + 2, \ldots with room r−wk+1r - w_{k+1}. This is the greedy fill of the child, and the value vk+1v_{k+1} moves from the fill into cvc_v. □\square

Claim 3 (the incumbent is real). At each point, bestbest is a feasible set of value zz. Proof. Initially best=∅best = \emptyset and z=0z = 0. Each pushed node has cw≤Wc_w \le W (the root has cw=0c_w = 0, the leave child keeps cwc_w, and line L9 tests the take child), and cwc_w and cvc_v are the weight and value of XX. Line L5 copies such a node. □\square

Claim 4 (invariant). At the start of each iteration of line L3, every feasible set YY with value greater than zz extends some node on SS.

Proof. At the start, SS holds the root, which every set extends. Suppose the claim holds at the start of an iteration. Let N=(k,cw,cv,X)N = (k, c_w, c_v, X) be the node popped, z0z_0 the incumbent before line L5, and z1≥z0z_1 \ge z_0 the incumbent at the end of the iteration. Let YY be feasible with value greater than z1z_1; it extends a node that was on SS. If that node is not NN, it is still on SS. Suppose it is NN, so Y∩{1,…,k}=XY \cap \{1, \ldots, k\} = X, and the value of YY is at most F(k,cw,cv)≤U(k,cw,cv)F(k, c_w, c_v) \le U(k, c_w, c_v) by Lemma 1.

  • If k=nk = n, then Y=XY = X, and line L5 gives z1≥cvz_1 \ge c_v, the value of YY: false.
  • If U(k,cw,cv)≤z1U(k, c_w, c_v) \le z_1 at line L6: false, as the value of YY is at most UU.
  • If k+1∉Yk + 1 \notin Y, then YY extends the leave child; its value is at most F(k+1,cw,cv)≤U(k+1,cw,cv)F(k + 1, c_w, c_v) \le U(k + 1, c_w, c_v), so this bound is greater than z1z_1 and line L7 pushes the child.
  • If k+1∈Yk + 1 \in Y, then cw+wk+1≤c_w + w_{k+1} \le the weight of Y≤WY \le W, so line L9 pushes the take child, which YY extends.

So the claim holds at the start of the next iteration. □\square

Claim 5 (termination). Each pushed node has a larger kk than its parent, and k≤nk \le n, so each node of the binary tree of decisions is pushed at most once and the loop runs at most 2n+1−12^{n+1} - 1 times. □\square

Theorem. KNAPSACK-BB returns (z,best)(z, best) where bestbest is an optimal packing and zz its value.

Proof. The loop ends by Claim 5 with SS empty. By Claim 4, no feasible set has value greater than zz. By Claim 3, bestbest is feasible with value zz. So bestbest is optimal. The implementation sorts the items first and maps the indices of bestbest back to the input order, which changes neither feasibility nor value. ■\blacksquare

Back to Knapsack branch and bound