- Hopcroft–Karp algorithm
The Hopcroft–Karp algorithm finds maximum cardinality
matching s inbipartite graph s in O(sqrt{V} E) time, where "V" is the number of vertices and "E" is the number of edges of the graph. [John E. Hopcroft, Richard M. Karp: "An n^{5/2} Algorithm for Maximum Matchings in Bipartite Graphs." SIAM J. Comput. 2(4), 225-231 (1973)] Introduction to Algorithms|2|chapter=26.5: The relabel-to-front algorithm|pages=pp. 696–697] In the worst case of dense graphs, i.e., when E=O(V^2), the worst-case time estimate is O(V^{5/2}).The algorithm is an adaptation of the
Edmonds-Karp algorithm for maximum flow, since bipartite matching is equivalent to finding the maximum (integer) flow if the vertices in each partition are considered sources (respectively sinks) of capacity 1. The minimal cut associated with the flow is equivalent to the minimal vertex cover associated with the matching.Instead of finding just a single augmenting path, the algorithm finds a maximal set of shortest paths in each iteration [cite web|author=Norbert Blum|title=A Simplified Realization of the Hopcroft-Karp Approach to Maximum Matching in Nonbipartite Graphs|url=http://citeseer.ist.psu.edu/258961.html|date=1999] . As a result only O(sqrt{V}) iterations are needed.
Definition
Consider a graph G(V,E). The following definitions are relative to a matching M in G.
* An alternating path is a path in which the edges would belong alternatively to M and E-M.
* A free vertex is a vertex which has no incoming edges which belong to M.
* Finally, an augmenting path P is an alternating path such that both its end points are free vertices.
* Symmetric difference M'=Moplus P has all edges of M and P except shared edges Mcap PAlgorithm
The Basic concept that the algorithm relies on is that if we have a matching N of size n, and P is the augmenting path relative to N, then the matching Noplus P would have a size of n+1. Thus, since there would be no matching greater in size than the maximum matching, the maximum matching would not have an augmenting path.
Lets name the two sets in which G can be partitioned as U and V. The matching from U to V at any time is represented as the set M.
The algorithm is run in phases. Each phase consists of:
* A BFS is run from free U vertices. This BFS stops at layer "k" where one or more free V vertices are reached. Collect "all" free V vertices at layer "k" and put them in the set F. A vertex "v" is put in F iff vin V and "v" ends a shortest augmenting path.
* Find a maximal set of "vertex disjoint" augmenting paths of length "k". This is set is computed by DFS from F (computed by BFS) to free U vertices. Every one of this paths is used to enlarge M."Pseudo Code" for all qin U cup V q.matching= null; end for while (true) // Find all potential beginnings of shortest augmenting paths B := empty; for all u:U s.t. (u.matching = null) insert u in B; end for // Find all endings of shortest augmenting paths F := Find_Augmenting_Path_Endings(B); if (F is empty) M:= empty; for all u:U s.t. (u.matching ≠ null) insert (u, u.matching) in M; // insert edge end for return M; end if Augment_Paths(F); end while
// Perform BFS starting with vertices of B. // Return: a list of free vertices which potentially // end the shortest vertex-disjoint augmenting paths. // Side effect: For any vertex v.layer has the index of // the BFS layer to reach this vertex (or -1) // B: potential beginning of augmenting paths Find_Augmenting_Path_Endings(B): Q := B; // F: The set of free endings (the other side of augmenting paths) F := empty; for all qin U cup V q.layer:=-1; end for // The potential beginnings of augmenting paths are at layer 0. for all b:B b.layer:=0; end for while (Q is not empty) Q' := empty; for all q: Q for all (q,p):E s.t. (p.layer = -1) p.layer:= q.layer+1; if (p.matching = null) insert p in F; // Collect the free vertex, continue BFS else insert p in Q'; end if end for end for if (F is not empty) return F; // Free vertices found, stop at this layer. end if Q:= empty; for all q:Q' // By construction, q matched. p:= q.matching; p.layer:= q.layer+1; insert p in Q; end for end while return empty;
Augment_Paths(F): for all qin U cup V q.visited= false; end for for all q:F P:=Get_Augmenting_Path(q); // Make sure q ends a "vertex-disjoint" augmenting path if (P is not empty) // Perform M := Moplus P odd := true; for all (t,q):P if (odd) // Make (t,q) match (first, third, ... edges) p.matching := q; q.matching := t; odd := false; else // t.matching has already been updated by the "odd" condition q.matching := null; // This is not strictly required odd := true; end if end for end if end for
// DFS from p, avoiding previously visited vertices. // Start at the last layer of BFS, and go backwards // to layer 0. Get_Augmenting_Path(p): if (p.visited) return empty; end if p.visited:= true; if (p.layer = 0) // layer 0 has free vertices. return (p); end if // Consider only edges going one layer backwards. for all (q,p):E such that p.layer+1=q.layer P= Get_Augmenting_Path(q); if (P not empty) return (q, P); // Prepend q to the path found so far. end if end for // Could not find a vertex-disjoint augmenting path ending with p. return empty;
References
Wikimedia Foundation. 2010.