Opentopia Directory Encyclopedia Tools

Breadth-first search

Encyclopedia : B : BR : BRE : Breadth-first search


Breadth-first search
Order in which the nodes get expandedOrder in which the nodes are expanded
General Data
Class: Search Algorithm
Data Structure: Graph
Time Complexity: V
E|)]
Space Complexity: V
E|)]
Optimal: no
Complete: yes
In graph theory, breadth-first search (BFS) is a tree search algorithm used for traversing or searching a tree, tree structure, or graph. The algorithm 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.

How it works

Tree search algorithms
Search
Tree traversal
Formally, BFS is an uninformed search method that aims to expand and examine all nodes of a graph systematically in search of a solution. In other words, it exhaustively searches the entire graph without considering the goal until it finds it. It does not use a heuristic.

From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFO 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".

Algorithm (informal)

  1. Put the starting node (the root node) in the queue.
  2. Pull a node from the beginning of the queue and examine it.
  3. * If the searched element is found in this node, quit the search and return a result.
  4. * Otherwise push all the (so-far-unexamined) successors of this node into the end of the queue, if there are any.
  5. If the queue is empty, every node on the graph has been examined -- quit the search and return "not found".
  6. repeat from step 2.

Pseudocode

bfs(v)
enqueue(v)
mark v as visited
while queue not empty
v=dequeue()
process(v)
for all unvisited vertices i adjacent to v
mark i as visited
enqueue(i)

Features

Space Complexity

Since all nodes discovered so far have to be saved, the space complexity of breadth-first search is O(|V| + |E|) where |V| is the number of nodes and |E| the number of edges in the graph. Note: another way of saying this is that it is O(B ^ M) where B is the maximum branching factor and M is the maximum path length of the tree. This immense demand for space is the reason why breadth-first search is impractical for larger problems.

Time Complexity

Since in the worst case breadth-first search has to consider all paths to all possible nodes the time complexity of breadth-first search is O(|V| + |E|) where |V| is the number of nodes and |E| the number of edges in the graph.

Completeness

Breadth-first search is complete. This means that if there is a solution breadth-first search will find it regardless of the kind of graph. However, if the graph is infinite and there is no solution breadth-first search will diverge.

Optimality

In general breadth-first search is not optimal since it always returns the result with the fewest edges between the start node and the goal node. If the graph is a weighted graph, and therefore has costs associated with each step, a goal next to the start does not have to be the cheapest goal available. This problem is solved by improving breadth-first search to uniform-cost search which considers the path costs. Nevertheless, if the graph is not weighted, and therefore all step costs are equal, breadth-first search will find the nearest and the best solution.

Applications of BFS

Breadth-first search can be used to solve many problems in graph theory, for example:

Finding connected Components

The set of nodes reached by a BFS are the largest connected component containing the start node.

Test for bipartiteness

If there are edges joining nodes in the same BFS layer, then the graph must contain an odd length cycle and be non-bipartite.

See also

References

External links

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: