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
39
votes
10 answers

How do implement a breadth first traversal?

This is what I have. I thought pre-order was the same and mixed it up with depth first! import java.util.LinkedList; import java.util.Queue; public class Exercise25_1 { public static void main(String[] args) { BinaryTree tree = new…
35
votes
16 answers

Printing BFS (Binary Tree) in Level Order with Specific Formatting

To begin with, this question is not a dup of this one, but builds on it. Taking the tree in that question as an example, 1 / \ 2 3 / / \ 4 5 6 How would you modify your program to print it so, 1 2 3 4 5 6 rather than the…
viksit
  • 7,542
  • 9
  • 42
  • 54
35
votes
7 answers

Finding all the shortest paths between two nodes in unweighted undirected graph

I need help finding all the shortest paths between two nodes in an unweighted undirected graph. I am able to find one of the shortest paths using BFS, but so far I am lost as to how I could find and print out all of them. Any idea of the…
user1946334
  • 389
  • 1
  • 4
  • 7
33
votes
4 answers

What is the difference between breadth first searching and level order traversal?

I don't need code, just an explanation. My textbook says level order: each node at level i is processed before any node at level i+1 My understanding of breadth first searching is that you explore nodes nearest the root first, starting from the…
30
votes
2 answers

Is the runtime of BFS and DFS on a binary tree O(N)?

I realize that runtime of BFS and DFS on a generic graph is O(n+m), where n is number of nodes and m is number of edges, and this is because for each node its adjacency list must be considered. However, what is the runtime of BFS and DFS when it is…
30
votes
8 answers

Shortest path: DFS, BFS or both?

I know BFS alone can find the shortest path in an unweighted graph but I also read on a couple of sites where people were claiming that either BFS or DFS could do this. I just wanted to confirm that these were probably mistakes and that only BFS can…
user1136342
  • 4,731
  • 10
  • 30
  • 40
29
votes
5 answers

Difference between Breadth First Search, and Iterative deepening

I understand BFS, and DFS, but for the life of me cannot figure out the difference between iterative deepening and BFS. Apparently Iterative deepening has the same memory usage as DFS, but I am unable to see how this is possible, as it just keeps…
28
votes
4 answers

Termination Criteria for Bidirectional Search

According to most of the reading I have done, a bidirectional search algorithm is said to terminate when the "forward" and "backward" frontiers first intersect. However, in Section 3.4.6 of Artificial Intelligence: A Modern Approach, Russel and…
Michael Koval
  • 8,207
  • 5
  • 42
  • 53
28
votes
8 answers

How to find the number of different shortest paths between two vertices, in directed graph and with linear-time?

Here is the exercise: Let v and w be two vertices in a directed graph G = (V, E). Design a linear-time algorithm to find the number of different shortest paths (not necessarily vertex disjoint) between v and w. Note: the edges in G are…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
27
votes
6 answers

How to detect if a directed graph is cyclic?

How can we detect if a directed graph is cyclic? I thought using breadth first search, but I'm not sure. Any ideas?
iva123
  • 3,395
  • 10
  • 47
  • 68
26
votes
1 answer

Explain BFS and DFS in terms of backtracking

Wikipedia about Depth First Search: Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible…
hhh
  • 50,788
  • 62
  • 179
  • 282
26
votes
3 answers

Why is depth-first search claimed to be space efficient?

In an algorithms course I'm taking, it's said that depth-first search (DFS) is far more space efficient than breadth-first search (BFS). Why is that? Although they are basically doing the same thing, in DFS we're stacking the current node's…
26
votes
2 answers

Depth First Search and Breadth First Search Understanding

I'm making Tetris as a fun side project (not homework) and would like to implement AI so the computer can play itself. The way I've heard to do it is use BFS to search through for available places, then create an aggregate score of the most sensible…
user3871
  • 12,432
  • 33
  • 128
  • 268
26
votes
10 answers

What is breadth-first search useful for?

Usually when I've had to walk a graph, I've always used depth-first search because of the lower space complexity. I've honestly never seen a situation that calls for a breadth-first search, although my experience is pretty limited. When does it…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
23
votes
4 answers

DFS and BFS Time and Space complexities of 'Number of islands' on Leetcode

Here is the question description. The first 2 suggested solutions involve DFS and BFS. This question refers to the 1st two approaches: DFS and BFS. I have included the problem statement here for easier reading. Given a 2d grid map of '1's (land)…
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33