Questions tagged [breadth-first-search]

In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

In graph theory, breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

BFS is an uninformed search method that aims to expand and examine all nodes of a graph or combination of sequences by systematically searching through every solution. In other words, it exhaustively searches the entire graph or sequence without considering the goal until it finds it. It does not use a heuristic algorithm.

Algorithm

From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFO (i.e., First In, First Out) queue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue or linked list) called open and then once examined are placed in the container closed.

  1. Enqueue the root node
  2. Dequeue a node and examine it
    • If the element sought is found in this node, quit the search and return a result.
    • Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.
  3. If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
  4. If the queue is not empty, repeat from Step 2.

In the figure below, you can see the animated figure of BFS.

enter image description here

Applications

  • Finding all nodes within one connected component
  • Copying Collection, Cheney's algorithm
  • Finding the shortest path between two nodes u and v (with path length measured by number of edges)
  • Testing a graph for bipartiteness

Source

Wikipedia

2216 questions
7
votes
2 answers

Python Implement Breadth-First Search

I found an example online, however, returning only the sequence of the BFS elements is not enough for calculation. Let's say the root is the first level of the BFS tree, then its children are second level, etc. How can I know which level are they…
user5575144
7
votes
4 answers

breadth first or depth first search

I know how this algorithm works, but cant decide when to use which algorithm ? Are there some guidelines, where one better perform than other or any considerations ? Thanks very much.
7
votes
3 answers

breadth-first-search on huge graph with little ram

I currently have a graph that has about 10 million nodes and 35 million edges. For now the complete graph is loaded into memory at program start. This takes a couple of minutes (it is Java after all) and needs about half a gigabyte of RAM. For now…
allesblinkt
  • 346
  • 2
  • 7
7
votes
1 answer

Why are there two listed time complexities for breadth-first search?

The Wikipedia article on breadth-first search lists two time complexities for breadth-first search over a graph: O(|E|) and O(bd). Later on the page, though, it only lists O(|E|). Why are there two different runtimes? Which one is correct?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
7
votes
1 answer

Counting unvisited nodes at distance n for every node in graph

For each point in a large graph I am trying to create a list that contains the number of unvisited nodes at distance n from the starting node. An example output is: [1,3,6] which means that at distance 0 there is the starting node itself, at…
Ben Ruijl
  • 4,973
  • 3
  • 31
  • 44
7
votes
3 answers

Breadth first search branching factor

The run time of BFS is O(b^d) b is the branching factor d is the depth(# of level) of the graph from starting node. I googled for awhile, but I still dont see anyone mention how they figure out this "b" So I know branching factor means the "# of…
7
votes
3 answers

How to find the distance between two nodes using BFS?

I wrote this code of BFS in c++ using wikipedia's pseudocode. The function takes two parameters s,t. Where s is the source node and t is the target, if the target is fount, the the search return the target itself, or else it return -1. Here is my…
2147483647
  • 1,177
  • 3
  • 13
  • 33
6
votes
2 answers

Listing values in a binary heap in sorted order using breadth-first search?

I'm currently reading this paper and on page five, it discusses properties of binary heaps that it considers to be common knowledge. However, one of the points they make is something that I haven't seen before and can't make sense of. The authors…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
6
votes
3 answers

Finding the shortest path to solve colored water sorting games

So my aunt plays this now popular mobile game, shown in the picture below. She got stuck on a certain level and asked me if I can solve it. Knowing that I'm not smart enough to find patterns or a strategy to solve it, and knowing only the basics in…
6
votes
4 answers

Binary Tree Level Order Traversal using Javascript

This is a leetcode question. Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree [3, 9, 20, null, null, 15, 7], 3 / \ 9 20 / \ 15 …
6
votes
2 answers

Lazy, breadth-first traversal of a Rose Tree?

I'm trying to refactor a component that currently produces a Seq[X] using a fairly expensive recursive algorithm so that it produces a Stream[X] instead, so X's can be loaded/calculated on-demand, and the producer doesn't have to try to guess…
Alex Cruise
  • 7,939
  • 1
  • 27
  • 40
6
votes
1 answer

Find the shortest path to reach a given destination cell in 2D matrix with obstacles

I am working on below interview question and I am confuse on how to use BFS search here. I am confuse on how to deal with the blockades here? Given a MxN matrix find the shortest path to reach a given destination cell. The robot can move up,…
john
  • 11,311
  • 40
  • 131
  • 251
6
votes
1 answer

BFS in JavaScript using Recursion

It is easy to do DFS using recursion: function dfs(tree, fn, level) { fn(tree, level) tree.children.forEach(function(child){ dfs(child, fn, level + 1) }) } However every example I have seen of BFS uses a queue and is iterative rather than…
Lance
  • 75,200
  • 93
  • 289
  • 503
6
votes
1 answer

Handling duplicate nodes in Breadth First Search

Imagine there is a graph. The nodes are of the form GraphNode. The graph can have duplicate nodes. We have to do a BFS on the graph. We do not know the entire graph in the beginning, i.e., there is no way to index the nodes of the graph. For eg,…
6
votes
3 answers

Why are you guaranteed to find your result if it is in the graph with BFS but not with DFS?

I've read somewhere that DFS is not gaurenteed to find a solution while BFS is.. why? I don't really get how this is true.. could someone demonstrate a case for me that proves this?
Skizit
  • 43,506
  • 91
  • 209
  • 269