{"id":178,"date":"2026-11-13T12:33:41","date_gmt":"2026-11-13T04:33:41","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fleury-avoids-burning-a-bridge-early\/"},"modified":"2026-11-13T21:51:53","modified_gmt":"2026-11-13T13:51:53","slug":"how-fleury-avoids-burning-a-bridge-early","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fleury-avoids-burning-a-bridge-early\/","title":{"rendered":"How Fleury avoids burning a bridge early"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Fleury&#8217;s algorithm<\/strong> builds an Eulerian tour by a walking rule: from the current vertex, take an edge that is not a bridge of the remaining graph, unless every remaining edge is a bridge. The rule keeps the unused edges in one connected piece until they are consumed. It produces the same kind of tour as Hierholzer and spends extra time on bridge tests, so its straightforward cost is <strong>O(E\u00b2)<\/strong> rather than linear.<\/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\">An Eulerian walk fails if it enters a region, leaves by the last exit, and still has unused edges inside. That last exit was a bridge of the remaining graph. Fleury&#8217;s rule refuses to use it while any non-bridge edge is available. The walk is then forced to finish the side region first.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The rule is local and greedy. It needs a bridge test after each choice, or at least a test of the candidate edges at the current vertex. Hierholzer never asks the question. It walks blindly and repairs the tour by splicing. Fleury is the version whose correctness you can see one edge at a time.<\/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\">On a connected undirected graph with every degree even, the rule produces an Eulerian circuit. With exactly two odd degrees, starting at one of them produces an Eulerian trail. Each bridge test by a depth-first search is O(E), and there are E steps, so a direct implementation is O(E\u00b2). Extra memory is the remaining adjacency and the current walk.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A bridge of the remaining graph is an edge whose removal disconnects the vertices that still have unused edges. Isolated vertices left behind by consumed edges do not count. The algorithm must recompute bridges on the remaining graph. Bridges of the original graph are not the right set once any edge has been used.<\/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>Check connectivity of the non-isolated vertices and the odd-degree count. Start at an odd-degree vertex if two exist, otherwise at any vertex of positive degree.<\/li>\n<li>While the current vertex has an unused edge, list those edges and find which of them are bridges of the remaining graph.<\/li>\n<li>Prefer a non-bridge. If every unused edge at this vertex is a bridge, take any of them.<\/li>\n<li>Delete the chosen edge from both ends and walk to its other end. Append it to the tour.<\/li>\n<li>When the vertex has no unused edge, the tour is complete if the preconditions held.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The tour is Eulerian, through edges. The quadratic bound is the cost of the bridge tests, not a property of Eulerian tours themselves. Hierholzer removes that cost. A Hamiltonian tour is still a different problem.<\/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 bridge test uses the original graph<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Avoid edges that were bridges before any edge was used<\/td>\n<td>An original bridge may be the correct first edge of a remaining side tour later, and an edge that became a bridge only after deletions is the one the rule must see.<\/td>\n<\/tr>\n<tr>\n<td>Take a bridge whenever it is the lightest edge<\/td>\n<td>The rule is about connectivity of the remainder, not about weight. A lightest-edge rule is Kruskal and answers a different question.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(E) time<\/td>\n<td>That bound is Hierholzer&#8217;s, with constant-time deletion and no bridge test. Fleury pays for the tests.<\/td>\n<\/tr>\n<tr>\n<td>Start at an even-degree vertex when two odd degrees exist<\/td>\n<td>The walk cannot be a single trail that covers every edge. One odd vertex would be left unserved as an endpoint.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A linear construction of the same tour is Hierholzer. Bridge finding as its own problem, on a static undirected graph, is the low-value depth-first search.<\/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 walks an Eulerian graph and refuses an edge that is a bridge of what remains, unless that edge is the only exit. The triangle 0-1-2 uses every edge. It prints <code>0 1 2 0<\/code>.<\/p>\n\n<pre><code class=\"language-python\">from collections import defaultdict\nadj = defaultdict(list)\nedges = [(0, 1), (1, 2), (2, 0)]\nfor u, v in edges:\n    adj[u].append(v)\n    adj[v].append(u)\n\ndef reachable(src, blocked):\n    seen = {src}\n    stack = [src]\n    while stack:\n        u = stack.pop()\n        for v in adj[u]:\n            if (u, v) == blocked or (v, u) == blocked:\n                continue\n            if v not in seen:\n                seen.add(v)\n                stack.append(v)\n    return seen\n\ndef is_bridge(u, v):\n    before = len(reachable(u, None))\n    adj[u].remove(v)\n    adj[v].remove(u)\n    after = len(reachable(u, None))\n    adj[u].append(v)\n    adj[v].append(u)\n    return after &lt; before\n\nwalk = [0]\nwhile any(adj.values()):\n    u = walk[-1]\n    choices = list(adj[u])\n    nxt = None\n    for v in choices:\n        if len(choices) == 1 or not is_bridge(u, v):\n            nxt = v\n            break\n    adj[u].remove(nxt)\n    adj[nxt].remove(u)\n    walk.append(nxt)\nassert walk[0] == walk[-1] and len(walk) == 4\nprint(*walk)\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\">Fleury&#8217;s algorithm builds an Eulerian tour by refusing to cross a bridge too early. Street-routing and drawing problems that need such a tour use <strong>Hierholzer&#8217;s<\/strong> algorithm instead, which is linear. Fleury remains the version whose lesson is the bridge test.<\/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 Fleury in production code that only needs the tour. Hierholzer is faster and just as constructive. Keep Fleury when the teaching point is the bridge invariant itself, or when a bridge subroutine is already the primitive being demonstrated.<\/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 Fleury&#8217;s algorithm returns the non-bridge preference and the quadratic cost of testing it on the remaining graph. <strong>Fleury&#8217;s algorithm builds an Eulerian tour by avoiding a bridge of the remaining graph unless no other edge is left, at O(E\u00b2) with a direct bridge test.<\/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: Fleury&#8217;s algorithm builds an Eulerian tour by a walking rule: from the current vertex, take an edge that is not a bridge of the remaining graph, unless every remaining edge is a bridge. The rule keeps the unused edges in one connected piece until they are consumed.&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-178","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\/178","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=178"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions\/577"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}