Algorithm Search⌘K

Proofs: Independent set on trees

These are the human proofs of Independent set on trees, at full length.

MWIS-TREE returns a maximum-weight independent set full

This proof shows: correctness, termination.

Root the tree at rr. For a vertex vv, let C(v)C(v) be its set of children and sub(v)\mathrm{sub}(v) its set of descendants, vv included. A set II of vertices is independent if no edge of the tree has both ends in II. Define

A(v)=max⁡{w(I):I⊆sub(v) independent,v∈I},B(v)=max⁡{w(I):I⊆sub(v) independent,v∉I}.A(v) = \max\{ w(I) : I \subseteq \mathrm{sub}(v) \text{ independent}, v \in I \}, \qquad B(v) = \max\{ w(I) : I \subseteq \mathrm{sub}(v) \text{ independent}, v \notin I \}.

Both maxima are over non-empty finite families ({v}\{v\} and ∅\emptyset), so they exist.

Lemma 1 (recurrence). For each vertex vv, A(v)=w(v)+∑c∈C(v)B(c)A(v) = w(v) + \sum_{c \in C(v)} B(c) and B(v)=∑c∈C(v)max⁡(A(c),B(c))B(v) = \sum_{c \in C(v)} \max(A(c), B(c)).

Proof. The sets sub(c)\mathrm{sub}(c), c∈C(v)c \in C(v), together with {v}\{v\}, partition sub(v)\mathrm{sub}(v). The only edges of the tree inside sub(v)\mathrm{sub}(v) are the edges inside some sub(c)\mathrm{sub}(c) and the edges {v,c}\{v, c\} for c∈C(v)c \in C(v): an edge between sub(c)\mathrm{sub}(c) and sub(c′)\mathrm{sub}(c') for c≠c′c \ne c' would close a cycle through vv.

So a set I⊆sub(v)I \subseteq \mathrm{sub}(v) with v∈Iv \in I is independent if and only if each Ic=I∩sub(c)I_c = I \cap \mathrm{sub}(c) is independent and c∉Icc \notin I_c. Its weight is w(v)+∑cw(Ic)w(v) + \sum_c w(I_c), and the IcI_c can be chosen separately, so the maximum is w(v)+∑cB(c)w(v) + \sum_c B(c).

A set I⊆sub(v)I \subseteq \mathrm{sub}(v) with v∉Iv \notin I is independent if and only if each IcI_c is independent, with no condition on cc. The maximum of w(Ic)w(I_c) over such IcI_c is max⁡(A(c),B(c))\max(A(c), B(c)), and again the choices are separate. □\square

Lemma 2 (first pass). After the loop of lines L2 to L4, in[v]=A(v)in[v] = A(v) and out[v]=B(v)out[v] = B(v) for each vv.

Proof. In reverse BFS order, each vertex comes after all of its descendants, since BFS lists a parent before its children. We show by induction along this order: when line L3 processes vv, in[v]=∑c∈C(v)B(c)in[v] = \sum_{c \in C(v)} B(c) and out[v]=∑c∈C(v)max⁡(A(c),B(c))out[v] = \sum_{c \in C(v)} \max(A(c), B(c)), and after line L3, in[v]=A(v)in[v] = A(v) and out[v]=B(v)out[v] = B(v).

Both arrays start at 0. The only writes to in[v]in[v] and out[v]out[v] before vv is processed are the writes of line L4 for the children cc of vv, one for each child, and each of them happens after cc is processed, when by the induction hypothesis in[c]=A(c)in[c] = A(c) and out[c]=B(c)out[c] = B(c). So when vv is reached, the two sums are as claimed. Line L3 adds w(v)w(v) to in[v]in[v], and by Lemma 1 the values are now A(v)A(v) and B(v)B(v). Later steps write only to the entries of parents, never again to vv's own entries. □\square

So line L5 sets best=max⁡(A(r),B(r))best = \max(A(r), B(r)), the maximum weight of an independent subset of sub(r)=V\mathrm{sub}(r) = V.

Lemma 3 (second pass). Let SS be the set built by lines L6 to L8. Then SS is independent, and for each vertex vv: (a) if v=rv = r or p(v)∉Sp(v) \notin S, then w(S∩sub(v))=max⁡(A(v),B(v))w(S \cap \mathrm{sub}(v)) = \max(A(v), B(v)); (b) if p(v)∈Sp(v) \in S, then v∉Sv \notin S and w(S∩sub(v))=B(v)w(S \cap \mathrm{sub}(v)) = B(v).

Proof. In BFS order the parent of vv is decided before vv, so the test of line L7 is well defined. If p(v)∈Sp(v) \in S, line L7 keeps vv out. So no edge {p(v),v}\{p(v), v\} has both ends in SS, and SS is independent.

We prove (a) and (b) by induction on the height of vv, the length of the longest path from vv down to a leaf. Write Sv=S∩sub(v)S_v = S \cap \mathrm{sub}(v).

Case (b). v∉Sv \notin S, and each child cc has p(c)=v∉Sp(c) = v \notin S, so (a) holds for cc by the induction hypothesis. Hence w(Sv)=∑cmax⁡(A(c),B(c))=B(v)w(S_v) = \sum_c \max(A(c), B(c)) = B(v) by Lemma 1.

Case (a), in[v]>out[v]in[v] > out[v]. Line L8 puts vv in SS. Each child cc then has p(c)∈Sp(c) \in S, so by (b) for cc, w(Sc)=B(c)w(S_c) = B(c). Hence w(Sv)=w(v)+∑cB(c)=A(v)=max⁡(A(v),B(v))w(S_v) = w(v) + \sum_c B(c) = A(v) = \max(A(v), B(v)), using Lemma 2.

Case (a), in[v]≤out[v]in[v] \le out[v]. Line L8 leaves vv out, and as in case (b), w(Sv)=B(v)=max⁡(A(v),B(v))w(S_v) = B(v) = \max(A(v), B(v)).

A leaf has no children, and the same three cases apply with empty sums. □\square

Theorem. MWIS-TREE returns best=max⁡{w(I):I independent}best = \max\{ w(I) : I \text{ independent} \} and an independent set SS with w(S)=bestw(S) = best. It stops after Θ(n)\Theta(n) steps.

Proof. By Lemma 2, best=max⁡(A(r),B(r))best = \max(A(r), B(r)), and every independent set of the tree either contains rr or not, so bestbest is the maximum. By Lemma 3(a) with v=rv = r, SS is independent and w(S)=w(S∩sub(r))=max⁡(A(r),B(r))=bestw(S) = w(S \cap \mathrm{sub}(r)) = \max(A(r), B(r)) = best. The BFS visits each vertex once and each edge twice, and each of the two loops visits each of the nn vertices once with O(1)O(1) work, so the algorithm stops after Θ(n)\Theta(n) steps. ■\blacksquare

Back to Independent set on trees