# 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

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)

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.

#### Questions & Answers

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
anyone know any internet site where one can find nanotechnology papers?
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
what does nano mean?
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
what is fullerene does it is used to make bukky balls
are you nano engineer ?
s.
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.
Tarell
what is the actual application of fullerenes nowadays?
Damian
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.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
so some one know about replacing silicon atom with phosphorous in semiconductors device?
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.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
for screen printed electrodes ?
SUYASH
What is lattice structure?
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
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!   By     By David Martin  