Short answer: Kuhn’s algorithm builds a bipartite matching by a depth-first search for one augmenting path at a time. Each free left vertex tries to find an alternating path to a free right vertex, flipping the matching if it succeeds. With a visited mark cleared every search, the time is O(VE). The matching is maximum when no free left vertex can still augment. The algorithm is Hopcroft-Karp without the phases.
What the problem actually asks
The augmenting-path theorem is enough: while an augmenting path exists, the matching is not maximum, and flipping one increases it by one. Depth-first search from a free left vertex, walking an unmatched edge to the right and the unique matched edge back to the left, either reaches a free right vertex or exhausts the alternating choices.
A visited mark is required inside one search so a right vertex is not tried twice. The mark must be cleared before the next left vertex starts, or a later search will treat reachable vertices as dead.
What the algorithm guarantees
The final matching is a maximum cardinality matching of a bipartite graph. At most V augmentations occur, and each search scans each edge a constant number of times, for O(VE). Extra memory is the mate array and the visited array. A greedy initialization, matching whatever is free as the adjacency lists are scanned once, cuts the constant and does not change the worst case.
The same DFS on a non-bipartite graph can fail to find an augmenting path that exists, because the path may need to travel an odd cycle in a way this search never represents. That case is a blossom.
The working steps
- Initialize mate[v] as unmatched for every vertex.
- For each left vertex u, clear the visited marks and call a search from u.
- The search tries each neighbor v of the current left vertex. If v is visited this attempt, skip it. Mark v.
- If v is free or a search from mate[v] succeeds, set mate[v] = u and mate[u] = v and report success.
- If the search returns failure, leave u unmatched for now and continue with the next left vertex. A second pass is unnecessary if every left vertex is tried once against the matching built so far. Trying them in a loop until a full pass adds nothing is also correct.
One search per left vertex, each allowed to see the whole graph, is O(VE) and maximum only in a bipartite graph. The phased version that groups shortest paths is Hopcroft-Karp. Weighted edges are not consulted. A visited mark that survives across left vertices makes the algorithm miss augmentations and return a matching that is not maximum.
Approaches that look simpler and fail
| Approach | Why it fails when the visited marks outlive one search |
|---|---|
| Keep visited marks across left vertices | A right vertex rejected for one starter can be the correct partner for the next starter. The mark has to mean “tried in this attempt.” |
| Forget to flip along the whole recursion | Setting the mate only at the free right vertex leaves the alternating edges above it unchanged. The recursive success has to reassign mates on the way back. |
| Apply it to a general graph | An augmenting path that winds through an odd cycle is not discovered by this bipartite recursion. |
| Quote O(E √V) | That is Hopcroft-Karp. One DFS per augmentation is O(VE). |
Phased shortest augmentations belong to Hopcroft-Karp. The flow version of the same matching is Dinic on a unit network. Assignment with weights belongs to the Hungarian algorithm.
A working example
This Python fragment matches the left side by a depth-first search for an augmenting path, and it clears the visited marks before each search. The maximum matching has size 3.
adj = {0: [0, 1], 1: [0], 2: [2]} # left -> right
match_r = {}
def dfs(u, seen):
for v in adj[u]:
if v in seen:
continue
seen.add(v)
if v not in match_r or dfs(match_r[v], seen):
match_r[v] = u
return True
return False
for u in adj:
dfs(u, set())
assert len(match_r) == 3
print(len(match_r))
Where it is used
Kuhn’s algorithm (augmenting DFS) computes a maximum matching in a bipartite graph: seats and applicants, or jobs and machines, with no costs. Hopcroft-Karp batches those paths. Costs move the problem to the Hungarian assignment algorithm.
When you can drop this algorithm
Drop Kuhn when the bipartite graph is large and an O(VE) matching will not finish. Hopcroft-Karp is the replacement. Keep Kuhn when the graph is small or clarity matters more than the √V factor, and clear the visited array on every attempt.
Why this stays published
We publish this so a search for Kuhn’s algorithm returns the single augmenting DFS and the visited array that must be cleared each time. Kuhn’s algorithm finds a maximum bipartite matching in O(VE) time by depth-first search for one augmenting path at a time.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
