How does the Hopcroft-Karp algorithm work?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The Hopcroft-Karp algorithm is a renowned algorithm in computer science for finding the maximum cardinality matching in a bipartite graph. The maximum cardinality matching refers to the largest possible set of edges that can be selected such that no two edges share a common vertex. This algorithm was introduced by John Hopcroft and Richard Karp in 1973 and is recognized for its efficiency, which allows it to outperform simpler algorithms on large graphs.
Understanding the Basics
Before delving into the workings of the Hopcroft-Karp algorithm, it's essential to understand a few concepts:
Bipartite Graph: A graph where the vertex set can be divided into two distinct sets and such that every edge connects a vertex from to a vertex in .
Matching: A set of edges without common vertices.
Maximum Matching: A matching that contains the largest possible number of edges.
Augmenting Path: A path that alternates between edges not in the matching and edges in the matching, beginning and ending at unmatched vertices.
The Hopcroft-Karp algorithm tackles the problem of finding the maximum matching by efficiently searching for augmenting paths within the graph, systematically increasing the size of the matching.
The Hopcroft-Karp Algorithm
The algorithm operates in phases, where each phase consists of two main steps: the Breadth-First Search (BFS) to find the shortest augmenting paths, and the Depth-First Search (DFS) to augment the matching along these paths. The key insight that makes Hopcroft-Karp efficient is finding multiple augmenting paths of the shortest length simultaneously, thereby minimizing the need to repeatedly search for long augmenting paths.
Algorithm Steps
- Initialize: Start with M as an empty matching.
- BFS (Breadth-First Search): • Create a layer graph using BFS starting with all free vertices (vertices not currently matched). • Construct levels of the bipartite graph where each level consists of free and matched vertices, ensuring that edges alternate between matched and unmatched edges. • Identify the shortest augmenting paths by reaching unmatched vertices on the opposite side of the graph. • If no such augmenting path exists, the current matching is maximum.
- DFS (Depth-First Search): • Using the layer graph created in the BFS step, recursively search for vertex-disjoint augmenting paths. • Augment the matching along these paths by alternating the status of edges (matched becomes unmatched and vice versa).
- Iterate: Repeat the BFS and DFS steps until no more augmenting paths are found.
Example
Consider a bipartite graph with sets $U = \{u_1, u_2, u_3\}$ and $V = \{v_1, v_2, v_3\}$ and edges .
- Initial Matching: Start with an empty set
M = \{\}. - BFS (Layered Graph): • Level 0: (since initially all are free) • Level 1:
- DFS (Augmenting Paths): • Path: (augment to )
- Iteration: Repeat the path search until no paths are found.
The matching is obtained, which is maximum.
Advantages and Efficiency
• Efficiency: The Hopcroft-Karp algorithm has a time complexity of , where is the number of vertices and is the number of edges. This is significantly faster than the earlier augmenting path methods. • Scalability: Its ability to find multiple shortest augmenting paths simultaneously makes it suitable for large graphs.
Conclusion
The Hopcroft-Karp algorithm revolutionized finding maximum matchings in bipartite graphs. Its structured approach of finding the shortest augmenting paths ensures efficiency and efficacy, maintaining its status as a preferred method for such problems.
| Feature | Description |
| Graph Type | Bipartite |
| Operation Method | BFS and DFS |
| Complexity | |
| Augmenting Path | Finds multiple shortest augmenting paths at once (in each BFS stage) |
| Use Cases | Stable marriages, scheduling, resource allocation |
In summary, for any practitioner or student working with graph theory and algorithm design, understanding and leveraging the Hopcroft-Karp algorithm is crucial to effectively manage problems related to bipartite matchings.
Related reading
- How does the HyperLogLog algorithm work?
- How does the JavaScript heap handle recursion
- How does the JavaScript sort function workas an algorithm?
- How does the KD-tree nearest neighbor search work?
- How does this algorithm to count the number of set bits in a 32-bit integer work?
- How does tuple comparison work in Python?
- How does the new Docker --squash work
- How does the storage backend influence Datomic?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.