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
12
votes
3 answers

Finding the shortest path nodes with breadth first search

I am running breadth first search on the above graph to find the shortest path from Node 0 to Node 6. My code public List shortestPathBFS(int startNode, int nodeToBeFound){ boolean shortestPathFound = false; …
underdog
  • 4,447
  • 9
  • 44
  • 89
12
votes
4 answers

How to implement breadth first search in Scala with FP

I'm wondering how to implement a Breadth-first search in Scala, using functional programing. Here is my first, impure, code : def bfs[S](init: S, f: S => Seq[S], finalS: S => Boolean): Option[S] = { val queue = collection.mutable.Queue[S]() …
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
12
votes
4 answers

How to find all shortest paths

I have a graph and I want to find all shortest paths between two nodes. I've found a shortest path between two nodes by BFS. However, it just gives me one of the shortest paths if there exists one more than. How could I get all of them using…
caesar
  • 2,865
  • 11
  • 29
  • 36
12
votes
1 answer

What's the purpose of BFS and DFS?

I've learned how these algorithms work, but what are they used for? Do we use them to: find a certain node in a graph or to find a shortest path or to find a cycle in a graph ? Both of them just visit all the nodes and mark them visited, and I…
Nayana
  • 1,513
  • 3
  • 24
  • 39
11
votes
1 answer

Time complexity of adjacency list representation?

I am going through this link for adjacency list representation. http://www.geeksforgeeks.org/graph-and-its-representations/ I have a simple doubt in some part of a code as follows : // A utility function to print the adjacenncy list representation…
11
votes
5 answers

How to modify dijkstra algorithm to find all possible paths?

I know that could be asked before already but I cannot find it. I need to modify below dijkstra algorithm which works good for finding shortest path between 2 nodes but I need to find all possible paths also. I know it should be relatively easy to…
Zulu Z
  • 1,103
  • 5
  • 14
  • 27
11
votes
2 answers

Breadth-first search on an 8x8 grid in Java

What I'm trying to do is count how many moves it takes to get to the goal using the shortest path. It must be done using a breadth first search. I put the 8x8 grid into a 2d array which is filled with one of four chars, E for empty (can move into…
Ethan
  • 155
  • 1
  • 1
  • 8
10
votes
3 answers

algorithm to enumerate all possible paths

Consider the following graph: I'm trying to find a way to enumerate all possible paths from a source node to a target node. For example, from A to E, we have the following possible paths: A B C D E A B C E A C D E A C E Note that for A C D E,…
10
votes
4 answers

Implementing BFS in Java

I am a beginner in Java, and I need some help. I am trying to implement Breadth First Search algorithm to solve a puzzle game (Unblock Me a game on Android). I am done with the GUI, but I am stuck with the algorithm. So far I can count the available…
Mabu
  • 174
  • 1
  • 1
  • 11
10
votes
1 answer

How do I stop the breadth-first search using Boost Graph Library when using a custom visitor?

Say I found the node that meets my criteria and I need to stop the search.
ypv
9
votes
3 answers

Breadth-First in Prolog

What is the general idea of using breadth-first over the default depth-first search scheme in Prolog? Not taking infinite branches? Is there any general way to use breadth-first in Prolog? I've been googling around and I didn't find too much useful…
Ricardo
  • 1,778
  • 1
  • 19
  • 32
9
votes
3 answers

Time complexity of this algorithm: Word Ladder

Question: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time. Each transformed word must exist in…
user1008636
  • 2,989
  • 11
  • 31
  • 45
9
votes
2 answers

best-first Vs. breadth-first

What is the difference between best-first-search and the breadth-first-search ? and which one do we call "BFS" ?
Hoda
  • 103
  • 1
  • 1
  • 7
9
votes
2 answers

BFS on Adjacency Matrix

I'm trying to implement a BFS on adjacency matrix of undirected unweighted graph which returns the number of nodes visited. I've come up with this till now but I think this is not right as when I print out the top/ visited node, I'm getting multiple…
TJain
  • 466
  • 1
  • 4
  • 18
9
votes
2 answers

Breadth-First Search using State monad in Haskell

Recently, I've asked a question for building DFS tree from Graph in Stackoverflow and had learned that it can be simply implemented by using State Monad. DFS in haskell While DFS requires to track only visited nodes, so that we can use 'Set' or…
MazaYong
  • 197
  • 1
  • 8