SlideShare a Scribd company logo
Breadth First Search (BFS)
Breadth First Search (BFS) algorithm traverses a graph in a
breadthward motion and uses a queue to remember to get
the next vertex to start a search, when a dead end occurs
in any iteration.
As in the example given above, BFS algorithm traverses
from A to B to E to F first then to C and G lastly to D. It
employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as
visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first
vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is
empty.
STE
P
TRAVERSAL DESCRIPTION
1 Initialize the stack.
2 We start from
visiting S (starting node),
and mark it as visited.
STE
P
TRAVERSAL DESCRIPTION
3 We then see an unvisited
adjacent node from S. In
this example, we have
three nodes but
alphabetically we choose A,
mark it as visited and
enqueue it.
4 Next, the unvisited
adjacent node from S is B.
We mark it as visited and
enqueue it.
STE
P
TRAVERSAL DESCRIPTION
5 Next, the unvisited
adjacent node from S is C.
We mark it as visited and
enqueue it.
6 Now, S is left with no
unvisited adjacent nodes.
So, we dequeue and find A.
STE
P
TRAVERSAL DESCRIPTION
7 From A we have D as
unvisited adjacent node.
We mark it as visited and
enqueue it.
At this stage, we are left with no unmarked (unvisited) nodes.
But as per the algorithm we keep on dequeuing in order to get
all unvisited nodes. When the queue gets emptied, the program
is over.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 5
struct Vertex {
char label;
bool visited;
};
int queue[MAX];
int rear = -1;
int front = 0;
int queueItemCount = 0;
struct Vertex* lstVertices[MAX];
int adjMatrix[MAX][MAX];
int vertexCount = 0;
Implementation in C
void insert(int data) {
queue[++rear] = data;
queueItemCount++;
}
int removeData() {
queueItemCount--;
return queue[front++];
}
bool isQueueEmpty() {
return queueItemCount == 0;
}
void addVertex(char label) {
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;
}
void addEdge(int start,int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
void displayVertex(int vertexIndex) {
printf("%c ",lstVertices[vertexIndex]->label);
}
int getAdjUnvisitedVertex(int vertexIndex) {
int i;
for(i = 0; i<vertexCount; i++) {
if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)
return i;
}
return -1;
}
void breadthFirstSearch() {
int i;
lstVertices[0]->visited = true;
displayVertex(0);
insert(0);
int unvisitedVertex;
while(!isQueueEmpty()) {
int tempVertex = removeData();
while((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) {
lstVertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
insert(unvisitedVertex);
}
}
for(i = 0;i<vertexCount;i++) {
lstVertices[i]->visited = false;
}
}
int main() {
int i, j;
for(i = 0; i<MAX; i++) { // set adjacency
for(j = 0; j<MAX; j++) // matrix to 0
adjMatrix[i][j] = 0;
}
addVertex('S'); // 0
addVertex('A'); // 1
addVertex('B'); // 2
addVertex('C'); // 3
addVertex('D'); // 4
addEdge(0, 1); // S - A
addEdge(0, 2); // S - B
addEdge(0, 3); // S - C
addEdge(1, 4); // A - D
addEdge(2, 4); // B - D
addEdge(3, 4); // C - D
printf("nBreadth First Search: ");
breadthFirstSearch();
return 0;
}

More Related Content

PPTX
Breadth-First-Search algorithm with Code
PPTX
Breadth first search (Bfs)
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
PPTX
Breadth First Search (BFS)
PPTX
Data structure
PPTX
p.p.pptx
PPTX
Breadth first search (bfs)
PPTX
Breath first Search and Depth first search
Breadth-First-Search algorithm with Code
Breadth first search (Bfs)
bfs tree searching ,sortingUntitled presentation.pptx
Breadth First Search (BFS)
Data structure
p.p.pptx
Breadth first search (bfs)
Breath first Search and Depth first search

Similar to Breadth First Searching Algorithm (BFS).pptx (20)

PPTX
Breadth First Search or BFS for a Graph traversal
PPT
14_Graph Traversalllllllllllllllllll.ppt
PDF
Implement Breadth-First Search with a QueueSolution Program .pdf
PPTX
Technical_Seminar .pptx
PPTX
Breadth First Search with example and solutions
DOC
BFS, Breadth first search | Search Traversal Algorithm
PPTX
Bfs present
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
PPTX
BFS (Breadth First Search) Tree Traversal
PPTX
Breadth First Search & Depth First Search
PPTX
BFS & DFS in Data Structure
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
PPTX
BFS_Presentation_Sourabh.pptx. Explain the bfs
PPTX
Topological Sort and BFS
PPTX
Bfs new
PPTX
Bfs new
PPTX
Breadth first search
PPTX
Data Structures - Lecture 10 [Graphs]
PPTX
Presentation on Breadth First Search (BFS)
Breadth First Search or BFS for a Graph traversal
14_Graph Traversalllllllllllllllllll.ppt
Implement Breadth-First Search with a QueueSolution Program .pdf
Technical_Seminar .pptx
Breadth First Search with example and solutions
BFS, Breadth first search | Search Traversal Algorithm
Bfs present
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
BFS (Breadth First Search) Tree Traversal
Breadth First Search & Depth First Search
BFS & DFS in Data Structure
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
BFS_Presentation_Sourabh.pptx. Explain the bfs
Topological Sort and BFS
Bfs new
Bfs new
Breadth first search
Data Structures - Lecture 10 [Graphs]
Presentation on Breadth First Search (BFS)
Ad

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Welding lecture in detail for understanding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
573137875-Attendance-Management-System-original
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Well-logging-methods_new................
PDF
composite construction of structures.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mechanical Engineering MATERIALS Selection
Internet of Things (IOT) - A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Welding lecture in detail for understanding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Model Code of Practice - Construction Work - 21102022 .pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
additive manufacturing of ss316l using mig welding
CH1 Production IntroductoryConcepts.pptx
Lecture Notes Electrical Wiring System Components
573137875-Attendance-Management-System-original
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Well-logging-methods_new................
composite construction of structures.pdf
Ad

Breadth First Searching Algorithm (BFS).pptx

  • 1. Breadth First Search (BFS) Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration.
  • 2. As in the example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D. It employs the following rules. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue. Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue. Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
  • 3. STE P TRAVERSAL DESCRIPTION 1 Initialize the stack. 2 We start from visiting S (starting node), and mark it as visited.
  • 4. STE P TRAVERSAL DESCRIPTION 3 We then see an unvisited adjacent node from S. In this example, we have three nodes but alphabetically we choose A, mark it as visited and enqueue it. 4 Next, the unvisited adjacent node from S is B. We mark it as visited and enqueue it.
  • 5. STE P TRAVERSAL DESCRIPTION 5 Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it. 6 Now, S is left with no unvisited adjacent nodes. So, we dequeue and find A.
  • 6. STE P TRAVERSAL DESCRIPTION 7 From A we have D as unvisited adjacent node. We mark it as visited and enqueue it. At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the program is over.
  • 7. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX 5 struct Vertex { char label; bool visited; }; int queue[MAX]; int rear = -1; int front = 0; int queueItemCount = 0; struct Vertex* lstVertices[MAX]; int adjMatrix[MAX][MAX]; int vertexCount = 0; Implementation in C
  • 8. void insert(int data) { queue[++rear] = data; queueItemCount++; } int removeData() { queueItemCount--; return queue[front++]; } bool isQueueEmpty() { return queueItemCount == 0; } void addVertex(char label) { struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex)); vertex->label = label; vertex->visited = false; lstVertices[vertexCount++] = vertex; } void addEdge(int start,int end) { adjMatrix[start][end] = 1; adjMatrix[end][start] = 1; }
  • 9. void displayVertex(int vertexIndex) { printf("%c ",lstVertices[vertexIndex]->label); } int getAdjUnvisitedVertex(int vertexIndex) { int i; for(i = 0; i<vertexCount; i++) { if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false) return i; } return -1; } void breadthFirstSearch() { int i; lstVertices[0]->visited = true; displayVertex(0); insert(0); int unvisitedVertex; while(!isQueueEmpty()) { int tempVertex = removeData();
  • 10. while((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) { lstVertices[unvisitedVertex]->visited = true; displayVertex(unvisitedVertex); insert(unvisitedVertex); } } for(i = 0;i<vertexCount;i++) { lstVertices[i]->visited = false; } } int main() { int i, j; for(i = 0; i<MAX; i++) { // set adjacency for(j = 0; j<MAX; j++) // matrix to 0 adjMatrix[i][j] = 0; }
  • 11. addVertex('S'); // 0 addVertex('A'); // 1 addVertex('B'); // 2 addVertex('C'); // 3 addVertex('D'); // 4 addEdge(0, 1); // S - A addEdge(0, 2); // S - B addEdge(0, 3); // S - C addEdge(1, 4); // A - D addEdge(2, 4); // B - D addEdge(3, 4); // C - D printf("nBreadth First Search: "); breadthFirstSearch(); return 0; }