HopcroftKarp Class |
The HopcroftKarp class represents a data type for computing a Maximum (cardinality) matching and a Minimum (cardinality) vertex cover in a bipartite graph. A Bipartite graph in a graph whose vertices can be partitioned into two disjoint sets such that every edge has one endpoint in either set. A Matching in a graph is a subset of its edges with no common vertices. A Maximum matching is a matching with the maximum number of edges.
A Perfect matching is a matching which matches all vertices in the graph. A Vertex cover in a graph is a subset of its vertices such that every edge is incident to at least one vertex. A Minimum vertex cover is a vertex cover with the minimum number of vertices. By Konig's theorem, in any biparite graph, the maximum number of edges in matching equals the minimum number of vertices in a vertex cover. The maximum matching problem in Nonbipartite graphs is also important, but all known algorithms for this more general problem are substantially more complicated.
This implementation uses the Hopcroft-Karp algorithm. The order of growth of the running time in the worst case is (E + V) sqrt(V), where E is the number of edges and V is the number of vertices in the graph. It uses extra space (not including the graph) proportional to V.
See also , which solves the problem in O(E V) time using the Alternating path algorithm and BipartiteMatchingToMaxflow, which solves the problem in O(E V) time via a reduction to the maxflow problem.
Namespace: Algs4Net
public class HopcroftKarp
The HopcroftKarp type exposes the following members.
Name | Description | |
---|---|---|
![]() | HopcroftKarp |
Determines a maximum matching (and a minimum vertex cover)
in a bipartite graph. |
Name | Description | |
---|---|---|
![]() | Count |
Returns the number of edges in any maximum matching. |
![]() | IsPerfect |
Returns true if the graph contains a perfect matching.
That is, the number of edges in a maximum matching is equal to one half
of the number of vertices in the graph (so that every vertex is matched). |
Name | Description | |
---|---|---|
![]() | InMinVertexCover |
Returns true if the specified vertex is in the minimum vertex cover
computed by the algorithm. |
![]() | IsMatched |
Returns true if the specified vertex is matched in the maximum matching
computed by the algorithm. |
![]() ![]() | MainTest |
Demo test the HopcroftKarp data type.
Takes three command-line arguments V1, V2, and E;
creates a random bipartite graph with V1 + V2 vertices
and E edges; computes a maximum matching and minimum vertex cover;
and prints the results. |
![]() | Mate |
Returns the vertex to which the specified vertex is matched in
the maximum matching computed by the algorithm. |
For additional documentation, see Section 6.5Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
This class is a C# port from the original Java class HopcroftKarp implementation by the respective authors.