Sub topic 15
Application: Maximum Bipartite Matching
This chapter assumes you have read the earlier chapters on the integrality theorem, the modeling tricks with super sources and sinks, and the MaxFlow class built with paired forward and reverse edges. The code below uses that class without redefining it.
The Matching Problem
A bipartite graph splits its vertices into a left set L and a right set R, and every edge joins a vertex in L to a vertex in R. A matching is a set of edges in which no vertex appears more than once. The goal is a matching with as many edges as possible.
Typical examples include assigning workers to jobs they are qualified for, students to project slots, or tasks to machines. Each item can be used at most once, and only some pairings are allowed.
Building the Flow Network
The reduction adds two vertices and gives every edge capacity 1:
- Add a super source s with an edge s → u of capacity 1 for every left vertex u.
- Add a super sink t with an edge v → t of capacity 1 for every right vertex v.
- Direct every allowed pair (u, v) from left to right with capacity 1.
Why Flow Value Equals Matching Size
All capacities are integers, so the integrality theorem guarantees that some maximum flow is integral, and Ford-Fulkerson-style algorithms find one. In an integral flow every edge carries 0 or 1. Each left vertex receives at most 1 unit from s, so it sends flow along at most one outgoing edge. Each right vertex can pass at most 1 unit to t, so it receives flow from at most one left vertex. The saturated left-to-right edges therefore form a matching, and its size equals the flow value.
The correspondence also works in reverse. Any matching of size k gives a valid flow of value k: send one unit along s → u → v → t for each matched pair. So the maximum flow equals the maximum matching.
Code: Size and Pairs
The function below builds the network, runs Edmonds-Karp, and then reads off the matched pairs. A forward left-to-right edge whose residual capacity dropped from 1 to 0 carries flow, so its pair is in the matching. Forward edges sit at even indices, and the tail of edge e is to[e ^ 1].
def matching_pairs(left, right, pairs):
n = left + right + 2
s, t = 0, n - 1
mf = MaxFlow(n) # class from the implementation chapter
for i in range(left):
mf.add_edge(s, 1 + i, 1)
for j in range(right):
mf.add_edge(1 + left + j, t, 1)
for i, j in pairs:
mf.add_edge(1 + i, 1 + left + j, 1)
size = mf.max_flow(s, t)
matched = []
for e in range(0, len(mf.to), 2):
u, v = mf.to[e ^ 1], mf.to[e]
if 1 <= u <= left and left < v <= left + right and mf.cap[e] == 0:
matched.append((u - 1, v - 1 - left))
return size, matched
print(matching_pairs(3, 3, [(0, 0), (0, 1), (1, 0), (2, 1), (2, 2)]))
# (3, [(0, 1), (1, 0), (2, 2)])During the run, the first BFS matches L0 with R0. The third augmenting path goes s → L1 → R0 → L0 → R1 → L2 → R2 → t. It travels the reverse edges R0 → L0 and R1 → L2, which reassigns L0 to R1 and frees R0 for L1. In matching theory this path is called an alternating path.
Cost
The flow value is at most min(|L|, |R|) ≤ V, and each BFS costs O(V + E). The total is O(V · E), which is better than the general O(V E²) bound for Edmonds-Karp. Hopcroft-Karp improves this to O(E √V) by augmenting along many shortest paths per phase, which is the same idea Dinic's algorithm uses.
Exercise
Each job Rj can accept up to cj workers, and each worker still takes one job. Which capacities change, and what does the maximum flow now represent?
Show answer
Set the capacity of each edge Rj → t to cj and leave all other capacities at 1. The maximum flow equals the largest number of workers that can be assigned without any job going over its limit. By integrality, an integral optimum exists, so each worker sends flow along exactly one edge or none.
Exercise
Left vertices L0 and L1 both connect only to R0. What maximum flow does the network give, and why can't it be larger?
Show answer
The maximum flow is 1. The cut {R0 → t} has capacity 1, so by max-flow min-cut no flow can be larger. In matching terms, only one of L0 and L1 can use R0.