简体   繁体   English

最大独立设定重量

[英]maximum independent set weight

I'm trying to solve the problem outlined in this previously asked question: 我正在尝试解决此先前提出的问题中概述的问题:

Finding size of max independent set in binary tree - why faulty "solution" doesn't work? 在二叉树中找到最大独立集的大小-为什么错误的“解决方案”不起作用?

Given an array A with n integers, its indexes start with 0 (ie, A[0], A[1], …, A[n-1]). 给定一个具有n个整数的数组A,其索引从0开始(即A [0],A [1],…,A [n-1])。 We can interpret A as a binary tree in which the two children of A[i] are A[2i+1] and A[2i+2], and the value of each element is the node weight of the tree. 我们可以将A解释为二叉树,其中A [i]的两个子元素分别是A [2i + 1]和A [2i + 2],每个元素的值是树的节点权重。 In this tree, we say that a set of vertices is "independent" if it does not contain any parent-child pair. 在这棵树中,我们说如果一组顶点不包含任何父子对,则它们是“独立的”。 The weight of an independent set is just the summation of all weights of its elements. 独立集合的权重只是其元素所有权重的总和。 Develop an algorithm to calculate the maximum weight of any independent set. 开发一种算法来计算任何独立集合的最大权重。

However, I'm trying to solve this in Java. 但是,我正在尝试用Java解决此问题。 I have looked at the python solution in the linked question, but this line doesn't make sense to me: 我已经在链接的问题中查看了python解决方案,但是这一行对我来说没有意义:

with_root = sum(map(find_max_independent, grandkids))+ max(true_root[root_i],0)

Is there a Java equivalent of this solution? 有与该解决方案等效的Java吗?

Of course there is a Java equivalent. 当然有Java等效项。 Though might depend on what you mean with "equivalent". 虽然可能取决于您所说的“等效”。 I'm not fluent in current Java, so I'll just make sense of that line for you. 我不会精通当前的Java,因此我只为您讲解这句话。

The part sum(map(find_max_independent, grandkids)) means: sum(map(find_max_independent, grandkids))意思是:

find_max_independent(grandkids[0]) + find_max_independent(grandkids[1]) + ...

And max(true_root[root_i], 0) is just the weight of the current node if it's not negative, and zero otherwise (note the weights are just known to be "integers", so they could be negative). max(true_root[root_i], 0)如果不是负数,则仅是当前节点的权重,否则为零(请注意,权重已知为“整数”,因此它们可能为负数)。 Although, that's really not necessary to check, as using the zero means not including the node, which is already covered by without_root . 虽然,实际上没有必要进行检查,因为使用零意味着不包括不包含在节点中的节点,而该节点已经被without_root覆盖。

That algorithm is btw not O(n) as claimed, I wrote a comment there already. 该算法不是所要求的O(n),我已经在此处写过评论。 Here's one that actually is, and which is also simpler: 这实际上是一个,也更简单:

def max_independant_weight(weights):
    def with_and_without(i):
        if i >= len(weights):
            return 0, 0
        left  = with_and_without(2*i + 1)
        right = with_and_without(2*i + 2)
        return (weights[i] + left[1] + right[1],
                max(left) + max(right))
    return max(with_and_without(0))

As Stefan was right that my solution did not work. 正如Stefan所说的那样,我的解决方案没有用。 So I translated his into Java. 所以我将他翻译成Java。 I made static methods but feel free to do whatever you want with that. 我制作了静态方法,但是可以随心所欲地做任何事情。

  public static void main(String[] args)
  {
    int[] tree = new int[] { 3, 2, 2 };

    System.out.println(max_independant_weight(tree));
  }

  private static int max(int[] array)
  {
    int max = Integer.MIN_VALUE;

    for (int i : array)
    {
      if (i > max)
      {
        max = i;
      }
    }

    return max;
  }

  private static int max_independant_weight(int[] weights)
  {
    return max(with_and_without(weights, 0));
  }

  private static int[] with_and_without(int[] weights, int i)
  {
    if (i >= weights.length)
    {
      return new int[] { 0, 0 };
    }
    int[] left = with_and_without(weights, (2 * i) + 1);
    int[] right = with_and_without(weights, (2 * i) + 2);
    return new int[] { weights[i] + left[1] + right[1], max(left) + max(right) };
  }

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

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