SlideShare a Scribd company logo
2
Most read
6
Most read
7
Most read
BFS
Breadth-first search
 Breadth-first search (BFS) is an algorithm for traversing
or searching tree or graph data structures.
 It starts at the tree root (or some arbitrary node of a
graph, sometimes referred to as a ‘search key’), and
explores all of the neighbor nodes at the present depth
prior to moving on to the nodes at the next depth level.
 It is implemented using a queue(FIFO).
 BFS traverses the tree “shallowest node first”, it would
always pick the shallower branch until it reaches the
solution (or it runs out of nodes, and goes to the next
branch).
Algorithm
3
 Declare a queue and insert the starting vertex.
 Initialize a visited array and mark the starting vertex as
visited.
 Follow the below process till the queue becomes empty:
 Remove the first vertex of the queue.
 Mark that vertex as visited.
 Insert all the unvisited neighbors of the vertex into the
queue.
⚫Consider the Following Example
Example-1
Breadth First Search- STEP1
 Choose the Starting Vertex as A
 VISIT A
 Insert A into the Queue
A
QUEUE:
Breadth First Search- STEP2
⚫Visit all adjacent vertices of A which
are not visited(Here: D,E,B)
⚫Insert newly visited vertices into the queue
and delete A from the Queue
D E B
QUEUE:
RESULT: A
Breadth First Search- STEP3
⚫Visit all adjacent vertices of D which
are not visited (No Vertex)
⚫Delete D from the Queue
E B
QUEUE:
RESULT: A D
Breadth First Search- STEP4
⚫ Visit all adjacent vertices of E which are not visited(Here
C, F)
⚫ Insert newly visited vertices into the queue and delete E
from the Queue
B C F
QUEUE:
RESULT: A D E
Breadth First Search- STEP5
⚫Visit all adjacent vertices of B which
are not visited(No Vertex)
⚫Delete B from the Queue
C F
QUEUE:
RESULT: A D E B
Breadth First Search- STEP6
⚫ Visit all adjacent vertices of C which are
not visited(Here G)
⚫ Insert newly visited vertices into the queue and delete
C from the Queue
F G
QUEUE:
RESULT: A D E B C
Breadth First Search- STEP7
⚫ Visit all adjacent vertices of F which are not
visited(Here No Vertex)
⚫ Delete F from the Queue
G
QUEUE:
RESULT: A D E B C F
Breadth First Search- STEP8
⚫Visit all adjacent vertices of G which are
not visited(Here No Vertex)
⚫Delete G from the Queue
QUEUE:
Queue Empty, End the
Process
RESULT: A D E B C F G
BFS Traversal - Result
AD EBCF G
14
Advantages
• BFS will provide a solution if any solution exists.
• If there are more than one solution for a given problem, then BFS
will provide minimum solution which requires the least number of
steps.
Disadvantage
• It requires lot of memory since each level of the tree must be saved
into memory to expand the next level.
• BFS needs lot of time if the solution is far a way from the root.
Example
15
 BFS Output will be ?
 Output of BFS : 1 2 3 4 5 6 7 8 9 10 11 12
16
17
Algorithm of BFS
18
19
Time complexity: Equivalent to the number of nodes
traversed in BFS until the shallowest solution.
T(n) = 1 + n^2 + n^3 + ... + n^s = O(n^s)
• s = the depth of the shallowest solution.
• n^i = number of nodes in level i.
Space complexity: Equivalent to how large can the fringe
get.
S(n) = O(n^s)
20
• Completeness: BFS is complete, meaning for a given search
tree, BFS will come up with a solution if it exists.
• Optimality: BFS is optimal as long as the costs of all edges
are equal.
21
22
23

More Related Content

PPTX
Discrete Mathematics Tree
PPTX
Natural Language Processing and Python
PPT
Tree and Binary Search tree
PPT
Bnf and ambiquity
PDF
Ai lab manual
PDF
Head first android_development
PDF
DBMS Unit 2 - Relational Model
PPTX
non linear data structure -introduction of tree
Discrete Mathematics Tree
Natural Language Processing and Python
Tree and Binary Search tree
Bnf and ambiquity
Ai lab manual
Head first android_development
DBMS Unit 2 - Relational Model
non linear data structure -introduction of tree

