With your own example explain breadth first traversal technique, and analyze its complexity
Breadth First Search (BFS)
Breadth-first-search is a graph search algorithm that begins at the root node (or arbitrarily selected vertex in graph) and explores all the neighboring nodes. Then for each of those nearest nodes it explores their unexplored neighboring nodes, and so on until it finds the goal.
With BFS technique, any element is searched in graph as following:
Algorithm
Step 1: Enqueue the root node
Step 2: Dequeue a node and examine it:
If the element searched is found in this node, return it and end the search
Else, enqueue all of its adjacent nodes which are not yet explored
Step 3: If the queue is empty then return ‘NOT FOUND’ because all the nodes are already scanned
Step 4: Repeat step 2
Consider a graph as follows and we are searching for element ‘f’ in it.
Let ‘c’ be the starting vertex from where we begin breadth first search. And key=’f’ which we are searching for. Enqueue ‘c’.
Q={c}
Now, let’s begin the search procedure. Dequeue vertex from queue and examine it.
u <– ‘c’
because u ≠ key , enqueue all unexplored adjacent vertices of ‘c’.
Q={b,a,e}
Returning to step 2 again, u <– ‘b’ and because u ≠ key , enqueue all adjacent vertices of ‘b’
Q={a,e,d}
Dequeue an element u <–’a’ and because u ≠ key enqueue all unexplored adjacent vertices of ‘a’. Because all of the adjacent vertices of a are already explored, nothing is enqueued.
Q={e,d}
U<–’e’ and because u ≠ key, enqueue unexplored adjacent vertices of ‘e’.
Q={d,f}
Now, it’s the turn of u<–’d’ and because u ≠ key and there are no unexplored vertices of ‘d’, nothing is added on queue.
Q={f}
When dequeuing we got u<–’f’ and because u = key, we got the element we were looking for. We’ll return this element and end the search here.
Complexity of Breadth First Search
It is obvious that each node is enqueued and, there for dequeued once and only once. Individual queue operations are O(1) and so the total time spent on queue operation os O(n).
The adjacency list for each node taken off the queue is scanned once. Hence the total time spent scanning adjacency list is O(m).
Hence, the total time spent by breadth-first-search is = O(m+n).
This was a post in response to Kigozi Ronald from Makerere University, Uganda. I have received a total of 5 questions and two assignments and this is the answer of his second questions. I’ll be answering them graudally.
Please find the answer to his first questions on : Define array. Write a program to store 10 salesmen’s amount in an array and find out total sale & best sales amount.
Apologies to Setimba Ivan as I’m not yet able to respond his/her Multiple Choice Questions. Please allow me some time.


6 Responses to “With your own example explain breadth first traversal technique, and analyze its complexity”
Tweets that mention With your own example explain breadth first traversal technique, and analyze its complexity | ICT Trends - Online Computer Jobs Exam Preparation -- Topsy.com on November 24, 2010
[...] This post was mentioned on Twitter by Kannan B, Suresh Khanal. Suresh Khanal said: http://bit.ly/fZ4gmi With your own #example explain breadth first traversal technique, and analyze its #complexity. #bfs #dataStructures [...]
nisa sanjaya on November 25, 2010
nice graph search algorithm
air max@ on November 25, 2010
very great article,Thanks for you write it!
Arabian on November 27, 2010
Wonderful blog! I actually love how it’s simple on my eyes and the details is well written. I am wondering how I may be notified whenever a new post has been made. I have subscribed to your rss feed which should do the trick! Have a nice day!
asset management on November 30, 2010
I was an impressive algorithm guidelines and very functional codes. I have a bit problem with this one before. But now I am on average level using it.
networking training chennai on February 4, 2011
Good guideness… algorithm of BFS nice graph search algorithm