SlideShare a Scribd company logo
Breadth-First Searc
hL
L
1

0
B

L
2

© 2004

G

drich, Tamassia

A

Breadth-First
Search

C

E

D

F

1
Breadth-First Searc (§ 12.3.3)
Breadth-first search h
BF on a graph ith n
w
(BFS) is a general
nique for traversing
te
ch
a graph
ABF traversal o a
graph G
S

f
v rtices and
edges of G
e
„ Determines whether G is
„ Visits all the

connected
„ Computes the connected
components of G
„ Computes a spanning
forest of G
© 2004

G

drich, Tamassia

Breadth-First
Search

vertices and m edges
S
ta s O(n + m ) time
ke
s
BF can be further
extended to solve other
S
graph problems
Find and report a p t
with the minimum
number of edges a h
between two given
vertices
„ Find a simple cycle, if
there is one
„

2
BF Algorithm
The algorithm uses a
S mechanism for setting and
getting “labels” of vertices
of
and edges

Algorithm BFS(G)
Input graph G
Output labeling of the edges
and partition of the
vertices of G
for all u ∈
G.ertce()
setLabel(u, UNEXPLORED)
for all e ∈ G.edges()
∈ v i s
setLabel(e, UNEXPLORED)
for all v ∈ G.vertices()
∈
if getLabel(v) = UNEXPLORED
BFS(G, v)
© 2004

G

drich, Tamassia

