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 \(r\) is included in an optimal solution, then we must solve the optimal subproblems rooted at the grandchildren of \(r\). If \(r\) is not included, then we must solve the optimal subproblems on trees rooted at the children of \(r\). The dynamic programming algorithm to solve this problem works as follows: We make a table \(C\) 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 \(G\) such that \(G[i]\) tells us the guest list we would use when vertex \(i\) is at the root. Let \(T\) be the tree of guests. To solve the problem, we need to examine the guest list stored at \(G[T.root]\). First solve the problem at each leaf \(L\). If the conviviality ranking at \(L\) is positive, \(G[L] = \\{L\\}\) and \(C[L] = L.conviv\). Otherwise \(G[L] = \emptyset\) and \(C[L] = 0\). Iteratively solve the subproblems located at parents of nodes at which the subproblem has been solved. In general for a node \(x\),
The runtime is \(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.
本页面的全部内容在 小熊老师 - 莆田青少年编程俱乐部 0594codes.cn 协议之条款下提供,附加条款亦可能应用