Learn
Lesson · Graph Algorithms · Flow and matching

Bipartite Matching

Pairing up two sides so that nobody is used twice is a maximum-flow problem in disguise: give every arc capacity 1, add a source feeding the left and a sink fed by the right, and the value of the maximum flow is the size of the largest matching. The minimum cut, read back through the construction, is a set of vertices touching every pair, and where the matching falls short it exhibits the reason.

source to every left vertex, capacity 1; every right vertex to the sink, capacity 1 one arc of capacity 1 per allowed pair, left to right max flow = size of the largest matching, and every flow value is a whole number min cut read back = a smallest set of vertices touching every pair maximal is not maximum: the greedy answer depends on the order
One construction, and the three things one run returns

Core ideas

What this lesson establishes

The hard idea is the correspondence, in both directions. The cover and the obstruction are then the max-flow certificate translated back.

1

The construction, and why it is exact both ways

Every matching of size k gives a flow of value k: push one unit along the source arc, the pair arc and the sink arc of each matched pair, and no capacity is exceeded because no vertex is matched twice. Every integer flow of value k gives a matching of size k: the pair arcs carrying flow share no endpoint, because each left vertex admits one unit in and each right vertex emits one unit out. So the two maxima are the same number, and neither direction has slack in it.

2

Whole numbers, because every capacity is one

The augmenting-path method pushes the bottleneck of each path, and on this network every residual capacity is 0 or 1, so every push is by 1 and every arc ends carrying 0 or 1. The lab checks this rather than assuming it — the flow is reported as whole on every case — because a fractional flow would carry no matching at all.

3

The cut translates into a cover, and the shortfall into an obstruction

The minimum cut of this network, read back, is a set containing some left vertices and some right ones such that every allowed pair has an end in it — a vertex cover — and its size equals the matching's. On the lab's opening case with three on the left and two on the right, the matching has size 2 and the cover is the left vertex 3 together with the right vertex 1, also size 2. Two left vertices between them reach only one right vertex, and that deficiency is what makes a perfect matching impossible.

The material

One network, and the three readings of its answer

The construction, the correspondence in both directions, the cover, the deficient set, and why greedy is not enough.

Definition · Matching, maximal, maximum

A matching in a bipartite graph is a set of allowed pairs no two of which share a vertex. It is maximal if no further pair can be added to it, and maximum if no matching is larger. A matching is perfect on the left if every left vertex is in it.

The construction

  1. Add a source with an arc of capacity 1 to every left vertex.
  2. Add a sink with an arc of capacity 1 from every right vertex.
  3. For each allowed pair, add an arc of capacity 1 from the left vertex to the right one.
  4. Find a maximum flow. The pair arcs carrying flow are a maximum matching.
Theorem · The two maxima coincide

In the network above, the maximum flow value equals the size of a maximum matching, and a maximum matching can be read off any integer maximum flow as the set of pair arcs carrying one unit.

A matching gives a flow of the same value, by routing one unit through each matched pair. The capacity 1 on the source arcs and the sink arcs is respected because a matching uses each vertex at most once, and conservation holds at every left and right vertex by construction.

An integer flow of value k gives a matching of size k. Each pair arc carries 0 or 1. If two pair arcs carrying flow shared a left vertex, that vertex would emit 2 while receiving at most 1, breaking conservation; the same argument on the right settles the other side. So the arcs carrying flow form a matching, and there are k of them because k units leave the source along distinct arcs.

Each maximum therefore bounds the other, so they are equal. The augmenting-path method returns an integer flow on this network because every capacity is 1 and every bottleneck is therefore 1.

□

Example · Three on the left competing for two

The lab opens on three left vertices and two right ones with the allowed pairs L1–R1, L2–R1, L3–R1 and L3–R2. The network has 7 vertices and 9 arcs, and the maximum flow is 2, reached in two augmentations. The matching is L1 with R1 and L3 with R2. The cut, read back, is the cover {L3, R1}, of size 2. A search over every subset of the four pairs, which knows nothing about flow, also returns 2.

Check the cover against the pairs: L1–R1 is covered by R1, L2–R1 by R1, L3–R1 by both, and L3–R2 by L3. Four pairs, two vertices, everything touched. And no single vertex could do it, because L1–R1 and L3–R2 share nothing — which is the matching bounding the cover from below.

Why the matching stopped at two

The left vertices 1 and 2 have only one right vertex between them: both are joined to R1 and nothing else. Two of them cannot both be matched into one, so no matching covers all three left vertices. The lab exhibits that set rather than describing it, reporting S = {L1, L2} with a neighbourhood of size 1 against a set of size 2.

That is the general shape of the obstruction: a set of left vertices whose combined neighbourhood is smaller than itself. Whenever the matching misses a left vertex, the cut hands one back. It is a proof that no better matching exists, in a form a reader can check by looking at four pairs rather than by trusting a flow.

The lab's star case is the same phenomenon at a larger scale: four on each side with the pairs L1–R1, L2–R1, L3–R1, L4–R1 and L4–R4. The maximum matching is 2, the cover is {L4, R1} of size 2, and the deficient set is {L1, L2, L3} with a neighbourhood of exactly one vertex. One popular right vertex is the whole story, and the cover names it.

Greedy is maximal, and the order decides how maximal

Example · Three pairs, two orders, two answers

Take two vertices on each side with the pairs L2–R2, L1–R2 and L2–R1. Taking them greedily in that order gives one pair: L2–R2 is taken and it blocks both of the others. Taking the same three pairs in the order L1–R2, L2–R1, L2–R2 gives two. The maximum is 2, confirmed by the exhaustive search and by the flow, and the cover has size 2 as well.

