15-6 Planning a company party

Professor Stewart is consulting for the president of a corporation that is planning a company party. The company has a hierarchical structure; that is, the supervisor relation forms a tree rooted at the president. The personnel office has ranked each employee with a conviviality rating, which is a real number. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

Professor Stewart is given the tree that describes the structure of the corporation, using the left-child, right-sibling representation described in Section 10.4. Each node of the tree holds, in addition to the pointers, the name of an employee and that employee's conviviality ranking. Describe an algorithm to make up a guest list that maximizes the sum of the conviviality ratings of the guests. Analyze the running time of your algorithm.

The problem exhibits optimal substructure in the following way: If the root rr is included in an optimal solution, then we must solve the optimal subproblems rooted at the grandchildren of rr. If rr is not included, then we must solve the optimal subproblems on trees rooted at the children of rr. The dynamic programming algorithm to solve this problem works as follows: We make a table CC indexed by vertices which tells us the optimal conviviality ranking of a guest list obtained from the subtree with root at that vertex. We also make a table GG such that G[i]G[i] tells us the guest list we would use when vertex ii is at the root. Let TT be the tree of guests. To solve the problem, we need to examine the guest list stored at G[T.root]G[T.root]. First solve the problem at each leaf LL. If the conviviality ranking at LL is positive, G[L]={L}G[L] = \{L\} and C[L]=L.convivC[L] = L.conviv. Otherwise G[L]=G[L] = \emptyset and C[L]=0C[L] = 0. Iteratively solve the subproblems located at parents of nodes at which the subproblem has been solved. In general for a node xx,

C[x]=max(y is a child of xC[y],x.conviv+y is a grandchild of xC[y]).C[x] = \max(\sum_{y\text{ is a child of } x} C[y], x.conviv + \sum_{y\text{ is a grandchild of } x} C[y]).

The runtime is O(n)O(n) since each node appears in at most two of the sums (because each node has at most 1 parent and 1 grandparent) and each node is solved once.