{"slug": "bfs-is-the-absolute-best-way-to-find-the-shortest-path-between", "title": "BFS is the absolute best way to find the shortest path between", "summary": "Breadth-First Search (BFS) is the optimal algorithm for finding the shortest path in unweighted graphs, as it explores nodes level by level, guaranteeing the shortest route. A practical JavaScript implementation using a queue and a Set to track visited nodes is provided, with the caveat that weighted graphs require Dijkstra's algorithm instead.", "body_md": "# BFS is the absolute best way to find the shortest path between\n\nFor anyone building an AI workflow or a custom LLM agent that needs to traverse relational data, understanding the adjacency list is the first step. It's the most efficient way to represent this in code:\n\n``` js\nconst graph = {\n Alexandra: [\"Maria\", \"John\"],\n Maria: [\"Alexandra\", \"Sofia\"],\n Sofia: [\"Maria\", \"Pedro\"],\n Pedro: [\"Sofia\"],\n John: [\"Alexandra\", \"Elena\"],\n Elena: [\"John\", \"Carlos\"],\n Carlos: [\"Elena\"],\n};\n```\n\nThe danger in graph traversal is the infinite loop. If you just wander randomly, you'll end up bouncing between two people forever (Alexandra → Maria → Sofia → Maria...). To fix this, you need a strict exploration order and a way to track where you've already been.\n\nThis is where Breadth-First Search (BFS) shines. Instead of diving deep into one friendship chain, BFS explores in \"levels.\" It checks everyone one connection away, then everyone two connections away, and so on. The second you hit your target, you've guaranteed the shortest possible path because every shorter route has already been exhausted.\n\nHere is a practical tutorial on how to implement this search logic from scratch. I've used a queue to manage the exploration and a `Set`\n\nto keep track of visited nodes so we don't loop.\n\n```\nfunction introductionsAway(graph, start, target) {\n if (start === target) return { degrees: 0, path: [start] };\n\n const visited = new Set([start]);\n const queue = [[start, [start]]]; \n\n while (queue.length > 0) {\n const [person, path] = queue.shift();\n\n for (const friend of graph[person] || []) {\n if (visited.has(friend)) continue;\n if (friend === target) {\n return { degrees: path.length, path: [...path, friend] };\n }\n\n visited.add(friend);\n queue.push([friend, [...path, friend]]);\n }\n }\n\n return { degrees: -1, path: [] };\n}\n```\n\nWhen you actually run this, the `queue`\n\nstores not just the current person, but the full path taken to get to them. This allows the function to return the exact chain of introductions. While this assumes all relationships are equal, real-world data is usually \"weighted\"—meaning some connections are stronger than others. If you start adding weights to your edges, you'll want to move from BFS to something like Dijkstra's algorithm to find the \"strongest\" path rather than just the shortest.\n\n[Next Mistral AI just gave us a way to pin inference to the US or EU →](/en/threads/5960/)\n\n## All Replies （8）\n\n[@ale3oula](/en/users/ale3oula/)? 😄\n\n[@ZenMaster](/en/users/ZenMaster/)haha no, but i've read his papers. his approach to state space search is honestly genius", "url": "https://wpnews.pro/news/bfs-is-the-absolute-best-way-to-find-the-shortest-path-between", "canonical_source": "https://promptcube3.com/en/threads/6046/", "published_at": "2026-08-12 16:00:50+00:00", "updated_at": "2026-08-12 16:24:59.445217+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning"], "entities": ["Alexandra", "Maria", "Sofia", "Pedro", "John", "Elena", "Carlos"], "alternates": {"html": "https://wpnews.pro/news/bfs-is-the-absolute-best-way-to-find-the-shortest-path-between", "markdown": "https://wpnews.pro/news/bfs-is-the-absolute-best-way-to-find-the-shortest-path-between.md", "text": "https://wpnews.pro/news/bfs-is-the-absolute-best-way-to-find-the-shortest-path-between.txt", "jsonld": "https://wpnews.pro/news/bfs-is-the-absolute-best-way-to-find-the-shortest-path-between.jsonld"}}