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

Why does the time complexity of DFS and BFS depend on the way the graph is represented?

The site http://web.eecs.utk.edu/~huangj/CS302S04/notes/graph-searching.html describes that when an adjacency list is used then, DFS and BFS have complexity O(V+E), and if an adjacency matrix is used, the complexity is O(V2). Why is this?
23
votes
1 answer

Why is the complexity of BFS O(V+E) instead of O(V*E)?

Some pseudocode here (disregard my style) Starting from v1(enqueued): function BFS(queue Q) v2 = dequeue Q enqueue all unvisited connected nodes of v2 into Q BFS(Q) end // maybe minor problems here Since there are V vertices in the graph, and…
OneZero
  • 11,556
  • 15
  • 55
  • 92
21
votes
3 answers

marking node as visited on BFS when dequeuing

Just a quick and silly question, about BFS traversal on graphs I found on many sites the pseudocode for a BFS is pretty much something like this: BFS (Graph, root): create empty set S create empty queue Q add root to S //mark as visited…
sir psycho sexy
  • 780
  • 7
  • 19
21
votes
4 answers

Difference between 'backtracking' and 'branch and bound'

In backtracking we use both bfs and dfs. Even in branch and bound we use both bfs and dfs in additional to least cost search. so when do we use backtracking and when do we use branch and bound Does using branch and bound decreases time…
21
votes
1 answer

How do you use a Bidirectional BFS to find the shortest path?

How do you use a Bidirectional BFS to find the shortest path? Let's say there is a 6x6 grid. The start point is in (0,5) and the end point is in (4,1). What is the shortest path using bidirectional bfs? There are no path costs. And it is undirected.
20
votes
3 answers

Using BFS for topological sort

Can Breadth first Search be used for finding topological sorting of vertices and strongly connected components in a graph? If yes how to do that? and If not why not? we generally use Depth first search in these problems but What will be the problem…
monkey
  • 561
  • 1
  • 7
  • 16
19
votes
2 answers

Why is time complexity for BFS/DFS not simply O(E) instead of O(E+V)?

I know there's a similar question in stack overflow, where one person has asked, why time complexity of BFS/DFS is not simply O(V). The appropriate answer given was that E can be as large as V^2 in case of complete graph, and hence it is valid to…
18
votes
3 answers

Question about breadth-first completeness vs depth-first incompleteness

According to Norvig in AIMA (Artificial Intelligence: A modern approach), the Depth-first algorithm is not complete (will not always produce a solution) because there are cases when the subtree being descended will be infinite. On the other hand,…
george
  • 231
  • 1
  • 2
  • 4
18
votes
3 answers

Finding the number of paths of given length in a undirected unweighted graph

'Length' of a path is the number of edges in the path. Given a source and a destination vertex, I want to find the number of paths form the source vertex to the destination vertex of given length k. We can visit each vertex as many times as we…
2147483647
  • 1,177
  • 3
  • 13
  • 33
17
votes
3 answers

Javascript-ONLY DOM Tree Traversal - DFS and BFS?

Can anyone provide either code, pseudocode, or even provide good links to implementing DFS (Depth-first search) and BFS (breadth-first search) in plain JavaScript (No JQuery, or any helper libraries)? I've been trying to understand how to implement…
17
votes
2 answers

Shortest path in JavaScript

I have been searching for weeks for a way to compute shortest paths in JavaScript. I have been playing with the book Data Structures and Algorithms by Groner (aptly named) at …
Tyler330
  • 395
  • 2
  • 6
  • 15
17
votes
6 answers

Recursive breadth-first travel function in Java or C++?

Here is a java code for breadth-first travel: void breadthFirstNonRecursive(){ Queue queue = new java.util.LinkedList(); queue.offer(root); while(!queue.isEmpty()){ Node node = queue.poll(); visit(node); …
joejax
  • 361
  • 2
  • 3
  • 10
17
votes
12 answers

How would you print out the data in a binary tree, level by level, starting at the top?

This is an interview question I think of a solution. It uses queue. public Void BFS() { Queue q = new Queue(); q.Enqueue(root); Console.WriteLine(root.Value); while (q.count > 0) { Node n = q.DeQueue(); …
Learner
  • 2,556
  • 11
  • 33
  • 38
16
votes
2 answers

How to get the path between 2 nodes using Breadth-First Search?

I am trying to find a path between two nodes in a graph, where the edges are unweighted. I am using a Breadth First Search, which stops when it finds the target, in order to find the existence of a path, but I am not sure how to get the path…
cleerline
  • 451
  • 1
  • 4
  • 9
16
votes
3 answers

How to find shortest path in this type of maze

Red Dot - Represents the initial location Black Dot - Already occupied Green - Free to occupy Destination - Boundry of the matrix [which means either x = 0 or y = 0 or x = 8 or y = 8] Example: The red dot can place itself only one move at a time…
jpm
  • 1,042
  • 2
  • 12
  • 36