Breadth First Search Algorithm
Breadth First Search Algorithm
2. Make a scenario and draw BFS algorithm in detail as showed an example in today's class
Breadth first search Algorithm
BFS is a traversing algorithm where you should start traversing from a selected node
(source or starting node) and traverse the graph layerwise thus exploring the neighbour
nodes (nodes which are directly connected to source node). You must then move towards
the next-level neighbour nodes.
Tree of BFS
A sample BFS tree is given in figure, where the order of the BFS traversal operation
is: sink, A, C, E, B, D, F, G and H. After BFS is completed, the edges chosen by BFS are called
tree edges, and the remaining edges are called cross edges. For example, the edges, (A,B), (C,D)
and (E,F), are tree edges; the edges, (A,C), (B,D) and (D,F), are cross edges in figure. BFS
assigns a level value to each node, such as the level of the sink node is zero and the level of the
other nodes are their shortest distance to the sink node, for example, A, C and E are nodes in
level 1 and G and H are nodes in level 3 in figure. This property is very important for routing in
sensor networks, and there are many BFS-based approaches , since a shortest path tree rooted at
the sink node can be constructed. BFS can be extended to detect bridges where routing and
bridge detection can be accomplished at the same time . In this study, we proposed bridge
detection algorithms that are integrated with BFS and complete their operation within a single
BFS execution.
Draw BFS algorithm
BFS (G, s) //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.
mark s as visited.
v = Q.dequeue( )
if w is not visited
mark w as visited.