SlideShare a Scribd company logo
1 of 15
Welcome to Data Structure and
Algorithm Presentation
Breadth First Search (BFS)
By,
R.ISHWARIYA, M.sc(cs).,
Breadth First Search (BFS)
Breadth first search is a graph traversal algorithm that
starts traversing the graph from root node and explores
all the neighbouring nodes. Then, it selects the nearest
node and explore all the unexplored nodes.
The algorithm follows the same process for each of the
nearest node until it finds the goal.
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.
Breadth First Search (BFS)
 Breadth-first search starts at a given vertex s, which is at
level 0.
 In the first stage, we visit all the vertices that are at the
distance of one edge away. When we visit there, we
paint as "visited," the vertices adjacent to the start vertex
s - these vertices are placed into level 1.
Breadth First Search (BFS) Conti…..
 In the second stage, we visit all the new vertices we can
reach at the distance of two edges away from the source
vertex s. These new vertices, which are adjacent to level
1 vertices and not previously assigned to a level, are
placed into level 2, and so on.
 The BFS traversal terminates when every vertex has
been visited.
Breadth First Search (BFS)
 The algorithm maintains a queue Q to manage the set of
vertices and starts with s, the source vertex.
 Initially, all vertices except s are colored white, and s is gray.
 BFS algorithm maintains the following information for each
vertex u:
color[u] (white, gray, or black) : indicates status
- white = not discovered yet
- gray = discovered, but not finished
- black = finished
- d[u] : distance from s to u
- π[u] : predecessor of u in BF tree
Each vertex is assigned a color.
In general, a vertex is white before we start processing it, it is gray during the period
the vertex is being processed, and it is black after the processing of the vertex is completed.
BFS Algorithm
 Inputs: Inputs are a graph(directed or undirected) G =(V, E) and a
source vertex s, where s is an element of V. The adjacency list
representation of a graph is used in this analysis.
 Outputs: The outputs are a predecessor graph, which represents
the paths travelled in the BFS traversal, and a collection of
distances, which represent the distance of each of the vertices
from the source vertex.
BFS Algorithm
1. for each u in V − {s}
2. do color[u] ← WHITE
3. d[u] ← infinity
4. π[u] ← NIL
5. color[s] ← GRAY
6. d[s] ← 0
7. π[s] ← NIL
8. Q ← {}
9. ENQUEUE(Q, s)
10. while Q is non-empty
11. do u ← DEQUEUE(Q)
12. for each v adjacent to u
13. do if color[v] ← WHITE
14. then color[v] ← GRAY
15. d[v] ← d[u] + 1
16. π[v] ← u
17. ENQUEUE(Q, v)
18. DEQUEUE(Q)
19. color[u] ← BLACK
1. Enqueue (Q,s)
2. while Q ≠
3. do u Dequeue(Q)
4. for each v adjacent to u
5. do if color[v] = white
6. then color[v] gray
7. d[v] d[u] + 1
8. [v] u
9. Enqueue(Q,v)
10. color[u] black
Note: If G is not connected, then BFS will not visit the entire graph
(without some extra provisions in the algorithm)
EXAMPLE: BFS FOR DIRECTED GRAPH
EXAMPLE: BFS FOR UNDIRECTED
GRAPH
Analysis of Breadth-First Search
 Shortest-path distance (s,v) : minimum number of edges in any
path from vertex s to v. If no path exists from s to v, then (s,v) =
∞
 The ultimate goal of the proof of correctness is to show that d[v]
= (s,v) when the algorithm is done and that a path is found from s
to all reachable vertices.
Applications of BFS
Connected components
 Bipartite
Breadth first search (Bfs)

More Related Content

What's hot (20)

Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Breadth first search signed
Breadth first search signedBreadth first search signed
Breadth first search signed
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Linked list
Linked listLinked list
Linked list
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Unit 3 dsa LINKED LIST
Unit 3 dsa LINKED LISTUnit 3 dsa LINKED LIST
Unit 3 dsa LINKED LIST
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 

Similar to Breadth first search (Bfs)

BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
ada-191015145338.pdf
ada-191015145338.pdfada-191015145338.pdf
ada-191015145338.pdfAshwin180668
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Deepak John
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchzukun
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Technical_Seminar .pptx
Technical_Seminar .pptxTechnical_Seminar .pptx
Technical_Seminar .pptxForJunks1
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxrandyburney60861
 
bfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxbfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxsaurabhpandey679381
 

Similar to Breadth first search (Bfs) (20)

BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
ada-191015145338.pdf
ada-191015145338.pdfada-191015145338.pdf
ada-191015145338.pdf
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Graps 2
Graps 2Graps 2
Graps 2
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 
Graphs
GraphsGraphs
Graphs
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first search
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Technical_Seminar .pptx
Technical_Seminar .pptxTechnical_Seminar .pptx
Technical_Seminar .pptx
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
bfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptxbfs tree searching ,sortingUntitled presentation.pptx
bfs tree searching ,sortingUntitled presentation.pptx
 

More from Ishucs

Renuga
RenugaRenuga
RenugaIshucs
 
Thresholding
ThresholdingThresholding
ThresholdingIshucs
 
Ishwariya
IshwariyaIshwariya
IshwariyaIshucs
 
Image compression
Image compressionImage compression
Image compressionIshucs
 
Deepika
DeepikaDeepika
DeepikaIshucs
 
Renuga
RenugaRenuga
RenugaIshucs
 
Soundharya
SoundharyaSoundharya
SoundharyaIshucs
 
Lavanya
LavanyaLavanya
LavanyaIshucs
 
M.srinandhini
M.srinandhiniM.srinandhini
M.srinandhiniIshucs
 
