Search

Search papers and lessons

Bipartite Matching

Level 3 · 12 min read

Bipartite matching pairs members of two groups so that nobody is used twice and as many pairs as possible are formed, and it is solved by repeatedly flipping an augmenting path.

On this page

  • Colour = topic mix
  • Discrete math
  • Algorithms
  • ReadReady to readNot yet

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.

  1. Start empty. The matching is the empty set. Its size is 0.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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 LL (left) and RR (right), with every edge joining one vertex of LL to one vertex of RR. Write G=(L∪R,E)G = (L \cup R, E).

A matching is a set of edges M⊆EM \subseteq E in which no two edges share a vertex. A vertex is matched if some edge of MM touches it, and free otherwise. The size of MM is ∣M∣|M|. A maximum matching has the largest size possible.

Step 1 in symbols: the allowed pairs

The allowed pairs are the edges EE. A row of the adjacency matrix lists the same information. For each ℓ∈L\ell \in L, the set of its neighbours is N(ℓ)={r∈R:(ℓ,r)∈E}N(\ell) = \{ r \in R : (\ell, r) \in E \}. In the example, N(Ana)={1,2}N(\text{Ana}) = \{1, 2\} and N(Ben)={1}N(\text{Ben}) = \{1\}.

Step 3 in symbols: why Ben is blocked

Ben cannot be added because N(Ben)={1}N(\text{Ben}) = \{1\} and task 1 is already matched to Ana. Put differently, the edge Ben–1 shares the vertex 1 with the edge Ana–1 in MM.

Step 4 in symbols: the augmenting path

An augmenting path with respect to MM is a path v0,v1,…,v2k+1v_0, v_1, \ldots, v_{2k+1} such that:

  • v0∈Lv_0 \in L and v2k+1∈Rv_{2k+1} \in R are both free;
  • the edges alternate: (v0,v1)(v_0, v_1) is not in MM, (v1,v2)(v_1, v_2) is in MM, (v2,v3)(v_2, v_3) is not in MM, and so on, ending with an edge not in MM.

In the example, v0=v_0 = Ben, v1=1v_1 = 1, v2=v_2 = Ana, v3=2v_3 = 2.

Step 5 in symbols: flipping

Flipping replaces MM by the symmetric difference M⊕PM \oplus P, which removes the matched edges of PP 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:

∣M⊕P∣=∣M∣+1.|M \oplus P| = |M| + 1.

In the example, M={Ana–1}M = \{\text{Ana–1}\} and M⊕P={Ben–1,Ana–2}M \oplus P = \{\text{Ben–1}, \text{Ana–2}\}.

Berge's theorem

A matching MM is maximum if and only if there is no augmenting path with respect to MM. 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 LL with a distinct vertex of RR. For a set S⊆LS \subseteq L, let N(S)N(S) be the set of all right vertices adjacent to some vertex in SS. Hall's condition says a matching covering all of LL exists exactly when

∣N(S)∣≥∣S∣for every S⊆L.|N(S)| \ge |S| \quad \text{for every } S \subseteq L.

In the example, S=LS = L gives N(S)={1,2}N(S) = \{1, 2\}, 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.

Let n=∣V∣n = |V| and m=∣E∣m = |E|. Searching for one augmenting path, for example by a breadth-first search from the free left vertices, takes time proportional to mm, which is written O(m)O(m) in big-O notation. Doing this for each of up to n/2n/2 flips gives O(nm)O(nm) in total. The Hopcroft–Karp variant finds many disjoint augmenting paths per phase and runs in O(mn)O(m\sqrt{n}).

Worked examples

Example
1. Tiny: one task, two people

Left vertices L={x,y}L = \{x, y\}, right vertices R={p,q}R = \{p, q\}, edges xx–pp and yy–pp. Vertex qq has no edges.

  1. Start with M=∅M = \emptyset.
  2. Add xx–pp. Now M={x–p}M = \{x\text{–}p\}, size 1.
  3. Try yy. Its only neighbour is pp, which is matched to xx. No direct addition is possible.
  4. Look for an augmenting path starting at the free left vertex yy. The first edge yy–pp is not in MM, and the next edge pp–xx is in MM, so the path reaches xx. The only edge at xx is xx–pp, which is already on the path, so the path cannot continue. It never reaches a free right vertex. Vertex qq is free, but no edge leads to it.
  5. No augmenting path exists, so by Berge's theorem MM 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.