Algorithm BFS(G, s)
L
L0 ← new empty
L0.insertL )
bel(s, VISITED
sequence
se 0 be(s
tLa
i←
ast(s)

l
while ,new empty sequence
←
Lii.isEmpty()
for
¬L all v ∈∈
i
+1

Breadth-First
Search

for all e ∈ G.incidentEdges(v)
if Li.elements
getLabel(e)
=
← opposite(v,e)
()
w←
UNEXPLO
if getLabel(w) =
setLabel(e, DISCOVERY)
RED
setLabel(w, VISITED)
w, UNEXPLO
Li
RED
Li
else
setLabel(e, CROSS
+1.insertLas )
e,
i ← i +1

i

t(w)

3
Example
unexplored vertex
visited vertex
unexplored edge
scovery edge
di
cross edge

A
A

L
L
1

0

G

L
1

B

B

L
C

drich, Tamassia

0

D
F

A
C
E

A

E
© 2004

L

L
1

0

F

A

B

C
E

Breadth-First
Search

D

D
F
4
Example (cont.)
L
L

0

B

1

L

A

0

C
E

L
L
1

0

2
© 2004

G

L

F

drich, Tamassia

C

2

D

L

E

L
C

E

B

1

A

B

L

D

L

A

A

0

B

1

L

F

2
Breadth-First
Search

D
F

C
E

D
F
5
Example (cont.)
L

L
L
1

0
B

C

L
L

E

L
1

0

B

L
2

© 2004

G

D

L
1

0

A

B

C

D

A

2

L

L

A

drich, Tamassia

L

F

C
E

2

E

F

D
F
Breadth-First
Search

6
Properties
Notation

G s:
Property
B
connecte all the vertices and
1
BFS(G, s) visits
d
edges
Property
of Gs 2
The discovery
la eled by
BFS(G, s) form a spanning tree
edges
L0
b
of G
Ts
Property 3
s
ach
L B
F r e vertex v in
„ The p h of Ts s to v has i 1
to
o edges
Li
L
2
at from
Every path from s to v in
„

le ii edges
a
© 2004
G drich, Tamassia
st

component of s

inGs a
has t
Breadth-First
Search

A
C
E

D
F

A
C
E

D
F
7
Analysi
s Setting/getting a vertex/edge
Each vertex is labeled twice

label takes O(1) time

O

a UNEXPLORE
aD
s VISITED
Each edge is
s UNEXPLOREtwice
labeled
„ once
aD
once
a DISCOVERYoor
s CROSS
Each vertex is inserted once into a sequen
s
r is called
Method incidentEdges
for each vertex
„ once
„ once

„

once
BFS
the Li
O + m) time providedce graph
i O the adjacency list structure
( +
represented by
runs
„

© 2004

G

n that Σv
n
Recall
deg(v) = 2m

drich, Tamassia

Breadth-First
Search

i
s
8
Applications
Using the template method pattern, we can
specialize the BFS traversal of a
G to
solve
graphthe 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
G number edges, or
them wi the mi
nimum
t
o
report that no such path exists

h

© 2004

G

drich, Tamassia

f

Breadth-First
Search

9
DF vs. BF
S
S
Applications
Spanning forest, connected
components, paths,
cycles
Shortest paths

DF
S

√
√

L

A

C
E

© 2004

DF
S
G drich, Tamassia

D
F

L
1

0

A

B

L
2

Breadth-First
Search

√
√

Biconnected components

B

BF
S

C
E

BF
S

D
F
10
DF vs. BF (cont.)
S edge (v,w)
S
Back
s edge ,w)
Cr swi i the, same level as
os
(v
ancestor of v in
wi a
„

„

next
vo n thew level in
i
s
the tree of discovery
r n
edges

the tree of discovery
sn
edges

L

A
B

C
E

© 2004

G

DF
S
drich, Tamassia

D
F

L
1

0

B

L
2

Breadth-First
Search

A
C
E

BF
S

D
F
11

More Related Content

PPT
Algorithm to count number of disjoint paths
DOCX
Write a program to draw a cubic Bezier curve.
PPTX
Chirp z algorithm 1
PPT
Cryptography and network security
PPTX
Graph theory basics
PPTX
Asymptotic Notation
PPT
Graphing day 2 worked
PDF
Network flow problems
Algorithm to count number of disjoint paths
Write a program to draw a cubic Bezier curve.
Chirp z algorithm 1
Cryptography and network security
Graph theory basics
Asymptotic Notation
Graphing day 2 worked
Network flow problems

What's hot (20)

PPT
Math(F5) Gradient And Area Under A Graph 6.2
PDF
PPTX
Bellmanfordwith negative cycle js
PPT
4.7 inverse functions.ppt worked
PPT
Hprec8 4
PPTX
Num Integration
PDF
[Question Paper] Computer Graphics (Old Course) [September / 2013]
PPT
B field homogenous sphere
PPT
Graphing day 1 worked
PPT
B field conducting sphere
PPT
0015.register allocation-graph-coloring
PDF
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
PPT
香港六合彩
DOCX
Math tcwag 6, p 248, no 26 27
PPT
Mensuration PPT - Class Project
PPT
CST 504 Area Probability
PPT
Results from WZ, Higgs and Top for the Summer Conferences
PPTX
Closest point on OBB to point by j3j3bu5t3r5
PPTX
Lecture 12 Heuristic Searches
PPTX
Asymptotic notation
Math(F5) Gradient And Area Under A Graph 6.2
Bellmanfordwith negative cycle js
4.7 inverse functions.ppt worked
Hprec8 4
Num Integration
[Question Paper] Computer Graphics (Old Course) [September / 2013]
B field homogenous sphere
Graphing day 1 worked
B field conducting sphere
0015.register allocation-graph-coloring
[Question Paper] Fundamentals of Digital Computing (Revised Course) [April / ...
香港六合彩
Math tcwag 6, p 248, no 26 27
Mensuration PPT - Class Project
CST 504 Area Probability
Results from WZ, Higgs and Top for the Summer Conferences
Closest point on OBB to point by j3j3bu5t3r5
Lecture 12 Heuristic Searches
Asymptotic notation
Ad

Viewers also liked (17)

PPT
PPTX
Caqa5e ch4
PDF
Visual studio-2012-product-guide
PPT
Advancedrn
PDF
Going beyond-data-and-analytics-v4
PDF
worklight_development_environment
PPT
Role of locking- cds
DOCX
Android sql examples
PDF
Big data trendsdirections nimführ.ppt
PDF
Hans enocson how big data creates opportunities for productivity improvements...
DOCX
Intellij idea features
PPT
Introto netthreads-090906214344-phpapp01
PPT
Collaborative filtering hyoungtae cho
DOCX
Asp controls
PPT
Caqa5e ch4
Visual studio-2012-product-guide
Advancedrn
Going beyond-data-and-analytics-v4
worklight_development_environment
Role of locking- cds
Android sql examples
Big data trendsdirections nimführ.ppt
Hans enocson how big data creates opportunities for productivity improvements...
Intellij idea features
Introto netthreads-090906214344-phpapp01
Collaborative filtering hyoungtae cho
Asp controls
Ad

Similar to (148064384) bfs (20)

PPT
Bfs
PPTX
Presentation on Breadth First Search (BFS)
DOC
BFS, Breadth first search | Search Traversal Algorithm
PPTX
Breadth-First Search and Depth-First Search.pptx
PPTX
Breadth first search (Bfs)
PPTX
bfs tree searching ,sortingUntitled presentation.pptx
PPTX
Algorithm Design and Complexity - Course 7
PPTX
Understanding Breadth First Search (BFS) Algorithm
PPT
Graphs bfs dfs
PPT
Chapter 23 aoa
PDF
Bfs dfs
PDF
Skiena algorithm 2007 lecture11 breadth deapth first search
PDF
U1 L5 DAA.pdf
PPTX
Technical_Seminar .pptx
PPTX
kumattt).pptx
PPTX
Graphs in Data Structure
PPT
Breadth first search
PPTX
DFS & BFS in Computer Algorithm
PPTX
Basic Graph Algorithms Vertex (Node): lk
PDF
Breadth First Search and Depth First Search Algorithm
Bfs
Presentation on Breadth First Search (BFS)
BFS, Breadth first search | Search Traversal Algorithm
Breadth-First Search and Depth-First Search.pptx
Breadth first search (Bfs)
bfs tree searching ,sortingUntitled presentation.pptx
Algorithm Design and Complexity - Course 7
Understanding Breadth First Search (BFS) Algorithm
Graphs bfs dfs
Chapter 23 aoa
Bfs dfs
Skiena algorithm 2007 lecture11 breadth deapth first search
U1 L5 DAA.pdf
Technical_Seminar .pptx
kumattt).pptx
Graphs in Data Structure
Breadth first search
DFS & BFS in Computer Algorithm
Basic Graph Algorithms Vertex (Node): lk
Breadth First Search and Depth First Search Algorithm

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Understanding_Digital_Forensics_Presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine learning based COVID-19 study performance prediction
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Monthly Chronicles - July 2025
Understanding_Digital_Forensics_Presentation.pptx
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation

(148064384) bfs

  • 1. Breadth-First Searc hL L 1 0 B L 2 © 2004 G drich, Tamassia A Breadth-First Search C E D F 1
  • 2. Breadth-First Searc (§ 12.3.3) Breadth-first search h BF on a graph ith n w (BFS) is a general nique for traversing te ch a graph ABF traversal o a graph G S f v rtices and edges of G e „ Determines whether G is „ Visits all the connected „ Computes the connected components of G „ Computes a spanning forest of G © 2004 G drich, Tamassia Breadth-First Search vertices and m edges S ta s O(n + m ) time ke s BF can be further extended to solve other S graph problems Find and report a p t with the minimum number of edges a h between two given vertices „ Find a simple cycle, if there is one „ 2
  • 3. BF Algorithm The algorithm uses a S mechanism for setting and getting “labels” of vertices of and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u ∈ G.ertce() setLabel(u, UNEXPLORED) for all e ∈ G.edges() ∈ v i s setLabel(e, UNEXPLORED) for all v ∈ G.vertices() ∈ if getLabel(v) = UNEXPLORED BFS(G, v) © 2004 G drich, Tamassia Algorithm BFS(G, s) L L0 ← new empty L0.insertL ) bel(s, VISITED sequence se 0 be(s tLa i← ast(s) l while ,new empty sequence ← Lii.isEmpty() for ¬L all v ∈∈ i +1 Breadth-First Search for all e ∈ G.incidentEdges(v) if Li.elements getLabel(e) = ← opposite(v,e) () w← UNEXPLO if getLabel(w) = setLabel(e, DISCOVERY) RED setLabel(w, VISITED) w, UNEXPLO Li RED Li else setLabel(e, CROSS +1.insertLas ) e, i ← i +1 i t(w) 3
  • 4. Example unexplored vertex visited vertex unexplored edge scovery edge di cross edge A A L L 1 0 G L 1 B B L C drich, Tamassia 0 D F A C E A E © 2004 L L 1 0 F A B C E Breadth-First Search D D F 4
  • 5. Example (cont.) L L 0 B 1 L A 0 C E L L 1 0 2 © 2004 G L F drich, Tamassia C 2 D L E L C E B 1 A B L D L A A 0 B 1 L F 2 Breadth-First Search D F C E D F 5
  • 7. Properties Notation G s: Property B connecte all the vertices and 1 BFS(G, s) visits d edges Property of Gs 2 The discovery la eled by BFS(G, s) form a spanning tree edges L0 b of G Ts Property 3 s ach L B F r e vertex v in „ The p h of Ts s to v has i 1 to o edges Li L 2 at from Every path from s to v in „ le ii edges a © 2004 G drich, Tamassia st component of s inGs a has t Breadth-First Search A C E D F A C E D F 7
  • 8. Analysi s Setting/getting a vertex/edge Each vertex is labeled twice label takes O(1) time O a UNEXPLORE aD s VISITED Each edge is s UNEXPLOREtwice labeled „ once aD once a DISCOVERYoor s CROSS Each vertex is inserted once into a sequen s r is called Method incidentEdges for each vertex „ once „ once „ once BFS the Li O + m) time providedce graph i O the adjacency list structure ( + represented by runs „ © 2004 G n that Σv n Recall deg(v) = 2m drich, Tamassia Breadth-First Search i s 8
  • 9. Applications Using the template method pattern, we can specialize the BFS traversal of a G to solve graphthe 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 G number edges, or them wi the mi nimum t o report that no such path exists h © 2004 G drich, Tamassia f Breadth-First Search 9
  • 10. DF vs. BF S S Applications Spanning forest, connected components, paths, cycles Shortest paths DF S √ √ L A C E © 2004 DF S G drich, Tamassia D F L 1 0 A B L 2 Breadth-First Search √ √ Biconnected components B BF S C E BF S D F 10
  • 11. DF vs. BF (cont.) S edge (v,w) S Back s edge ,w) Cr swi i the, same level as os (v ancestor of v in wi a „ „ next vo n thew level in i s the tree of discovery r n edges the tree of discovery sn edges L A B C E © 2004 G DF S drich, Tamassia D F L 1 0 B L 2 Breadth-First Search A C E BF S D F 11