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 AlgorithmsFinds 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
- Keep matchRight[v] = the left vertex currently paired with right vertex v.
- For each unmatched left vertex u, run a DFS looking for an augmenting path.
- In the DFS, try each neighbour v of u; skip v if it was already visited this round.
- If v is free, or its partner can be re-matched elsewhere, pair u with v.
- 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.
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
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
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.