Algorithm Search⌘K

Proofs: Hopcroft-Karp algorithm

These are the human proofs of Hopcroft-Karp algorithm, at full length.

Hopcroft-Karp runs in O((n + m) n^(1/2)) time full

This proof shows: time.worst.

Let G=(X∪Y,E)G = (X \cup Y, E) be bipartite with nn vertices and mm edges, and let ν=ν(G)\nu = \nu(G). Each operation on an index, a label or a queue costs O(1)O(1) on the word RAM.

One phase costs O(n+m)O(n + m). Line L3 costs O(∣X∣)O(|X|). In the search of lines L5 to L8, each vertex of XX enters the queue at most once, because it is enqueued only when its label changes from ∞\infty, and its edges are scanned once when it leaves the queue: O(n+m)O(n + m). Lines L10 and L11 cost O(∣X∣)O(|X|) outside the calls. A call DFS(xx) sets dist[x]=∞\text{dist}[x] = \infty in line L12 before it looks at any edge, and no call enters a vertex whose label is ∞\infty, so each vertex of XX is entered at most once per phase. Each edge xyxy is scanned at most once, in the single call on xx, and the work of line L14 outside the inner call is O(1)O(1) per edge. Line L15 runs once per vertex of XX on a found path, so at most ∣X∣|X| times. The total is O(n+m)O(n + m).

The number of phases. Let DtD_t be the value DD of phase tt. By Claim 4 of the correctness proof, a shortest augmenting path after phase tt has more than 2Dt−12D_t - 1 edges, and the length is odd, so Dt+1≥Dt+1D_{t+1} \ge D_t + 1. As D1≥1D_1 \ge 1, phase tt has Dt≥tD_t \ge t whenever it augments.

Let MtM_t be the matching after phase tt and M∗M^* a maximum matching. The symmetric difference Mt△M∗M_t \triangle M^* contains ν−∣Mt∣\nu - |M_t| vertex-disjoint MtM_t-augmenting paths (augmenting path). Each of them has at least Dt+1≥t+1D_{t+1} \ge t + 1 vertices of YY, hence at least t+1t + 1 edges of M∗M^*, and they are disjoint, so (t+1)(ν−∣Mt∣)≤∣M∗∣=ν(t + 1)(\nu - |M_t|) \le |M^*| = \nu, that is, ν−∣Mt∣≤ν/(t+1)\nu - |M_t| \le \nu / (t + 1).

If ν=0\nu = 0, the first phase has D=∞D = \infty and is the only one. Otherwise take t=⌊ν⌋t = \lfloor \sqrt \nu \rfloor. If phase tt is reached and augments, then ν−∣Mt∣≤ν/(⌊ν⌋+1)<ν\nu - |M_t| \le \nu / (\lfloor \sqrt \nu \rfloor + 1) < \sqrt \nu, and each later phase that augments adds at least one edge, so fewer than ν\sqrt \nu augmenting phases follow. In all cases there are at most 2ν2\sqrt \nu augmenting phases, and one more phase that returns. Since ν≤n/2\nu \le n / 2, the number of phases is at most 2n+1\sqrt{2n} + 1.

Total. O(n)O(\sqrt n) phases of O(n+m)O(n + m) time give O((n+m)n)O((n + m)\sqrt n) time, which is O(mn)O(m \sqrt n) when no vertex is isolated. The space is Θ(n+m)\Theta(n + m): the adjacency lists, the arrays mate\text{mate} and dist\text{dist}, the queue, and the path stack of at most ∣X∣|X| entries. ■\blacksquare

Hopcroft-Karp returns a maximum matching full

This proof shows: correctness, termination.

Let G=(X∪Y,E)G = (X \cup Y, E) be a finite bipartite graph, every edge joining XX to YY. Fix one phase, let MM be the matching at its start, and let dist\text{dist} and DD be the values computed by lines L3 to L8. An alternating path from a free vertex is a path x0 y1 x1 y2…x_0\, y_1\, x_1\, y_2 \ldots with x0∈Xx_0 \in X free, xi−1yi∉Mx_{i-1} y_i \notin M and yixi∈My_i x_i \in M.

Claim 1 (the layers). For every x∈Xx \in X with dist[x]<∞\text{dist}[x] < \infty, dist[x]\text{dist}[x] is the least number of matched edges on an alternating path from a free vertex to xx; and DD is the least number of YY vertices on an MM-augmenting path, or ∞\infty if there is none. Proof. The search is a breadth-first search in the directed graph with an arc x→x′x \to x' for each yy with xy∈E∖Mxy \in E \setminus M and yx′∈Myx' \in M, started from all free vertices of XX at distance 0. If xy∈Mxy \in M, then mate[y]=x\text{mate}[y] = x, whose label is already finite, so line L8 adds nothing; hence line L8 follows exactly the arcs above. Breadth-first search dequeues the vertices in order of their distance, so every vertex at distance less than DD is expanded in line L6, and these distances are correct. An augmenting path has its ends in different sides, because its length is odd, so it has the form x0 y1…xk−1 ykx_0\, y_1 \ldots x_{k-1}\, y_k with x0x_0 and yky_k free; it is found at the moment its vertex xk−1x_{k-1} is expanded, which gives D≤kD \le k, and line L7 sets DD only from such paths. □\square

