How Hopcroft-Karp phases bipartite matching

Short answer: Hopcroft-Karp computes a maximum matching in a bipartite graph in phases. Each phase breadth-first searches for the shortest augmenting paths and then depth-first searches a maximal set of vertex-disjoint paths of that length. The matching grows by all of them at once. There are O(√V) phases and each phase is O(E), so the time is O(E √V).


What the problem actually asks

A matching pairs vertices so that no two pairs share a vertex. It is maximum when it covers as many vertices as possible. An augmenting path starts and ends at a free vertex and alternates unmatched and matched edges. Flipping the matched state along that path increases the matching by one. Kuhn’s algorithm finds one such path at a time and can take O(V) searches, each O(E).

Shortest augmenting paths can be taken together when they are vertex-disjoint. One phase does that, and the length of the shortest augmenting path strictly increases after the phase. The length can increase only √V times before a counting argument says the matching is maximum.


What the algorithm guarantees

The algorithm returns a maximum cardinality matching. Time is O(E √V). Extra memory is the distance layers, the matching array of mates, and the usual adjacency lists. The same bound is what Dinic achieves on the flow network that splits each matching edge into a unit-capacity network. Hopcroft-Karp is that idea written directly on the bipartition, without an explicit flow network.

The graph must be bipartite. A shortest augmenting path in a general graph can be tangled by an odd cycle, and the disjoint-path phase does not go through. General graphs use the blossom algorithm. Weighted bipartite assignment uses the Hungarian algorithm, not this one.


The working steps

  1. Start from the empty matching, or from any greedy matching. A larger start only reduces the work.
  2. Breadth-first search from all free vertices on the left, along unmatched edges to the right and matched edges back to the left. Layer the vertices until a free vertex on the right is reached. That distance is the phase length.
  3. If no free right vertex is reached, the matching is maximum.
  4. Depth-first search from each free left vertex, following only edges that advance one layer, and commit every path that reaches a free right vertex. Vertices used by a committed path are removed from this phase so the paths stay disjoint.
  5. Flip the matching along each committed path and start the next phase. The next shortest augmenting path is strictly longer.

The √V phase bound is for unweighted bipartite cardinality. It does not assign preferences, and it does not survive odd cycles. A flow implementation with Dinic on the unit network has the same asymptotics. A single DFS per augmentation is Kuhn and is O(VE).


Approaches that look simpler and fail

Approach Why it fails when the phase paths are not shortest or not disjoint
Flip the first augmenting path and restart Correct, and that is Kuhn’s algorithm. The phase bound of √V requires taking a maximal set of shortest paths before the next BFS.
Allow two phase paths to share a vertex Flipping both corrupts the matching. The DFS has to consume a vertex when a path is committed.
Run it on a graph with an odd cycle The alternating-layer argument uses the bipartition. A blossom can appear, and this code does not shrink it.
Use the cardinality algorithm for a weighted assignment Maximum cardinality ignores weights. The assignment problem needs the Hungarian algorithm or min-cost flow.

One augmenting DFS at a time is Kuhn. Weighted assignment is the Hungarian algorithm. General-graph cardinality matching is Edmonds’ blossom algorithm.


A working example

This Python fragment phases the matching. Each phase adds a maximal set of vertex-disjoint shortest augmenting paths. The same bipartite graph matched by one-at-a-time search has size 2 here: left vertices 0 and 1 both reach only right vertex 0, and left vertex 2 reaches right vertex 1.

from collections import deque
adj = [[0], [0], [1]]
pair_u = [-1, -1, -1]
pair_v = [-1, -1]
dist = [0, 0, 0]

def bfs():
    q = deque()
    for u in range(3):
        if pair_u[u] < 0:
            dist[u] = 0
            q.append(u)
        else:
            dist[u] = 10**9
    found = False
    while q:
        u = q.popleft()
        for v in adj[u]:
            matched = pair_v[v]
            if matched < 0:
                found = True
            elif dist[matched] == 10**9:
                dist[matched] = dist[u] + 1
                q.append(matched)
    return found

def dfs(u):
    for v in adj[u]:
        matched = pair_v[v]
        if matched < 0 or (dist[matched] == dist[u] + 1 and dfs(matched)):
            pair_u[u] = v
            pair_v[v] = u
            return True
    dist[u] = 10**9
    return False

matching = 0
while bfs():
    for u in range(3):
        if pair_u[u] < 0 and dfs(u):
            matching += 1
assert matching == 2
print(matching)

Where it is used

Hopcroft-Karp computes a maximum bipartite matching in phases, which is the assignment of jobs to machines or applicants to seats when only the number of pairs matters. Kuhn’s one-path search is the simpler version. Costs on the edges move the problem to the Hungarian algorithm.


When you can drop this algorithm

Drop Hopcroft-Karp when the graph is tiny and Kuhn’s DFS is easier to check. Keep it for large bipartite cardinality matching, where the √V phases are the difference between a finished run and an O(VE) run.


Why this stays published

We publish this so a search for Hopcroft-Karp returns the phased shortest augmenting paths and the O(E √V) bound. Hopcroft-Karp finds a maximum bipartite matching in O(E √V) time by flipping a maximal set of vertex-disjoint shortest augmenting paths in each phase.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *