Search code examples
algorithmgraphtreeminimumcover

Find the Optimal vertex cover of a tree with k blue vertices


I need to find a 'dynamic - programming' kind of solution for the following problem:

Input:

  • Perfect Binary-Tree, T = (V,E) - (each node has exactly 2 children except the leafs).
    V = V(blue) ∪ V(black). V(blue) ∩ V(black) = ∅.

(In other words, some vertices in the tree are blue)

  • Root of the tree 'r'.
  • integer k

A legal Solution:

A subset of vertices V' ⊆ V which is a vertex cover of T, and |V' ∩ V(blue)| = k. (In other words, the cover V' contains k blue vertices)


Solution Value:

The value of a legal solution V' is the number of vertices in the set = |V'|. For convenience, we will define the value of an "un-legal" solution to be ∞.


What we need to find:

A solution with minimal Value.

(In other words, The best solution is a solution which is a cover, contains exactly k blue vertices and the number of vertices in the set is minimal.)


I need to define a typical sub-problem. (Like, if i know what is the value solution of a sub tree I can use it to find my value solution to the problem.)

and suggest a formula to solve it.


Solution

  • To me, looks like you are on the right track! Still, I think you will have to use an additional parameter to tell us how far is any picked vertex from the current subtree's root. For example, it can be just the indication whether we pick the current vertex, as below.

    Let fun (v, b, p) be the optimal size for subtree with root v such that, in this subtree, we pick exactly b blue vertices, and p = 1 if we pick vertex v or p = 0 if we don't.

    The answer is the minimum of fun (r, k, 0) and fun (r, k, 1): we want the answer for the full tree (v = r), with exactly k vertices covered in blue (b = k), and we can either pick or not pick the root.

    Now, how do we calculate this? For the leaves, fun (v, 0, 0) is 0 and fun (v, t, 1) is 1, where t tells us whether vertex v is blue (1 if yes, 0 if no). All other combinations are invalid, and we can simulate it by saying the respective values are positive infinities: for example, for a leaf vertex v, the value fun (v, 3, 1) = +infinity. In the implementation, the infinity can be just any value greater than any possible answer.

    For all internal vertices, let v be the current vertex and u and w be its children. We have two options: to pick or not to pick the vertex v.

    Suppose we pick it. Then the value we get for f (v, b, 1) is 1 (the picked vertex v) plus the minimum of fun (u, x, q) + fun (w, y, r) such that x + y is either b if the vertex v is black or b - 1 if it is blue, and q and r can be arbitrary: if we picked the vertex v, the edges v--u and v--w are already covered by our vertex cover.

    Now let us not pick the vertex v. Then the value we get for f (v, b, 0) is just the minimum of fun (u, x, 1) + fun (w, y, 1) such that x + y = b: if we did not pick the vertex v, the edges v--u and v--w have to be covered by u and w.