Breadth-first search (BFS) is an important graph search algorithm that is used to solve many problems including finding the shortest path in a graph and solving puzzle games (such as Rubik's Cubes). Many problems in computer science can be thought of in terms of graphs. For example, analyzing networks, mapping routes, and scheduling are graph problems. Graph search algorithms like breadth-first search are useful for analyzing and solving graph problems.
Breadth-first search starts by searching a start node, followed by its adjacent nodes, then all nodes that can be reached by a path from the start node containing two edges, three edges, and so on. Formally, the BFS algorithm visits all vertices in a graph that are edges away from the source vertex before visiting any vertex edges away. This is done until no more vertices are reachable from . The image below demonstrates exactly how this traversal proceeds:
For a graph and a source vertex , breadth-first search traverses the edges of to find all reachable vertices from . It also computes the shortest distance to any reachable vertex. Any path between two points in a breadth-first search tree corresponds to the shortest path from the root to any other node .
There are three types of vertices in BFS: tree vertices, those that have been visited; fringe vertices, those adjacent to tree vertices but not yet visited; and undiscovered vertices, those that we have not encountered yet. In the animation above, white indicates vertices that are undiscovered, grey indicates fringe vertices, and black indicates tree vertices.
To systematically search a connected component of a graph, begin with one vertex on the fringe, all others unseen, and perform the following step until all vertices have been visited: "move one vertex from the fringe to the tree, and put any unseen vertices adjacent to on the fringe." Graph traversal methods differ in how it is decided which vertex should be moved from the fringe to the tree. For breadth-first search, choose the vertex from the fringe that was least recently encountered; this corresponds using a queue to hold vertices on the fringe.
Fill out the following graph by labeling each node 1 through 12 according to the order that breadth-first search would visit the nodes in.
What is the state of the queue at each iteration of BFS if it is called from node 'a'?
The below table shows the contents of the queue as the procedure. BFS visits vertices in the graph above. BFS will visit the same vertices as DFS.
Here is a pseudocode implementation of breadth-first search.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
For the following graph, perform a breadth-first search. Determine the source node, the list of visited nodes (), and the state of the queue () at each step.
1: Source node = , ,
2: Source node = , ,
3: Source node = , ,
4: Source node = , ,
5: Source node = , ,
6: Source node = , ,
7: Source node = , ,
8: Source node = , ,
9: Source node = , ,
10:Source node = , ,
11:Source node = , ,
12:Source node = , ,
13:Source node = , ,
14:Source node = , ,
15:Source node = , ,
10:Source node = , ,
Breadth-first search has a running time of since every vertex and every edge will be checked once. Depending on the input to the graph, could be between and .
DFS vs BFS
Breadth-first search is less space efficient than depth-first search because BFS keeps a priority queue of the entire frontier while DFS maintains a few pointers at each level.
If it is known that an answer will likely be found far into a tree, DFS is a better option than BFS. BFS is good to use when the depth of the tree can vary or if a single answer is needed — for example, the shortest path in a tree. If the entire tree should be traversed, DFS is a better option.
BFS always returns an optimal answer, but this is not guaranteed for DFS.
Breadth-first search can be used to solve games where a series of choices result in either a winning or losing state. For example, BFS can help a player determine a winning sequence of moves for solving a Rubik's cube.
Here is an example of a map that BFS can take and return the shortest paths.
BFS runs on the map above and determines what the shortest routes between the cities are and produces the map below, which shows the best paths from city to city.
- Matheny, B. Animated BFS.gif. Retrieved June 8, 2016, from https://en.wikipedia.org/wiki/File:Animated_BFS.gif
- Drichel, A. Breadth-first-tree. Retrieved June 8, 2016, from https://en.wikipedia.org/wiki/File:Breadth-first-tree.svg
- , R. MapGermanyGraph.svg. Retrieved June 8, 2016, from https://en.wikipedia.org/wiki/File:MapGermanyGraph.svg
- , R. GermanyBFS.svg. Retrieved June 8, 2016, from https://en.wikipedia.org/wiki/File:GermanyBFS.svg