# Hopcroft–Karp algorithm

In computer science, the Hopcroft–Karp algorithm is an algorithm that takes as input a bipartite graph and produces as output a maximum cardinality matching – a set of as many edges as possible with the property that no two edges share an endpoint. It runs in ${\displaystyle O(|E|{\sqrt {|V|}})}$ time in the worst case, where ${\displaystyle E}$ is set of edges in the graph, and ${\displaystyle V}$ is set of vertices of the graph. In the case of dense graphs the time bound becomes ${\displaystyle O(|V|^{2.5})}$, and for random graphs it runs in near-linear time.

The algorithm was found by Template:Harvs. As in previous methods for matching such as the Hungarian algorithm and the work of Template:Harvtxt, the Hopcroft–Karp algorithm repeatedly increases the size of a partial matching by finding augmenting paths. However, instead of finding just a single augmenting path per iteration, the algorithm finds a maximal set of shortest augmenting paths. As a result only ${\displaystyle O({\sqrt {n}})}$ iterations are needed. The same principle has also been used to develop more complicated algorithms for non-bipartite matching with the same asymptotic running time as the Hopcroft–Karp algorithm.

## Augmenting paths

A vertex that is not the endpoint of an edge in some partial matching ${\displaystyle M}$ is called a free vertex. The basic concept that the algorithm relies on is that of an augmenting path, a path that starts at a free vertex, ends at a free vertex, and alternates between unmatched and matched edges within the path. If ${\displaystyle M}$ is a matching, and ${\displaystyle P}$ is an augmenting path relative to ${\displaystyle M}$, then the symmetric difference of the two sets of edges, ${\displaystyle M\oplus P}$, would form a matching with size ${\displaystyle |M|+1}$. Thus, by finding augmenting paths, an algorithm may increase the size of the matching.

Conversely, suppose that a matching ${\displaystyle M}$ is not optimal, and let ${\displaystyle P}$ be the symmetric difference ${\displaystyle M\oplus M^{*}}$ where ${\displaystyle M^{*}}$ is an optimal matching. Then ${\displaystyle P}$ must form a collection of disjoint augmenting paths and cycles or paths in which matched and unmatched edges are of equal number; the difference in size between ${\displaystyle M}$ and ${\displaystyle M^{*}}$ is the number of augmenting paths in ${\displaystyle P}$. Thus, if no augmenting path can be found, an algorithm may safely terminate, since in this case ${\displaystyle M}$ must be optimal.

An augmenting path in a matching problem is closely related to the augmenting paths arising in maximum flow problems, paths along which one may increase the amount of flow between the terminals of the flow. It is possible to transform the bipartite matching problem into a maximum flow instance, such that the alternating paths of the matching problem become augmenting paths of the flow problem.[1] In fact, a generalization of the technique used in Hopcroft–Karp algorithm to arbitrary flow networks is known as Dinic's algorithm.

Input: Bipartite graph ${\displaystyle G(U\cup V,E)}$
Output: Matching ${\displaystyle M\subseteq E}$
${\displaystyle M\leftarrow \emptyset }$
repeat
${\displaystyle {\mathcal {P}}\leftarrow \{P_{1},P_{2},\dots ,P_{k}\}}$ maximal set of vertex-disjoint shortest augmenting paths
${\displaystyle M\leftarrow M\oplus (P_{1}\cup P_{2}\cup \dots \cup P_{k})}$
until ${\displaystyle {\mathcal {P}}=\emptyset }$

## Algorithm

Let ${\displaystyle U}$ and ${\displaystyle V}$ be the two sets in the bipartition of ${\displaystyle G}$, and let the matching from ${\displaystyle U}$ to ${\displaystyle V}$ at any time be represented as the set ${\displaystyle M}$.

The algorithm is run in phases. Each phase consists of the following steps.

• A breadth-first search partitions the vertices of the graph into layers. The free vertices in ${\displaystyle U}$ are used as the starting vertices of this search, and form the first layer of the partition. At the first level of the search, only unmatched edges may be traversed (since the free vertices in ${\displaystyle U}$ are by definition not adjacent to any matched edges); at subsequent levels of the search, the traversed edges are required to alternate between matched and unmatched. That is, when searching for successors from a vertex in ${\displaystyle U}$, only unmatched edges may be traversed, while from a vertex in ${\displaystyle V}$ only matched edges may be traversed. The search terminates at the first layer ${\displaystyle k}$ where one or more free vertices in ${\displaystyle V}$ are reached.
• All free vertices in ${\displaystyle V}$ at layer ${\displaystyle k}$ are collected into a set ${\displaystyle F}$. That is, a vertex ${\displaystyle v}$ is put into ${\displaystyle F}$ if and only if it ends a shortest augmenting path.
• The algorithm finds a maximal set of vertex disjoint augmenting paths of length ${\displaystyle k}$. This set may be computed by depth first search from ${\displaystyle F}$ to the free vertices in ${\displaystyle U}$, using the breadth first layering to guide the search: the depth first search is only allowed to follow edges that lead to an unused vertex in the previous layer, and paths in the depth first search tree must alternate between matched and unmatched edges. Once an augmenting path is found that involves one of the vertices in ${\displaystyle F}$, the depth first search is continued from the next starting vertex.
• Every one of the paths found in this way is used to enlarge ${\displaystyle M}$.

