Algorithms · Visualized

Bipartite Matching

Graph Algorithms

Traverse, order, and find shortest paths and spanning trees on graphs — the machinery behind maps, networks, schedulers and dependency resolvers.

Finds the largest set of pairs between two sides of a bipartite graph with no vertex used twice — the assignment problem in its simplest form. Time: O(V·E) average, O(V·E) worst; space: O(V + E) for the graph and match arrays.

Big-O: Traversals O(V + E); Dijkstra O(E log V) with a heap; Bellman–Ford O(V·E); Floyd–Warshall O(V³); MST O(E log E).

Bipartite Matching

Graph Algorithms

Finds the largest set of pairs between two sides of a bipartite graph with no vertex used twice — the assignment problem in its simplest form. The algorithm repeatedly looks for an augmenting path: an unmatched left vertex tries each of its neighbours, and if a neighbour is already matched the algorithm asks that neighbour’s partner to relocate. A successful relocation increases the matching by one, and the process stops when no augmenting path exists.

How Bipartite Matching Works

  1. Keep matchRight[v] = the left vertex currently paired with right vertex v.
  2. For each unmatched left vertex u, run a DFS looking for an augmenting path.
  3. In the DFS, try each neighbour v of u; skip v if it was already visited this round.
  4. If v is free, or its partner can be re-matched elsewhere, pair u with v.
  5. Count one match per successful DFS and repeat until every left vertex has been tried.

Bipartite Matching Worked Example

Input:

Left {1,2,3} and right {a,b}: 1–a, 2–a, 2–b, 3–b. Pair 1–a, then 2 wants a and pushes 1 to no alternative, so 2 takes b; 3 then finds b taken and no free neighbour — the maximum matching has size 2.

Bipartite MatchingO(V·E)
Maximum left–right pairing via augmenting paths with rerouting.
ABCXYZ
matched0
Step 1 / 11
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Bipartite matching: match each left node (A,B,C) to a free right node (X,Y,Z), rerouting through matched edges when blocked.
Python · Graph Algorithms in Code
def max_matching(adj, left):
        pair_r = {}                 # right -> left partner
        def bpm(u, seen):
            for v in adj[u]:
                if v in seen: continue
                seen.add(v)
                if v not in pair_r or bpm(pair_r[v], seen):
                    pair_r[v] = u   # match / reroute
                    return True
            return False
        return sum(bpm(u, set()) for u in left)  # O(V·E)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Bipartite Matching Time & Space Complexity

BestO(V·E)
AverageO(V·E)
WorstO(V·E)
SpaceO(V + E) for the graph and match arrays

When to Use Bipartite Matching

  • Assigning workers to shifts or students to projects where each side may be used once.
  • The building block for weighted assignment (Hungarian algorithm) and Kőnig’s theorem.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.