learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Vertex Cover

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

Time: O(n + m) approx., exponential exact
Space: O(n + m)
Use Case: Network monitoring, conflict resolution, bioinformatics
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Vertex Cover (2-Approximation and König)

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.

How it works

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).

Applications

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.

Pseudocode

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.

Worked example, step by step

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.

  1. First edge. A–E is not covered, so both A and E are taken.
  2. Covered edges. A–F, A–G and B–E are already covered, by A or E, and are skipped.
  3. Second edge. C–H is not covered: C and H are taken. D–H is then covered by H. The approximation has 4 vertices from 2 matching edges.
  4. Exact minimum. The branching search finds a cover of 3: A, B and H. The approximation took 1.33 times as many. C, D, E, F and G, the rest, form a maximum independent set.
  5. König. A maximum matching has 3 edges. From the free left vertex D, alternating paths reach H and then C: Z = {C, D, H}. The cover (L \ Z) ∪ (R ∩ Z) is {A, B, H}, 3 vertices.

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.

Complexity, and where it comes from

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.

When to use Vertex Cover (2-Approximation and König), and when not to

How to find a cover depends on how good it must be and on the graph.

AlternativePrefer it whenCost
Matching 2-approximationAny graph, when a cover at most twice the minimum is good enough.O(n + m)
Greedy by highest degreeOften 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 matchingBipartite graphs: the exact minimum in polynomial time.O(m·√n)

Common pitfalls

  • Taking only one end. Taking just one end of each uncovered edge feels smarter, but without a rule for which end it loses the guarantee and can be far from the minimum.
  • Using König on a non-bipartite graph. In a triangle the maximum matching has 1 edge but the minimum cover needs 2 vertices. The equality holds only for bipartite graphs.
  • Confusing cover and independent set. They are complements: a set is a vertex cover exactly when the other vertices are independent. A minimum cover gives a maximum independent set, but approximation guarantees do not carry over.

Frequently asked questions

Is vertex cover NP-hard?
Yes, deciding whether a cover of size k exists is NP-complete in general graphs. It is one of Karp's 21 problems from 1972.
Why is the approximation at most twice the minimum?
The edges it takes form a matching, and any cover must contain at least one end of each matching edge. So the minimum is at least the number of those edges, and the approximation takes exactly twice that number.
What is König's theorem?
In a bipartite graph, the size of a minimum vertex cover equals the size of a maximum matching. It is a special case of the duality between linear programs.
How is vertex cover related to cliques?
A set is independent in a graph exactly when it is a clique in the complement graph, and a cover is the complement of an independent set, so all three are equally hard to solve exactly.

Related algorithms: Hopcroft-Karp Matching, Maximal Clique, Bipartite Check, Edmonds' Blossom Algorithm (Maximum Matching), Graph Coloring

An example graph: you can change it, or start blank.

Zoom Controls

50%
Nodes: 8
Edges: 6