Upload
aravindh-ramanan
View
80
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
Breadth-First
Search
L0A
L1
L2
Breadth-First Search
© 2004
drich, Tamassia
B C D
E F
Goo
1
Breadth-First
Search
(§ 12.3.3)Breadth-first
searchBFS
on a graph
ith n(BFS) is a
general
vertices and
m edgesniqu
efor traversing
s O(n + m )
timegrap
h BFS
can
be furtherBF
Straversal extended to
solveothergrap
hG
graph
problems
Visits all the
rtices
and Find and report
aedges of G
�with the minimum
Determines whether
G isnumber of edges
connected between two
givenComputes the connected vertice
scomponents
of GComputes a
spanning
Find a simple cycle, ifforest of
Gthere is one
Breadth-First Search
© 2004
drich, Tamassia
w
tech takesaA of a
�
ve
pa
th
�
�
� �
Goo
2
BFS
Algorithm Algorithm BFS(G, s)Thealgorithm uses
a L0 ← new empty sequencemechanism for setting and L0.insertLast(s)getting
“labels”
of vertices bel(s, VISITED)an
dedges i ← 0
Algorithm BFS(G) while ¬Li.isEmpty()Input graph G Li +1 ← new empty sequenceOutput labeling of the edges for all v ∈ Li.elements()
and tition of the for e ∈ G.incidentEdges(v)vertices of G if getLabel(e) = UNEXPLORED
← opposite(v,e)for all u ∈if getLabel(w) = UNEXPLOREDsetLabel(u, UNEXPLORED)
setLabel(e, DISCOVERY)for all e ∈ G.edges() setLabel(w, VISITED)
setLabel(e, UNEXPLORED)Li +1.insertLast(w)
for all v ∈ G.vertices()
elseif getLabel(v) = UNEXPLORED
setLabel(e, CROSS)BFS(G, v) i ← i
+1Breadth-First Search
© 2004
drich, Tamassia
Lof L
se tLa bel (s,
Li
∈par all e
=G.v erti ces () w ←
∈w,
Li∈
e,i
Goo
3
Example L0
Aunexplored vertex
A
L1visited vertex
AB C D
unexplored edgescovery
edgeE F
cross
edge
L0 L0A A
L1 L1B C D B C D
E F E F
Breadth-First Search
© 2004
drich, Tamassia
L
di
L L
Goo
4
Example
(cont.)L0
L1 L1
L0A A
L1
L2 L2
Breadth-First Search
© 2004
drich, Tamassia
L0
A A
B C D B C D
L2E F E F
L0
L1
B C D B C D
E F E F
Goo
5
Example
(cont.)L0 L0
A
L1 L1B C D
L2
E F E F
L0A
L1B C D
L2
E F
Breadth-First Search
© 2004
drich, Tamassia
LA
B C D
L L2
L
L
Goo
6
PropertiesNotatio
n Gs: connected component of sPropert
yB C D
s) visits
all the
vertices and
BFS(G,edges of Gs
E FProperty The discovery
edgeseled bys) form
aspanning
tree TsBFS(G, L
Property L1ac
hvertex
v in Li B C DThe h of Ts from s to v has
iedgesEvery path from
s to v in Gs hasi edges
Breadth-First Search
© 2004
drich, Tamassia
A
1s)
2lab
s)of Gs
0 A
3For e v
� pat
h toL2
� s in at E Fleast i
Goo
7
Analysis Setting/getting a
vertex/edgelabel
takes
O(1) timeEach vertex is labeled
twiceonce
UNEXPLOREDonc
eVISITEDEach edge is
labeledtwiceonc
eUNEXPLOREDonc
eDISCOVERY or CROSSEach vertex is
insertedonce into a
sequence Li
Method incidentEdges is called once
for each
vertexBFS
runs+ m) time provided the graph
Orepresented by the adjacency
list structureRecall that Σv deg(v) = 2m�
Breadth-First Search
© 2004
drich, Tamassia
O
� as
� as
� as
� as or
in O (n + is
Goo
8
Applications
Using the template
method pattern,
we canspecialize the BFS traversal of a
graphG to
solve
the
following
problems
in O(n + m)
timeCompute the connected
components ofG
Compute a spanning forest of GFind a simple cycle
in G, or report that
G is a
�forestGiven
two
vertices of
G, find a
path in G betweenthe
mthe
nimum number
edges, orreport that no
suchpath exists
Breadth-First Search
© 2004
drich, Tamassia
�
�
� Gwith mi of
Goo
9
DFS
vs.
BFSApplicatio
nsDFS
BFSSpanning forest,
connected√
components, paths, cyclesShortest pathsBiconnected
components
√
L0A A
L1
L2
DFS
BFSBreadth-First
Search10©
2004drich, Tamassia
√
√
B C D B C D
E F E F
Goo
DFS
vs.
BFS
(cont.)Bac
kedge (v,w)
s edge
,w)ancestor of v
in the same level asthe
treeof discover
ythe next level inedge
sthe tree
of discoveryedge
sL0
L1
L2
DFS
BFS
Breadth-First Search
11© 2004
drich, Tamassia
Cross (v ,w
� w is an � w is inv or in
A A
B C D B C D
E F E F
Goo