{"id":141,"date":"2026-11-13T12:32:33","date_gmt":"2026-11-13T04:32:33","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bidirectional-bfs-meets-in-the-middle-of-a-path\/"},"modified":"2026-11-13T21:51:10","modified_gmt":"2026-11-13T13:51:10","slug":"how-bidirectional-bfs-meets-in-the-middle-of-a-path","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bidirectional-bfs-meets-in-the-middle-of-a-path\/","title":{"rendered":"How bidirectional BFS meets in the middle of a path"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Bidirectional breadth-first search<\/strong> grows one frontier from the source and one from the target on an unweighted graph. A shortest path has some meeting vertex, and the two searches together expand about <strong>O(b^{d\/2})<\/strong> vertices instead of <strong>O(b^d)<\/strong> when every vertex has about b unused edges and the shortest path has d edges. The recorded answer is the best sum of a forward distance and a backward distance at a meeting vertex.<\/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 one-sided BFS toward a distant target explores a ball of radius d. Searching from both ends explores two balls of radius about d\/2, which is dramatically smaller at large branching factors. The path is the concatenation of a forward shortest path and a backward shortest path at a vertex both searches have reached.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The first collision is not automatically the minimum sum unless the searches expand in an order that respects distance. A forward vertex at distance i and a backward vertex at distance j meet at total i+j. A collision at a large i and a tiny j can be worse than a later collision at two moderate distances. The stopping rule has to account for that.<\/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 an unweighted undirected graph, or a directed graph when the backward search walks reversed edges, the minimum of distF[v]+distB[v] over vertices reached by both sides is the shortest-path length. Expanding the side whose next vertex has the smaller distance, and closing the search when the sum of the two sides&#8217; next distances is at least the best meeting already found, returns that minimum after O(b^{d\/2}) expansions in the uniform-branching model. Extra memory is two distance arrays and two queues.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Weighted graphs need bidirectional Dijkstra, with the same meeting logic on tentative distances and with nonnegative weights. Plain bidirectional BFS on unequal weights returns a minimum-hop meeting, which may be an expensive path.<\/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 a forward queue with the source at distance 0 and a backward queue with the target at distance 0. If source equals target, the distance is 0.<\/li>\n<li>Always expand one vertex from the side whose front distance is smaller.<\/li>\n<li>When an edge from the expanded vertex lands on a vertex the other side has already reached, consider dist_this + 1 + dist_other as a candidate answer and keep the minimum.<\/li>\n<li>Stop when either queue is empty or the sum of the two front distances is at least the best candidate. Further expansions cannot beat it on unit weights.<\/li>\n<li>Recover the path by walking forward parents to the meeting vertex and backward parents to the target.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The square-root improvement is for unit weights and a single target. It is not a faster all-pairs algorithm, and it is not correct for unequal weights unless the expansion order is Dijkstra&#8217;s. Both searches must see the same graph, with the backward search using reversed arcs when the graph is directed.<\/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 first collision is returned immediately<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Return the first vertex generated by both sides<\/td>\n<td>That vertex may sit at distances whose sum is not minimal. Another meeting vertex, still inside the current layers, can be shorter.<\/td>\n<\/tr>\n<tr>\n<td>Run both searches to completion with no stop rule<\/td>\n<td>The answer becomes correct and the search has expanded far more than the meeting required. The sum-of-fronts test is the stop.<\/td>\n<\/tr>\n<tr>\n<td>Walk backward along outgoing edges in a directed graph<\/td>\n<td>The reverse path has to follow incoming edges. Otherwise the backward distances are distances in the wrong graph.<\/td>\n<\/tr>\n<tr>\n<td>Use it for weighted road networks<\/td>\n<td>Unit-weight meetings minimize hops. Bidirectional Dijkstra minimizes weight, with a heap on each side.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">One-source unweighted distances belong to ordinary BFS. Weighted bidirectional search is bidirectional Dijkstra. A memory-tight unweighted search that does not store both frontiers is iterative deepening.<\/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 searches from both ends. The answer is the best sum of a forward distance and a backward distance, which on this graph is the length of the shortest path. It prints <code>2<\/code>.<\/p>\n\n<pre><code class=\"language-python\">from collections import deque\nadj = {0: [1, 4], 1: [0, 2], 2: [1, 3], 3: [2, 4], 4: [0, 3]}\n\ndef bfs(src):\n    dist = {src: 0}\n    q = deque([src])\n    order = [src]\n    while q:\n        u = q.popleft()\n        for v in adj[u]:\n            if v not in dist:\n                dist[v] = dist[u] + 1\n                q.append(v)\n                order.append(v)\n    return dist\n\ndf, db = bfs(0), bfs(3)\nbest = min(df[v] + db[v] for v in df if v in db)\nassert best == 2\nprint(best)\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\">Bidirectional breadth-first search is how an unweighted route or a puzzle solver searches from the start and the goal at once. Social-distance queries on an unweighted graph use the same meeting point. A weighted road still wants Dijkstra or A*.<\/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 bidirectional BFS when there is no single target, or when the edges are weighted. Keep it for one unweighted path in a large graph with a high branching factor, where two balls of radius d\/2 fit and one ball of radius d does not.<\/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 bidirectional BFS returns the meeting-sum rule and the reason the first shared vertex is not enough. <strong>Bidirectional BFS finds an unweighted shortest path by meeting in the middle, and the answer is the best sum of forward and backward distances rather than the first collision.<\/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: Bidirectional breadth-first search grows one frontier from the source and one from the target on an unweighted graph. A shortest path has some meeting vertex, and the two searches together expand about O(b^{d\/2}) vertices instead of O(b^d) when every vertex has about b unused edges and the&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-141","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\/141","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=141"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}