<< Chapter < Page Chapter >> Page >
Pic.15 Animated example of a breadth-first search

Algorithm (informal)

  1. Put the ending node (the root node) in the queue.
  2. 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.
  3. If the queue is empty, every node on the graph has been examined -- quit the search and return "not found".
  4. Repeat from Step 2.

C implementation

Algorithm of Breadth-first search:

void BFS(VLink G[], int v) {

int w;

VISIT(v); /*visit vertex v*/

visited[v] = 1; /*mark v as visited : 1 */


while(!QMPTYQ(Q)) {

v = DELQ(Q); /*Dequeue v*/

w = FIRSTADJ(G,v); /*Find first neighbor, return -1 if no neighbor*/

while(w != -1) {

if(visited[w] == 0) {

VISIT(w); /*visit vertex v*/

ADDQ(Q,w); /*Enqueue current visited vertext w*/

visited[w] = 1; /*mark w as visited*/


W = NEXTADJ(G,v); /*Find next neighbor, return -1 if no neighbor*/




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

void TRAVEL_BFS(VLink G[], int visited[], int n) {

int i;

for(i = 0; i<n; i ++) {

visited[i] = 0; /* Mark initial value as 0 */


for(i = 0; i<n; i ++)

if(visited[i] == 0)



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:

struct Vertex {





and an array of vertices: (the algorithm will use the indexes of this array, to handle the vertices)


the algorithm starts from start and returns true if there is a directed path from start to end:

bool BFS(const std::vector<Vertex>&graph, int start, int end) {



parent[start] = -1;


while (!next.empty()) {

int u = next.front();


// Here is the point where you can examine the u th vertex of graph

// For example:

if (u == end) return true;

for (std::vector<int>::const_iterator j = graph[u].out.begin(); j != graph[u].out.end(); ++j) {

// Look through neighbors.

int v = *j;

if (parent.count(v) == 0) {

// If v is unvisited.

parent[v] = u;





return false;


it also stores the parents of each node, from which you can get the path.


  • 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(BM) 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. The best case of this search is o(1). It occurs when the node is found at first time.

Questions & Answers

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Data structures and algorithms. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10765/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Data structures and algorithms' conversation and receive update notifications?