In this post, I want to tell you about what I think might be the world’s simplest interesting algorithm.
The vertex cover problem. Given a graph , we want to find the smallest set of vertices
such that every edge
is covered by the set
. This means that for each edge
, at least one of
or
is in
.
The vertex cover problem is known to be NP-complete, meaning that it is very hard (or impossible) to find a polynomial-time algorithm for the problem.
But what we can do is find an approximately optimal solution to the problem. There’s a beautiful and simple algorithm that finds a set whose size is guaranteed to be within a factor of
of optimal.
The algorithm. To build our set , we repeatedly find some edge
that is not yet covered, and we add both
and
to our set
. That’s the entire algorithm.
At first sight, this seems like a crazy algorithm. Why would we put both and
into
, when we only need one of
or
in order to cover the edge
?
The answer to this question is simple. If we define to be the optimal solution to the vertex-cover problem, then
must contain at least one of
or
. By placing both of
and
into our set
, we guarantee that at least half the elements in our set
also appear in
. That means that, once our algorithm finishes, we are guaranteed that
.
A challenge. This might be the world’s simplest interesting algorithm (and analysis). But it also might not be. What do you think?