{"id":211,"date":"2026-11-13T12:34:41","date_gmt":"2026-11-13T04:34:41","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-kuhn-matches-by-augmenting-dfs\/"},"modified":"2026-11-13T21:52:53","modified_gmt":"2026-11-13T13:52:53","slug":"how-kuhn-matches-by-augmenting-dfs","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-kuhn-matches-by-augmenting-dfs\/","title":{"rendered":"How Kuhn matches by augmenting DFS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Kuhn&#8217;s algorithm<\/strong> builds a bipartite matching by a depth-first search for one augmenting path at a time. Each free left vertex tries to find an alternating path to a free right vertex, flipping the matching if it succeeds. With a visited mark cleared every search, the time is <strong>O(VE)<\/strong>. The matching is maximum when no free left vertex can still augment. The algorithm is Hopcroft-Karp without the phases.<\/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\">The augmenting-path theorem is enough: while an augmenting path exists, the matching is not maximum, and flipping one increases it by one. Depth-first search from a free left vertex, walking an unmatched edge to the right and the unique matched edge back to the left, either reaches a free right vertex or exhausts the alternating choices.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A visited mark is required inside one search so a right vertex is not tried twice. The mark must be cleared before the next left vertex starts, or a later search will treat reachable vertices as dead.<\/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 final matching is a maximum cardinality matching of a bipartite graph. At most V augmentations occur, and each search scans each edge a constant number of times, for O(VE). Extra memory is the mate array and the visited array. A greedy initialization, matching whatever is free as the adjacency lists are scanned once, cuts the constant and does not change the worst case.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The same DFS on a non-bipartite graph can fail to find an augmenting path that exists, because the path may need to travel an odd cycle in a way this search never represents. That case is a blossom.<\/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>Initialize mate[v] as unmatched for every vertex.<\/li>\n<li>For each left vertex u, clear the visited marks and call a search from u.<\/li>\n<li>The search tries each neighbor v of the current left vertex. If v is visited this attempt, skip it. Mark v.<\/li>\n<li>If v is free or a search from mate[v] succeeds, set mate[v] = u and mate[u] = v and report success.<\/li>\n<li>If the search returns failure, leave u unmatched for now and continue with the next left vertex. A second pass is unnecessary if every left vertex is tried once against the matching built so far. Trying them in a loop until a full pass adds nothing is also correct.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">One search per left vertex, each allowed to see the whole graph, is O(VE) and maximum only in a bipartite graph. The phased version that groups shortest paths is Hopcroft-Karp. Weighted edges are not consulted. A visited mark that survives across left vertices makes the algorithm miss augmentations and return a matching that is not maximum.<\/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 visited marks outlive one search<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Keep visited marks across left vertices<\/td>\n<td>A right vertex rejected for one starter can be the correct partner for the next starter. The mark has to mean &#8220;tried in this attempt.&#8221;<\/td>\n<\/tr>\n<tr>\n<td>Forget to flip along the whole recursion<\/td>\n<td>Setting the mate only at the free right vertex leaves the alternating edges above it unchanged. The recursive success has to reassign mates on the way back.<\/td>\n<\/tr>\n<tr>\n<td>Apply it to a general graph<\/td>\n<td>An augmenting path that winds through an odd cycle is not discovered by this bipartite recursion.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(E \u221aV)<\/td>\n<td>That is Hopcroft-Karp. One DFS per augmentation is O(VE).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Phased shortest augmentations belong to Hopcroft-Karp. The flow version of the same matching is Dinic on a unit network. Assignment with weights belongs 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>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment matches the left side by a depth-first search for an augmenting path, and it clears the visited marks before each search. The maximum matching has size <code>3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">adj = {0: [0, 1], 1: [0], 2: [2]}  # left -&gt; right\nmatch_r = {}\n\ndef dfs(u, seen):\n    for v in adj[u]:\n        if v in seen:\n            continue\n        seen.add(v)\n        if v not in match_r or dfs(match_r[v], seen):\n            match_r[v] = u\n            return True\n    return False\n\nfor u in adj:\n    dfs(u, set())\nassert len(match_r) == 3\nprint(len(match_r))\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\">Kuhn&#8217;s algorithm (augmenting DFS) computes a maximum matching in a bipartite graph: seats and applicants, or jobs and machines, with no costs. <strong>Hopcroft-Karp<\/strong> batches those paths. Costs move the problem to the Hungarian assignment 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 Kuhn when the bipartite graph is large and an O(VE) matching will not finish. Hopcroft-Karp is the replacement. Keep Kuhn when the graph is small or clarity matters more than the \u221aV factor, and clear the visited array on every attempt.<\/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 Kuhn&#8217;s algorithm returns the single augmenting DFS and the visited array that must be cleared each time. <strong>Kuhn&#8217;s algorithm finds a maximum bipartite matching in O(VE) time by depth-first search for one augmenting path at a time.<\/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: Kuhn&#8217;s algorithm builds a bipartite matching by a depth-first search for one augmenting path at a time. Each free left vertex tries to find an alternating path to a free right vertex, flipping the matching if it succeeds. With a visited mark cleared every search, the time&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-211","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\/211","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=211"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/211\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}