0% found this document useful (0 votes)
76 views

Breadth First Search Algorithm

This document contains an assignment submission for a computer science course. It includes: 1) A C++ implementation of the breadth-first search (BFS) graph traversal algorithm. 2) A scenario describing how BFS could be used for routing in wireless sensor networks and detecting critical network connections called bridges. 3) A diagram and explanation of how BFS forms a tree by exploring each level of the graph and assigns distances from the source node.

Uploaded by

Wasim Sajjad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Breadth First Search Algorithm

This document contains an assignment submission for a computer science course. It includes: 1) A C++ implementation of the breadth-first search (BFS) graph traversal algorithm. 2) A scenario describing how BFS could be used for routing in wireless sensor networks and detecting critical network connections called bridges. 3) A diagram and explanation of how BFS forms a tree by exploring each level of the graph and assigns distances from the source node.

Uploaded by

Wasim Sajjad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Qurtuba University of Science & Information Technology Peshawar

Name: Waseem Sajjad


Class: M.phil Computer Science
Semester :2nd
ID no : 16676
Course Name: Advance Design and Analysis of Algorithms
Assignment no 1
Assignment topic:
1. Make coding of BFS 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.

// Program to print BFS traversal from a given


// source vertex. BFS(int s) traverses vertices
// reachable from s.
#include<iostream>
#include <list>
 
using namespace std;
 
// This class represents a directed graph using
// adjacency list representation
class Graph
{
    int V;    // No. of vertices
 
// Pointer to an array containing adjacency
// lists
    list<int> *adj;  
public:
Graph(int V); // Constructor
 
    // function to add an edge to graph
    void addEdge(int v, int w);
 
    // prints BFS traversal from a given source s
    void BFS(int s); 
};
 
Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
void Graph::BFS(int s)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;
 
    // Create a queue for BFS
    list<int> queue;
 
    // Mark the current node as visited and enqueue it
    visited[s] = true;
    queue.push_back(s);
 
    // 'i' will be used to get all adjacent
    // vertices of a vertex
    list<int>::iterator i;
 
    while(!queue.empty())
    {
        // Dequeue a vertex from queue and print it
        s = queue.front();
        cout << s << " ";
        queue.pop_front();
 
        // Get all adjacent vertices of the dequeued
        // vertex s. If a adjacent has not been visited,
        // then mark it visited and enqueue it
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }
}
 
// Driver program to test methods of graph class
int main()
{
    // Create a graph given in the above diagram
    Graph g(4);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);
 
    cout << "Following is Breadth First Traversal "
         << "(starting from vertex 2) \n";
    g.BFS(2);
 
    return 0;
}

2. Make a scenario and draw BFS algorithm in detail


Broadcasting Networking makes use of what we call as packets for communication. These
packets follow a traversal method to reach various networking nodes. One of the most commonly
used traversal methods is Breadth-First Search. It is being used as an algorithm that is used to
communicate broadcasted packets across all the nodes in a network.
Wireless sensor networks (WSNs) are promising technologies for exploring harsh environments,
such as oceans, wild forests, volcanic regions and outer space. Since sensor nodes may have
limited transmission range, application packets may be transmitted by multi-hop communication.
Thus, connectivity is a very important issue. A bridge is a critical edge whose removal breaks the
connectivity of the network. Hence, it is crucial to detect bridges and take preventions. Since
sensor nodes are battery-powered, services running on nodes should consume low energy. In this
paper, we propose energy-efficient and distributed bridge detection algorithms for WSNs. Our
algorithms run single phase and they are integrated with the Breadth-First Search (BFS)
algorithm, which is a popular routing algorithm. Our first algorithm is an extended version of
Milic’s algorithm, which is designed to reduce the message length. Our second algorithm is
novel and uses ancestral knowledge to detect bridges. We explain the operation of the
algorithms, analyze their proof of correctness, message, time, space and computational
complexities. 
Wireless communication in harsh environments has many challenges, such as channel (edge)
failures, generally, as a consequence of the direct impact of the physical world. Some edges can
have a critical role in data delivery operation. If one of these edges fails, the connectivity of the
network is broken, and the network is divided into partitions. These type of edges are called
bridges (cut edges), which should be detected, and preventions should be taken. Bridge detection
is an important research area for various types of networks . After bridge detection is completed,
bridge neutralization algorithms can be applied.
Breadth-first search (BFS) is a fundamental graph traversal algorithm, which starts from the sink
node and search proceeds in a breadth-first manner . Firstly, the sink node is visited; then, all
neighbor nodes (Nr) of the sink node are visited. In the following step, neighbor lists of nodes,
∈ Nr, are visited sequentially. This operation continues, until there is no unvisited node in the
graph.

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.

while ( Q is not empty)

//Removing that vertex from queue,whose neighbour will be visited now

v = Q.dequeue( )

//processing all the neighbours of v

for all neighbours w of v in Graph G

if w is not visited

Q.enqueue( w ) //Stores w in Q to further visit its neighbour

mark w as visited.

You might also like