Hopcroft-Karp Algorithm for Maximum Matching6 Feb 2025 | 5 min read Introduction:In a Bipartite graph, we can say that the matching is a type of set of edges that is chosen in such a way that one endpoint doesn't share more than one edge. We can also say that the matching of the maximum number of edges is known as maximum matching. If we have added any other edges to a maximum matching, then it is no longer matching. In a bipartite graph, there may be a chance of more than one maximum matching. Let's discuss some terms which are related to this algorithm: 1. Free Node or Vertex: In a matching M, there is a node that is not a part of the matching. We can call this node a free node. In the below graph, u2 and v2 are free. In the third graph, no vertex is free. 2. Matching and Not-Matching edges: In the matching M, the edges which are the part of the machine, we can call those edges matching edges. The edges that are not a part of matching can be called Non-matching edges. In the below graph, the first graph contains all the non-matching edges; , (u0, v1), (u1, v0) and (u3, v3) are matching in the second graph, and others are not matching. 3. Alternating Paths: In a given matching M, when a path is connected to both matching and not matching edges, then we can call that path an alternating path. We can also say that all the single-edge paths are known as alternating paths. In the below graph, u0-v1-u2 and u2-v1-u0-v2 are known as alternating paths. 4. Augmenting path: In a given matching M, when an alternating path starts and ends from a free node, then we can call this path an augmenting path. We can also say that All the single-edge paths which are start and end with free vertices are augmenting paths. We have to note one thing: the augmenting path always has one extra matching edge. In the Hopcroft Karp algorithm, if there is an existing augmenting path, then the matching M is not maximum there. Hopcroft Karp Algorithm:This algorithm is based on the below steps. These are as follows:
In the diagram below, we have implemented the above things. ![]() Implementation of Hopcroft Karp algorithm:Before implementing this algorithm, we have to note some points. These are as follows:
The main idea behind the Breadth First search is to find the argumented path. The BFS can divide the graph into layers of matching and not matching edges because the BFS can only traverse level by level. Then, we have to add a dummy vertex NIL, which is used to connect all vertices on the left side and all vertices on the right side. With the help of some of the below arrays, we can find the augmenting path. We have to initialize the Distance to NIL as infinite. If we start from a dummy vertex and come back to it using alternating paths of distinct vertices, then there must be an augmenting path. The required arrays are as follows:
The size of this array is m+1. Here, m is the number of vertices on the left side of the Bipartite Graph. With the help of this array, we can store the pair of u on the right side if u is matched and if there is nothing mathed then it will be NIL .
The size of this array is n+1. Here, n represents the number of vertices on the right side of the Bipartite Graph. With the help of this array, we can store the pair of v on the left side if v is matched and NIL otherwise.
The size of this array is m+1. Here, m represents the number of vertices on the left side of the Bipartite Graph. If u is not matching and INF (infinite), then we have to initialize the dist[] as 0. Also, we have to initialize the dist[] of NIL as INF. After finding the augmenting path, we have to take the help of DFS (Depth First Search) to add augmenting paths to the current matching. Let's understand the implementation of this algorithm with the help of the below programs. Implementation in C++ 14:Code: Output: ![]() Explanation: In the above code, we have implemented the Hopcroft-Karp Algorithm to find the maximum matching with the help of C++. Time complexity: Here, the time complexity for this algorithm is O(V x E), where E is the number of Edges and V is the number of vertices. Space complexity: Here, the space complexity for this algorithm is O(V). |
Allocate Minimum Number of Pages
Problem statement You are given an array of integers of size N, which represents the number of pages in N books. You are also given one integer M which represents the number of students. You have to distribute the N books to these M students for reading...
5 min read
K-way Merge Sort
Introduction K-way merge sort is a sophisticated sorting algorithm that extends the merge sort approach. The goal of the k-way merge problem is to combine k-sorted arrays into one sorted array that has the same elements. While the traditional merge sort algorithm merges two subarrays at a...
4 min read
Reversal algorithm for Array rotation
Introduction Array rotation is a fundamental operation in computer science, frequently utilized in different algorithms and applications. It includes moving the components of an array consistently to the left or right by a specific number of positions. While there are numerous ways to deal with accomplish array...
4 min read
Design a data structure that supports insert, delete, search, and getRandom in constant time
Designing a data structure that allows for constant-time insertion, deletion, searching, and random access is an interesting issue in computer science. Obtaining consistent time complexity for these activities sometimes necessitates a trade-off between various characteristics of data storage and access. This article delves into the core...
5 min read
Pair of strings having longest common prefix of maximum length in given array
In mathematics and computer programming, a combination of exactly two strings is referred to as a pair of strings. Each string in the pair can have any combination of letters, words, or other characters. In order to express or compare two related text passages, operate on two...
7 min read
Priority Queue using Linked list
A priority queue is a type of queue in which each element in a queue is associated with some priority, and they are served based on their priorities. If the elements have the same priority, they are served based on their order in a queue. Mainly, the...
3 min read
Print a given matrix in spiral form
Introduction In computer science and mathematics, matrices are fundamental structures that are used as the building blocks of different algorithms and computations. Different matrix manipulation techniques can produce intriguing patterns and effective solutions. Printing a matrix in spiral form is one such fascinating process.When we refer to...
4 min read
Counting Beautiful Numbers in a Specified Range
Problem Statement A positive integer is considered Beautiful if it satisfies two criteria: The quantity of even digits in the number matches the count of odd digits. The number is evenly divisible by a given integer k. Our task is to calculate and return the total count of Beautiful numbers...
10 min read
How to efficiently implement k stacks in a single array
Introduction The ability to effectively manage and use memory is crucial when programming with multiple stacks. We will look at how to implement K stacks in a single array in this article, making sure to use the least amount of memory and perform operations as quickly as...
5 min read
Introduction to Heavy Light Decomposition
To improve different operations and queries on tree structures, graph theory and algorithms employ the Heavy-Light Decomposition (HLD) tree decomposition technique. It includes dividing a tree into disjoint pathways so that operations on the tree can be carried out efficiently. Each path is then composed of...
6 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

