SlideShare a Scribd company logo
1 of 11
Breadth-First Search 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
© 2004 Goodrich, Tamassia Breadth-First Search 1
Breadth-First Search 
Breadth-first search 
(BFS) is a general 
technique for traversing 
a graph 
A BFS traversal of a 
graph G 
 Visits all the vertices and 
edges of G 
 Determines whether G is 
connected 
 Computes the connected 
components of G 
 Computes a spanning 
forest of G 
BFS on a graph with n 
vertices and m edges 
takes O(n + m ) time 
BFS can be further 
extended to solve other 
graph problems 
 Find and report a path 
with the minimum 
number of edges 
between two given 
vertices 
 Find a simple cycle, if 
there is one 
© 2004 Goodrich, Tamassia Breadth-First Search 2
BFS Algorithm 
The algorithm uses a 
mechanism for setting and 
getting “labels” of vertices 
and edges 
Algorithm BFS(G, s) 
L0 ¬ new empty sequence 
L0.insertLast(s) 
setLabel(s, VISITED) 
i ¬ 0 
while ØLi.isEmpty() 
Li +1 ¬ new empty sequence 
for all v Î Li.elements() 
for all e Î G.incidentEdges(v) 
if getLabel(e) = UNEXPLORED 
w ¬ opposite(v,e) 
if getLabel(w) = UNEXPLORED 
setLabel(e, DISCOVERY) 
setLabel(w, VISITED) 
Li +1.insertLast(w) 
else 
setLabel(e, CROSS) 
i ¬ i +1 
Algorithm BFS(G) 
Input graph G 
Output labeling of the edges 
and partition of the 
vertices of G 
for all u Î G.vertices() 
setLabel(u, UNEXPLORED) 
for all e Î G.edges() 
setLabel(e, UNEXPLORED) 
for all v Î G.vertices() 
if getLabel(v) = UNEXPLORED 
BFS(G, v) 
© 2004 Goodrich, Tamassia Breadth-First Search 3
Example 
A unexplored vertex 
A visited vertex 
unexplored edge 
discovery edge 
cross edge 
A 
L0 
B C 
E 
D 
L1 
F 
A 
B C 
E 
A 
© 2004 Goodrich, Tamassia Breadth-First Search 4 
D 
L0 
L1 
F 
B C 
E 
D 
L0 
L1 
F
Example (cont.) 
A 
L0 
B C 
E 
D 
L1 
F 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
A 
B C 
E 
A 
© 2004 Goodrich, Tamassia Breadth-First Search 5 
D 
L0 
L1 
F 
L2 
B C 
E 
D 
L0 
L1 
F 
L2
Example (cont.) 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
© 2004 Goodrich, Tamassia Breadth-First Search 6
Properties 
Notation 
Gs: connected component of s 
Property 1 
BFS(G, s) visits all the vertices and 
edges of Gs 
Property 2 
The discovery edges labeled by 
BFS(G, s) form a spanning tree Ts of 
Gs 
Property 3 
For each vertex v in Li 
 The path of Ts from s to v has i 
edges 
 Every path from s to v in Gs has at 
least i edges 
A 
B C 
E 
A 
L0 
B C 
E 
D 
D 
L1 
F 
F 
L2 
© 2004 Goodrich, Tamassia Breadth-First Search 7
Analysis 
Setting/getting a vertex/edge label takes O(1) time 
Each vertex is labeled twice 
 once as UNEXPLORED 
 once as VISITED 
Each edge is labeled twice 
 once as UNEXPLORED 
 once as DISCOVERY or CROSS 
Each vertex is inserted once into a sequence Li 
Method incidentEdges is called once for each vertex 
BFS runs in O(n + m) time provided the graph is 
represented by the adjacency list structure 
 Recall that Sv deg(v) = 2m 
© 2004 Goodrich, Tamassia Breadth-First Search 8
Applications 
Using the template method pattern, we can 
specialize the BFS traversal of a graph G to 
solve the following problems in O(n + m) time 
 Compute the connected components of G 
 Compute a spanning forest of G 
 Find a simple cycle in G, or report that G is a 
forest 
 Given two vertices of G, find a path in G between 
them with the minimum number of edges, or report 
that no such path exists 
© 2004 Goodrich, Tamassia Breadth-First Search 9
DFS vs. BFS 
Applications DFS BFS 
Spanning forest, connected 
Ö Ö 
components, paths, cycles 
Shortest paths Ö 
Biconnected components Ö 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
A 
B C 
E 
D 
F 
DFS BFS 
© 2004 Goodrich, Tamassia Breadth-First Search 10
DFS vs. BFS (cont.) 
Back edge (v,w) 
 w is an ancestor of v in 
the tree of discovery 
edges 
Cross edge (v,w) 
 w is in the same level as 
v or in the next level in 
the tree of discovery 
edges 
A 
L0 
B C 
E 
D 
L1 
F 
L2 
A 
B C 
E 
D 
F 
DFS BFS 
© 2004 Goodrich, Tamassia Breadth-First Search 11

More Related Content

What's hot

Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure Ankit Kumar Singh
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)Dhrumil Panchal
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )Sazzad Hossain
 
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Md. Shafiuzzaman Hira
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First SearchNisha Soms
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
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
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsSujith Jay Nair
 

What's hot (20)

Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
BFS
BFSBFS
BFS
 
