47.04.04 · theoretical-cs / algorithms-analysis

Bipartite Matching: Hall's Theorem, Augmenting Paths, and the Hungarian Algorithm

shipped3 tiersLean: none

Anchor (Master): Cormen, Leiserson, Rivest & Stein 2022 Introduction to Algorithms 4e (MIT Press) §26.3; Schrijver 2003 Combinatorial Optimization (Springer) §4.1-4.2 (the Hungarian method via linear programming duality, the primal-dual framework, extensions to general matching via Edmonds' blossom algorithm)

Intuition Beginner

Imagine a group of workers and a set of jobs. Each worker is qualified for some subset of the jobs. You want to assign workers to jobs so that no two workers get the same job and no worker gets more than one job. This is a bipartite matching problem.

The word "bipartite" means the graph has two sides (workers and jobs), and all edges go between the two sides — never within the same side. A "matching" is a set of edges where no two edges share a vertex — each worker gets at most one job, and each job gets at most one worker.

The central question is: what is the largest possible matching? How many workers can you simultaneously assign?

Hall's theorem gives a beautiful answer. It says: every worker can be matched to a distinct job if and only if for every subset of workers, the total number of jobs they are collectively qualified for is at least as large as the number of workers in the subset. This makes intuitive sense: if three workers are collectively qualified for only two jobs, you cannot match all three.

The augmenting-path algorithm finds a maximum matching by starting with any matching and repeatedly improving it. An augmenting path is a path that alternates between unmatched and matched edges, starting and ending at unmatched vertices. Flipping the edges along this path (matched becomes unmatched and vice versa) increases the matching size by one.

When no augmenting path exists, the matching is maximum. This is a consequence of the max-flow min-cut theorem applied to the flow network constructed from the bipartite graph.

The Hungarian algorithm solves the weighted version: each worker-job pair has a cost or value, and you want the matching with minimum total cost (or maximum value). It runs in time by maintaining a feasible "potential" function on the vertices and iteratively improving the matching.

Visual Beginner

A bipartite graph with a matching:

Workers (U)          Jobs (V)
  w1 ──────── j1
  w2 ──────── j2
  │  ╲           │
  │    ╲         │
  w3 ──────── j3
              /
            /
  w4 ──────── j4

The bold edges form a matching of size 3: , , . Worker is unmatched.

An augmenting path: . This path has 3 edges, alternating unmatched (w4-j3), matched (w3-j3), unmatched (w3-j4). Flipping:

Before: M = {w1-j1, w2-j2, w3-j3}, size 3
After:  M' = {w1-j1, w2-j2, w3-j4, w4-j3}, size 4 (perfect!)
Concept Definition Condition
Matching Set of disjoint edges No shared vertices
Maximum matching Largest possible No augmenting path exists
Perfect matching Covers all vertices $
Hall's condition $ N(S)

Worked example Beginner

Three students apply for three projects . The compatibility edges are: can do ; can do ; can do .

Start with an empty matching. Find an augmenting path from to : just the edge . Add it. Matching: , size 1.

Find an augmenting path from . is unmatched. Try : matched to . From , try : unmatched. Path: . Flip: remove , add and . Matching: , size 2.

Find an augmenting path from . is unmatched. Try : matched to . From , all neighbours () are explored. is matched to . From , try : unmatched. Path: . Flip: remove and , add , , and . Matching: , size 3. This is a perfect matching.

Check your understanding Beginner

Formal definition Intermediate+

Definition (bipartite matching). Let be a bipartite graph. A matching is a set such that no two edges in share a common endpoint. A maximum matching maximises . A perfect matching satisfies .

Theorem (Hall's marriage theorem). Let be a bipartite graph with . There exists a matching that covers every vertex in if and only if for every :

Proof. The forward direction is immediate: if a matching covers , then every is matched to distinct vertices in , so .

For the backward direction, we prove the contrapositive. Suppose no matching covers . Let be a maximum matching, and let be unmatched. Consider the set of all vertices reachable from by alternating paths (paths alternating between edges not in and edges in ). Let and .

Every vertex in is matched (reached by a non-matching edge from and followed by a matching edge to ), so (one unmatched vertex is in ). Every neighbour of is in (otherwise there would be an augmenting path, contradicting maximality). So and . Hall's condition is violated.

Theorem (König's theorem). In a bipartite graph, the size of a maximum matching equals the size of a minimum vertex cover.

Proof. The max-flow reduction connects this to the max-flow min-cut theorem. Construct a flow network with source connected to (capacity 1), edges from to (capacity ), and connected to sink (capacity 1). The maximum flow equals the maximum matching. The minimum - cut corresponds to a vertex cover: the -side vertices in the cut plus the -side vertices in the cut form a vertex cover of the same size as the max flow.

Counterexamples to common slips

  • "Hall's theorem applies to any graph." Hall's theorem specifically requires a bipartite graph. For general graphs, the Tutte-Berge formula replaces Hall's condition.

  • "The Hungarian algorithm finds maximum-cardinality matchings." The Hungarian algorithm finds maximum-weight matchings in weighted bipartite graphs. For unweighted maximum matching, the augmenting-path or Hopcroft-Karp algorithm is used.

  • "A maximum matching is the same as a maximal matching." A maximal matching cannot be extended by adding an edge. A maximum matching has the largest possible size. A maximal matching can be half the size of a maximum matching.

Key theorem with proof Intermediate+

Theorem (augmenting-path algorithm). A matching in a bipartite graph is maximum if and only if there is no -augmenting path.

Proof. If an -augmenting path exists, flipping edges along produces a matching of size , so is not maximum.

Conversely, suppose is not maximum. Let be a maximum matching with . Consider the symmetric difference . Each vertex is incident to at most one edge from and at most one from , so every component of is a path or an even cycle. Since , there must be a component that is a path starting and ending with -edges. This path starts and ends at -unmatched vertices and alternates between -edges (not in ) and -edges (in ). This is an -augmenting path.

Theorem (Hungarian algorithm complexity). The Hungarian algorithm finds a maximum-weight perfect matching in a complete bipartite graph with edge weights in time.

Proof sketch. The algorithm maintains a feasible potential where for and for satisfy for all . The equality subgraph has edge iff . The algorithm iteratively:

  1. Finds a maximum matching in (using the augmenting-path method).
  2. If the matching is perfect, it is optimal by LP duality.
  3. If not, updates the potential to add edges to (tightening the dual) and repeats.

Each potential update increases the size of the matching or adds edges, and the total number of iterations is , each taking time. Total: .

Bridge. The augmenting-path method for bipartite matching builds toward the entire theory of combinatorial optimisation as the interplay between primal solutions (matchings) and dual certificates (vertex covers, potentials): the central insight is that Hall's condition provides the structural reason a matching exists, and this is exactly what makes bipartite matching the model problem for the max-flow min-cut framework. This appears again in 47.04.03 where the Ford-Fulkerson algorithm generalises augmenting paths from matchings to arbitrary flows, and in 47.04.06 where minimum spanning trees use a dual cut-based optimality condition analogous to König's theorem. The foundational reason bipartite matching is tractable is that the bipartite structure prevents the "blossom" obstructions that Edmonds' general matching algorithm must handle. Putting these together with the Hungarian algorithm shows that the primal-dual framework — simultaneously maintaining a feasible matching and a feasible potential — is the generalises from the specific bipartite setting to all of linear programming.

Exercises Intermediate+

Lean formalization Intermediate+

Mathlib has Combinatorics.SimpleGraph.Matching and Combinatorics.Hall with a proof of Hall's marriage theorem. The augmenting-path algorithm, König's theorem, and the Hungarian algorithm are not formalised. The foundational gap is the absence of a verified maximum-matching algorithm (either augmenting-path or Hopcroft-Karp) with a correctness proof, and the Hungarian algorithm for weighted matching. König's theorem connecting matchings to vertex covers in bipartite graphs would be a natural target for formalisation. This unit ships without a lean_module.

Advanced results Master

Three directions extend bipartite matching theory.

Theorem 1 (Edmonds' blossom algorithm). Maximum matching in general (non-bipartite) graphs can be found in time. The algorithm handles "blossoms" — odd cycles that arise in non-bipartite graphs and prevent the simple augmenting-path approach from working [CLRS §26.3].

In bipartite graphs, alternating paths never cycle back to a previously visited vertex on the same side. In general graphs, odd cycles ("blossoms") can trap the search. Edmonds' algorithm contracts blossoms into single vertices, finds augmenting paths in the contracted graph, and then expands the blossoms to recover the augmenting path in the original graph.

Theorem 2 (LP relaxation integrality). The natural linear programming relaxation of bipartite matching (maximise subject to for all , ) has an integral optimum. This is dual to the LP relaxation of vertex cover, and both are integral for bipartite graphs.

This integrality is the foundational reason bipartite matching can be solved in polynomial time via linear programming. The Hungarian algorithm is essentially a primal-dual method for this LP pair.

Theorem 3 (online bipartite matching). In the online setting where vertices on one side arrive one at a time and must be matched immediately or never, the optimal competitive ratio is (Karp, Vazirani, and Vazirani 1990). The RANKING algorithm achieves this ratio.

Online matching has applications in ad allocation, job scheduling, and kidney exchange. The competitive ratio of matches the performance of the greedy algorithm for the secretary problem.

Synthesis. Bipartite matching is the foundational reason that the max-flow framework provides a unified theory of combinatorial optimisation: the central insight is that the reduction from matching to max-flow, combined with LP duality and Hall's theorem, creates a three-way equivalence between combinatorial, algebraic, and optimisation perspectives on the same problem. This builds toward the general matching theory of Edmonds' blossom algorithm in 47.04.06 where the bipartite restriction is removed, and appears again in 47.02.03 where the contrast between tractable matching and NP-complete Hamiltonian cycle illustrates the boundary of polynomial-time computation. The generalises from the specific augmenting-path method to the entire primal-dual framework, and putting these together with König's theorem shows that the matching-cover duality is the structural backbone of bipartite graph theory.

Full proof set Master

Proposition 1 (Hall's theorem). A bipartite graph has a matching covering iff for all .

Proof. Forward: if matching covers , every is matched to distinct vertices in , all in , so .

Backward (contrapositive): suppose no matching covers . Let be a maximum matching and unmatched. Build = vertices reachable from via alternating paths. Set , . Every is reached from some via a non-matching edge, then followed by a matching edge back to (since is maximum, no alternating path reaches an unmatched vertex in ). So gives a bijection from to , giving . Every neighbour of is in (otherwise there is an augmenting path). So .

Proposition 2 (König's theorem). In a bipartite graph, the maximum matching size equals the minimum vertex cover size.

Proof. Let be a maximum matching. Construct from unmatched -vertices via alternating paths. The vertex cover is .

Cover: every edge with is covered. If and : the edge is in (otherwise would be in ). But matched edges go from to , so . Contradiction. So either (in ) or (in ).

Size: the matching pairs with and with (by the alternating-path structure). So . Since any vertex cover has size , is minimum.

Connections Master

  • 47.04.03 — The max-flow min-cut theorem underpins the matching-to-flow reduction; Ford-Fulkerson augmenting paths generalise matching augmenting paths.

  • 40.04.01 — Basic graph definitions (bipartite, adjacency, degree) provide the foundation on which matching theory is built.

  • 47.02.03 — Bipartite matching is in P while Hamiltonian cycle is NP-complete; the contrast illustrates the tractability boundary for graph problems.

  • 47.04.06 — Minimum spanning trees and maximum matchings are both fundamental P-time graph optimisation problems with elegant structural characterisations.

  • 47.03.03 — The LP integrality of bipartite matching connects to circuit complexity through the theory of totally unimodular matrices.

Historical & philosophical context Master

Hall's theorem was proved by Philip Hall in 1935, though equivalent results were known to König (1916) and Menger (1927) in related forms. Hall's formulation in terms of the marriage problem (matching men to women subject to compatibility constraints) gave the theorem its popular name and直观 appeal.

The Hungarian algorithm was published by Harold Kuhn in 1955, who attributed the underlying ideas to the work of Hungarian mathematicians König and Egerváry. Kuhn showed that the primal-dual method for the assignment problem runs in time. James Munkres (1957) proved that Kuhn's algorithm always terminates in steps, and the algorithm is often called the Kuhn-Munkres algorithm.

König's theorem (1916) was one of the earliest results connecting matchings to vertex covers, establishing the minimax relationship that later generalised to the max-flow min-cut theorem. The equivalence between maximum matching and minimum vertex cover in bipartite graphs was a precursor to LP duality theory [CLRS §26.3].

Bibliography Master

@book{clrs2022,
  author    = {Cormen, Thomas H. and Leiserson, Charles E. and Rivest, Ronald L. and Stein, Clifford},
  title     = {Introduction to Algorithms},
  edition   = {4th},
  publisher = {MIT Press},
  year      = {2022},
}
@article{hall1935representatives,
  author  = {Hall, Philip},
  title   = {On Representatives of Subsets},
  journal = {Journal of the London Mathematical Society},
  volume  = {10},
  number  = {1},
  pages   = {26--30},
  year    = {1935},
}
@article{kuhn1955hungarian,
  author  = {Kuhn, Harold W.},
  title   = {The Hungarian Method for the Assignment Problem},
  journal = {Naval Research Logistics Quarterly},
  volume  = {2},
  pages   = {83--97},
  year    = {1955},
}
@book{schrijver2003combinatorial,
  author    = {Schrijver, Alexander},
  title     = {Combinatorial Optimization: Polyhedra and Efficiency},
  publisher = {Springer},
  year      = {2003},
}