简体   繁体   English

动态编程最佳解决方案

[英]Dynamic Programming Optimal Solution

A company is planning a party for its employees. 公司正在为其员工计划聚会。 A fun rating is assigned to every employee. 有趣的等级被分配给每个雇员。 The employees are organized into a strict hierarchy, ie a tree rooted the president. 员工被组织成严格的等级制度,即植根于总裁的树。 There is one restriction, though, on the guest list to the party: an employee and his/her immediate supervisor (parent in the tree) cannot both attend the party. 但是,聚会的来宾名单上有一个限制:雇员及其直属上司(树中的父母)不能同时参加聚会。 You wish to prepare a guest list for the party that maximizes the sum of fun ratings of the guests. 您希望为聚会准备宾客名单,以最大程度地提高宾客的娱乐评分总和。 Show that greedily choosing guests according to fun rating, will not work. 证明根据趣味等级贪婪地选择客人不会起作用。 Then, formulate a dynamic programming solution 然后,制定一个动态编程解决方案

I could not understand some of the conditions like is the fun rate of the president higher than that of his descendants and how many employees are there for each of his supervisor. 我无法理解某些条件,例如总统的娱乐率是否高于其后代的娱乐率,以及每个主管的雇员人数。 Can someone help me in proceeding with this ? 有人可以帮助我进行此操作吗?

From the phrasing on the problem, the fun rating assigned to someone in the hierarchy tree is not necessarily greater than their descendants in the hierarchy tree. 从问题的措辞来看,在层次结构树中分配给某人的娱乐等级不一定高于其在层次结构树中的后代。

However, even if this were the case, to see that it is not optimal to pick the best employee, consider a tree of height 2 with a root of fun=10 and 20 children of fun=1. 但是,即使是这种情况,要发现挑选最佳员工也不是最佳选择,请考虑树高2的树,其根为fun = 10,并且有20个子代fun = 1。 Then the optimal solution is to skip the greedy choice (the root) and choose the 20 children. 那么最佳的解决方案是跳过贪婪的选择(根)并选择20个孩子。

In any case, with dynamic programming you can find the best solution even if parents can have lower fun than their descendants. 在任何情况下,即使父母的后代乐趣比后代低,使用动态编程也可以找到最佳的解决方案。 For a node v in the tree, let F(v) be the maximum fun that can be attained within subtree rooted at v. Then either you choose v in which case the children are skipped and you look at all subtrees that are rooted at children of children (taking the sum of max fun over these subtrees, and adding to fun(v)), or you skip v and then you get the maximum fun is the sum of maximum fun over all subtrees rooted at children of v. This gives a linear time dynamic programming algorithm. 对于树中的节点v,令F(v)为以v为根的子树可以获得的最大乐趣。然后选择v,在这种情况下,子级将被跳过,然后查看以子为根的所有子树的子项(将这些子树上的最大乐趣的总和添加到fun(v)中),或者跳过v然后获得最大乐趣,这是根于v的孩子的所有子树上的最大乐趣的总和。线性时间动态规划算法。

For greedy example show a simple counterexample. 对于贪婪的例子,请举一个简单的反例。

    1
    |
 1--3--1
    |
    1

Choosing greedily ie first selecting 3 won't allow us to select any other employee. 贪婪地选择,即首先选择3不允许我们选择任何其他雇员。 But if we don't select 3, than max will be 4(all 1's). 但是,如果我们不选择3,则max将为4(全为1)。 So greedy approach won't work. 因此,贪婪的方法行不通。

For dynamic programming we can formulate problem as selecting a given root of subtree. 对于动态编程,我们可以将问题表达为选择子树的给定根。 If we select a node, than none of its children can be selected. 如果我们选择一个节点,则不能选择其子节点。 However if we don't select the node, than we can either select the child or not select the child. 但是,如果不选择节点,则可以选择子节点,也可以不选择子节点。

for all v initialize
  c(v,true) = fun(v)
  c(v,false)= 0

Than use the following recursion to solve the problem 比用下面的递归来解决问题

weight(v,true)  = for all children sum( weight(ci,false) ) + fun(i)
weight(v,false) = for all children sum( max(weight(ci,false), weight(ci,true)))

The answer will be max(weight(v,true),weight(v,false)) for root node. 对于根节点max(weight(v,true),weight(v,false))答案将为max(weight(v,true),weight(v,false))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM