TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Everyone gets bidirectional BFS wrong

8 pointsby zdimension5 months ago

4 comments

quokka5 months ago
I saw this via Lobsters yesterday and enjoyed reading it. The animations helped me understand bidiBFS.<p>But I&#x27;m confused by the example showing the buggy version of the algorithm fail. This is in the section &quot;Finding the Wrong Path&quot;.<p>Consider the state of the algorithm after the first step. We&#x27;ve explored S and added its neighbors to the queue, Q = [T, a1, b1].<p>In the next step we explore T, the first node in the queue. We add its neighbors , a3 and b2, to the queue.<p>Now, I would expect the new value of Q to be something like [a1, b1, a3, b2] (depending on the order we add T&#x27;s neighbors). In this case we would process a1, and b1 next, notice that b1 is adjacent to b2, and correctly find the shortest path S-b1-b2-T.<p>But the animation actually shows that after step 2, Q = [a1, a3, b1, b3]. In other words, a3 didn&#x27;t join the back of the queue but jumped in front of b1. This is what leads to the buggy behavior that is shown.<p>So as I understand the example, this would have worked fine if Q were actually a queue. But as shown it is not.<p>Why is this?
评论 #42462828 未加载
spartanatreyu5 months ago
A connection that tickled my fancy...<p>In the &quot;Finding Paths, Faster&quot; section it specifies:<p>1. that we can only use the standard A* algo when we know enough about &quot;the graph in question&quot; to exploit that knowledge to pick a heuristic.<p>2. When we don&#x27;t know anything specific to &quot;the graph in question&quot;, we need to rely on BFS or by extension BiDi BFS (Bi-directional BFS) to find the shortest path in general.<p>Then way down the page in the &quot;Finding the Right Path, Faster&quot; section it specifies an optimisation for speeding up a correct BiDi BFS algo in a general graph is to order our searching by always check the side with the least queued branches.<p>The connection comes from realising that while we might not know anything specific to the graph in question, we know enough about graphs in general (of which &quot;the graph&quot; is one) that gives us enough knowledge to exploit the graph in question.<p>So a BiDi BFS where we repeatedly choose the side with the fewest branches IS essentially a BiDi A* algo that can be applied on a general graph.<p>Or put another way, Unidirectional A* search can&#x27;t be used on a general graph, but Bidirectional A* search can.
zdimension5 months ago
Had a lot of fun making the animations for this one.
sebstefan5 months ago
I was about to post this after seeing it on lobste.rs. Really underrated on hackernews for some reason