Breadth-first search
|
Order in which the nodes are expanded |
|
| Class | Search algorithm |
|---|---|
| Data structure | Graph |
| Worst case performance | O( | V | + | E | ) = O(bd) |
| Worst case space complexity | O( | V | ) = O(bd) |
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.
Contents |
[edit] How it works
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.
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".
|
An example map of Germany with some connections between cities
|
The breadth-first tree obtained when running BFS on the given map and starting in Frankfurt
|
[edit] Algorithm (informal)
- Enqueue the root node
- 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.
- If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
- If the queue is not empty, repeat from Step 2.
Note: Using a stack instead of a queue would turn this algorithm into a depth-first search.
[edit] Pseudocode
Input: A graph G and a root v of G
1 procedure BFS(G,v): 2 create a queue Q 3 enqueue v onto Q 4 mark v 5 while Q is not empty: 6 t ← Q.dequeue() 7 if t is what we are looking for: 8 return t 9 for all edges e in G.incidentEdges(t) do 10 o ← G.opposite(t,e) 11 if o is not marked: 12 mark o 13 enqueue o onto Q
[edit] Features
[edit] Space complexity
Since all of the nodes of a level must be saved until their child nodes in the next level have been generated, the space complexity is proportional to the number of nodes at the deepest level. Given a branching factor b and graph depth d the asymptotic space complexity is the number of nodes at the deepest level, O(bd). When the number of vertices in the graph is known ahead of time, and additional data structures are used to determine which vertices have already been added to the queue, the space complexity can also be expressed as O( | V | ) where | V | is the cardinality of the set of vertices. In the worst case the graph has a depth of 1 and all vertices must be stored. Since it is exponential in the depth of the graph, breadth-first search is often impractical for large problems on systems with bounded space.
[edit] 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
which is O(bd). The time complexity can also be expressed as O( | E | + | V | ) since every vertex and every edge will be explored in the worst case.
[edit] 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.
[edit] Proof of completeness
If the shallowest goal node is at some finite depth say d, breadth-first search will eventually find it after expanding all shallower nodes (provided that the branching factor b is finite).[1]
[edit] Optimality
For unweighted graphs (that is, graphs in which all step costs are equal), breadth-first search finds the shortest path from the start node to a goal node. However, it does not in general find shortest paths in weighted graphs; for instance, the goal node may be connected directly to the start node by an edge of high weight, but there may exist a shorter path with multiple edges. Uniform-cost search is an alternative search method to breadth-first search that does consider the path costs.
[edit] Bias towards nodes of high degree
It has been empirically observed (and analytically shown for random graphs) that incomplete breadth-first search is biased towards nodes of high degree. This makes a BFS sample of a graph very difficult to interpret. For example, a breadth-first sample of 1 million nodes in Facebook (less than 1% of the entire graph) overestimates the average node degree by 240%.[2] Under minor assumptions, this bias can be corrected.[3]
[edit] Applications
Breadth-first search can be used to solve many problems in graph theory, for example:
- Finding all nodes within one connected component
- Copying Collection, Cheney's algorithm
- Finding the shortest path between two nodes u and v
- Testing a graph for bipartiteness
- (Reverse) Cuthill–McKee mesh numbering
- Ford–Fulkerson method for computing the maximum flow in a flow network
- Serialization/Deserialization of a binary tree vs serialization in sorted order, allows the tree to be re-constructed in an efficient manner.
[edit] Finding connected components
The set of nodes reached by a BFS (breadth-first search) form the connected component containing the starting node.
[edit] Testing bipartiteness
BFS can be used to test bipartiteness, by starting the search at any vertex and giving alternating labels to the vertices visited during the search. That is, give label 0 to the starting vertex, 1 to all its neighbours, 0 to those neighbours' neighbours, and so on. If at any step a vertex has (visited) neighbours with the same label as itself, then the graph is not bipartite. If the search ends without such a situation occurring, then the graph is bipartite.
[edit] See also
[edit] References
- ^ Artificial Intelligence, a modern approach S. Russel and P. Norvig, 2003
- ^ M. Kurant, A. Markopoulou and P. Thiran. (2010). "On the bias of BFS (Breadth First Search)". International Teletraffic Congress (ITC 22). http://arxiv.org/abs/1004.1729.
- ^ M. Kurant, A. Markopoulou and P. Thiran. (2011). "Towards Unbiased BFS Sampling". IEEE JSAC 29 (9): 1799-1809. http://arxiv.org/abs/1102.4599.
- Knuth, Donald E. (1997), The Art Of Computer Programming Vol 1. 3rd ed., Boston: Addison-Wesley, ISBN 0-201-89683-4, http://www-cs-faculty.stanford.edu/~knuth/taocp.html
| Wikimedia Commons has media related to: Breadth-first search |
