# 0.7 Graphs  (Page 12/21)

 Page 12 / 21
 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

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)

BFS(G,i);

}

## 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 {

...

std::vector<int>out;

...

};

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

std::vector<Vertex>graph(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) {

std::queue<int>next;

std::map<int,int>parent;

parent[start] = -1;

next.push(start);

while (!next.empty()) {

int u = next.front();

next.pop();

// 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;

next.push(v);

}

}

}

return false;

}

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

## 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(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.

where we get a research paper on Nano chemistry....?
what are the products of Nano chemistry?
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
learn
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
learn
da
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Bhagvanji
Preparation and Applications of Nanomaterial for Drug Delivery
revolt
da
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
I only see partial conversation and what's the question here!
what about nanotechnology for water purification
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
Nasa has use it in the 60's, copper as water purification in the moon travel.
Alexandre
nanocopper obvius
Alexandre
what is the stm
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
what is Nano technology ?
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
why we need to study biomolecules, molecular biology in nanotechnology?
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
why?
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
how did you get the value of 2000N.What calculations are needed to arrive at it
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!