SlideShare a Scribd company logo
Objectives
▪ To understand the concept of BFS Traversal
▪ How to use BFS Traversal in solving the real
world problems
Breadth First Search Traversal(BFS)
▪ BFS traversal of a graph produces a spanning
tree as final result.
▪ Spanning Tree is a graph without loops.
▪ We use Queue data structure with maximum
size of total number of vertices in the graph
to implement BFS traversal.
Steps to implement BFS
Traversal
▪ Step 1 - Define a Queue of size total number of vertices in
the graph.
▪ Step 2 - Select any vertex as starting point for traversal.
Visit that vertex and insert it into the Queue.
▪ Step 3 - Visit all the non-visited adjacent vertices of the
vertex which is at front of the Queue and insert them into
the Queue.
▪ Step 4 - When there is no new vertex to be visited from
the vertex which is at front of the Queue then delete that
vertex.
▪ Step 5 - Repeat steps 3 and 4 until queue becomes empty.
▪ Step 6 - When queue becomes empty, then produce final
spanning tree by removing unused edges from the graph
Example
Consider the following graph to
perform BFS Traversal
◻Graph Traversals
A B C
D E F
G
BFS Traversal
▪ Step-1:Select the vertex A as starting
vertex(visit A)
▪Insert A into the Queue
A B C
D E F
G
Queue A
BFS Traversal
▪ Step-2:Visit the adjacent vertex of A which is not
visited(D,E,B)
▪ Insert newly visited vertices into the Queue and
delete A from the queue
A B C
D E F
G
Queue D E B
BFS Traversal
▪ Step-3:Visit all adjacent vertices of D which are not
visited(there is no vertex)
▪ Delete D from the queue
A B C
D E F
G
Queue E B
BFS Traversal
▪ Step-4:Visit all the adjacent vertices of E which are
not visited(C,F)
▪ Insert newly visited vertices into the Queue and
delete E from the queue
A B C
D E F
G
Queue B C F
BFS Traversal
▪ Step-5:Visit all the adjacent vertices of B which are
not visited(there is no vertex)
▪ Delete B from the queue
A B C
D E F
G
Queue C F
BFS Traversal
▪ Step-6:Visit all the adjacent vertices of C which are
not visited(G)
▪ Insert newly visited vertex into the queue and delete
C from the queue
A B C
D E F
G
Queue F G
BFS Traversal
▪ Step-7:Visit all the adjacent vertices of F which are
not visited(there is no vertex)
▪ Delete F from the queue
A B C
D E F
G
Queue G
BFS Traversal
▪ Step-8:Visit all the adjacent vertices of G which are
not visited(there is no vertex)
▪ Delete G from the queue
A B C
D E F
G
Queue
BFS Traversal
▪ Queue becomes empty.so,stop the process of BFS
▪ Final result of the spanning tree is shown below
A B C
D E F
G
Thank you

More Related Content

PPTX
Graph traversals in Data Structures
PPTX
Bfs new
PPTX
Bfs new
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
PDF
graphtraversals.pdf
PPTX
BFS (Breadth First Search) Tree Traversal
PPTX
Breath first Search and Depth first search
PPTX
Data structure
Graph traversals in Data Structures
Bfs new
Bfs new
bfs tree searching ,sortingUntitled presentation.pptx
graphtraversals.pdf
BFS (Breadth First Search) Tree Traversal
Breath first Search and Depth first search
Data structure

Similar to Breadth First Search or BFS for a Graph traversal (20)

