Hopcroft-Karp algorithm
bipartite graph matching
computational complexity
graph theory
maximum matching algorithm

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.

Practice algorithms

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 G=(UV,E)G = (U \cup V, E) where the vertex set GG can be divided into two distinct sets UU and VV such that every edge connects a vertex from UU to a vertex in VV.

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

  1. Initialize: Start with M as an empty matching.
  2. 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.
  3. 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).
  4. 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 E=(u1,v1),(u1,v2),(u2,v2),(u3,v3)E = {(u_1,v_1), (u_1,v_2), (u_2,v_2), (u_3,v_3)}.

  1. Initial Matching: Start with an empty set M = \{\} .
  2. BFS (Layered Graph): • Level 0: u1,u2,u3{u_1, u_2, u_3} (since initially all are free) • Level 1: v1,v2,v3{v_1, v_2, v_3}
  3. DFS (Augmenting Paths): • Path: u1v1u_1 \xrightarrow{} v_1 (augment MM to (u1,v1){(u_1,v_1)})
  4. Iteration: Repeat the path search until no paths are found.

The matching (u1,v1),(u3,v3){(u_1,v_1), (u_3,v_3)} is obtained, which is maximum.

Advantages and Efficiency

Efficiency: The Hopcroft-Karp algorithm has a time complexity of O(VE)O(\sqrt{V}E), where VV is the number of vertices and EE 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.

FeatureDescription
Graph TypeBipartite
Operation MethodBFS and DFS
ComplexityO(VE)O(\sqrt{V}E)
Augmenting PathFinds multiple shortest augmenting paths at once (in each BFS stage)
Use CasesStable 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.