- Breadth-first search
Infobox Algorithm

class=Search Algorithm

Order in which the nodes are expanded

data=Graph

time=$O(|V|+|E|)\; =\; O(b^d)$

space=$O(|V|+|E|)\; =\; O(b^d)$

optimal=yes (for unweighted graphs)

complete=yesIn

graph theory ,**breadth-first search**(**BFS**) is agraph 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.**How it works**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 aFIFO queue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue orlinked list ) called "open" and then once examined are placed in the container "closed".**Algorithm (informal)**# Put the root node on the queue.

# Pull a node from the beginning of the queue and examine it.

#* If the searched element is found in this node, quit the search and return a result.

#* Otherwise push all the (so-far-unexamined) successors (the direct child nodes) of this node into the end of the queue, if there are any.

# If the queue is empty, every node on the graph has been examined -- quit the search and return "not found".

# Repeat from Step 2.**Note**: Using a stack instead of a queue to store the nodes yet to be visited would turn this algorithm into adepth-first search .**Python implementation**Assume we have a graph made up of Node objects, each containing a value and a list of child Node objects:

Then this function performs a breadth-first search on that graph when passed the graph's root node (a Node instance) and any function to be applied to each node in the graph:

For example, this script finds the sum of the integer values of each node in the graph:

# Define a "visit" functiontotal = 0def sum_graph(node): global total total += node.value

# Visit the whole graphbfs(the_graph, sum_graph)print total

**C implementation**Algorithm of Breadth-first search：

Main Algorithm of apply Breadth-first search to graph G=(V,E)：

**C++ implementation**This is the implementation of the above informal algorithm, where the "so-far-unexamined" is handled by the parent array. For actual C++ applications, see the

Boost Graph Library .:Suppose we have a struct:**Features****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(b^d)$. When the number of vertices and edges in the graph are known ahead of time, the space complexity can also be expressed as $O(|E|+|V|)$ where $|E|$ is thecardinality of the set of edges (the number of edges), and $|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.**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 $b+b^2+b^3+...+b^d$ which asymptotically approaches $O(b^d)$. The time complexity can also be expressed as $O(|E|+|V|)$ since every vertex and every edge will be explored in the worst case.

**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**For unit-step cost, breadth-first search is optimal. 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 all connected components in a graph.

* Finding all nodes within one connected component

* Copying Collection,Cheney's algorithm

* Finding theshortest path between two nodes "u" and "v" (in anunweighted graph )

* Finding theshortest path between two nodes "u" and "v" (in aweighted graph : see talk page)

* Testing a graph for bipartiteness

* (Reverse) Cuthill–McKee mesh numbering**Finding connected Components**The set of nodes reached by a BFS (breadth-first search) are the largest connected component containing the start node.

**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.

**Literature**Citation

last=Knuth

first=Donald E.

title=The Art Of Computer Programming Vol 1. 3rd ed.

publisher=Addison-Wesley

place=Boston

year=1997

ISBN=0-201-89683-4

url=http://www-cs-faculty.stanford.edu/~knuth/taocp.html**External links*** [

*http://www.martienus.com/code/t-sql-breadth-first-shortest-route-search.html Breadth-First shortest-route search implemented in T-SQL by Martin Withaar*]

*Wikimedia Foundation.
2010.*

### Look at other dictionaries:

**Breadth First Search**— Algorithme de parcours en largeur Pour les articles homonymes, voir BFS. L algorithme de parcours en largeur (ou BFS, pour Breadth First Search) permet le parcours d un graphe de manière itérative, en utilisant une file. Il peut par exemple… … Wikipédia en Français**breadth-first-search**— paieška į plotį statusas T sritis informatika apibrėžtis ↑Paieškos medžio apėjimo būdas, kai išanalizavus visus to paties lygio mazgus pereinama prie kito lygio mazgų. atitikmenys: angl. breadth first search ryšiai: dar žiūrėk – paieškos medis… … Enciklopedinis kompiuterijos žodynas**Depth-first search**— Order in which the nodes are visited Class Search algorithm Data structure Graph Worst case performance … Wikipedia**Iterative deepening depth-first search**— Graph and tree search algorithms Alpha beta pruning A* B* Beam Bellman–Ford algorithm Best first Bidirectional … Wikipedia**Search algorithm**— In computer science, a search algorithm, broadly speaking, is an algorithm that takes a problem as input and returns a solution to the problem, usually after evaluating a number of possible solutions. Most of the algorithms studied by computer… … Wikipedia**First Roumanian-American congregation**— First Roumanian American congregation … Wikipedia**A* search algorithm**— In computer science, A* (pronounced A star ) is a best first, graph search algorithm that finds the least cost path from a given initial node to one goal node (out of one or more possible goals). It uses a distance plus cost heuristic function… … Wikipedia**Beam search**— is a heuristic search algorithm that is an optimization of best first search that reduces its memory requirement. Best first search is a graph search which orders all partial solutions (states) according to some heuristic which attempts to… … Wikipedia**Uniform-cost search**— In computer science, uniform cost search (UCS) is a tree search algorithm used for traversing or searching a weighted tree, tree structure, or graph. Intuitively, the search begins at the root node. The search continues by visiting the next node… … Wikipedia**Depth-limited search**— Class Search Algorithm Data structure Graph Worst case performance O( | V | + | E | ) … Wikipedia