Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

Calculate the Maximum Cardinality Bipartite Matching

Introduction to Maximum Cardinality Bipartite Matching

Maximum Cardinality Bipartite Matching (MCBM) is a classical combinatorial optimization problem that aims to find the maximum number of pairings between two disjoint sets, such that each element in both sets is paired with at most one element from the other set.

In simpler terms, imagine you have two sets of nodes, say, A and B, and some connections between nodes of A and nodes of B. The goal is to find the largest possible number of matches between these nodes, where each node can be a part of only one match.

Real-World Examples and Scenarios

MCBM is used in various real-world scenarios, such as:

  1. Job allocation: Assigning tasks to workers, where each worker can perform specific tasks and each task can be performed by specific workers.
  2. Resource allocation: Allocating resources to satisfy the requirements of different projects, where each project requires a specific set of resources, and each resource can be used for multiple projects.
  3. Stable Marriage Problem: Finding a stable matching between two equally sized sets of elements, where each element ranks the elements of the other set in order of preference.

Real-World Scenario: College Admissions Problem

Let's consider the college admissions problem, where we have a set of students and a set of colleges. Each student has a preference list of colleges they'd like to attend, and each college has a list of students they'd like to admit. Moreover, each college has a maximum number of students they can admit.

Our goal is to find the maximum number of students that can be admitted to colleges, in such a way that the preferences of both students and colleges are respected.

Problem Statement and Formal Definition

Given a bipartite graph G = (A, B, E), where A is the set of students, B is the set of colleges, and E is the set of edges representing the preferences between students and colleges, find the maximum cardinality matching in G.

Tying the Problem Statement with the Real-World Scenario

The problem statement directly correlates with the college admissions scenario, where A represents the students, B represents the colleges, and the edges in E represent the preferences between students and colleges.

Solution to the Problem

The Hopcroft-Karp algorithm is an efficient algorithm to solve the maximum cardinality bipartite matching problem. It is based on finding augmenting paths in the graph and incrementally improving the matching.

Here's a high-level overview of the Hopcroft-Karp algorithm:

  1. Initialize an empty matching M.
  2. Find an augmenting path P in the graph G.
  3. If an augmenting path is found, update the matching M by alternating the edges along the path P.
  4. Repeat steps 2 and 3 until no more augmenting paths can be found.

Solving the Problem with the College Admissions Scenario

Let's implement the Hopcroft-Karp algorithm for our college admissions scenario:

from collections import defaultdict

def bfs(graph, match, dist, A):
    queue = [u for u in A if match[u] == None]
    for u in A:
        dist[u] = len(A) + 1 if match[u] is None else 0

    while queue:
        u = queue.pop(0)
        for v in graph[u]:
            if match[v] is None:
                dist[u] = len(A)
            elif dist[match[v]] == len(A) + 1:
                dist[match[v]] = dist[u] + 1
                queue.append(match[v])

    return any(dist[u] == len(A) + 1 for u in A if match[u] is None)

def dfs(graph, match, dist, u):
    if u is None:
        return True

    for v in graph[u]:
        if match[v] is None or (dist[match[v]] == dist[u] + 1 and dfs(graph, match, dist, match[v])):
            match[u] = v
            match[v] = u
            return True
    dist[u] = 0
    return False

def hopcroft_karp(graph, A, B):
    match = {u: None for u in A + B}
    dist = {u: 0 for u in A}
    matching = 0
    while bfs(graph, match, dist, A):
        for u in A:
            if match[u] is None and dfs(graph, match, dist, u):
                matching += 1
    return match, matching

Now, let's create a sample graph representing the preferences of students and colleges:

graph = {
    's1': ['c1', 'c2'],
    's2': ['c1', 'c3'],
    's3': ['c2'],
    'c1': ['s1', 's2'],
    'c2': ['s1', 's3'],
    'c3': ['s2']
}
A = ['s1', 's2', 's3']
B = ['c1', 'c2', 'c3']

match, matching = hopcroft_karp(graph, A, B)
print(match, matching)  # Output: {'s1': 'c2', 's2': 'c1', 's3': None, 'c1': 's2', 'c2': 's1', 'c3': None} 2

In this example, we have found a matching of size 2, where student s1 goes to college c2, and student s2 goes to college c1. Student s3 remains unassigned.

Intuitions and Analogies

The Hopcroft-Karp algorithm can be thought of as a game, where we need to find the maximum number of matches between two sets of elements while following certain rules. In our college admissions scenario, the students and colleges have their preferences, and we are trying to maximize the number of students who get into a college that is on their preference list.

The algorithm is based on alternating paths, which are sequences of vertices such that the first and last vertices are unmatched, and every other vertex is matched. Each iteration of the algorithm tries to find more alternating paths, thereby increasing the overall matching.

How the Solution Can Solve Other Real-World Problems

The Hopcroft-Karp algorithm can be applied to other real-world problems that involve matching elements from two sets, such as job allocation, resource allocation, or transportation optimization. For example, in a ride-sharing scenario, we have a set of drivers and a set of passengers, and we need to match them based on their preferences and proximity. By applying the Hopcroft-Karp algorithm, we can find the maximum number of passenger-driver pairings, ensuring that both parties are satisfied with the outcome.

Another example is a dating app that needs to find good matches between its users. Let's say the app has a set of male users and a set of female users. Each user has a list of preferred partners based on their preferences (e.g., hobbies, interests, and location). Using the Hopcroft-Karp algorithm, the dating app can find the maximum number of good matches between its users, ensuring that both parties are satisfied with the result.

Conclusion

In this article, we have discussed the Maximum Cardinality Bipartite Matching problem and described a real-world scenario in which it can be applied. We have introduced the Hopcroft-Karp algorithm as a solution to the problem, providing a step-by-step guide on how to implement it using Python.

Understanding the Maximum Cardinality Bipartite Matching problem and the Hopcroft-Karp algorithm can be beneficial for solving various real-world problems that require matching elements from two sets. By mastering this concept, you will be better equipped to tackle such problems in the world of programming and computer science.