<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Software Atlas]]></title><description><![CDATA[Software Atlas]]></description><link>https://software-atlas.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 10:06:41 GMT</lastBuildDate><atom:link href="https://software-atlas.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Two ways to solve a maze. The faster one gives the worse answer.]]></title><description><![CDATA[Same maze, two algorithms. One looked at 40 cells. The other looked at 117. You'd think the one that did less work is better. It isn't. It found a path…]]></description><link>https://software-atlas.hashnode.dev/two-ways-to-solve-a-maze-the-faster-one-gives-the-worse-answer</link><guid isPermaLink="true">https://software-atlas.hashnode.dev/two-ways-to-solve-a-maze-the-faster-one-gives-the-worse-answer</guid><category><![CDATA[algorithms]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[beginnersguide]]></category><category><![CDATA[Computer Science]]></category><dc:creator><![CDATA[Thới Hải Đức]]></dc:creator><pubDate>Mon, 02 Nov 2026 13:03:55 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6ae860c0b60792e47e4b3a35/b2e7a0d7-8268-42c8-b7fc-5e8af8762189.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Same maze, two algorithms. One looked at 40 cells. The other looked at 117.</p>
<p>You'd think the one that did less work is better. It isn't. It found a path that's 10 moves longer.</p>
<img src="https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/ia0uhr1l5be6fdl1yfdd.gif" alt="BFS and DFS solving the same maze side by side. DFS finishes first, then BFS keeps going and finds a shorter path." style="display:block;margin:0 auto" />

<h2>BFS spreads out</h2>
<img src="https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/pbvq59i0dm62b4oun1wf.png" alt="BFS part way through the maze, with yellow cells spreading out from the start" style="display:block;margin:0 auto" />

<p>BFS tries everything one step away, then everything two steps away, and so on. It moves out like a ripple in a pond.</p>
<p>The yellow cells are the queue. First in, first out.</p>
<img src="https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/wbyprqtbzo9ulg32j00x.png" alt="BFS finished: 117 cells visited, 28 move path" style="display:block;margin:0 auto" />

<p>It visited 117 cells, so it did a lot of work. But the path is 28 moves, and that's the shortest one there is. Because it goes out in rings, the first time it reaches the goal has to be the shortest way there.</p>
<h2>DFS picks a corridor and keeps going</h2>
<img src="https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/m6khduzvpjao53lyul92.png" alt="DFS halfway through, following one long corridor" style="display:block;margin:0 auto" />

<p>DFS goes as far as it can down one corridor. It only backs up when it hits a dead end.</p>
<p>Same code basically. The only big change is a stack instead of a queue.</p>
<img src="https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/b1ndi6n2boio0gaovzyu.png" alt="DFS finished: 40 cells visited, 38 move path" style="display:block;margin:0 auto" />

<p>It got there after 40 cells. Nice. But the path is 38 moves.</p>
<p>And it just got lucky here. Add a few walls and DFS can wander through almost the whole maze first.</p>
<h2>So which one do I use?</h2>
<p>Need the shortest path? BFS. Think fewest hops between two people in a social network.</p>
<p>Just need to know if you can get there, or want to visit everything? DFS is usually less code.</p>
<p>You can try it here. Click cells to add walls and see what you can break: <a href="https://software-atlas.haiduc0147.dev/t/graphs">https://software-atlas.haiduc0147.dev/t/graphs</a></p>
]]></content:encoded></item></channel></rss>