MWIS-TREE returns a maximum-weight independent set full
This proof shows: correctness, termination.
Root the tree at r. For a vertex v, let C(v) be its set of children and sub(v) its set of descendants, v included. A set I of vertices is independent if no edge of the tree has both ends in I. Define
A(v)=max{w(I):I⊆sub(v) independent,v∈I},B(v)=max{w(I):I⊆sub(v) independent,v∈/I}. Both maxima are over non-empty finite families ({v} and ∅), so they exist.
Lemma 1 (recurrence). For each vertex v,
A(v)=w(v)+∑c∈C(v)B(c) and B(v)=∑c∈C(v)max(A(c),B(c)).
Proof. The sets sub(c), c∈C(v), together with {v}, partition sub(v). The only edges of the tree inside sub(v) are the edges inside some sub(c) and the edges {v,c} for c∈C(v): an edge between sub(c) and sub(c′) for c=c′ would close a cycle through v.
So a set I⊆sub(v) with v∈I is independent if and only if each Ic=I∩sub(c) is independent and c∈/Ic. Its weight is w(v)+∑cw(Ic), and the Ic can be chosen separately, so the maximum is w(v)+∑cB(c).
A set I⊆sub(v) with v∈/I is independent if and only if each Ic is independent, with no condition on c. The maximum of w(Ic) over such Ic is max(A(c),B(c)), and again the choices are separate. □
Lemma 2 (first pass). After the loop of lines L2 to L4, in[v]=A(v) and out[v]=B(v) for each v.
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 v, in[v]=∑c∈C(v)B(c) and out[v]=∑c∈C(v)max(A(c),B(c)), and after line L3, in[v]=A(v) and out[v]=B(v).
Both arrays start at 0. The only writes to in[v] and out[v] before v is processed are the writes of line L4 for the children c of v, one for each child, and each of them happens after c is processed, when by the induction hypothesis in[c]=A(c) and out[c]=B(c). So when v is reached, the two sums are as claimed. Line L3 adds w(v) to in[v], and by Lemma 1 the values are now A(v) and B(v). Later steps write only to the entries of parents, never again to v's own entries. □
So line L5 sets best=max(A(r),B(r)), the maximum weight of an independent subset of sub(r)=V.
Lemma 3 (second pass). Let S be the set built by lines L6 to L8. Then S is independent, and for each vertex v:
(a) if v=r or p(v)∈/S, then w(S∩sub(v))=max(A(v),B(v));
(b) if p(v)∈S, then v∈/S and w(S∩sub(v))=B(v).
Proof. In BFS order the parent of v is decided before v, so the test of line L7 is well defined. If p(v)∈S, line L7 keeps v out. So no edge {p(v),v} has both ends in S, and S is independent.
We prove (a) and (b) by induction on the height of v, the length of the longest path from v down to a leaf. Write Sv=S∩sub(v).
Case (b). v∈/S, and each child c has p(c)=v∈/S, so (a) holds for c by the induction hypothesis. Hence w(Sv)=∑cmax(A(c),B(c))=B(v) by Lemma 1.
Case (a), in[v]>out[v]. Line L8 puts v in S. Each child c then has p(c)∈S, so by (b) for c, w(Sc)=B(c). Hence w(Sv)=w(v)+∑cB(c)=A(v)=max(A(v),B(v)), using Lemma 2.
Case (a), in[v]≤out[v]. Line L8 leaves v out, and as in case (b), w(Sv)=B(v)=max(A(v),B(v)).
A leaf has no children, and the same three cases apply with empty sums. □
Theorem. MWIS-TREE returns best=max{w(I):I independent} and an independent set S with w(S)=best. It stops after Θ(n) steps.
Proof. By Lemma 2, best=max(A(r),B(r)), and every independent set of the tree either contains r or not, so best is the maximum. By Lemma 3(a) with v=r, S is independent and w(S)=w(S∩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 n vertices once with O(1) work, so the algorithm stops after Θ(n) steps. ■