A matching in a Bipartite Graph is a set of edges chosen in such a way that no two edges share an endpoint. A maximum matching is a matching of maximum size (maximum number of edges). In a maximum matching, if any edge is added to it, it is no longer a matching. There can be more than one maximum matching for a given Bipartite Graph.
Hopcroft Karp Algorithm:
The Hopcroft–Karp algorithm is a clever method used in computer science to solve a specific problem: finding the maximum matching in a bipartite graph.
Step-by-step algorithm:
- Initialize Maximal Matching M as empty.
- While there exists an Augmenting Path p.
- Remove matching edges of p from M and add not-matching edges of p to M
- (This increases the size of M by 1 as p starts and ends with a free vertex)
- Return M.
In the initial graph, all single edges are augmenting paths and we can pick in any order. In the middle stage, there is only one augmenting path. We remove matching edges of this path from M and add not-matching edges. In final matching, there are no augmenting paths so the matching is maximum.
Below is the implementation of the above approach:
# Python3 implementation of Hopcroft Karp algorithm for
# maximum matching
from queue import Queue
INF = 2147483647
NIL = 0
# A class to represent Bipartite graph for Hopcroft
# 3 Karp implementation
class BipGraph(object):
# Constructor
def __init__(self, m, n):
# m and n are number of vertices on left
# and right sides of Bipartite Graph
self.__m = m
self.__n = n
# adj[u] stores adjacents of left side
# vertex 'u'. The value of u ranges from 1 to m.
# 0 is used for dummy vertex
self.__adj = [[] for _ in range(m+1)]
# To add edge from u to v and v to u
def addEdge(self, u, v):
self.__adj[u].append(v) # Add u to v’s list.
# Returns true if there is an augmenting path, else returns
# false
def bfs(self):
Q = Queue()
# First layer of vertices (set distance as 0)
for u in range(1, self.__m+1):
# If this is a free vertex, add it to queue
if self.__pairU[u] == NIL:
# u is not matched3
self.__dist[u] = 0
Q.put(u)
# Else set distance as infinite so that this vertex
# is considered next time
else:
self.__dist[u] = INF
# Initialize distance to NIL as infinite
self.__dist[NIL] = INF
# Q is going to contain vertices of left side only.
while not Q.empty():
# Dequeue a vertex
u = Q.get()
# If this node is not NIL and can provide a shorter path to NIL
if self.__dist[u] < self.__dist[NIL]:
# Get all adjacent vertices of the dequeued vertex u
for v in self.__adj[u]:
# If pair of v is not considered so far
# (v, pairV[V]) is not yet explored edge.
if self.__dist[self.__pairV[v]] == INF:
# Consider the pair and add it to queue
self.__dist[self.__pairV[v]] = self.__dist[u] + 1
Q.put(self.__pairV[v])
# If we could come back to NIL using alternating path of distinct
# vertices then there is an augmenting path
return self.__dist[NIL] != INF
# Returns true if there is an augmenting path beginning with free vertex u
def dfs(self, u):
if u != NIL:
# Get all adjacent vertices of the dequeued vertex u
for v in self.__adj[u]:
if self.__dist[self.__pairV[v]] == self.__dist[u] + 1:
# If dfs for pair of v also returns true
if self.dfs(self.__pairV[v]):
self.__pairV[v] = u
self.__pairU[u] = v
return True
# If there is no augmenting path beginning with u.
self.__dist[u] = INF
return False
return True
def hopcroftKarp(self):
# pairU[u] stores pair of u in matching where u
# is a vertex on left side of Bipartite Graph.
# If u doesn't have any pair, then pairU[u] is NIL
self.__pairU = [0 for _ in range(self.__m+1)]
# pairV[v] stores pair of v in matching. If v
# doesn't have any pair, then pairU[v] is NIL
self.__pairV = [0 for _ in range(self.__n+1)]
# dist[u] stores distance of left side vertices
# dist[u] is one more than dist[u'] if u is next
# to u'in augmenting path
self.__dist = [0 for _ in range(self.__m+1)]
# Initialize result
result = 0
# Keep updating the result while there is an
# augmenting path.
while self.bfs():
# Find a free vertex
for u in range(1, self.__m+1):
# If current vertex is free and there is
# an augmenting path from current vertex
if self.__pairU[u] == NIL and self.dfs(u):
result += 1
return result
# Driver Program
if __name__ == "__main__":
g = BipGraph(4, 4)
g.addEdge(1, 2)
g.addEdge(1, 3)
g.addEdge(2, 1)
g.addEdge(3, 2)
g.addEdge(4, 2)
g.addEdge(4, 4)
print("Size of maximum matching is %d" % g.hopcroftKarp())
Output
Size of maximum matching is 4
Time Complexity : O(√V x E), where E is the number of Edges and V is the number of vertices.
Auxiliary Space : O(V) as we are using extra space for storing u and v.