Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Maximum matching, odd cycles included
Grows alternating trees from free vertices, shrinks odd cycles (blossoms) into one vertex, and flips augmenting paths until the matching is maximum in any graph
Select an algorithm and generate steps to begin visualization
A matching pairs up vertices along edges so that no vertex is in two pairs, and a maximum matching has as many pairs as possible. In a bipartite graph, where every edge goes between two sides, augmenting paths found by a simple search are enough (Hopcroft-Karp does this). In a general graph, odd cycles break that search: a vertex can be reachable by an even path one way round a cycle and by an odd path the other way, and the search gets confused. In 1965 Jack Edmonds solved the problem with the blossom algorithm, the first polynomial algorithm for maximum matching in general graphs, and the paper that made "polynomial time" the standard of an efficient algorithm.
The algorithm starts from an empty matching and searches from each free vertex. It grows an alternating tree by breadth-first search: the root is outer, a matched neighbour of an outer vertex joins as inner, and that neighbour's partner joins as outer, so tree paths alternate unmatched and matched edges. An edge from an outer vertex to a free vertex completes an augmenting path, and flipping it (unmatched edges in, matched edges out) adds one pair. An edge between two outer vertices closes an odd cycle, the blossom. Edmonds' insight is to shrink the blossom to a single vertex at its base: every vertex of the cycle can be reached by an even path one way round or the other, so all of them become outer and the search continues. When a path is found through a blossom, it is expanded on the side that keeps the edges alternating. With careful bookkeeping (bases and parent pointers) the whole algorithm runs in O(V³). The visualizer colours outer vertices green and inner ones purple, draws each blossom in pink and shows the augmenting path before it is flipped.
Maximum matching in general graphs is the right model whenever partners come from one pool rather than two sides: assigning roommates or teammates in pairs, pairing players in a tournament round, matching donor-recipient pairs in kidney exchange programmes, or pairing atoms in chemistry (perfect matchings of molecular graphs are Kekulé structures). It is the core of the Chinese postman problem, which pairs up odd-degree vertices, and of the Christofides approximation for the travelling salesman problem. Weighted versions of the blossom algorithm solve minimum-cost perfect matching, used in computer vision, in error correction for quantum computers, and in scheduling.
One search per free vertex. Each search grows an alternating tree and handles three kinds of edges: to a free vertex, to a matched vertex, and between two outer vertices.
Blossom(G):
match[v] = none for every vertex
for each vertex r with match[r] == none:
grow an alternating tree from r by BFS:
outer v, edge v–w:
w free (and w ≠ r): augmenting path found
w matched: w inner, match[w] outer
w outer: odd cycle, the blossom
shrink it to its base: all its vertices outer
if a path was found:
flip it (expanding blossoms on the even side)
return matchThis is the O(V³) formulation: shrinking a blossom only rewrites a base array and the parent pointers, and the augmenting path is read back through those pointers. A vertex from which no augmenting path exists never needs to be searched again, so each vertex is searched once.
Find a maximum matching on the example: a stem G-A-B joined to the odd cycle B-C-D-E-F, with H hanging off C.
Example graph: Eight vertices, edges G-A, A-B, B-C, C-D, D-E, E-F, F-B and C-H. B, C, D, E and F form a 5-cycle.
A perfect matching of 4 pairs. Without shrinking the blossom the search from G would have labelled C as inner and stopped: the only way to H goes round the odd cycle the long way, which a bipartite search cannot see.
Time: O(V³) · Space: O(V + E)
There are at most V searches, one per vertex. A search is a BFS that touches each edge a constant number of times, O(E), plus the blossom shrinking: each shrink costs O(V) to update the bases and there are at most O(V) shrinks per search, so a search costs O(V²). V searches give O(V³). Micali and Vazirani later brought maximum matching down to O(E √V), the same bound as Hopcroft-Karp, with a far more intricate algorithm.
Which matching algorithm to use depends on whether the graph is bipartite and whether the edges have weights.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Hopcroft-Karp | The graph is bipartite (two sides, no odd cycle). | O(E √V) |
| Hungarian method | Bipartite, and every pair has a cost: the cheapest perfect assignment. | O(n³) |
| Weighted blossom (Edmonds, Gabow) | General graph with edge weights: minimum-cost perfect matching, as in the Chinese postman problem. | O(V³) |
Read the full article: Bipartite Graph: Definition, Examples and How to Test
Read the full article: Cycles in Graphs: Cycle Detection Explained
Related algorithms: Hopcroft-Karp Matching, Gale-Shapley (Stable Matching), Bipartite Check, Chinese Postman, Christofides