What's hot (20)

PDF
Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
PPTX
While petlja
PPTX
Minimization of DFA.pptx
PPT
Avl trees
PPTX
Nltk
PPTX
1.7. eqivalence of nfa and dfa
PDF
Natural Language Processing (NLP)
PPT
Architecture of Linux
PPTX
Prolog Programming : Basics
PDF
Ai lecture 7(unit02)
PPTX
Backus Naur and Chomsky Normal Forms
PPT
Top down parsing
PPTX
heap Sort Algorithm
PDF
Countdown to the singularity
PPTX
Discrete Mathematics Presentation
PPTX
Single source Shortest path algorithm with example
PPTX
Aspect Based Sentiment analysis of Afaan Oromoo Movie reviews using machine l...
PPT
5 csp
PDF
IE: Named Entity Recognition (NER)
PPTX
Compilers
Introduction to the Theory of Computation, Winter 2003 A. Hevia and J. Mao S...
While petlja
Minimization of DFA.pptx
Avl trees
Nltk
1.7. eqivalence of nfa and dfa
Natural Language Processing (NLP)
Architecture of Linux
Prolog Programming : Basics
Ai lecture 7(unit02)
Backus Naur and Chomsky Normal Forms
Top down parsing
heap Sort Algorithm
Countdown to the singularity
Discrete Mathematics Presentation
Single source Shortest path algorithm with example
Aspect Based Sentiment analysis of Afaan Oromoo Movie reviews using machine l...
5 csp
IE: Named Entity Recognition (NER)
Compilers
Ad

Similar to Breadth First Search with example and solutions (20)

PPTX
Introduction of Depth First Search with example
PPTX
Breadth First Search or BFS for a Graph traversal
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
PPTX
Problem Solving through Search - Uninformed Search
PPTX
Lecture 12 Heuristic Searches
PPTX
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
PPTX
Lec 15-graph Searching in data structure and lgorithm.pptx
PPTX
Bfs and dfs
PPTX
Uninformed search /Blind search in AI
PPT
AI-search-metodsandeverythingelsenot.ppt
PPTX
Final slide4 (bsc csit) chapter 4
PPTX
Artificial Intelligence uninformed Search techniques.pptx
PPTX
6CS4_AI_Unit-1.pptx helo to leairn dsa in a eay
PPTX
RPT_AI_03_PartB_UNINFORMED_FINAL.pptx
PDF
Lecture 16 - Dijkstra's Algorithm.pdf
PPTX
Graph traversals in Data Structures
PDF
graphtraversals.pdf
PPTX
uninformed search part 1.pptx
PDF
Trees second part in data structures with examples
PPTX
Depth first Search.pptx
Introduction of Depth First Search with example
Breadth First Search or BFS for a Graph traversal
bfs tree searching ,sortingUntitled presentation.pptx
Problem Solving through Search - Uninformed Search
Lecture 12 Heuristic Searches
PPT ON INTRODUCTION TO AI- UNIT-1-PART-2.pptx
Lec 15-graph Searching in data structure and lgorithm.pptx
Bfs and dfs
Uninformed search /Blind search in AI
AI-search-metodsandeverythingelsenot.ppt
Final slide4 (bsc csit) chapter 4
Artificial Intelligence uninformed Search techniques.pptx
6CS4_AI_Unit-1.pptx helo to leairn dsa in a eay
RPT_AI_03_PartB_UNINFORMED_FINAL.pptx
Lecture 16 - Dijkstra's Algorithm.pdf
Graph traversals in Data Structures
graphtraversals.pdf
uninformed search part 1.pptx
Trees second part in data structures with examples
Depth first Search.pptx
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
DOCX
573137875-Attendance-Management-System-original
PPTX
Artificial Intelligence
PDF
Well-logging-methods_new................
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
composite construction of structures.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Sustainable Sites - Green Building Construction
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
additive manufacturing of ss316l using mig welding
PPT
introduction to datamining and warehousing
Lecture Notes Electrical Wiring System Components
573137875-Attendance-Management-System-original
Artificial Intelligence
Well-logging-methods_new................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
composite construction of structures.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Foundation to blockchain - A guide to Blockchain Tech
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Sustainable Sites - Green Building Construction
Automation-in-Manufacturing-Chapter-Introduction.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
CH1 Production IntroductoryConcepts.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
additive manufacturing of ss316l using mig welding
introduction to datamining and warehousing