Claim 2 (each success flips an augmenting path). Within one phase, let P1,…,PjP_1, \ldots, P_j be the paths found by successful calls of line L11 so far, and Mj=M△P1△⋯△PjM_j = M \triangle P_1 \triangle \cdots \triangle P_j. Then the paths are vertex-disjoint, each PiP_i is an MM-augmenting path with DD vertices of YY whose XX vertices have labels 0,1,…,D−10, 1, \ldots, D - 1, and mate\text{mate} describes MjM_j. Proof. By induction on jj. A call DFS(x0x_0) from line L11 that succeeds follows a chain of calls DFS(x0x_0), DFS(x1x_1), ..., DFS(xD−1x_{D-1}): line L14 enters xi+1=mate[yi+1]x_{i+1} = \text{mate}[y_{i+1}] only when its label is d+1d + 1, so the entry labels are 0,1,2,…0, 1, 2, \ldots, and the last call succeeds through a free yDy_D with d+1=Dd + 1 = D. The edges xiyi+1x_i y_{i+1} are edges of GG; the edges yi+1xi+1y_{i+1} x_{i+1} are in MjM_j at the time of the call. Every vertex xx entered in this phase got the label ∞\infty in line L12, so no vertex of XX is entered twice, and a vertex of XX on an earlier path PiP_i is never entered again. A vertex yy of an earlier path is matched in MjM_j to a vertex of XX on that path, whose label is ∞\infty, so line L14 rejects it; in particular it is not free. Hence the new path shares no vertex with P1,…,PjP_1, \ldots, P_j, its matched edges are edges of MM, and yDy_D and x0x_0 were free in MM. So it is an MM-augmenting path, and also an MjM_j-augmenting path. Line L15 runs for xD−1,…,x0x_{D-1}, \ldots, x_0 on the way back and sets mate[xi]=yi+1\text{mate}[x_i] = y_{i+1} and mate[yi+1]=xi\text{mate}[y_{i+1}] = x_i; this is exactly Mj△Pj+1M_j \triangle P_{j+1}, because every vertex of the path is matched in the result to its neighbour on the path along an edge outside MjM_j. □\square

Claim 3 (the set is maximal). At the end of a phase with D<∞D < \infty, every MM-augmenting path with DD vertices of YY shares a vertex with one of the paths found in the phase. Proof. Let Q=x0 y1 x1…xD−1 yDQ = x_0\, y_1\, x_1 \ldots x_{D-1}\, y_D be such a path and suppose it is disjoint from all the paths found. By Claim 1, dist[xi]=i\text{dist}[x_i] = i for each ii, since a shorter alternating path to xix_i followed by the rest of QQ would contain an augmenting path with fewer vertices of YY. The vertex x0x_0 is free and has label 0, so line L11 calls DFS(x0x_0), unless it was entered earlier; either way x0x_0 is entered. Let ii be the largest index such that xix_i is entered in the phase. Since xix_i is on no path found, its call returned false, so line L13 scanned every edge of xix_i, including xiyi+1x_i y_{i+1}, with d=id = i. If i=D−1i = D - 1, the vertex yDy_D was free at that time, because it is on no path found and the matching changes only along those paths, and d+1=Dd + 1 = D, so the call would have succeeded. If i<D−1i < D - 1, then mate[yi+1]=xi+1\text{mate}[y_{i+1}] = x_{i+1} at that time, for the same reason. The label of xi+1x_{i+1} was then i+1i + 1 or ∞\infty. If it was i+1i + 1, line L14 entered xi+1x_{i+1}; if it was ∞\infty, xi+1x_{i+1} had been entered before. Both contradict the choice of ii. □\square

Claim 4 (augmenting paths grow). Let ℓ=2D−1\ell = 2D - 1 and let P1,…,PkP_1, \ldots, P_k, k≥1k \ge 1, be the paths found in a phase, with M′=M△P1△⋯△PkM' = M \triangle P_1 \triangle \cdots \triangle P_k. Then every M′M'-augmenting path has more than ℓ\ell edges. Proof. Let PP be an M′M'-augmenting path. If PP shares no vertex with the PiP_i, its edges have the same status in MM and in M′M', so it is MM-augmenting with at least ℓ\ell edges by Claim 1, and with exactly ℓ\ell it would contradict Claim 3. So ∣P∣>ℓ|P| > \ell. Otherwise, PP shares a vertex vv with some PiP_i. Every vertex of PiP_i is matched in M′M', by an edge of PiP_i, so vv is not an end of PP, and PP contains the M′M'-edge at vv, an edge of PiP_i. Now N=M′△PN = M' \triangle P is a matching with ∣M∣+k+1|M| + k + 1 edges, and M△N=(P1∪⋯∪Pk)△PM \triangle N = (P_1 \cup \cdots \cup P_k) \triangle P, as sets of edges. The symmetric difference of two matchings is a union of vertex-disjoint paths and even cycles, and since ∣N∣−∣M∣=k+1|N| - |M| = k + 1 it contains at least k+1k + 1 vertex-disjoint MM-augmenting paths (augmenting path), each with at least ℓ\ell edges. So ∣(P1∪⋯∪Pk)△P∣≥(k+1)ℓ|(P_1 \cup \cdots \cup P_k) \triangle P| \ge (k + 1)\ell. The left side equals kℓ+∣P∣−2∣P∩(P1∪⋯∪Pk)∣≤kℓ+∣P∣−2k\ell + |P| - 2|P \cap (P_1 \cup \cdots \cup P_k)| \le k\ell + |P| - 2, since PP shares at least one edge with the PiP_i. Hence ∣P∣≥ℓ+2>ℓ|P| \ge \ell + 2 > \ell. □\square

Termination. If a phase has D<∞D < \infty, a shortest augmenting path QQ exists, and by Claim 3 it meets a path found in the phase, so at least one path is found and ∣M∣|M| grows by one at least. A matching has at most ∣X∣|X| edges, so after at most ∣X∣+1|X| + 1 phases line L9 returns.

Correctness. Line L9 returns only when D=∞D = \infty. By Claim 1 there is then no MM-augmenting path, and by Berge's lemma MM is a maximum matching. By Claim 2, mate\text{mate} describes MM. ■\blacksquare

Back to Hopcroft-Karp algorithm