Breadth First Search


Back to cs141

Breadth First Search is used to compute the distance (in terms of the number of edges) from a given source node in a graph to all other nodes in the graph and can be used on both directed and undirected graghs. The general idea is to start at a source node and then look at all of its neighbors. After you have looked at the source's neighbors, you look at the neighbors of the sources neighbors, and so on. With each iteration, the frontier of "discovered" nodes expands radially outward from the original source.

To implement a Breadth First Search, a few additional data structures are needed:

The pseudo-code for BFS:

The following is an example of BFS on a directed graph with ( c ) as the source vertex. The letter next to each vertex represent the name of that vertex, the text inside the vertex correspond to the vertex's distance information. The parent information is not included.


Tim Mauch
Last modified: Fri Sep 26 16:51:00 PDT 2003