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

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
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
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
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
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 

Recently uploaded (20)

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
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 )
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
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
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
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
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

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