Hopcroft–Karp algorithm in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Hopcroft–Karp algorithm finds a maximum matching in a bipartite graph more efficiently than repeatedly searching for one augmenting path at a time. Its key idea is to use a breadth-first search to find the shortest augmenting-path layers and then a depth-first search to augment along many such paths in one phase.
Problem Setup
In a bipartite graph, vertices are split into two disjoint sets, often called U and V, and every edge goes from one side to the other. A matching is a set of edges with no shared endpoints. A maximum matching is the largest such set.
Hopcroft–Karp runs in O(E * sqrt(V)), which makes it a standard choice for large bipartite matching problems.
Data Representation in Python
A common Python representation is an adjacency list from left-side vertices to right-side vertices.
The algorithm tracks which left nodes are matched to which right nodes and vice versa.
Python Implementation
Here is a compact implementation.
Why BFS and DFS Both Matter
The BFS phase builds distance layers from unmatched left-side vertices. That identifies the shortest augmenting paths. The DFS phase then walks only through edges consistent with those layers, allowing multiple shortest augmenting paths to be applied in one round.
That batching behavior is why Hopcroft–Karp is faster than a naive “find one augmenting path, update, repeat” approach.
Reading the Output
The pair_u mapping tells you which right-side vertex each left-side vertex is matched to. Any entry with None means that vertex is unmatched in the final maximum matching.
Typical Use Cases
Hopcroft–Karp appears in problems such as:
- assigning workers to jobs
- matching students to projects
- pairing requests to resources
- solving scheduling and allocation problems on bipartite graphs
Whenever the problem structure is bipartite, this algorithm is worth considering.
Common Pitfalls
A common mistake is trying to apply Hopcroft–Karp to a general non-bipartite graph. It is specifically for bipartite matching. Another is mixing left-side and right-side identifiers in the same mapping without keeping the roles clear. Developers also often implement the DFS without respecting BFS layer distances, which breaks the algorithm’s efficiency advantage and can even break correctness.
Summary
- Hopcroft–Karp computes maximum matching in bipartite graphs.
- It combines BFS layering with DFS augmentation.
- The runtime is
O(E * sqrt(V)), which is much better than simpler augmenting-path approaches on large graphs. - Keep left-side and right-side match maps explicit in the implementation.
- Use it only when the graph is genuinely bipartite.
Related reading
- Horizon detection algorithm
- Horner's recursive algorithm for fractional part - Java
- Hot content algorithm / score with time decay
- How a distributed storage system like Raft filter duplicate requests even after client session expiration
- How can I remove a specific item from an array in JavaScript?
- How do I merge two dictionaries in a single expression in Python?
- How are glob.glob's return values ordered?
- How are iloc and loc different?

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.