Deepika t
Deepika tDeepika t
Deepika tIshucs
 
Sragavi (1)
Sragavi (1)Sragavi (1)
Sragavi (1)Ishucs
 
Ishwariya
IshwariyaIshwariya
IshwariyaIshucs
 
Software enginnering
Software enginneringSoftware enginnering
Software enginneringIshucs
 
Partial redundancy elimination
Partial redundancy eliminationPartial redundancy elimination
Partial redundancy eliminationIshucs
 
Software engineering
Software engineeringSoftware engineering
Software engineeringIshucs
 
Web programming
Web programmingWeb programming
Web programmingIshucs
 
Big data
Big dataBig data
Big dataIshucs
 
Affine array index
Affine array indexAffine array index
Affine array indexIshucs
 

More from Ishucs (20)

Renuga
RenugaRenuga
Renuga
 
Snega
SnegaSnega
Snega
 
Thresholding
ThresholdingThresholding
Thresholding
 
Ishwariya
IshwariyaIshwariya
Ishwariya
 
Image compression
Image compressionImage compression
Image compression
 
Deepika
DeepikaDeepika
Deepika
 
Snega
SnegaSnega
Snega
 
Renuga
RenugaRenuga
Renuga
 
Soundharya
SoundharyaSoundharya
Soundharya
 
Lavanya
LavanyaLavanya
Lavanya
 
M.srinandhini
M.srinandhiniM.srinandhini
M.srinandhini
 
Deepika t
Deepika tDeepika t
Deepika t
 
Sragavi (1)
Sragavi (1)Sragavi (1)
Sragavi (1)
 
Ishwariya
IshwariyaIshwariya
Ishwariya
 
Software enginnering
Software enginneringSoftware enginnering
Software enginnering
 
Partial redundancy elimination
Partial redundancy eliminationPartial redundancy elimination
Partial redundancy elimination
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Web programming
Web programmingWeb programming
Web programming
 
Big data
Big dataBig data
Big data
 
Affine array index
Affine array indexAffine array index
Affine array index
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

Breadth first search (Bfs)

  • 1. Welcome to Data Structure and Algorithm Presentation Breadth First Search (BFS) By, R.ISHWARIYA, M.sc(cs).,
  • 2. Breadth First Search (BFS) Breadth first search is a graph traversal algorithm that starts traversing the graph from root node and explores all the neighbouring nodes. Then, it selects the nearest node and explore all the unexplored nodes. The algorithm follows the same process for each of the nearest node until it finds the goal.
  • 3. 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.
  • 4. Breadth First Search (BFS)  Breadth-first search starts at a given vertex s, which is at level 0.  In the first stage, we visit all the vertices that are at the distance of one edge away. When we visit there, we paint as "visited," the vertices adjacent to the start vertex s - these vertices are placed into level 1.
  • 5. Breadth First Search (BFS) Conti…..  In the second stage, we visit all the new vertices we can reach at the distance of two edges away from the source vertex s. These new vertices, which are adjacent to level 1 vertices and not previously assigned to a level, are placed into level 2, and so on.  The BFS traversal terminates when every vertex has been visited.
  • 6. Breadth First Search (BFS)  The algorithm maintains a queue Q to manage the set of vertices and starts with s, the source vertex.  Initially, all vertices except s are colored white, and s is gray.  BFS algorithm maintains the following information for each vertex u:
  • 7. color[u] (white, gray, or black) : indicates status - white = not discovered yet - gray = discovered, but not finished - black = finished - d[u] : distance from s to u - π[u] : predecessor of u in BF tree Each vertex is assigned a color. In general, a vertex is white before we start processing it, it is gray during the period the vertex is being processed, and it is black after the processing of the vertex is completed.
  • 8. BFS Algorithm  Inputs: Inputs are a graph(directed or undirected) G =(V, E) and a source vertex s, where s is an element of V. The adjacency list representation of a graph is used in this analysis.  Outputs: The outputs are a predecessor graph, which represents the paths travelled in the BFS traversal, and a collection of distances, which represent the distance of each of the vertices from the source vertex.
  • 9. BFS Algorithm 1. for each u in V − {s} 2. do color[u] ← WHITE 3. d[u] ← infinity 4. π[u] ← NIL 5. color[s] ← GRAY 6. d[s] ← 0 7. π[s] ← NIL 8. Q ← {} 9. ENQUEUE(Q, s) 10. while Q is non-empty 11. do u ← DEQUEUE(Q) 12. for each v adjacent to u 13. do if color[v] ← WHITE 14. then color[v] ← GRAY 15. d[v] ← d[u] + 1 16. π[v] ← u 17. ENQUEUE(Q, v) 18. DEQUEUE(Q) 19. color[u] ← BLACK
  • 10. 1. Enqueue (Q,s) 2. while Q ≠ 3. do u Dequeue(Q) 4. for each v adjacent to u 5. do if color[v] = white 6. then color[v] gray 7. d[v] d[u] + 1 8. [v] u 9. Enqueue(Q,v) 10. color[u] black Note: If G is not connected, then BFS will not visit the entire graph (without some extra provisions in the algorithm)
  • 11. EXAMPLE: BFS FOR DIRECTED GRAPH
  • 12. EXAMPLE: BFS FOR UNDIRECTED GRAPH
  • 13. Analysis of Breadth-First Search  Shortest-path distance (s,v) : minimum number of edges in any path from vertex s to v. If no path exists from s to v, then (s,v) = ∞  The ultimate goal of the proof of correctness is to show that d[v] = (s,v) when the algorithm is done and that a path is found from s to all reachable vertices.
  • 14. Applications of BFS Connected components  Bipartite