learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Edmonds' Blossom Algorithm

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

Time: O(V³)
Space: O(V + E)
Use Case: Pairing with odd cycles: roommates, kidney exchange, chemistry
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Edmonds' Blossom Algorithm (Maximum Matching)

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.

How it works

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.

Applications

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.

Pseudocode

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 match

This 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.

Worked example, step by step

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.

  1. Easy pairs. Searches from A, C and E each find a free neighbour at once: A-B, C-D and E-F join the matching. Three pairs, and G and H are still free.
  2. Tree from G. G is the root, outer. Its neighbour A is matched to B, so A is inner and B outer. From B, C and F are matched to D and E, so C and F are inner and D and E outer.
  3. The blossom. Now the edge D-E joins two outer vertices. That closes the odd cycle B-C-D-E-F. Every vertex of it can be reached by an even path from B, one way round or the other, so it shrinks to its base B and all five vertices become outer.
  4. Augmenting path. C is now outer, and its neighbour H is free. Expanding the blossom on the even side gives the path G-A=B-F=E-D=C-H, seven edges, three of them matched.
  5. Flip. Unmatched edges in, matched edges out: G-A, B-F, E-D and C-H. Four pairs, every vertex matched.

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.

Complexity, and where it comes from

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.

When to use Edmonds' Blossom Algorithm (Maximum Matching), and when not to

Which matching algorithm to use depends on whether the graph is bipartite and whether the edges have weights.

AlternativePrefer it whenCost
Hopcroft-KarpThe graph is bipartite (two sides, no odd cycle).O(E √V)
Hungarian methodBipartite, 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³)

Common pitfalls

  • Using a bipartite matching algorithm on a general graph. Hopcroft-Karp and simple augmenting-path searches assume no odd cycle. On a graph with one they can stop with a matching that is not maximum, as the search from G would here.
  • Treating vertices as visited once. In a general graph a vertex can need to be outer after it was reached as inner. Shrinking the blossom is what makes this right. Marking vertices visited the bipartite way loses augmenting paths.
  • Rebuilding the path through a blossom by hand. The path must go round each blossom on the side that keeps the edges alternating. Implementations get this from the parent pointers set while shrinking. Reconstructing it any other way easily gives a path with two matched edges in a row.

Frequently asked questions

What is a blossom?
An odd cycle met during the search, closed by an edge between two outer vertices. Its vertices can all be reached by an even alternating path, so the algorithm treats the whole cycle as one vertex, its base, until the path is found.
Why can't Hopcroft-Karp handle general graphs?
Its layered search gives each vertex one parity, outer or inner. On an odd cycle a vertex can be reached with both parities, and the search may block the one path that augments.
Is the maximum matching unique?
Usually not. Different search orders can give different matchings of the same maximum size. Berge's theorem guarantees only the size: a matching is maximum exactly when no augmenting path exists.
What is a perfect matching?
A matching that covers every vertex. It needs an even number of vertices, and Tutte's theorem says when one exists. The blossom algorithm finds one whenever there is one.

Related algorithms: Hopcroft-Karp Matching, Gale-Shapley (Stable Matching), Bipartite Check, Chinese Postman, Christofides

An example graph: you can change it, or start blank.

Zoom Controls

31%
Nodes: 8
Edges: 8