Tree 求树的最大独立集
有人能帮我做这个算法吗?我知道如何找到树的独立集的实际最大和:Tree 求树的最大独立集,tree,dynamic-programming,graph-algorithm,Tree,Dynamic Programming,Graph Algorithm,有人能帮我做这个算法吗?我知道如何找到树的独立集的实际最大和: int[] picked = new int[t.noOfVertices]; int[] notPicked = new int[t.noOfVertices]; for (int i = 0; i < t.noOfVertices; i++) { int node = postOrder[i]; int parent = parIds[node];
int[] picked = new int[t.noOfVertices];
int[] notPicked = new int[t.noOfVertices];
for (int i = 0; i < t.noOfVertices; i++) {
int node = postOrder[i];
int parent = parIds[node];
if (t.edges[node].length == 1 && parent >= 0) {
picked[node] = weights[node];
notPicked[node] = 0;
}
else{
picked[node] = weights[node];
for (int j = 0; j < t.edges[node].length; j++) {
int neighbor = t.edges[node][j];
if (neighbor == parent) continue;
picked[node] += notPicked[neighbor];
notPicked[node] += Math.max(picked[neighbor], notPicked[neighbor]);
}
}
}
int[]拾取=新的int[t.noofvices];
int[]notpick=新的int[t.noofvices];
for(int i=0;i=0){
拾取的[节点]=权重[节点];
未拾取的[节点]=0;
}
否则{
拾取的[节点]=权重[节点];
对于(int j=0;j
“t”是一个树对象,它包含用于树的前序和后序遍历的数组。我现在需要做的是找到集合中包含的实际节点,如果有多个最大独立集合,则返回最大的一个。一般策略是为每个节点计算最佳解决方案中包含的子节点数,以确定何时选择节点,何时不选择节点。然后,按post顺序遍历树,并在picked[node]>notPicked[node]的情况下向集合中添加一个节点。如果picked[node]==notPicked[node],根据前面讨论的计算,选择解决方案中节点较多的路径。然而,我不知道如何计算。有人能帮上忙吗