Example
2. Standard: three people, three tasks

Left L={a,b,c}L = \{a, b, c\}, right R={1,2,3}R = \{1, 2, 3\}. Edges: aa–11, aa–22, bb–11, cc–22, cc–33.

  1. Start greedily with aa–11. Size 1.
  2. Vertex bb has only the neighbour 11, which is matched to aa. Find an augmenting path from bb: bb–11–aa–22. The vertex 22 is free. Flip it: remove aa–11, add bb–11 and aa–22. Size 2, matching {b–1,a–2}\{b\text{–}1, a\text{–}2\}.
  3. Vertex cc has neighbours 22 and 33. Vertex 22 is matched to aa. Vertex 33 is free, so cc–33 is itself an augmenting path of length one. Add it. Size 3.
  4. All of LL is matched, so the matching is perfect and therefore maximum. The answer is 3.

Interpretation: the greedy first choice aa–11 was not wrong to make, but it blocked bb. A single flip recovered the blocked person. This is the whole method.

Example
3. Realistic: four workers, four shifts

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.

  1. Start with W2–S1 and W1–S2. Size 2.
  2. W3 can take S2 (taken) or S3 (free). Add W3–S3. Size 3.
  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.
  4. 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.
  5. Certificate: the four workers together can reach only the three shifts S1, S2, S3, and ∣N({W1,W2,W3,W4})∣=3<4|N(\{\text{W1},\text{W2},\text{W3},\text{W4}\})| = 3 < 4 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 aa–11, aa–22, bb–11: choosing aa–11 first leaves no addable pair, giving size 1, while aa–22 and bb–11 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 O(n3)O(n^3). 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 O(mn)O(m\sqrt{n}) 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.

Check yourself

QuestionWhat is a matching in a graph?Show answer
A set of edges in which no two edges share a vertex. Every vertex is used at most once.
QuestionA bipartite graph has left vertices {p,q}\{p, q\}, right vertices {1,2}\{1, 2\}, and edges pp–11, qq–11, qq–22. Starting from the matching {p–1}\{p\text{–}1\}, what augmenting path do you use, and what is the new size?Show answer
Use the path qq–22 alone. Vertex qq is free and vertex 22 is free, so this single edge is an augmenting path of length one. Adding it gives {p–1,q–2}\{p\text{–}1, q\text{–}2\}, of size 2.
QuestionA bipartite graph has left vertices {a,b,c}\{a, b, c\} and right vertices {1,2}\{1, 2\} with edges aa–11, bb–11, cc–22. What is the size of a maximum matching?Show answer
Size 2. The edges aa–11 and cc–22 share no vertex, giving size 2. Size 3 is impossible because the right side has only two vertices, so no matching can use three distinct right vertices.
QuestionSuppose three workers W1, W2 and W3 can each work only S1 or S2. Why does Hall's condition fail, and what does this tell you about the best matching?Show answer
Take S={W1,W2,W3}S = \{\text{W1}, \text{W2}, \text{W3}\}. Their combined neighbourhood is N(S)={S1,S2}N(S) = \{\text{S1}, \text{S2}\}, of size 2, which is smaller than ∣S∣=3|S| = 3. Hall's condition therefore fails, so no matching can cover all three workers. At most two of them can be placed, so the maximum matching has size 2.
QuestionApplied: a dispatcher has drivers D1, D2, D3 and trips T1, T2, T3, with edges D1–T1, D1–T2, D2–T1, D3–T2 and D3–T3. Explain in words how the dispatcher can check whether all three trips can be covered, and what a failed check would tell them.Show answer
Start with any matching and apply the augmenting-path loop until no path remains. If the final matching covers all three trips, every trip is served. If it does not, Hall's condition gives the reason: some set of trips has fewer than that many drivers able to reach them, and that set is the bottleneck to report.

Sources