Welcome back! In our previous lesson, we explored how to find a minimal "skeleton" of a graph by counting its spanning trees. We learned to use powerful tools like Kirchhoff's Theorem and Cayley's Formula for this task. Today, we shift our focus from connecting all vertices to a different kind of optimization problem: finding the largest possible set of independent pairings between two groups of vertices. This is the problem of maximum bipartite matching.
This lesson will equip you to find a maximum matching in a bipartite graph, a common problem in GATE with applications ranging from job assignments to network routing. We will start by defining the core concepts and then dive into the central algorithmic idea: the augmenting path. You will learn how to find these paths and use them to systematically build a maximum matching. We'll also cover the more advanced Hopcroft-Karp algorithm and an elegant alternative method using network flows, both of which are important for a complete understanding of the topic.
1. Bipartite Graphs and Matchings
Let's begin by formalizing the problem. Imagine you have a set of applicants and a set of open positions. An edge exists between an applicant and a position if the applicant is qualified for it. The goal is to hire as many people as possible, with the constraint that each applicant can take at most one job, and each job can be filled by at most one applicant. This is a classic bipartite matching problem.
A bipartite graph is a graph whose vertices can be divided into two disjoint and independent sets, and , such that every edge in connects a vertex in to one in . There are no edges within or within .
A matching in a graph is a subset of its edges such that no two edges in share a common vertex.
A maximum matching (or maximum cardinality matching) is a matching that contains the largest possible number of edges.
The following video provides an excellent visual introduction to these concepts using the job assignment analogy.
Can we assign everyone a job? (maximum matchings) | Bipartite Matchings
This video from the OptWhiz channel clearly sets up the problem of maximum bipartite matching. It uses a relatable example to define what a bipartite graph is and what constitutes a valid matching.
Watch the initial segment from the beginning up to this point. Pay close attention to how the "people" and "jobs" form two distinct sets of vertices and how a "matching" corresponds to a valid set of assignments.
A simple way to check if a graph is bipartite is to try and color it with two colors. Starting from an arbitrary vertex, color it red. Then, color all its neighbors blue. Then, color all their uncolored neighbors red, and so on. If you ever find an edge connecting two vertices of the same color, the graph is not bipartite. This can be implemented efficiently using Breadth-First Search (BFS).
2. The Augmenting Path Method
A greedy approach, like the one tried in the "books and people" example in one of our upcoming resources, often fails to find a maximum matching. We need a more systematic way to improve an existing matching. The key to this is the concept of an augmenting path.
An augmenting path with respect to a matching is a simple path in the graph that:
- Starts at an unmatched vertex in one partition (say, ).
- Ends at an unmatched vertex in the other partition ().
- Alternates between edges that are not in and edges that are in .
If we find such a path, we can increase the size of our matching by one. We do this by "flipping" the edges along the path: we remove the path's matched edges from and add the path's unmatched edges to . Since the path starts and ends with an unmatched edge, it contains one more unmatched edge than matched edges. This flip, therefore, results in a new matching with exactly one more edge.
This leads to a profound result known as Berge's Theorem:
A matching in a graph is a maximum matching if and only if there are no augmenting paths with respect to .
This theorem gives us a complete algorithm: start with any matching (even an empty one) and repeatedly find and "flip" augmenting paths until no more can be found.
The next video segment beautifully illustrates this idea of "clever swaps."
Can we assign everyone a job? (maximum matchings) | Bipartite Matchings
This part of the OptWhiz video explains the concept of an augmenting path and visually demonstrates how swapping edges along such a path increases the size of the matching. It also introduces Berge's theorem.
Watch the segment from this point. Focus on how the path alternates between edges in and out of the initial matching. Understanding this "flipping" mechanism is the most crucial part of the algorithm.
3. Finding Augmenting Paths
Berge's theorem tells us what to look for, but how do we find an augmenting path efficiently? We can use a graph traversal algorithm like BFS or DFS.
Here is the general procedure using BFS, starting from the set of vertices :
- Initialize an empty matching .
- For every unmatched vertex :
a. Run a BFS or DFS starting from to find an alternating path to any unmatched vertex .
b. The traversal must be modified: from a vertex in , you can only traverse an edge not in . From a vertex in , you can only traverse an edge that is in .
c. If such a path is found, augment the matching by flipping the edges along the path. Break the inner loop and start again from the new, larger matching. - If the loop completes without finding any augmenting paths for any of the unmatched vertices in , the current matching is maximum.
The complexity of finding one augmenting path using BFS/DFS is , where is the number of edges. Since in the worst case we might find a path for up to vertices, the total complexity of this naive Ford-Fulkerson-style method for matching is .
The following video demonstrates this search process.
Can we assign everyone a job? (maximum matchings) | Bipartite Matchings
This segment shows the technical details of finding an augmenting path using a graph traversal algorithm, similar to the BFS approach described above. It also analyzes the runtime of this method.
Please watch from the technical details. Try to trace the BFS on paper as the video explains it. Notice how the search expands layer by layer, alternating between unmatched and matched edges.
4. Hopcroft-Karp and the Max-Flow Method
While the algorithm is correct, more efficient methods exist. For your GATE exam, you should be aware of these.
Hopcroft-Karp Algorithm
The Hopcroft-Karp algorithm improves upon the simple augmenting path search. Instead of finding one path at a time, it performs the following in each phase:
- Use BFS to find all the shortest augmenting paths.
- Use DFS to find a maximal set of vertex-disjoint paths among these shortest paths.
- Augment the matching with all paths in this set simultaneously.
By augmenting with multiple paths in one phase, it reaches the maximum matching much faster. Its time complexity is , which is a significant improvement and the most efficient known algorithm for unweighted bipartite matching. For GATE, you are not expected to implement this but should know its name and complexity.
Can we assign everyone a job? (maximum matchings) | Bipartite Matchings
This final, brief clip from the OptWhiz video introduces the Hopcroft-Karp algorithm as a more efficient improvement.
Watch the short segment from the improvement. The key takeaway is its name and better runtime.
Reduction to Maximum Flow
An entirely different and very powerful way to solve bipartite matching is to transform it into a maximum flow problem. Given your IT background, you've likely encountered max-flow algorithms (like Edmonds-Karp or Dinic). The conversion is elegant:
- Given a bipartite graph , create a new directed graph (a flow network).
- Add a source node and a sink node .
- For every vertex , add a directed edge from to with a capacity of 1.
- For every vertex , add a directed edge from to with a capacity of 1.
- For every original edge in (with ), add a directed edge from to with a capacity of 1 (or infinity, as the flow is limited by the source/sink edges).
Now, run any max-flow algorithm on this network. The Max-Flow Min-Cut Theorem implies that the value of the maximum flow from to is exactly equal to the size of the maximum matching in the original bipartite graph. The edges from to that have a flow of 1 in the solution correspond to the edges in the maximum matching.
This reduction is a critical technique to have in your toolkit for GATE, as it connects two major topics in algorithms.
Unweighted Bipartite Matching | Network Flow | Graph Theory
This video by WilliamFiset provides a detailed, step-by-step walkthrough of converting a bipartite matching problem into a network flow problem and solving it.
Watch the segment from this example. Follow the construction of the flow network carefully: the addition of the source and sink, the direction of edges, and the assignment of capacities. This is a very common and testable pattern.
Conclusion
In this lesson, we tackled the problem of finding the maximum number of independent pairs in a bipartite graph. We saw that a simple greedy approach can fail, leading us to the powerful concept of the augmenting path.
Key Takeaways:
- A bipartite graph models relationships between two distinct groups of items.
- A matching is a set of edges with no shared vertices; a maximum matching has the largest possible size.
- An augmenting path is an alternating path between two unmatched vertices. According to Berge's Theorem, a matching is maximum if and only if no such path exists.
- We can find a maximum matching by repeatedly finding and "flipping" augmenting paths. A simple implementation using BFS/DFS has a complexity of .
- The Hopcroft-Karp algorithm is a more efficient method that finds multiple shortest augmenting paths in each phase, running in time.
- Bipartite matching can be solved by converting the graph into a flow network and finding the maximum flow. The max flow value equals the size of the maximum matching.
In our next lesson, we will shift gears to another fundamental graph problem: Graph Coloring. Instead of selecting edges, we will focus on assigning "colors" (labels) to vertices under certain constraints, a problem with applications in scheduling, register allocation, and many other areas of computer science.