简体   繁体   中英

How do I prove the correctness of my greedy algorithm for vertex cover on a tree?

The vertex cover problem on trees is as follows.

Input: an acyclic simple undirected graph G
Output: a set of vertices W such that, for every edge uv, u ∈ W or v ∈ W. We want to minimize the size of W.

My greedy algorithm is to initialize W = ∅, then, while G is not empty, repeat the following steps. Let L be the leaf vertices of G. Let N(L) be the set of vertices adjacent to some vertex in L. Update W = W ∪ N(L). Delete the vertices L ∪ N(L) and their incident edges from G.

This algorithm works in all of the cases that I have tried so far. How do I go about proving it correct? Here's what I have so far.

Assume that there is another set S that is an optimal solution. By contradiction, I want to establish either that S does not cover all of the edges or that S is the same set as the one produced by my greedy algorithm.

That's a reasonable start, but I see two issues. First, the optimal solution may not be unique. Consider the four-vertex path abcd , which has three optimal solutions: {a,c}, {b,c}, {b,d} . Second (and you're probably doing this already, but you didn't say so), it's necessary to consider the tree to be rooted. Otherwise, on the graph ab for example, we have L = {a,b} and N(L) = {b,a} , and the vertex cover produced is W = {b,a} , which is not optimal. By designating a as a root, it is by definition excluded from the set of leaves.

To prove formally the correctness of a program involving a loop, it is often a good idea to use induction to establish a loop invariant. Allow me to suggest two.

  1. For all times t (where time = number of loop iterations), let G(t) be what's left of G at time t and let W(t) be the value of W at time t. For every vertex cover X of G(t), the set W(t) ∪ X is a vertex cover of G(0), where 0 is the starting time.

  2. For all times t, there exists an optimal solution that contains W(t) as a subset.

Let T be the ending time. Since G(T) is the empty graph, X = ∅ is a valid vertex cover of G(T), so Invariant #1 establishes that W(T) is a vertex cover of G(0). Invariant #2 establishes that W(T) is contained in some optimal solution. Since W(T) itself is a vertex cover, W(T) itself must be that optimal solution.

The inductive step in proving Invariant #2 is, given an optimal solution that contains W(t-1) but not W(t), to massage it into another optimal solution that contains W(t). This involves formalizing your intuition that it's always at least as productive to take a leaf's neighbor over the leaf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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