Depth First Search ( DFS )
Depth First Search ( DFS )Depth First Search ( DFS )
Depth First Search ( DFS )
 
Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)Graph Algorithms: Breadth-First Search (BFS)
Graph Algorithms: Breadth-First Search (BFS)
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
(148064384) bfs
(148064384) bfs(148064384) bfs
(148064384) bfs
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
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
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint paths
 

Similar to Bfs

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
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmA. S. M. Shafi
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras algdouglaslyon
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Traian Rebedea
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 
Testing Forest-Isomorphism in the Adjacency List Model
Testing Forest-Isomorphismin the Adjacency List ModelTesting Forest-Isomorphismin the Adjacency List Model
Testing Forest-Isomorphism in the Adjacency List Modelirrrrr
 
context free grammars automata therory and compiler design
context free grammars automata therory and compiler designcontext free grammars automata therory and compiler design
context free grammars automata therory and compiler designsunitachalageri1
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 

Similar to Bfs (20)

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
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
35 dijkstras alg
35 dijkstras alg35 dijkstras alg
35 dijkstras alg
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Temporal graph
Temporal graphTemporal graph
Temporal graph
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Network flows
Network flowsNetwork flows
Network flows
 
Graphs
GraphsGraphs
Graphs
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
09-graphs.ppt
09-graphs.ppt09-graphs.ppt
09-graphs.ppt
 
Algorithms Design Assignment Help
Algorithms Design Assignment HelpAlgorithms Design Assignment Help
Algorithms Design Assignment Help
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Testing Forest-Isomorphism in the Adjacency List Model
Testing Forest-Isomorphismin the Adjacency List ModelTesting Forest-Isomorphismin the Adjacency List Model
Testing Forest-Isomorphism in the Adjacency List Model
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
context free grammars automata therory and compiler design
context free grammars automata therory and compiler designcontext free grammars automata therory and compiler design
context free grammars automata therory and compiler design
 
Graps 2
Graps 2Graps 2
Graps 2
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 

Recently uploaded

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

Bfs

  • 1. Breadth-First Search A L0 B C E D L1 F L2 © 2004 Goodrich, Tamassia Breadth-First Search 1
  • 2. Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G  Visits all the vertices and edges of G  Determines whether G is connected  Computes the connected components of G  Computes a spanning forest of G BFS on a graph with n vertices and m edges takes O(n + m ) time BFS can be further extended to solve other graph problems  Find and report a path with the minimum number of edges between two given vertices  Find a simple cycle, if there is one © 2004 Goodrich, Tamassia Breadth-First Search 2
  • 3. BFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G, s) L0 ¬ new empty sequence L0.insertLast(s) setLabel(s, VISITED) i ¬ 0 while ØLi.isEmpty() Li +1 ¬ new empty sequence for all v Î Li.elements() for all e Î G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ¬ opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) setLabel(w, VISITED) Li +1.insertLast(w) else setLabel(e, CROSS) i ¬ i +1 Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u Î G.vertices() setLabel(u, UNEXPLORED) for all e Î G.edges() setLabel(e, UNEXPLORED) for all v Î G.vertices() if getLabel(v) = UNEXPLORED BFS(G, v) © 2004 Goodrich, Tamassia Breadth-First Search 3
  • 4. Example A unexplored vertex A visited vertex unexplored edge discovery edge cross edge A L0 B C E D L1 F A B C E A © 2004 Goodrich, Tamassia Breadth-First Search 4 D L0 L1 F B C E D L0 L1 F
  • 5. Example (cont.) A L0 B C E D L1 F A L0 B C E D L1 F L2 A B C E A © 2004 Goodrich, Tamassia Breadth-First Search 5 D L0 L1 F L2 B C E D L0 L1 F L2
  • 6. Example (cont.) A L0 B C E D L1 F L2 A L0 B C E D L1 F L2 A L0 B C E D L1 F L2 © 2004 Goodrich, Tamassia Breadth-First Search 6
  • 7. Properties Notation Gs: connected component of s Property 1 BFS(G, s) visits all the vertices and edges of Gs Property 2 The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs Property 3 For each vertex v in Li  The path of Ts from s to v has i edges  Every path from s to v in Gs has at least i edges A B C E A L0 B C E D D L1 F F L2 © 2004 Goodrich, Tamassia Breadth-First Search 7
  • 8. Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice  once as UNEXPLORED  once as VISITED Each edge is labeled twice  once as UNEXPLORED  once as DISCOVERY or CROSS Each vertex is inserted once into a sequence Li Method incidentEdges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure  Recall that Sv deg(v) = 2m © 2004 Goodrich, Tamassia Breadth-First Search 8
  • 9. Applications Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time  Compute the connected components of G  Compute a spanning forest of G  Find a simple cycle in G, or report that G is a forest  Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists © 2004 Goodrich, Tamassia Breadth-First Search 9
  • 10. DFS vs. BFS Applications DFS BFS Spanning forest, connected Ö Ö components, paths, cycles Shortest paths Ö Biconnected components Ö A L0 B C E D L1 F L2 A B C E D F DFS BFS © 2004 Goodrich, Tamassia Breadth-First Search 10
  • 11. DFS vs. BFS (cont.) Back edge (v,w)  w is an ancestor of v in the tree of discovery edges Cross edge (v,w)  w is in the same level as v or in the next level in the tree of discovery edges A L0 B C E D L1 F L2 A B C E D F DFS BFS © 2004 Goodrich, Tamassia Breadth-First Search 11