the edges that have been covered and proceed greedily.
A na¨
ıve implementation of the above process would take
O
(
n
2
) time. This direct algorithm ±rst
identi±es the leaves in time
O
(
n
), removes them along with the associated edges and adds their
parents to the minimum vertex cover. Then it iterates over the remaining vertices. There can be
at most
O
(
n
) such iterations hence the total required time for the na¨
ıve algorithm is
O
(
n
2
).
For an
O
(
n
) algorithm we maintain a boolean array
V
() that marks whether the node is in the
minimum vertex cover or not. Implicity an edge
e
= (
u,v
) is covered if either
V
(
u
) or
V
(
v
) is equals
one. We perform a
DFS
of the tree. Every time we traverse an edge, say (
c,p
), in the
reverse
direction
(i.e. going back from the child
c
to the parent
p
), we check to see if either
V
(
c
) = 1 or
V
(
p
) = 1. If that is so we move on, else we set
V
(
p
) = 1. (It is important that we set
V
(
p
) equal
to 1, and not
V
(
c
).) Finally, we perform DFS once again and output every node that is set to 1.
The correctness of the algorithm follows from an inductive argument. We also use the fact that
the
O
(
n
2
) na¨
ıve algorithm described above is correct. We claim that the two algorithms pick the
same set of nodes. Therefore correctness follows. To prove the claim, we use the invariant that
when we follow an edge in the reverse direction, all edges in the subtree below that edge are covered
by the vertices that have been set to 1 so far; furthermore, the nodes used to cover these edges
are exactly the same as those picked by the na¨
ıve algorithm.
This is easy to prove inductively.
The base case is when the subtree below the edge (
c,p
) is a single node, and therefore it holds
trivially. For the inductive case, suppose that the edge (
c,p
) has already been covered, then again
the inductive step holds trivially. If (
c,p
) has not been covered so far, then in some iteration of the
na¨
ıve algorithm, the subtree below this edge will be removed (as it has been covered already). In
the remaining tree,
c
will be a leaf, so
p
must be picked in the vertex cover, and is picked by both
the na¨
ıve algorithm and our algorithm. Therefore, the inductive step is proved.
The entire process is just