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
16
votes
7 answers

Efficiently finding the shortest path in large graphs

I'm looking to find a way to in real-time find the shortest path between nodes in a huge graph. It has hundreds of thousands of vertices and millions of edges. I know this question has been asked before and I guess the answer is to use a…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
15
votes
1 answer

How can I find the actual path found by BFS?

The problem I'm trying to solve concerns a tree of MRT system. Each node can be connected to 4 points at most, which simplify thing by a lot. Here's my thought. struct stop { int path, id; stop* a; stop* b; stop* c; stop*…
Shane Hsu
  • 7,937
  • 6
  • 39
  • 63
15
votes
4 answers

What is time complexity of BFS depending on the representation of the graph?

I was wondering what is the time complexity of BFS, if I use: an adjacency matrix adjacency list edge list Is it same as their space complexity?
15
votes
2 answers

Find all *vertices* on all simple paths between two vertices in an undirected graph

Enumerating all simple paths between two vertices in an arbitrary graph takes exponential time in general, because there may be an exponential number of simple paths between the vertices. But what about if we're only interested in the vertices that…
15
votes
7 answers

How to implement a breadth first search to a certain depth?

I understand and can easily implement BFS. My question is, how can we make this BFS limited to a certain depth? Suppose, I just need to go 10 level deep.
user1220022
  • 11,167
  • 19
  • 41
  • 57
14
votes
3 answers

Why is Depth-First Search said to suffer from infinite loops?

I have read about DFS and BFS many times but I have this doubt lingering my mind since long. In a lot of articles it is mentioned that DFS can get stuck in infinite loops. As far as I know, this limitation can easily be removed by keeping track of…
vjain27
  • 3,514
  • 9
  • 41
  • 60
13
votes
3 answers

Implementation of BFS, DFS and Dijkstra

Is it true that the implementation of BFS, DFS and Dijkstra are almost the same, except that BFS uses queue, DFS uses stack, while Dijkstra uses min priority queue? More precisely. Can we use the following code for all of BFS, DFS, and Dijkstra,…
13
votes
3 answers

Edge classification during Breadth-first search on a directed graph

I am having difficulties finding a way to properly classify the edges while a breadth-first search on a directed graph. During a breadth-first or depth-first search, you can classify the edges met with 4 classes: TREE BACK CROSS FORWARD Skiena…
13
votes
5 answers

Shortest path (fewest nodes) for unweighted graph

I'm trying build a method which returns the shortest path from one node to another in an unweighted graph. I considered the use of Dijkstra's but this seems a bit overkill since I only want one pair. Instead I have implemented a breadth-first…
Robert
  • 8,406
  • 9
  • 38
  • 57
13
votes
5 answers

Shortest Root to Leaf Path

What is the easiest way, preferably using recursion, to find the shortest root-to-leaf path in a BST (Binary Search Tree). Java prefered, pseudocode okay. Thanks!
Sev
  • 15,401
  • 9
  • 56
  • 75
12
votes
5 answers

Random-first search?

The two most common ways to traverse a graph are breadth-first search and depth-first search. Both of these search algorithms follow a common template: Create a worklist W, seeded with the start node s. While the worklist isn't empty: Remove the…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
12
votes
2 answers

Breadth First Search Traversal VS Pre-order Traversal VS Depth First Search Traversal

For a binary tree, is Breadth First Search traversal (BFS) the same as Pre-order traversal? I am a little bit confused by these two different types of traversals. Can anyone please explain this to me? Additionally, how does Pre-order traversal…
Liu
  • 413
  • 4
  • 11
12
votes
2 answers

In what cases would BFS and DFS be more efficient than A* search algorithm?

I've tested A* search against Breadth First Searching (BFS) and Depth First Searching (DFS) and I find that fewer nodes are being expanded with A*. I understand that A* expands paths that are already less expensive by using the heuristic and edge…
12
votes
1 answer

In what sense is DFS faster than BFS?

While reading about DFS vs BFS, I came across a statement that DFS is faster than BFS, and requires less memory. My implementation is in C++ for both, making a stack for DFS and queue for BFS. Can someone please explain what, and how are the speed…
12
votes
2 answers

Spanning Tree VS. Spanning Forest

What's the difference between a Spanning Tree and a Spanning Forest in graphs, conceptually. Also, is it possible to construct a Spanning Forest through DFS or BFS traversals? Why? How? I understand the Spanning Tree, but I couldn't find any clear…