Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Touch every edge with as few vertices as possible
Picks vertices that touch every edge: the fast 2-approximation, the exact minimum on small graphs, and König's theorem on bipartite graphs
Select an algorithm and generate steps to begin visualization
A vertex cover is a set of vertices that touches every edge: every edge has at least one end in the set. Think of guards placed at street corners so that every street is watched. The smallest such set, the minimum vertex cover, is one of the classic NP-hard problems, and its complement is a maximum independent set (vertices with no edge between them). Because the exact problem is hard in general, it is also the textbook example of an approximation algorithm with a guarantee.
The 2-approximation walks over the edges. When an edge is not covered yet, it takes both of its ends. The edges taken this way share no vertex, they form a matching M, and any cover needs at least one end of each of them, so the minimum has at least |M| vertices while the result has 2|M|: never more than twice the best. The visualizer then finds the exact minimum on small graphs by branching (for an uncovered edge u–v, either u or v is in the cover). On a bipartite graph no search is needed: by König's theorem the minimum cover has exactly as many vertices as a maximum matching, and the cover is built from the matching with alternating paths (L \ Z) ∪ (R ∩ Z).
Vertex covers model monitoring (place the fewest sensors so every link is observed), conflict resolution (remove the fewest items so no two conflicting ones remain), and in bioinformatics the removal of the fewest sequences to clear all conflicts. The problem is central in complexity theory and parameterized algorithms, where the branching shown here is the starting point of fast exact methods for small covers. König's theorem links covers and matchings, a duality that reappears in network flow and linear programming.
Two methods side by side: the matching-based 2-approximation, and the exact branching search.
ApproxVertexCover(G):
C = empty set
for each edge u–v:
if u not in C and v not in C:
add u and v to C // u–v joins the matching M
return C // |C| = 2|M| <= 2 * minimum
MinimumCover(G, C): // exact, exponential
if every edge is covered: return C
pick an uncovered edge u–v
return the smaller of MinimumCover(G, C + u) and MinimumCover(G, C + v)The exact search branches on an edge, not on a vertex: one of the two ends must be in any cover, so trying both covers every possibility. Stopping as soon as the current set is as large as the best found keeps it fast on small graphs.
The bipartite example loaded on this page: A, B, C, D on the left, E, F, G, H on the right.
Example graph: Edges A–E, A–F, A–G, B–E, C–H and D–H, visited in that order.
The minimum cover has 3 vertices, as many as the maximum matching has edges, which is König's theorem. The 2-approximation found 4.
Time: O(n + m) for the approximation · Space: O(n + m)
The approximation looks at each edge once. The exact search can branch twice for each vertex it adds to the cover, so it takes O(2^k · m) for a cover of size k: exponential, but only in the size of the cover, which is why it is the classic example of a fixed-parameter algorithm. König's method needs a maximum matching, O(n·m) with simple augmenting paths or O(m·√n) with Hopcroft-Karp, plus one BFS.
How to find a cover depends on how good it must be and on the graph.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Matching 2-approximation | Any graph, when a cover at most twice the minimum is good enough. | O(n + m) |
| Greedy by highest degree | Often smaller covers in practice, but no factor-2 guarantee (it can be a log n factor off). | O(m log n) |
| König via maximum matching | Bipartite graphs: the exact minimum in polynomial time. | O(m·√n) |
Read the full article: Cliques, Independent Sets and Vertex Covers Explained
Read the full article: Bipartite Graph: Definition, Examples and How to Test
Related algorithms: Hopcroft-Karp Matching, Maximal Clique, Bipartite Check, Edmonds' Blossom Algorithm (Maximum Matching), Graph Coloring