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
466
votes
15 answers

What are the practical factors to consider when choosing between Depth-First Search (DFS) and Breadth-First Search (BFS)?

I understand the differences between DFS and BFS, but I'm interested to know what factors to consider when choosing DFS vs BFS. Things like avoiding DFS for very deep trees, etc.
Parth
  • 4,749
  • 3
  • 16
  • 4
211
votes
4 answers

Breadth First Vs Depth First

When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great.
209
votes
24 answers

Performing Breadth First Search recursively

Let's say you wanted to implement a breadth-first search of a binary tree recursively. How would you go about it? Is it possible using only the call-stack as auxiliary storage?
Nate
  • 6,779
  • 8
  • 28
  • 21
193
votes
9 answers

Why is the time complexity of both DFS and BFS O( V + E )

The basic algorithm for BFS: set start vertex to visited load it into queue while queue not empty for each edge incident to vertex if its not visited load into queue mark vertex So I would think the time…
ordinary
  • 5,943
  • 14
  • 43
  • 60
175
votes
9 answers

How does a Breadth-First Search work when looking for Shortest Path?

I've done some research, and I seem to be missing one small part of this algorithm. I understand how a Breadth-First Search works, but I don't understand how exactly it will get me to a specific path, as opposed to just telling me where each…
Jake
  • 3,142
  • 4
  • 30
  • 48
154
votes
7 answers

How to trace the path in a Breadth-First Search?

How do you trace the path of a Breadth-First Search, such that in the following example: If searching for key 11, return the shortest list connecting 1 to 11. [1, 4, 7, 11]
Christopher Markieta
  • 5,674
  • 10
  • 43
  • 60
154
votes
7 answers

Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?

Both can be used to find the shortest path from single source. BFS runs in O(E+V), while Dijkstra's runs in O((V+E)*log(V)). Also, I've seen Dijkstra used a lot like in routing protocols. Thus, why use Dijkstra's algorithm if BFS can do the same…
gingercat
  • 1,543
  • 2
  • 10
  • 4
113
votes
11 answers

Why DFS and not BFS for finding cycle in graphs

Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph.
109
votes
8 answers

Breadth First Search time complexity analysis

The time complexity to go over each adjacent edge of a vertex is, say, O(N), where N is number of adjacent edges. So, for V numbers of vertices the time complexity becomes O(V*N) = O(E), where E is the total number of edges in the graph. Since…
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
90
votes
11 answers

Find all paths between two graph nodes

I am working on an implementation of Dijkstra's Algorithm to retrieve the shortest path between interconnected nodes on a network of routes. I have the implementation working. It returns all the shortest paths to all the nodes when I pass the start…
Paul
  • 1,103
  • 1
  • 13
  • 18
67
votes
5 answers

What is difference between BFS and Dijkstra's algorithms when looking for shortest path?

I was reading about Graph algorithms and I came across these two algorithms: Dijkstra's algorithm Breadth-first search What is the difference between Dijkstra's algorithm and BFS while looking for the shortest-path between nodes? I searched a lot…
harrythomas
  • 1,407
  • 1
  • 13
  • 17
50
votes
3 answers

Using BFS for Weighted Graphs

I was revising single source shortest path algorithms and in the video, the teacher mentions that BFS/DFS can't be used directly for finding shortest paths in a weighted graph (I guess everyone knows this already) and said to work out the reason on…
user2125722
  • 1,289
  • 3
  • 18
  • 29
48
votes
16 answers

How can I remember which data structures are used by DFS and BFS?

I always mix up whether I use a stack or a queue for DFS or BFS. Can someone please provide some intuition about how to remember which algorithm uses which data structure?
captcadaver
  • 1,093
  • 2
  • 9
  • 7
43
votes
5 answers

Explanation of runtimes of BFS and DFS

Why are the running times of BFS and DFS O(V+E), especially when there is a node that has a directed edge to a node that can be reached from the vertex, like in this example in the following…
40
votes
4 answers

Is A* the best pathfinding algorithm?

It is generally said that A* is the best algorithm to solve pathfinding problems. Is there any situation when A* is not the best algorithm to find solution? How good is A* compared to BFS, DFS, UCS, etc?
Zombie
  • 493
  • 1
  • 8
  • 11
1
2 3
99 100