0% found this document useful (0 votes)
16 views5 pages

Applications of BFS Algorithm

Breadth-first search (BFS) is a graph traversal algorithm that explores all neighboring nodes starting from a root node, categorizing nodes as visited or non-visited. It has various applications including finding neighboring locations, web crawling, and determining shortest paths. The algorithm operates with a time complexity of O(V+E) and a space complexity of O(V), and is implemented using a queue data structure.

Uploaded by

dayalraj6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Applications of BFS Algorithm

Breadth-first search (BFS) is a graph traversal algorithm that explores all neighboring nodes starting from a root node, categorizing nodes as visited or non-visited. It has various applications including finding neighboring locations, web crawling, and determining shortest paths. The algorithm operates with a time complexity of O(V+E) and a space complexity of O(V), and is implemented using a queue data structure.

Uploaded by

dayalraj6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BFS algorithm

Breadth-first search is a graph traversal algorithm that starts traversing the graph from
the root node and explores all the neighbouring nodes. Then, it selects the nearest
node and explores all the unexplored nodes. While using BFS for traversal, any node
in the graph can be considered as the root node.

There are many ways to traverse the graph, but among them, BFS is the most
commonly used approach. It is a recursive algorithm to search all the vertices of a tree
or graph data structure. BFS puts every vertex of the graph into two categories - visited
and non-visited. It selects a single node in a graph and, after that, visits all the nodes
adjacent to the selected node.

Applications of BFS algorithm


The applications of breadth-first-algorithm are given as follows -

o BFS can be used to find the neighbouring locations from a given source
location.
o In a peer-to-peer network, BFS algorithm can be used as a traversal method to
find all the neighbouring nodes. Most torrent clients, such as BitTorrent,
uTorrent, etc. employ this process to find "seeds" and "peers" in the network.
o BFS can be used in web crawlers to create web page indexes. It is one of the
main algorithms that can be used to index web pages. It starts traversing from
the source page and follows the links associated with the page. Here, every web
page is considered as a node in the graph.
o BFS is used to determine the shortest path and minimum spanning tree.
o BFS is also used in Cheney's technique to duplicate the garbage collection.
o It can be used in ford-Fulkerson method to compute the maximum flow in a
flow network.

Algorithm
The steps involved in the BFS algorithm to explore a graph are given as follows -

Step 1: SET STATUS = 1 (ready state) for each node in G

Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)

Step 3: Repeat Steps 4 and 5 until QUEUE is empty


Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).

Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS =
1) and set

their STATUS = 2

(waiting state)

[END OF LOOP]

Step 6: EXIT

Example of BFS algorithm


Now, let's understand the working of BFS algorithm by using an example. In the
example given below, there is a directed graph having 7 vertices.

In the above graph, minimum path 'P' can be found by using the BFS that will start
from Node A and end at Node E. The algorithm uses two queues, namely QUEUE1 and
QUEUE2. QUEUE1 holds all the nodes that are to be processed, while QUEUE2 holds
all the nodes that are processed and deleted from QUEUE1.

Now, let's start examining the graph starting from Node A.

Step 1 - First, add A to queue1 and NULL to queue2.

QUEUE1 = {A}
QUEUE2 = {NULL}

Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbours of
node A to queue1.

QUEUE1 = {B, D}
QUEUE2 = {A}
Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbours of
node B to queue1.
QUEUE1 = {D, C, F}
QUEUE2 = {A, B}

Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbours of
node D to queue1. The only neighbour of Node D is F since it is already inserted, so it will not
be inserted again.
QUEUE1 = {C, F}
QUEUE2 = {A, B, D}

Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbours of node C
to queue1.
QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}

Step 5 - Delete node F from queue1 and add it into queue2. Insert all neighbours of node F
to queue1. Since all the neighbours of node F are already present, we will not insert them
again.
QUEUE1 = {E, G}
QUEUE2 = {A, B, D, C, F}

Step 6 - Delete node E from queue1. Since all of its neighbours have already been added, so
we will not insert them again. Now, all the nodes are visited, and the target node E is
encountered into queue2.
QUEUE1 = {G}
QUEUE2 = {A, B, D, C, F, E}

Complexity of BFS algorithm


Time complexity of BFS depends upon the data structure used to represent the graph.
The time complexity of BFS algorithm is O(V+E), since in the worst case, BFS algorithm
explores every node and edge. In a graph, the number of vertices is O(V), whereas the
number of edges is O(E).
The space complexity of BFS can be expressed as O(V), where V is the number of
vertices.

Implementation of BFS algorithm


#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1, rear = -1;
int visited[MAX_SIZE];
int adj_matrix[MAX_SIZE][MAX_SIZE];
int num_vertices;

void enqueue(int v) {
if (rear == MAX_SIZE - 1)
printf("Queue overflow!\n");
else {
if (front == -1)
front = 0;
rear++;
queue[rear] = v;
}
}

int dequeue() {
int item;
if (front == -1 || front > rear) {
printf("Queue underflow!\n");
exit(1);
}
item = queue[front];
front++;
return item;
}

void bfs(int v) {
int i;
visited[v] = 1;
enqueue(v);

while (front != -1) {


v = dequeue();
printf("%d ", v);

for (i = 0; i < num_vertices; i++) {


if (adj_matrix[v][i] == 1 && visited[i] == 0) {
visited[i] = 1;
enqueue(i);
}
}
}
}

int main() {
int i, j, start_vertex;

printf("Enter the number of vertices: ");


scanf("%d", &num_vertices);

printf("Enter the adjacency matrix:\n");


for (i = 0; i < num_vertices; i++) {
for (j = 0; j < num_vertices; j++) {
scanf("%d", &adj_matrix[i][j]);
}
}

printf("Enter the starting vertex: ");


scanf("%d", &start_vertex);

printf("BFS Traversal: ");


bfs(start_vertex);

return 0;
}
Output:
Enter the number of vertices: 4
Enter the adjacency matrix:
0110
1001
1001
0110
Enter the starting vertex: 0
BFS Traversal: 0 1 2 3

You might also like