PPTX
Data structure note
PPT
14_Graph Traversalllllllllllllllllll.ppt
PPTX
BFS & DFS in Data Structure
PPTX
Breadth-First-Search algorithm with Code
PPTX
Breadth First Search with example and solutions
PPTX
Breadth First Searching Algorithm (BFS).pptx
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
PDF
Implement Breadth-First Search with a QueueSolution Program .pdf
PPTX
Lecture 16 graphs traversal
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
PPT
Graph traversal-BFS & DFS
PDF
Graph Traversals
PPTX
Sec B Graph traversal.pptx
PPTX
PPTX
breadth first traversal- tree traversal
PPT
8-Graph.ppt
PPTX
Topological Sort and BFS
PPTX
CS8391-Data Structures Unit 4
PPTX
DS - Graph Traversal
Data structure note
14_Graph Traversalllllllllllllllllll.ppt
BFS & DFS in Data Structure
Breadth-First-Search algorithm with Code
Breadth First Search with example and solutions
Breadth First Searching Algorithm (BFS).pptx
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
Implement Breadth-First Search with a QueueSolution Program .pdf
Lecture 16 graphs traversal
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
Graph traversal-BFS & DFS
Graph Traversals
Sec B Graph traversal.pptx
breadth first traversal- tree traversal
8-Graph.ppt
Topological Sort and BFS
CS8391-Data Structures Unit 4
DS - Graph Traversal
Ad

Recently uploaded (20)

PDF
Digital Logic Computer Design lecture notes
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
web development for engineering and engineering
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PPT on Performance Review to get promotions
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Artificial Intelligence
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
introduction to datamining and warehousing
Digital Logic Computer Design lecture notes
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
R24 SURVEYING LAB MANUAL for civil enggi
web development for engineering and engineering
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Automation-in-Manufacturing-Chapter-Introduction.pdf
Foundation to blockchain - A guide to Blockchain Tech
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT on Performance Review to get promotions
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx
Internet of Things (IOT) - A guide to understanding
Artificial Intelligence
Operating System & Kernel Study Guide-1 - converted.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
introduction to datamining and warehousing
Ad

Breadth First Search or BFS for a Graph traversal

  • 1. Objectives ▪ To understand the concept of BFS Traversal ▪ How to use BFS Traversal in solving the real world problems
  • 2. Breadth First Search Traversal(BFS) ▪ BFS traversal of a graph produces a spanning tree as final result. ▪ Spanning Tree is a graph without loops. ▪ We use Queue data structure with maximum size of total number of vertices in the graph to implement BFS traversal.
  • 3. Steps to implement BFS Traversal ▪ Step 1 - Define a Queue of size total number of vertices in the graph. ▪ Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue. ▪ Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and insert them into the Queue. ▪ Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue then delete that vertex. ▪ Step 5 - Repeat steps 3 and 4 until queue becomes empty. ▪ Step 6 - When queue becomes empty, then produce final spanning tree by removing unused edges from the graph
  • 4. Example Consider the following graph to perform BFS Traversal ◻Graph Traversals A B C D E F G
  • 5. BFS Traversal ▪ Step-1:Select the vertex A as starting vertex(visit A) ▪Insert A into the Queue A B C D E F G Queue A
  • 6. BFS Traversal ▪ Step-2:Visit the adjacent vertex of A which is not visited(D,E,B) ▪ Insert newly visited vertices into the Queue and delete A from the queue A B C D E F G Queue D E B
  • 7. BFS Traversal ▪ Step-3:Visit all adjacent vertices of D which are not visited(there is no vertex) ▪ Delete D from the queue A B C D E F G Queue E B
  • 8. BFS Traversal ▪ Step-4:Visit all the adjacent vertices of E which are not visited(C,F) ▪ Insert newly visited vertices into the Queue and delete E from the queue A B C D E F G Queue B C F
  • 9. BFS Traversal ▪ Step-5:Visit all the adjacent vertices of B which are not visited(there is no vertex) ▪ Delete B from the queue A B C D E F G Queue C F
  • 10. BFS Traversal ▪ Step-6:Visit all the adjacent vertices of C which are not visited(G) ▪ Insert newly visited vertex into the queue and delete C from the queue A B C D E F G Queue F G
  • 11. BFS Traversal ▪ Step-7:Visit all the adjacent vertices of F which are not visited(there is no vertex) ▪ Delete F from the queue A B C D E F G Queue G
  • 12. BFS Traversal ▪ Step-8:Visit all the adjacent vertices of G which are not visited(there is no vertex) ▪ Delete G from the queue A B C D E F G Queue
  • 13. BFS Traversal ▪ Queue becomes empty.so,stop the process of BFS ▪ Final result of the spanning tree is shown below A B C D E F G