Maximum Bipartite Matching - Problem

Given a bipartite graph represented as an adjacency list, find the maximum matching. A matching is a set of edges where no two edges share a common vertex. A maximum matching is a matching that contains the largest possible number of edges.

The bipartite graph has vertices divided into two disjoint sets: left vertices (numbered 0 to n-1) and right vertices (numbered 0 to m-1). The input provides an adjacency list where graph[i] contains all right vertices that left vertex i is connected to.

Return the size of the maximum matching.

Input & Output

Example 1 — Complete Bipartite Graph
$ Input: graph = [[1,2],[0,2],[0,1]]
Output: 3
💡 Note: Perfect matching possible: Left 0→Right 1, Left 1→Right 2, Left 2→Right 0. All vertices matched.
Example 2 — Partial Matching
$ Input: graph = [[0,1],[1],[0]]
Output: 2
💡 Note: Maximum matching: Left 0→Right 0, Left 1→Right 1. Left 2 cannot be matched (Right 0 already taken).
Example 3 — No Edges
$ Input: graph = [[],[],[]]
Output: 0
💡 Note: No edges exist, so no matching is possible.

Constraints

  • 1 ≤ graph.length ≤ 100
  • 0 ≤ graph[i].length ≤ 100
  • 0 ≤ graph[i][j] ≤ 99

Visualization

Tap to expand
Maximum Bipartite MatchingINPUT GRAPHL0L1L2R0R1R2HUNGARIAN ALGORITHM1Find augmenting paths2Use DFS to explore paths3Update matching along path4Repeat until no paths existMAXIMUM MATCHINGL0L1L2R0R1R2Size: 3Key Insight:Augmenting paths allow systematic improvement of bipartite matching until optimality is achieved.TutorialsPoint - Maximum Bipartite Matching | Hungarian Algorithm
Asked in
Google 35 Microsoft 28 Amazon 22 Facebook 18
30.3K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen