TL;DR
Suppose you have two groups, such as workers and shifts, and some pairs are allowed. A matching is a set of allowed pairs in which nobody appears twice. Bipartite matching asks for the largest such set. It is solved by starting with any matching and repeatedly flipping an alternating path of edges that adds one pair, until no such path is left.
Builds on: Graph · Leads to: Linear Programming
Where it's used
Object detection. The DETR detector (DETR, 2020), an object detector, predicts a fixed set of candidate boxes, and each one must be paired with at most one real object in the image. The pairing that minimises total cost is found with a bipartite matching, and the same step is used during training to decide which prediction is responsible for which label.
Job and task assignment. A dispatcher has drivers on one side and trips on the other, with an edge wherever a driver can reach a trip in time. Choosing a maximum matching tells the dispatcher how many trips can be served at once, and which driver takes which trip.
Wireless and scheduling. A base station can serve a set of users, each user can hear some stations, and each station carries one user at a time. Matching finds the largest set of user-station links that can be active together.
How it works
Take a tiny instance. Two people, Ana and Ben, and two tasks, 1 and 2. Ana can do both tasks. Ben can do only task 1. The allowed pairs are Ana–1, Ana–2 and Ben–1. We want as many people as possible each doing one task, with no task given to two people.
- Start empty. The matching is the empty set. Its size is 0.
- Pick any allowed pair that touches nobody yet (a greedy choice). Add Ana–1. Check: Ana and task 1 are both free, so the pair is valid. The size is now 1.
- Look at Ben. Ben's only allowed task is 1, and task 1 is taken by Ana. Ben cannot be added directly. Check: adding Ben–1 would give task 1 two people, so it is not allowed.
- Look for a path that fixes this. Ben – task 1 – Ana – task 2. The first edge is not in the matching, the middle edge is, and the last edge is not. Task 2 is free, so this path ends at a free task and starts at a free person. Call such a path augmenting.
- Flip the path. Remove the matched edge Ana–1 and add the two unmatched edges Ben–1 and Ana–2. Check: every person and task now appears at most once, and the matching is Ben–1, Ana–2.
- The size went from 1 to 2. Check: both people are matched, so no bigger matching exists, and the algorithm stops.
The same idea works on any size. Start with any matching. Search for an augmenting path. Flip it. Stop when there is none. The number of pairs grows by exactly one at each flip.
flowchart TD
A[Start with any matching M] --> B{Is there an augmenting path?}
B -- yes --> C[Flip the path: matched edges leave, unmatched edges join]
C --> B
B -- no --> D[M is a maximum matching]
The math
A bipartite graph is a Graph whose vertices split into two disjoint sets (left) and (right), with every edge joining one vertex of to one vertex of . Write .
A matching is a set of edges in which no two edges share a vertex. A vertex is matched if some edge of touches it, and free otherwise. The size of is . A maximum matching has the largest size possible.
Step 1 in symbols: the allowed pairs
The allowed pairs are the edges . A row of the adjacency matrix lists the same information. For each , the set of its neighbours is . In the example, and .
Step 3 in symbols: why Ben is blocked
Ben cannot be added because and task 1 is already matched to Ana. Put differently, the edge Ben–1 shares the vertex 1 with the edge Ana–1 in .
Step 4 in symbols: the augmenting path
An augmenting path with respect to is a path such that:
- and are both free;
- the edges alternate: is not in , is in , is not in , and so on, ending with an edge not in .
In the example, Ben, , Ana, .
Step 5 in symbols: flipping
Flipping replaces by the symmetric difference , which removes the matched edges of and adds its unmatched edges. Because the path starts and ends at free vertices, every vertex keeps at most one edge, and the size rises by one:
In the example, and .
Berge's theorem
A matching is maximum if and only if there is no augmenting path with respect to . One direction is Step 5: if an augmenting path exists, the matching can grow. The converse says that if no path exists, no bigger matching exists. This is why the loop in "How it works" is correct: it stops only when the answer is optimal.
When a perfect matching exists
A perfect matching pairs every vertex of with a distinct vertex of . For a set , let be the set of all right vertices adjacent to some vertex in . Hall's condition says a matching covering all of exists exactly when
In the example, gives , of size 2, so the condition holds and a perfect matching exists.
Size and vertex covers
A vertex cover is a set of vertices that touches every edge. In a bipartite graph, the size of a maximum matching equals the size of a minimum vertex cover (König's theorem). This gives a certificate: a small cover proves that no larger matching is possible.
Cost of the search
Let and . Searching for one augmenting path, for example by a breadth-first search from the free left vertices, takes time proportional to , which is written in big-O notation. Doing this for each of up to flips gives in total. The Hopcroft–Karp variant finds many disjoint augmenting paths per phase and runs in .
Worked examples
Left vertices , right vertices , edges – and –. Vertex has no edges.
- Start with .
- Add –. Now , size 1.
- Try . Its only neighbour is , which is matched to . No direct addition is possible.
- Look for an augmenting path starting at the free left vertex . The first edge – is not in , and the next edge – is in , so the path reaches . The only edge at is –, which is already on the path, so the path cannot continue. It never reaches a free right vertex. Vertex is free, but no edge leads to it.
- No augmenting path exists, so by Berge's theorem is maximum. The answer is 1.
Interpretation: with one task reachable by both people, only one person can be assigned, however the matching is chosen.
Left , right . Edges: –, –, –, –, –.
- Start greedily with –. Size 1.
- Vertex has only the neighbour , which is matched to . Find an augmenting path from : –––. The vertex is free. Flip it: remove –, add – and –. Size 2, matching .
- Vertex has neighbours and . Vertex is matched to . Vertex is free, so – is itself an augmenting path of length one. Add it. Size 3.
- All of is matched, so the matching is perfect and therefore maximum. The answer is 3.
Interpretation: the greedy first choice – was not wrong to make, but it blocked . A single flip recovered the blocked person. This is the whole method.
Four workers W1 to W4 and four shifts S1 to S4. Allowed pairs: W1 can work S1 or S2; W2 can work S1; W3 can work S2 or S3; W4 can work S3.
- Start with W2–S1 and W1–S2. Size 2.
- W3 can take S2 (taken) or S3 (free). Add W3–S3. Size 3.
- W4 can take only S3, which is taken by W3. Follow every alternating route from W4: W4–S3 leads to W3, W3–S2 leads to W1, and W1–S1 leads to W2. Each of these ends at a matched shift (S3, S2, S1), so none reaches a free shift. No augmenting path exists.
- By Berge's theorem the matching of size 3 is maximum. So at most 3 shifts can be filled, even though there are 4 workers.
- Certificate: the four workers together can reach only the three shifts S1, S2, S3, and violates Hall's condition.
Try it: a fifth worker W5 can work only S2. Does the maximum number of filled shifts change? Answer: no, it stays 3. The shifts S1, S2 and S3 are the only ones anyone can work, so no matching can fill S4 or more than three shifts. The extra worker adds no new shift.
Common mistakes and tradeoffs
Mistake: a matching that cannot be extended is maximum. It is tempting to think that once no pair can be added directly, the matching is as large as possible. In fact a maximal matching (one you cannot extend) can be as small as half the size of a maximum one. With edges –, –, –: choosing – first leaves no addable pair, giving size 1, while – and – give size 2. Only the absence of an augmenting path, not the absence of a direct addition, proves optimality.
Mistake: any graph can be split into two sides. It is tempting to think every graph is bipartite. In fact a triangle is not: any split puts two of its three vertices on the same side, and an edge joins them. The algorithms here need the two sides. For general graphs, matching needs Edmonds' blossom algorithm, which is harder.
Mistake: weights do not matter. Plain bipartite matching counts pairs. If each pair has a cost, as in assigning workers to shifts with different wages, the question becomes minimum-cost matching. The Hungarian algorithm solves that version in . Plain matching gives the wrong answer if weights are ignored.
When to use it. Use bipartite matching when there are two groups, each member of a group can take at most one partner, and you only care about how many pairs form or whether a full pairing exists. When not to use it. When the two sides are not really separate, or when pairs have costs, use the weighted version. For very large graphs, Hopcroft–Karp is preferred to the simple depth-first loop. An alternative is to write the problem as a linear program, which is correct here because the constraints have whole-number solutions, but it is usually slower than the combinatorial algorithms.
History
In 1931 Dénes König and Jenő Egerváry independently proved that, in a bipartite graph, the largest matching has the same size as the smallest vertex cover. In 1935 Philip Hall proved the condition that now bears his name, which says when every member of one side can be matched. In 1955 Harold Kuhn gave an explicit algorithm for the assignment problem, which he called the Hungarian method because it built on the work of König and Egerváry. James Munkres later refined it in 1957. Claude Berge proved the augmenting-path characterisation in 1957. John Hopcroft and Richard Karp published the faster method in 1973. The subject caught on because the same problem appears whenever resources must be paired, and the augmenting-path idea gave a simple, provably correct procedure.
Recap
- A matching is a set of pairs in which no vertex is used twice; a bipartite graph has two sides and every edge crosses between them.
- An augmenting path starts and ends at free vertices and alternates between unmatched and matched edges. Flipping it grows the matching by one.
- A matching is maximum exactly when no augmenting path exists (Berge's theorem).
- Hall's condition says when a matching covers one whole side; König's theorem ties the size of the largest matching to the smallest vertex cover.
- Read next: DETR, which uses matching to pair predictions with objects; and Linear Programming, the general framework behind the assignment problem.