Bipartite Matching
100 students, 100 companies. Each student applied to a few companies, and each company can hire at most one student. What's the maximum number of students who get a job?
That's bipartite matching — one of the most practically useful graph problems in computer science, with applications from scheduling to ad auctions to DNA analysis.
What Makes a Graph Bipartite?
A graph is bipartite if you can split all vertices into two groups — call them Left (L) and Right (R) — where every edge goes between L and R. No edges within L, no edges within R. Students on the left, companies on the right, job applications as edges.
A matching is a subset of edges where no vertex appears twice — each student in at most one pair, each company in at most one pair. We want the maximum matching: as many pairs as possible.
Augmenting Paths: The Key Idea
Given a partial matching, an augmenting path is a path that alternates between unmatched and matched edges, starting and ending at unmatched vertices. If you flip all edges on this path (matched→unmatched, unmatched→matched), the matching gains one pair.
Berge's theorem: a matching is maximum if and only if no augmenting path exists. So the algorithm is: keep finding and augmenting along augmenting paths until none remain. Simple DFS/BFS to find each path gives \(O(VE)\).
Hopcroft-Karp: The Faster Way
Instead of finding one augmenting path at a time, Hopcroft-Karp finds all shortest augmenting paths simultaneously in each phase:
- BFS phase: find the length of all shortest augmenting paths and build a layered graph.
- DFS phase: find a maximal set of vertex-disjoint augmenting paths in the layered graph and augment all at once.
After each phase, the length of the shortest remaining augmenting path strictly increases. This happens at most \(O(√V)\) times, giving \(O(√V)\) phases × \(O(E)\) per phase = \(O(E√V)\).
Reduction to Max Flow
Bipartite matching is secretly a max flow problem. Add a super-source s connected to every L vertex (capacity 1), a super-sink t from every R vertex (capacity 1), and keep all L-R edges with capacity 1. Run any max flow algorithm — the result equals the maximum matching.
Running Dinic's on this unit-capacity network gives \(O(E√V)\) — the same as Hopcroft-Karp.
König's Theorem
A vertex cover is a set of vertices that "touches" every edge (every edge has at least one endpoint in the cover). In bipartite graphs:
maximum matching size = minimum vertex cover size
This elegant duality — König's theorem — does not hold for general graphs (where min vertex cover is NP-hard). A minimum vertex cover can be found from a maximum matching using an alternating-path construction.
Bipartite Matching — DFS Augmenting Paths
Complexity
Quick check
Continue reading