Matchings Hall and Konig
Matchings formalize pairing without conflicts. A matching can assign students to projects, workers to jobs, tasks to machines, or vertices in one part of a bipartite graph to compatible vertices in the other part. The central obstruction is local competition: too many vertices may demand too few possible partners.
Hall's theorem gives an exact condition for perfect matching on one side of a bipartite graph. Konig's theorem then links matchings to vertex covers, showing that in bipartite graphs the largest set of disjoint edges has the same size as the smallest set of vertices touching all edges.
Definitions
A matching in a graph is a set of pairwise nonadjacent edges: no two selected edges share a vertex. A vertex incident with a matching edge is saturated; otherwise it is unsaturated.
In a bipartite graph with parts and , a matching saturates if every vertex of is matched. If and both parts are saturated, the matching is perfect.
For , the neighborhood is the set of vertices in adjacent to at least one vertex of .
A vertex cover is a set of vertices meeting every edge. A minimum vertex cover has least possible size.
An augmenting path relative to a matching is a path whose edges alternate between not in and in , starting and ending at unsaturated vertices. Flipping membership along such a path increases the matching size by .
Key results
Hall's marriage theorem. A bipartite graph with parts and has a matching saturating if and only if
for every subset .
Necessity is immediate: the vertices in require distinct partners inside . Sufficiency is deeper and can be proved by induction or augmenting paths.
Berge augmenting-path theorem. A matching is maximum if and only if there is no augmenting path with respect to .
Konig's theorem. In every bipartite graph,
This equality is special to bipartite graphs. In non-bipartite graphs, maximum matching and minimum vertex cover are related but not usually equal.
Hall deficiency. If Hall's condition fails, the amount of failure is measured by
A positive value means the vertices of are competing for too few partners. The largest such deficiency controls how far a bipartite graph is from saturating the left side. This is why Hall's theorem is more than a yes/no test: the offending subset identifies the bottleneck.
From matching to vertex cover. The standard constructive proof of Konig's theorem starts with a maximum matching. From unmatched vertices on the left, follow alternating paths that begin with nonmatching edges. Let be the set of vertices reached. Then
is a minimum vertex cover. This recipe is also how many matching algorithms certify optimality: they return both a matching and a cover of the same size.
Perfect matchings and regular bipartite graphs. Every -regular bipartite graph with has a perfect matching. Hall's condition proves this by counting edges from a subset to its neighborhood: there are such incident edges, while at most can enter , so .
Alternating trees. Matching algorithms often grow an alternating tree from an unsaturated vertex. Edges not in the matching are used from left to right, and matched edges are used from right to left. If the search reaches an unsaturated vertex on the other side, the discovered path is augmenting. If it cannot, the reached and unreached vertices help form a vertex cover certificate.
Why bipartiteness matters. In general graphs, odd cycles create complications because alternating paths can interact with blossoms: odd cycles that hide possible augmentations. Bipartite graphs avoid blossoms, which is why Hall's theorem, Konig's theorem, and simpler augmenting-path algorithms have especially clean statements there.
Matching size bounds. A matching can never be larger than either part of a bipartite graph, and it can never be larger than a vertex cover. Konig's theorem says that in bipartite graphs the best possible upper bound from a vertex cover is always tight. This duality is the matching analogue of max-flow min-cut.
Visual
The highlighted alternating route suggests an augmenting path from an unmatched left vertex to an unmatched right vertex.
| Concept | Object | Test or theorem |
|---|---|---|
| Matching | disjoint edges | no shared endpoints |
| Perfect matching in bipartite graph | saturates both parts | Hall condition on both sides when sizes match |
| Maximum matching | largest matching | no augmenting path |
| Vertex cover | vertices touching all edges | equals max matching size in bipartite graphs |
Worked example 1: Check Hall's condition
Problem. Let and with neighborhoods
Does there exist a matching saturating ?
Method.
We check Hall's condition for all subsets of .
Singletons:
Pairs:
All three:
Hall's condition holds, so a matching saturating exists. One such matching is
Checked answer. Yes, the graph has a matching saturating .
Worked example 2: Use an augmenting path
Problem. In the bipartite graph with edges
start with matching . Find a larger matching.
Method.
- The unmatched vertices on the left are .
- The unmatched vertices on the right are and .
- The edge is not in and immediately connects two unmatched vertices.
- Therefore is an augmenting path of length .
- Add this edge to the matching:
This saturates all vertices in .
Alternative augmenting route. If the starting matching had been , then
would alternate unmatched, matched, unmatched, matched, unmatched. Flipping along it gives .
Checked answer. The larger matching has size , which is maximum because it saturates the left part of size .
The final sentence is an upper-bound certificate. No matching can contain more edges than there are vertices on the smaller side of the bipartition. Once a matching saturates all three left vertices, it has reached that bound.
Code
This is a compact DFS-based bipartite matching implementation.
def max_bipartite_matching(graph):
match_y = {}
def try_match(x, seen):
for y in graph[x]:
if y in seen:
continue
seen.add(y)
if y not in match_y or try_match(match_y[y], seen):
match_y[y] = x
return True
return False
for x in graph:
try_match(x, set())
return {(x, y) for y, x in match_y.items()}
graph = {
"a": {"1", "2"},
"b": {"2"},
"c": {"2", "3"},
}
print(max_bipartite_matching(graph))
The DFS routine is simple rather than asymptotically optimal. For large bipartite graphs, Hopcroft-Karp improves performance by finding many shortest augmenting paths in phases. The mathematical certificate remains the same: when no augmenting path exists, the current matching is maximum.
For small Hall checks, it is usually unnecessary to list all subsets if a pattern is visible. Start with the most constrained vertices, then examine unions of their neighborhoods. If a Hall violation exists, it often appears among vertices with few choices or strongly overlapping choices.
When a matching is proposed as maximum, look for a certificate. Either it saturates the smaller side, or it is paired with a vertex cover of the same size, or an augmenting-path search has failed. A large matching without one of these checks may still be improvable.
This certificate habit is the matching analogue of checking an answer with a cut in flow problems.
When a bipartite matching problem is small, draw the two parts in columns and keep matched edges visually distinct from unmatched edges. Alternating paths are much easier to see when matching edges have a different style. After augmenting, recount saturated vertices on both sides, because a successful flip changes several matched partners at once.
Common pitfalls
- Checking Hall's condition only for single vertices. The obstruction often appears in larger subsets.
- Forgetting that a matching edge saturates two vertices but conflicts with every other edge incident to either endpoint.
- Assuming a maximal matching is maximum. A maximal matching cannot be enlarged by one disjoint edge, but it may still be smaller than a maximum matching.
- Using Konig's theorem in non-bipartite graphs.
- Confusing a vertex cover with an independent set. A vertex cover touches all edges; an independent set contains no internal edge.
- Flipping an alternating path that does not start and end at unsaturated vertices. Only augmenting paths increase matching size.