Algorithm
boolean tryKuhn(int v, boolean[] used, int[] match, List[] adj) {
for (int to : adj[v]) {
if (used[to]) continue;
used[to] = true;
if (match[to] == -1 || tryKuhn(match[to], used, match, adj)) {
match[to] = v;
return true;
}
}
return false;
}
int maxMatching(int leftN, List[] adj) {
int[] match = new int[rightN];
Arrays.fill(match, -1);
int result = 0;
for (int v = 0; v < leftN; v++) {
boolean[] used = new boolean[rightN];
if (tryKuhn(v, used, match, adj)) result++;
}
return result;
} Advertisement
Complexity
Each augmenting path DFS: O(V+E). V augmenting paths. Total: O(V·E).
Advertisement
vs Hopcroft-Karp
Hopcroft-Karp: O(E·√V) but complex. Kuhn: O(V·E) but 20-line implementation.
Applications
Interview problems. Small-to-medium bipartite matching. Assignment sanity checks.