{"id":192,"date":"2026-11-13T12:34:07","date_gmt":"2026-11-13T04:34:07","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-hopcroft-karp-phases-bipartite-matching\/"},"modified":"2026-11-13T21:52:16","modified_gmt":"2026-11-13T13:52:16","slug":"how-hopcroft-karp-phases-bipartite-matching","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-hopcroft-karp-phases-bipartite-matching\/","title":{"rendered":"How Hopcroft-Karp phases bipartite matching"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Hopcroft-Karp<\/strong> computes a maximum matching in a bipartite graph in phases. Each phase breadth-first searches for the shortest augmenting paths and then depth-first searches a maximal set of vertex-disjoint paths of that length. The matching grows by all of them at once. There are <strong>O(\u221aV)<\/strong> phases and each phase is <strong>O(E)<\/strong>, so the time is <strong>O(E \u221aV)<\/strong>.<\/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\">A matching pairs vertices so that no two pairs share a vertex. It is maximum when it covers as many vertices as possible. An augmenting path starts and ends at a free vertex and alternates unmatched and matched edges. Flipping the matched state along that path increases the matching by one. Kuhn&#8217;s algorithm finds one such path at a time and can take O(V) searches, each O(E).<\/p>\n\n\n<p class=\"wp-block-paragraph\">Shortest augmenting paths can be taken together when they are vertex-disjoint. One phase does that, and the length of the shortest augmenting path strictly increases after the phase. The length can increase only \u221aV times before a counting argument says the matching is maximum.<\/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. Time is O(E \u221aV). Extra memory is the distance layers, the matching array of mates, and the usual adjacency lists. The same bound is what Dinic achieves on the flow network that splits each matching edge into a unit-capacity network. Hopcroft-Karp is that idea written directly on the bipartition, without an explicit flow network.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The graph must be bipartite. A shortest augmenting path in a general graph can be tangled by an odd cycle, and the disjoint-path phase does not go through. General graphs use the blossom algorithm. Weighted bipartite assignment uses the Hungarian algorithm, not this one.<\/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 from any greedy matching. A larger start only reduces the work.<\/li>\n<li>Breadth-first search from all free vertices on the left, along unmatched edges to the right and matched edges back to the left. Layer the vertices until a free vertex on the right is reached. That distance is the phase length.<\/li>\n<li>If no free right vertex is reached, the matching is maximum.<\/li>\n<li>Depth-first search from each free left vertex, following only edges that advance one layer, and commit every path that reaches a free right vertex. Vertices used by a committed path are removed from this phase so the paths stay disjoint.<\/li>\n<li>Flip the matching along each committed path and start the next phase. The next shortest augmenting path is strictly longer.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The \u221aV phase bound is for unweighted bipartite cardinality. It does not assign preferences, and it does not survive odd cycles. A flow implementation with Dinic on the unit network has the same asymptotics. A single DFS per augmentation is Kuhn and is O(VE).<\/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 the phase paths are not shortest or not disjoint<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Flip the first augmenting path and restart<\/td>\n<td>Correct, and that is Kuhn&#8217;s algorithm. The phase bound of \u221aV requires taking a maximal set of shortest paths before the next BFS.<\/td>\n<\/tr>\n<tr>\n<td>Allow two phase paths to share a vertex<\/td>\n<td>Flipping both corrupts the matching. The DFS has to consume a vertex when a path is committed.<\/td>\n<\/tr>\n<tr>\n<td>Run it on a graph with an odd cycle<\/td>\n<td>The alternating-layer argument uses the bipartition. A blossom can appear, and this code does not shrink it.<\/td>\n<\/tr>\n<tr>\n<td>Use the cardinality algorithm for a weighted assignment<\/td>\n<td>Maximum cardinality ignores weights. The assignment problem needs the Hungarian algorithm or min-cost flow.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">One augmenting DFS at a time is Kuhn. Weighted assignment is the Hungarian algorithm. General-graph cardinality matching is Edmonds&#8217; blossom algorithm.<\/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 phases the matching. Each phase adds a maximal set of vertex-disjoint shortest augmenting paths. The same bipartite graph matched by one-at-a-time search has size <code>2<\/code> here: left vertices 0 and 1 both reach only right vertex 0, and left vertex 2 reaches right vertex 1.<\/p>\n\n<pre><code class=\"language-python\">from collections import deque\nadj = [[0], [0], [1]]\npair_u = [-1, -1, -1]\npair_v = [-1, -1]\ndist = [0, 0, 0]\n\ndef bfs():\n    q = deque()\n    for u in range(3):\n        if pair_u[u] &lt; 0:\n            dist[u] = 0\n            q.append(u)\n        else:\n            dist[u] = 10**9\n    found = False\n    while q:\n        u = q.popleft()\n        for v in adj[u]:\n            matched = pair_v[v]\n            if matched &lt; 0:\n                found = True\n            elif dist[matched] == 10**9:\n                dist[matched] = dist[u] + 1\n                q.append(matched)\n    return found\n\ndef dfs(u):\n    for v in adj[u]:\n        matched = pair_v[v]\n        if matched &lt; 0 or (dist[matched] == dist[u] + 1 and dfs(matched)):\n            pair_u[u] = v\n            pair_v[v] = u\n            return True\n    dist[u] = 10**9\n    return False\n\nmatching = 0\nwhile bfs():\n    for u in range(3):\n        if pair_u[u] &lt; 0 and dfs(u):\n            matching += 1\nassert matching == 2\nprint(matching)\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\">Hopcroft-Karp computes a maximum bipartite matching in phases, which is the assignment of jobs to machines or applicants to seats when only the number of pairs matters. Kuhn&#8217;s one-path search is the simpler version. Costs on the edges move the problem to the Hungarian algorithm.<\/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 Hopcroft-Karp when the graph is tiny and Kuhn&#8217;s DFS is easier to check. Keep it for large bipartite cardinality matching, where the \u221aV phases are the difference between a finished run and an O(VE) run.<\/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 Hopcroft-Karp returns the phased shortest augmenting paths and the O(E \u221aV) bound. <strong>Hopcroft-Karp finds a maximum bipartite matching in O(E \u221aV) time by flipping a maximal set of vertex-disjoint shortest augmenting paths in each phase.<\/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: Hopcroft-Karp computes a maximum matching in a bipartite graph in phases. Each phase breadth-first searches for the shortest augmenting paths and then depth-first searches a maximal set of vertex-disjoint paths of that length. The matching grows by all of them at once. There are O(\u221aV) phases and&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-192","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\/192","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=192"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":591,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/192\/revisions\/591"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}