Discrete Mathematics and Combinatorics
Bipartite Graphs and Matching Problems
How to recognize a two-sided network, find the largest set of pairings with augmenting paths, and certify the answer.
Key takeaways
- A graph is bipartite exactly when it has no cycle with an odd number of edges.
- A maximal matching, one that cannot simply be extended, may still be far from the largest possible.
- A matching is maximum exactly when there is no augmenting path, which is also how algorithms improve it.
- In bipartite graphs, the largest matching has the same size as the smallest set of vertices touching every edge.
- Weighted and preference-based versions need further ideas built on the same foundation.
A bipartite graph is a network whose points fall into two groups, with every link joining a point in one group to a point in the other. A matching is a set of links that share no points, so each point is paired with at most one partner. The central problem is to find a matching that is as large as possible.
Applicants and jobs, students and project places, tasks and machines all fit this pattern. The good news is that the largest matching can be found efficiently, and you can also produce a short proof that no larger one exists.
Recognizing a bipartite graph
A graph consists of vertices (points) and edges (links). It is bipartite if you can color each vertex red or blue so that every edge joins two different colors.
To test this, color any vertex red. Color its neighbors blue, their neighbors red, and keep spreading outward. If you are ever forced to join two vertices of the same color, the graph is not bipartite. Start again in each separate piece of the graph.
The obstruction is always an odd cycle. A cycle is a closed route without repeats, and colors alternate along it, so its length must be even. Conversely, if a graph has no odd cycle, the spreading procedure never runs into a conflict. A triangle is therefore never bipartite, while every tree, square grid and even ring is.
Matchings: maximal is not maximum
Four workers A, B, C and D can each do certain tasks:
| Worker | Can do tasks |
|---|---|
| A | 1, 2 |
| B | 1 |
| C | 2, 3 |
| D | 3, 4 |
Suppose you assign greedily in order: A takes task 1. Now B is stuck. C takes 2 and D takes 3. Three pairs are formed, and no free worker can take a free task, since task 4 can only go to D, who is busy. This matching is maximal: you cannot just add to it.
Yet it is not maximum. All four workers can be placed. You need to rearrange.
Augmenting paths
Look for a route that starts at an unmatched worker, ends at an unmatched task and alternates between edges outside the matching and edges inside it. This is an augmenting path. Here it is:
B – 1 not in the matching
1 – A in the matching
A – 2 not in the matching
2 – C in the matching
C – 3 not in the matching
3 – D in the matching
D – 4 not in the matching (task 4 is free) Now flip every edge on the path: those outside come in, those inside go out. The result is B with 1, A with 2, C with 3 and D with 4. The path had four edges outside the matching and three inside, so the matching grew by exactly one.
Why the search is complete
Berge's lemma (1957) says that a matching in any graph is maximum if and only if it has no augmenting path.
One direction is clear from flipping. For the other, suppose a larger matching N exists beside your matching M. Look at the edges that belong to exactly one of the two. Each vertex meets at most two of them, one from each, so they form paths and cycles that alternate between M and N. Cycles have equal numbers from each. Since N has more edges overall, some path has one more N edge than M edge. That path is augmenting for M.
So the algorithm is simple: search for augmenting paths until there are none. In a bipartite graph, a breadth-first search (exploring nearest vertices first) from the free vertices finds them. The Hopcroft–Karp method finds many shortest augmenting paths in each round, with running time proportional to about E·√V for E edges and V vertices.
Certifying that you cannot do better
A vertex cover is a set of vertices that touches every edge. Since matched edges share no vertices, a cover must use a different vertex for each of them. Therefore every matching is no larger than every cover.
Kőnig's theorem says that in a bipartite graph the two optima are equal: the size of a maximum matching equals the size of a minimum vertex cover.
Change the example so that A, B and D can only do task 1, while C can do tasks 1, 2, 3 and 4. The set {C, task 1} touches every edge, so no matching has more than 2 pairs. Pairing A with 1 and C with 2 achieves 2. The cover is a certificate anyone can check at a glance.
The theorem fails for general graphs. In a triangle, the largest matching has 1 edge, but covering all three edges needs 2 vertices.
Closely related is Hall's marriage theorem, which says exactly when every vertex on one side can be matched. Kőnig's theorem is also a special case of the max-flow min-cut theorem: add a source feeding all workers and a sink collecting all tasks, with capacity 1 on each link.
Parity arguments with colors
Bipartite thinking gives slick impossibility proofs. Remove two opposite corners of a chessboard. Can 31 dominoes cover the remaining 62 squares?
Squares form a bipartite graph, and a domino covers one dark and one light square, so a tiling is a perfect matching. A perfect matching pairs every vertex. Opposite corners have the same color, leaving 32 squares of one color and 30 of the other. No perfect matching can exist. This is a classic invariant argument.
Equal color counts are necessary but not sufficient. A shape can be balanced overall and still contain a region where one color has too few neighbors.
Hidden two-sided structure: rooks on a board
Bipartite graphs often hide inside puzzles that do not mention two groups. Suppose some squares of a 4 by 4 board are open and the rest are blocked. How many rooks can you place on open squares so that no two share a row or column?
Make one side of the graph the four rows and the other the four columns. Each open square is an edge joining its row to its column. A set of non-attacking rooks is exactly a matching.
Let the open squares be these, written as (row, column):
row 1: (1,1) (1,2) (1,3) (1,4)
row 2: (2,1)
row 3: (3,1)
row 4: (4,1) (4,3) Rooks at (1,2), (2,1) and (4,3) give three. To see that four is impossible, look for a small cover. Row 1, row 4 and column 1 together contain every open square. Each rook must sit in one of those three lines, and no line holds two rooks, so three is the maximum.
That is Kőnig's theorem in board form: the largest number of non-attacking rooks equals the smallest number of rows and columns that cover all open squares. You get both the best arrangement and a reason it cannot be beaten.
Modeling advice
Before applying matching methods, check three things.
- Are there really two kinds of object, with pairings allowed only across kinds? Pairing roommates from one pool is not bipartite.
- Is each object used at most once? If a job takes three people, either make three copies of the job or use a flow model.
- Do you care only about the number of pairs? If some pairings are better than others, add weights or preferences before choosing a method.
Copying vertices is a simple, powerful trick. A tutor who can take two students becomes two identical vertices with the same list of acceptable students, and a maximum matching in the enlarged graph solves the original problem.
Variations you will meet
| Problem | What is added | Standard tool |
|---|---|---|
| Assignment problem | A cost on each pairing | Hungarian method |
| Stable matching | Ranked preferences on both sides | Gale–Shapley algorithm |
| Capacitated matching | Places that accept several people | Network flow |
| General graph matching | No two-sided structure | Edmonds' blossom algorithm |
The Hungarian method, published by Harold Kuhn in 1955 and named for work of Kőnig and Jenő Egerváry, finds a perfect matching of least total cost. When preferences matter, size is not the main issue; you want no two people who would both rather be together, the subject of the Gale–Shapley algorithm.
In graphs that are not bipartite, odd cycles confuse the alternating search. Jack Edmonds showed in 1965 how to shrink them into single points, which he called blossoms, and still find a maximum matching efficiently.
A useful guarantee
A graph is regular if every vertex has the same degree, meaning the same number of edges. Every regular bipartite graph with degree k ≥ 1 has a perfect matching. Removing it leaves a regular graph of degree k − 1, so the edges split completely into k perfect matchings.
Say 6 teachers each teach 4 of 6 classes, and each class has 4 of the teachers. The lessons can be arranged in exactly 4 time slots with no one in two places at once.
Common mistakes
- Stopping at a maximal matching and calling it maximum.
- Flipping a route that ends at a matched vertex, which does not increase the size.
- Applying Kőnig's theorem to a graph containing an odd cycle.
- Assuming that every worker having at least one possible task guarantees a full assignment. Two workers may depend on the same single task.
If the terms are new, this introduction to graphs covers vertices, edges and paths from the beginning.