The algorithm terminates when no more augmenting paths are found in the breadth first search part of one of the phases.

## Analysis

Each phase consists of a single breadth first search and a single depth first search. Thus, a single phase may be implemented in linear time. Therefore, the first ${\displaystyle {\sqrt {|V|}}}$ phases, in a graph with ${\displaystyle |V|}$ vertices and ${\displaystyle |E|}$ edges, take time ${\displaystyle O(|E|{\sqrt {|V|}})}$.

It can be shown that each phase increases the length of the shortest augmenting path by at least one: the phase finds a maximal set of augmenting paths of the given length, so any remaining augmenting path must be longer. Therefore, once the initial ${\displaystyle {\sqrt {|V|}}}$ phases of the algorithm are complete, the shortest remaining augmenting path has at least ${\displaystyle {\sqrt {|V|}}}$ edges in it. However, the symmetric difference of the eventual optimal matching and of the partial matching M found by the initial phases forms a collection of vertex-disjoint augmenting paths and alternating cycles. If each of the paths in this collection has length at least ${\displaystyle {\sqrt {|V|}}}$, there can be at most ${\displaystyle {\sqrt {|V|}}}$ paths in the collection, and the size of the optimal matching can differ from the size of ${\displaystyle M}$ by at most ${\displaystyle {\sqrt {|V|}}}$ edges. Since each phase of the algorithm increases the size of the matching by at least one, there can be at most ${\displaystyle {\sqrt {|V|}}}$ additional phases before the algorithm terminates.

Since the algorithm performs a total of at most ${\displaystyle 2{\sqrt {|V|}}}$ phases, it takes a total time of ${\displaystyle O(|E|{\sqrt {|V|}})}$ in the worst case.

In many instances, however, the time taken by the algorithm may be even faster than this worst case analysis indicates. For instance, in the average case for sparse bipartite random graphs, Template:Harvtxt (improving a previous result of Template:Harvnb) showed that with high probability all non-optimal matchings have augmenting paths of logarithmic length. As a consequence, for these graphs, the Hopcroft–Karp algorithm takes ${\displaystyle O(\log |V|)}$ phases and ${\displaystyle O(|E|\log |V|)}$ total time.

## Comparison with other bipartite matching algorithms

For sparse graphs, the Hopcroft–Karp algorithm continues to have the best known worst-case performance, but for dense graphs a more recent algorithm by Template:Harvtxt achieves a slightly better time bound, ${\displaystyle O\left(n^{1.5}{\sqrt {\frac {m}{\log n}}}\right)}$. Their algorithm is based on using a push-relabel maximum flow algorithm and then, when the matching created by this algorithm becomes close to optimum, switching to the Hopcroft–Karp method.

Several authors have performed experimental comparisons of bipartite matching algorithms. Their results in general tend to show that the Hopcroft–Karp method is not as good in practice as it is in theory: it is outperformed both by simpler breadth-first and depth-first strategies for finding augmenting paths, and by push-relabel techniques.[2]

## Non-bipartite graphs

The same idea of finding a maximal set of shortest augmenting paths works also for finding maximum cardinality matchings in non-bipartite graphs, and for the same reasons the algorithms based on this idea take ${\displaystyle O({\sqrt {|V|}})}$ phases. However, for non-bipartite graphs, the task of finding the augmenting paths within each phase is more difficult. Building on the work of several slower predecessors, Template:Harvtxt showed how to implement a phase in linear time, resulting in a non-bipartite matching algorithm with the same time bound as the Hopcroft–Karp algorithm for bipartite graphs. The Micali–Vazirani technique is complex, and its authors did not provide full proofs of their results; subsequently, a "clear exposition" was published by Template:Harvtxt and alternative methods were described by other authors.[3] In 2012, Vazirani offerred a new simplified proof of the Micali-Vazirani algorithm.[4]

## Pseudocode

/*
G = G1 ∪ G2 ∪ {NIL}
where G1 and G2 are partition of graph and NIL is a special null vertex
*/

function BFS ()
for v in G1
if Pair_G1[v] == NIL
Dist[v] = 0
Enqueue(Q,v)
else
Dist[v] = ∞
Dist[NIL] = ∞
while Empty(Q) == false
v = Dequeue(Q)
if Dist[v] < Dist[NIL]
if Dist[ Pair_G2[u] ] == ∞
Dist[ Pair_G2[u] ] = Dist[v] + 1
Enqueue(Q,Pair_G2[u])
return Dist[NIL] != ∞

function DFS (v)
if v != NIL
if Dist[ Pair_G2[u] ] == Dist[v] + 1
if DFS(Pair_G2[u]) == true
Pair_G2[u] = v
Pair_G1[v] = u
return true
Dist[v] = ∞
return false
return true

function Hopcroft-Karp
for each v in G
Pair_G1[v] = NIL
Pair_G2[v] = NIL
matching = 0
while BFS() == true
for each v in G1
if Pair_G1[v] == NIL
if DFS(v) == true
matching = matching + 1
return matching


## Notes

1. Template:Harvtxt, section 12.3, bipartite cardinality matching problem, pp. 469–470.
2. Template:Harvtxt

## References

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}. As cited by Template:Harvtxt.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}. As cited by Template:Harvtxt.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}. As cited by Template:Harvtxt.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.

• {{#invoke:citation/CS1|citation

|CitationClass=citation }}.