So a matching nothing can be added to may be half the size of the largest one, and which happens depends only on the order the pairs were considered in. That is the same distinction “Augmenting Paths” drew between maximal and maximum flow, appearing here in its most concrete form — and the augmenting path is exactly what repairs it, because an augmenting path in this network alternates between unmatched and matched pairs and swaps them.

Where the matching is perfect there is nothing to exhibit. On three left and three right with six pairs the lab returns a matching of size 3, a cover of size 3, and a greedy pass that also finds 3; the network has 8 vertices and 12 arcs. The construction is unchanged, and the absence of a deficient set is the report.

Measured against proved, for the last time on this course. The exhaustive search over subsets of the pairs is refused above the size it states — seventeen pairs is declined rather than run slowly — so below that cap “this matching is maximum” is a fact about a complete list, and above it the claim rests on the flow and its cut. The equality of the matching and the cover is proved by the max-flow min-cut theorem and confirmed on every case the panel runs, which is the right division of labour: the enumeration catches a construction that is wrong, and the theorem covers the sizes the enumeration cannot reach.

Interactive

Bipartite matching as a flow, and the cover the cut names

Unit capacities make the answer whole, and the minimum cut is a vertex cover of the same size

Matching as a flow, and the cut as a coverunit capacities make the flow whole, and the cut names the cover
matchedin the vertex covera pair left unmatched

Method

Solving a pairing problem

Build the network, run one maximum flow, then read three things off it.

1

Build the network before you look for a pairing

Source to left at capacity 1, right to sink at capacity 1, one arc per allowed pair. Every constraint of the problem is now a capacity, and nothing about matching has to be re-implemented — which is the point of the reduction and the reason it is worth the extra vertices.

2

Check that the flow came back whole

Every capacity is 1, so every bottleneck is 1 and every arc ends at 0 or 1. If any arc carries a fraction, the flow does not correspond to a matching at all, and the check costs one pass over the arcs.

3

Read the matching off the middle layer

The pair arcs carrying one unit are the matching. Not the source arcs, which only say which left vertices were used, and not the saturated arcs generally — the distinction drawn in “Max Flow and Min Cut” applies here too.

4

When the matching is short, produce the obstruction

The cut gives a vertex cover of the same size, and where a left vertex is unmatched it also gives a set of left vertices with too few neighbours. Report that set: it is what makes the shortfall explicable, and it is the difference between “no” and “no, because these two both need that one”.

Matchings, covers and obstructions

Score 0 / 0

Three on the left, two on the right, and why two is the answer

The allowed pairs are L1 with R1, L2 with R1, L3 with R1, and L3 with R2. Three left vertices, two right ones, four pairs.

the network 7 vertices, 9 arcs, every capacity 1 s > L1, s > L2, s > L3 L1 > R1, L2 > R1, L3 > R1, L3 > R2 R1 > t, R2 > t augmentation path bottleneck value 1 s > L1 > R1 > t 1 1 2 s > L3 > R2 > t 1 2 3 no path remains 2 matching L1 - R1 , L3 - R2 size 2 cover L3 and R1 size 2 L1-R1 covered by R1 L2-R1 covered by R1 L3-R1 covered by both L3-R2 covered by L3 every subset of the four pairs, searched exhaustively: 2 greedy, in the order typed: 2 the obstruction S = {L1, L2} their neighbours = {R1} 2 left vertices reaching 1 right vertex

The cover and the matching have the same size, and that is not a coincidence of this instance: it is the max-flow min-cut theorem, since the cover is the minimum cut read back through the construction and the matching is the maximum flow. Every pair must be touched by the cover, and no two matched pairs can be touched by one vertex, so the cover is at least the matching and the theorem makes it equal.

The deficient set is the part a human can check without any of the machinery. L1 and L2 both need R1 and nothing else; one of them will go unmatched whatever is done. That sentence is a complete proof that 3 is impossible, and it came out of the cut.

For a faded rehearsal, add the pair L2 with R2 and predict the matching, the cover and the obstruction before running it. The supplied first move is this: L2 now has two choices, so the set {L1, L2} reaches both right vertices and is no longer deficient. Say what the maximum matching becomes, whether any left vertex is still unmatched, and what the cover should then be.

Common mistakes

1
Giving the source or sink arcs a capacity above oneThe capacity 1 on those arcs is what encodes “each vertex may be used once”. Raise it and the maximum flow rises too, and what comes back is no longer a matching — it is a set of pairs in which some vertex appears twice. Nothing about the flow looks wrong; the constraint has simply stopped being expressed.
2
Taking the saturated arcs as the matchingThe source and sink arcs are saturated too, and they are not pairs. The matching is the middle layer: the arcs from a left vertex to a right vertex that carry one unit. This is the same confusion “Max Flow and Min Cut” drew out between saturation and membership of a cut, in a setting where the wrong answer has the right size.
3
Reporting a shortfall without the obstruction“The largest matching has size 2” is an answer nobody can act on. “L1 and L2 both need R1, so one of them cannot be placed” names what would have to change. The cut supplies it at no extra cost, and the lab prints both the cover and the deficient set for exactly that reason.

Check your understanding

✓
Finish when you can build the network from a pairing problem and read all three answers off one run.

You should be able to write down the source and sink arcs with the right capacities, argue the correspondence in both directions, extract the matching from the middle layer, translate the cut into a vertex cover, and produce the deficient set that explains a shortfall rather than reporting only its size.

Note: This is the last lesson of the course. What carries forward is the pattern rather than the algorithm: a problem that mentions no flow at all was solved by building a network, and the certificate came back translated into the original vocabulary. The next course generalises a different result from this one — the cut property and the sorted method are the instance that the matroid theorem is the abstraction of, which is why they were built here first.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.