Breadth First Search with example and solutions

  • 1. BFS
  • 2. Breadth-first search  Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures.  It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a ‘search key’), and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.  It is implemented using a queue(FIFO).  BFS traverses the tree “shallowest node first”, it would always pick the shallower branch until it reaches the solution (or it runs out of nodes, and goes to the next branch).
  • 3. Algorithm 3  Declare a queue and insert the starting vertex.  Initialize a visited array and mark the starting vertex as visited.  Follow the below process till the queue becomes empty:  Remove the first vertex of the queue.  Mark that vertex as visited.  Insert all the unvisited neighbors of the vertex into the queue.
  • 4. ⚫Consider the Following Example Example-1
  • 5. Breadth First Search- STEP1  Choose the Starting Vertex as A  VISIT A  Insert A into the Queue A QUEUE:
  • 6. Breadth First Search- STEP2 ⚫Visit all adjacent vertices of A which are not visited(Here: D,E,B) ⚫Insert newly visited vertices into the queue and delete A from the Queue D E B QUEUE: RESULT: A
  • 7. Breadth First Search- STEP3 ⚫Visit all adjacent vertices of D which are not visited (No Vertex) ⚫Delete D from the Queue E B QUEUE: RESULT: A D
  • 8. Breadth First Search- STEP4 ⚫ Visit all adjacent vertices of E which are not visited(Here C, F) ⚫ Insert newly visited vertices into the queue and delete E from the Queue B C F QUEUE: RESULT: A D E
  • 9. Breadth First Search- STEP5 ⚫Visit all adjacent vertices of B which are not visited(No Vertex) ⚫Delete B from the Queue C F QUEUE: RESULT: A D E B
  • 10. Breadth First Search- STEP6 ⚫ Visit all adjacent vertices of C which are not visited(Here G) ⚫ Insert newly visited vertices into the queue and delete C from the Queue F G QUEUE: RESULT: A D E B C
  • 11. Breadth First Search- STEP7 ⚫ Visit all adjacent vertices of F which are not visited(Here No Vertex) ⚫ Delete F from the Queue G QUEUE: RESULT: A D E B C F
  • 12. Breadth First Search- STEP8 ⚫Visit all adjacent vertices of G which are not visited(Here No Vertex) ⚫Delete G from the Queue QUEUE: Queue Empty, End the Process RESULT: A D E B C F G
  • 13. BFS Traversal - Result AD EBCF G
  • 14. 14 Advantages • BFS will provide a solution if any solution exists. • If there are more than one solution for a given problem, then BFS will provide minimum solution which requires the least number of steps. Disadvantage • It requires lot of memory since each level of the tree must be saved into memory to expand the next level. • BFS needs lot of time if the solution is far a way from the root.
  • 15. Example 15  BFS Output will be ?  Output of BFS : 1 2 3 4 5 6 7 8 9 10 11 12
  • 16. 16
  • 17. 17
  • 19. 19 Time complexity: Equivalent to the number of nodes traversed in BFS until the shallowest solution. T(n) = 1 + n^2 + n^3 + ... + n^s = O(n^s) • s = the depth of the shallowest solution. • n^i = number of nodes in level i. Space complexity: Equivalent to how large can the fringe get. S(n) = O(n^s)
  • 20. 20 • Completeness: BFS is complete, meaning for a given search tree, BFS will come up with a solution if it exists. • Optimality: BFS is optimal as long as the costs of all edges are equal.
  • 21. 21
  • 22. 22
  • 23. 23