{"id":269,"date":"2026-11-13T12:36:28","date_gmt":"2026-11-13T04:36:28","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-blossom-algorithm-matches-a-general-graph\/"},"modified":"2026-11-13T21:54:06","modified_gmt":"2026-11-13T13:54:06","slug":"how-the-blossom-algorithm-matches-a-general-graph","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-blossom-algorithm-matches-a-general-graph\/","title":{"rendered":"How the blossom algorithm matches a general graph"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Edmonds&#8217; blossom algorithm<\/strong> 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 <strong>blossom<\/strong>. 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 <strong>O(V\u00b3)<\/strong> or <strong>O(V\u00b2E)<\/strong> time, depending on how the search and the shrink are represented.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The blossom is not an optimization trick. Without the shrink, depth-first or breadth-first alternating search is wrong on some graphs.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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\u00b3) 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&#8217;s attachment becomes matched.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start from the empty matching or any greedy matching.<\/li>\n<li>Build an alternating forest from free vertices, using unmatched edges to reach new vertices and matched edges to continue.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when an odd alternating cycle is left unshrunk<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run Kuhn&#8217;s DFS on a general graph<\/td>\n<td>The search assumes a bipartition. An augmenting path that passes through a blossom is missed, and the matching is not maximum.<\/td>\n<\/tr>\n<tr>\n<td>Shrink an even alternating cycle<\/td>\n<td>An even cycle does not create the parity problem. Shrinking it can identify vertices that must stay distinct and can destroy a real matching.<\/td>\n<\/tr>\n<tr>\n<td>Forget to flip the matching inside an expanded blossom<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Sort edges by weight and take a greedy cardinality matching<\/td>\n<td>That is a heuristic for a different objective. It is not Edmonds&#8217; algorithm and it is not a maximum-weight matching.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>2<\/code>, matching a brute-force enumeration.<\/p>\n\n<pre><code class=\"language-python\">from collections import deque\n\ndef maximum_matching(graph):\n    n = len(graph)\n    match = [-1] * n\n\n    def augment(root):\n        parent = [-1] * n\n        base = list(range(n))\n        queued = [False] * n\n        q = deque([root])\n        queued[root] = True\n\n        def lca(a, b):\n            seen = [False] * n\n            while True:\n                a = base[a]\n                seen[a] = True\n                if match[a] == -1:\n                    break\n                a = parent[match[a]]\n            while True:\n                b = base[b]\n                if seen[b]:\n                    return b\n                b = parent[match[b]]\n\n        def blossom(v, other, blossom_base):\n            while base[v] != blossom_base:\n                flower[base[v]] = flower[base[match[v]]] = True\n                parent[v] = other\n                other = match[v]\n                v = parent[match[v]]\n\n        while q:\n            v = q.popleft()\n            for u in graph[v]:\n                if base[v] == base[u] or match[v] == u:\n                    continue\n                if u == root or (match[u] != -1 and parent[match[u]] != -1):\n                    flower = [False] * n\n                    b = lca(v, u)\n                    blossom(v, u, b)\n                    blossom(u, v, b)\n                    for i in range(n):\n                        if flower[base[i]]:\n                            base[i] = b\n                            if not queued[i]:\n                                queued[i] = True\n                                q.append(i)\n                elif parent[u] == -1:\n                    parent[u] = v\n                    if match[u] == -1:\n                        while u != -1:\n                            prev = parent[u]\n                            nxt = match[prev]\n                            match[u] = prev\n                            match[prev] = u\n                            u = nxt\n                        return True\n                    nxt = match[u]\n                    queued[nxt] = True\n                    q.append(nxt)\n        return False\n\n    for v in range(n):\n        if match[v] == -1:\n            augment(v)\n    return sum(x != -1 for x in match) \/\/ 2\n\ndef brute(graph):\n    edges = [(i, j) for i, nbrs in enumerate(graph) for j in nbrs if i &lt; j]\n    best = 0\n    for mask in range(1 &lt;&lt; len(edges)):\n        used = [False] * len(graph)\n        count = 0\n        ok = True\n        for bit, (u, v) in enumerate(edges):\n            if mask &gt;&gt; bit &amp; 1:\n                if used[u] or used[v]:\n                    ok = False\n                    break\n                used[u] = used[v] = True\n                count += 1\n        if ok:\n            best = max(best, count)\n    return best\n\ngraph = [[] for _ in range(5)]\nfor u, v in ((0, 1), (1, 2), (2, 0), (2, 3), (3, 4)):\n    graph[u].append(v)\n    graph[v].append(u)\nsize = maximum_matching(graph)\nassert size == brute(graph) == 2\nprint(size)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>Blossom V<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the blossom machinery when a check shows the graph is bipartite. Use Hopcroft-Karp. Keep Edmonds&#8217; 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the blossom algorithm returns the odd-cycle shrink that bipartite matching never needs. <strong>Edmonds&#8217; blossom algorithm finds a maximum matching in a general graph by shrinking odd alternating cycles and expanding them after augmentation.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Edmonds&#8217; 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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,32],"tags":[],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=269"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/269\/revisions\/668"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}