How the blossom algorithm matches a general graph

Short answer: Edmonds’ blossom algorithm finds a maximum matching in a general undirected graph. It searches for an alternating path and, when that search closes an odd cycle, shrinks the cycle into a blossom. The shrunken graph has an augmenting path exactly when the original graph has one. Expanding the blossom after an augmentation restores a real matching. A straightforward implementation runs in O(V³) or O(V²E) time, depending on how the search and the shrink are represented.


What the problem actually asks

Bipartite augmenting paths never need an odd cycle, because the graph has none. In a general graph an alternating walk can return to a vertex of the same side of the attempted bipartition and close a blossom: an odd cycle whose edges alternate, attached to the rest of the search by a stem. Ignoring the cycle misses augmenting paths that enter the cycle and leave on the other parity. Treating the cycle as an ordinary vertex, after shrinking, preserves the existence of an augmentation.

The blossom is not an optimization trick. Without the shrink, depth-first or breadth-first alternating search is wrong on some graphs.


What the algorithm guarantees

The algorithm returns a maximum cardinality matching. Each augmentation increases the matching by one, so there are O(V) of them. With blossoms represented so that a shrink and a dual parent in the alternating forest can be found quickly, a classical bound is O(V³) for the dense case. Extra memory is the mate array, the forest, and a stack or parent pointer for the blossoms so they can be expanded. The matching inside an expanded blossom is fixed up by flipping the cycle so the stem’s attachment becomes matched.

Weights change the problem to maximum-weight matching, which adds dual variables on blossoms and is a heavier algorithm. Cardinality is the version stated here. A graph that happens to be bipartite still works: no odd cycle is ever found, and the search reduces to an augmenting-path search.


The working steps

  1. Start from the empty matching or any greedy matching.
  2. Build an alternating forest from free vertices, using unmatched edges to reach new vertices and matched edges to continue.
  3. If an unmatched edge reaches a free vertex outside the forest, an augmenting path has been found. Expand every blossom on that path and flip the matching.
  4. If an unmatched edge reaches a vertex in the same forest tree and the cycle through their lowest common ancestor is odd, shrink that cycle into a blossom and continue the search from the blossom as a single vertex.
  5. If the forest stalls with no augmentation and no new blossom, the matching is maximum. Expand any bookkeeping so the mate array refers to original vertices.

Shrinking is required for correctness on general graphs, and it is unnecessary overhead on graphs already known to be bipartite. Those should use Hopcroft-Karp. Maximum-weight matching is not obtained by running cardinality on the heaviest edges first. Greedy weight is not optimal.


Approaches that look simpler and fail

Approach Why it fails when an odd alternating cycle is left unshrunk
Run Kuhn’s DFS on a general graph The search assumes a bipartition. An augmenting path that passes through a blossom is missed, and the matching is not maximum.
Shrink an even alternating cycle An even cycle does not create the parity problem. Shrinking it can identify vertices that must stay distinct and can destroy a real matching.
Forget to flip the matching inside an expanded blossom The stem arrives matched to one vertex of the cycle. The rest of the cycle has to be flipped so conservation holds at every vertex of the blossom.
Sort edges by weight and take a greedy cardinality matching That is a heuristic for a different objective. It is not Edmonds’ algorithm and it is not a maximum-weight matching.

Bipartite cardinality belongs to Hopcroft-Karp or Kuhn. Weighted bipartite assignment belongs to the Hungarian algorithm. The blossom shrink is the step those algorithms do not have.


A working example

This Python fragment grows an alternating forest and shrinks an odd cycle into a blossom so the search can continue through it. On a triangle with a tail, the maximum matching has size 2, matching a brute-force enumeration.

from collections import deque

def maximum_matching(graph):
    n = len(graph)
    match = [-1] * n

    def augment(root):
        parent = [-1] * n
        base = list(range(n))
        queued = [False] * n
        q = deque([root])
        queued[root] = True

        def lca(a, b):
            seen = [False] * n
            while True:
                a = base[a]
                seen[a] = True
                if match[a] == -1:
                    break
                a = parent[match[a]]
            while True:
                b = base[b]
                if seen[b]:
                    return b
                b = parent[match[b]]

        def blossom(v, other, blossom_base):
            while base[v] != blossom_base:
                flower[base[v]] = flower[base[match[v]]] = True
                parent[v] = other
                other = match[v]
                v = parent[match[v]]

        while q:
            v = q.popleft()
            for u in graph[v]:
                if base[v] == base[u] or match[v] == u:
                    continue
                if u == root or (match[u] != -1 and parent[match[u]] != -1):
                    flower = [False] * n
                    b = lca(v, u)
                    blossom(v, u, b)
                    blossom(u, v, b)
                    for i in range(n):
                        if flower[base[i]]:
                            base[i] = b
                            if not queued[i]:
                                queued[i] = True
                                q.append(i)
                elif parent[u] == -1:
                    parent[u] = v
                    if match[u] == -1:
                        while u != -1:
                            prev = parent[u]
                            nxt = match[prev]
                            match[u] = prev
                            match[prev] = u
                            u = nxt
                        return True
                    nxt = match[u]
                    queued[nxt] = True
                    q.append(nxt)
        return False

    for v in range(n):
        if match[v] == -1:
            augment(v)
    return sum(x != -1 for x in match) // 2

def brute(graph):
    edges = [(i, j) for i, nbrs in enumerate(graph) for j in nbrs if i < j]
    best = 0
    for mask in range(1 << len(edges)):
        used = [False] * len(graph)
        count = 0
        ok = True
        for bit, (u, v) in enumerate(edges):
            if mask >> bit & 1:
                if used[u] or used[v]:
                    ok = False
                    break
                used[u] = used[v] = True
                count += 1
        if ok:
            best = max(best, count)
    return best

graph = [[] for _ in range(5)]
for u, v in ((0, 1), (1, 2), (2, 0), (2, 3), (3, 4)):
    graph[u].append(v)
    graph[v].append(u)
size = maximum_matching(graph)
assert size == brute(graph) == 2
print(size)

Where it is used

Blossom V and other Edmonds matching codes pair vertices in a general graph, including pairing problems such as kidney exchange that are not bipartite. Hopcroft-Karp applies only when the graph is bipartite. The blossom is the odd cycle the bipartite algorithms never meet.


When you can drop this algorithm

Drop the blossom machinery when a check shows the graph is bipartite. Use Hopcroft-Karp. Keep Edmonds’ algorithm when the graph is general and the matching is unweighted cardinality, and test it on a triangle attached to an augmenting stem, which is the smallest blossom.


Why this stays published

We publish this so a search for the blossom algorithm returns the odd-cycle shrink that bipartite matching never needs. Edmonds’ blossom algorithm finds a maximum matching in a general graph by shrinking odd alternating cycles and expanding them after augmentation.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *