Note

This documents the development version of NetworkX. Documentation for the current release can be found here.

networkx.algorithms.bipartite.matching.hopcroft_karp_matching

hopcroft_karp_matching(G, top_nodes=None)[source]

Returns the maximum cardinality matching of the bipartite graph G.

A matching is a set of edges that do not share any nodes. A maximum cardinality matching is a matching with the most edges possible. It is not always unique. Finding a matching in a bipartite graph can be treated as a networkx flow problem.

The functions hopcroft_karp_matching and maximum_matching are aliases of the same function.

Parameters
  • G (NetworkX graph) – Undirected bipartite graph

  • top_nodes (container of nodes) – Container with all nodes in one bipartite node set. If not supplied it will be computed. But if more than one solution exists an exception will be raised.

Returns

matches – The matching is returned as a dictionary, matches, such that matches[v] == w if node v is matched to node w. Unmatched nodes do not occur as a key in matches.

Return type

dictionary

Raises

AmbiguousSolution – Raised if the input bipartite graph is disconnected and no container with all nodes in one bipartite set is provided. When determining the nodes in each bipartite set more than one valid solution is possible if the input graph is disconnected.

Notes

This function is implemented with the Hopcroft–Karp matching algorithm for bipartite graphs.

See bipartite documentation for further details on how bipartite graphs are handled in NetworkX.

References

1

John E. Hopcroft and Richard M. Karp. “An n^{5 / 2} Algorithm for Maximum Matchings in Bipartite Graphs” In: SIAM Journal of Computing 2.4 (1973), pp. 225–231. <https://doi.org/10.1137/0202019>.