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 be bipartite with vertices and edges, and let . Each operation on an index, a label or a queue costs on the word RAM.
One phase costs . Line L3 costs . In the search of lines L5 to L8, each vertex of enters the queue at most once, because it is enqueued only when its label changes from , and its edges are scanned once when it leaves the queue: . Lines L10 and L11 cost outside the calls. A call DFS() sets in line L12 before it looks at any edge, and no call enters a vertex whose label is , so each vertex of is entered at most once per phase. Each edge is scanned at most once, in the single call on , and the work of line L14 outside the inner call is per edge. Line L15 runs once per vertex of on a found path, so at most times. The total is .
The number of phases. Let be the value of phase . By Claim 4 of the correctness proof, a shortest augmenting path after phase has more than edges, and the length is odd, so . As , phase has whenever it augments.
Let be the matching after phase and a maximum matching. The symmetric difference contains vertex-disjoint -augmenting paths (augmenting path). Each of them has at least vertices of , hence at least edges of , and they are disjoint, so , that is, .
If , the first phase has and is the only one. Otherwise take . If phase is reached and augments, then , and each later phase that augments adds at least one edge, so fewer than augmenting phases follow. In all cases there are at most augmenting phases, and one more phase that returns. Since , the number of phases is at most .
Total. phases of time give time, which is when no vertex is isolated. The space is : the adjacency lists, the arrays and , the queue, and the path stack of at most entries.
Hopcroft-Karp returns a maximum matching full
This proof shows: correctness, termination.
Let be a finite bipartite graph, every edge joining to . Fix one phase, let be the matching at its start, and let and be the values computed by lines L3 to L8. An alternating path from a free vertex is a path with free, and .
Claim 1 (the layers). For every with , is the least number of matched edges on an alternating path from a free vertex to ; and is the least number of vertices on an -augmenting path, or if there is none. Proof. The search is a breadth-first search in the directed graph with an arc for each with and , started from all free vertices of at distance 0. If , then , 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 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 with and free; it is found at the moment its vertex is expanded, which gives , and line L7 sets only from such paths.
Claim 2 (each success flips an augmenting path). Within one phase, let be the paths found by successful calls of line L11 so far, and . Then the paths are vertex-disjoint, each is an -augmenting path with vertices of whose vertices have labels , and describes . Proof. By induction on . A call DFS() from line L11 that succeeds follows a chain of calls DFS(), DFS(), ..., DFS(): line L14 enters only when its label is , so the entry labels are , and the last call succeeds through a free with . The edges are edges of ; the edges are in at the time of the call. Every vertex entered in this phase got the label in line L12, so no vertex of is entered twice, and a vertex of on an earlier path is never entered again. A vertex of an earlier path is matched in to a vertex of on that path, whose label is , so line L14 rejects it; in particular it is not free. Hence the new path shares no vertex with , its matched edges are edges of , and and were free in . So it is an -augmenting path, and also an -augmenting path. Line L15 runs for on the way back and sets and ; this is exactly , because every vertex of the path is matched in the result to its neighbour on the path along an edge outside .
Claim 3 (the set is maximal). At the end of a phase with , every -augmenting path with vertices of shares a vertex with one of the paths found in the phase. Proof. Let be such a path and suppose it is disjoint from all the paths found. By Claim 1, for each , since a shorter alternating path to followed by the rest of would contain an augmenting path with fewer vertices of . The vertex is free and has label 0, so line L11 calls DFS(), unless it was entered earlier; either way is entered. Let be the largest index such that is entered in the phase. Since is on no path found, its call returned false, so line L13 scanned every edge of , including , with . If , the vertex was free at that time, because it is on no path found and the matching changes only along those paths, and , so the call would have succeeded. If , then at that time, for the same reason. The label of was then or . If it was , line L14 entered ; if it was , had been entered before. Both contradict the choice of .
Claim 4 (augmenting paths grow). Let and let , , be the paths found in a phase, with . Then every -augmenting path has more than edges. Proof. Let be an -augmenting path. If shares no vertex with the , its edges have the same status in and in , so it is -augmenting with at least edges by Claim 1, and with exactly it would contradict Claim 3. So . Otherwise, shares a vertex with some . Every vertex of is matched in , by an edge of , so is not an end of , and contains the -edge at , an edge of . Now is a matching with edges, and , as sets of edges. The symmetric difference of two matchings is a union of vertex-disjoint paths and even cycles, and since it contains at least vertex-disjoint -augmenting paths (augmenting path), each with at least edges. So . The left side equals , since shares at least one edge with the . Hence .
Termination. If a phase has , a shortest augmenting path exists, and by Claim 3 it meets a path found in the phase, so at least one path is found and grows by one at least. A matching has at most edges, so after at most phases line L9 returns.
Correctness. Line L9 returns only when . By Claim 1 there is then no -augmenting path, and by Berge's lemma is a maximum matching. By Claim 2, describes .