Vous êtes sur la page 1sur 2

Breadth-First Search 5/7/2002 11:06 AM

Outline and Reading


Breadth-first search (§6.3.3)
Breadth-First Search „ Algorithm
„ Example
L0 „ Properties
A
„ Analysis
L1 „ Applications
B C D

L2
DFS vs. BFS (§6.3.3)
E F „ Comparison of applications
„ Comparison of edge labels

5/7/2002 11:06 AM Breadth-First Search 1 5/7/2002 11:06 AM Breadth-First Search 2

Breadth-First Search BFS Algorithm


The algorithm uses a Algorithm BFS(G, s)
Breadth-first search BFS on a graph with n mechanism for setting and L0 ← new empty sequence
(BFS) is a general vertices and m edges getting “labels” of vertices L0.insertLast(s)
technique for traversing takes O(n + m ) time
and edges setLabel(s, VISITED)
i←0
a graph Algorithm BFS(G) while ¬Li.isEmpty()
BFS can be further
A BFS traversal of a Input graph G Li +1 ← new empty sequence
extended to solve other Output labeling of the edges for all v ∈ Li.elements()
graph G
Visits all the vertices and
graph problems and partition of the for all e ∈ G.incidentEdges(v)
„ vertices of G if getLabel(e) = UNEXPLORED
edges of G „ Find and report a path for all u ∈ G.vertices() w ← opposite(v,e)
„ Determines whether G is with the minimum setLabel(u, UNEXPLORED) if getLabel(w) = UNEXPLORED
connected number of edges for all e ∈ G.edges() setLabel(e, DISCOVERY)
„ Computes the connected between two given setLabel(e, UNEXPLORED)
setLabel(w, VISITED)
vertices Li +1.insertLast(w)
components of G for all v ∈ G.vertices() else
„ Computes a spanning „ Find a simple cycle, if if getLabel(v) = UNEXPLORED setLabel(e, CROSS)
forest of G there is one BFS(G, v) i ← i +1
5/7/2002 11:06 AM Breadth-First Search 3 5/7/2002 11:06 AM Breadth-First Search 4

Example Example (cont.)


L0
A L0 L0
A unexplored vertex A A
A visited vertex L1
B C D L1 L1
unexplored edge B C D B C D

discovery edge E F L2
E F E F
cross edge

L0 L0
A A L0 L0
A A
L1 L1
B C D B C D L1 L1
B C D B C D

E F E F
L2 L2
E F E F

5/7/2002 11:06 AM Breadth-First Search 5 5/7/2002 11:06 AM Breadth-First Search 6

1
Breadth-First Search 5/7/2002 11:06 AM

Example (cont.) Properties


L0 L0
A A Notation A
Gs: connected component of s
L1 L1
B C D B C D Property 1 B C D
BFS(G, s) visits all the vertices and
L2 L2 edges of Gs
E F E F E F
Property 2
The discovery edges labeled by
BFS(G, s) form a spanning tree Ts
L0 L0
of Gs A
A
Property 3
L1
L1 For each vertex v in Li B C D
B C D „ The path of Ts from s to v has i
edges L2
L2
„ Every path from s to v in Gs has at E F
E F
least i edges
5/7/2002 11:06 AM Breadth-First Search 7 5/7/2002 11:06 AM Breadth-First Search 8

Analysis Applications
Setting/getting a vertex/edge label takes O(1) time
Using the template method pattern, we can
Each vertex is labeled twice
„ once as UNEXPLORED
specialize the BFS traversal of a graph G to
„ once as VISITED solve the following problems in O(n + m) time
Each edge is labeled twice „ Compute the connected components of G
„ once as UNEXPLORED „ Compute a spanning forest of G
„ once as DISCOVERY or CROSS
„ Find a simple cycle in G, or report that G is a
Each vertex is inserted once into a sequence Li
forest
Method incidentEdges is called once for each vertex
„ Given two vertices of G, find a path in G between
BFS runs in O(n + m) time provided the graph is
them with the minimum number of edges, or
represented by the adjacency list structure
report that no such path exists
„ Recall that Σv deg(v) = 2m
5/7/2002 11:06 AM Breadth-First Search 9 5/7/2002 11:06 AM Breadth-First Search 10

DFS vs. BFS DFS vs. BFS (cont.)


Applications DFS BFS Back edge (v,w) Cross edge (v,w)
Spanning forest, connected „ w is an ancestor of v in „ w is in the same level as
√ √
components, paths, cycles the tree of discovery v or in the next level in
edges the tree of discovery
Shortest paths √
edges
Biconnected components √
L0
L0 A A
A A
L1
L1 B C D B C D
B C D B C D
L2
L2 E F E F
E F E F
DFS BFS
DFS BFS
5/7/2002 11:06 AM Breadth-First Search 11 5/7/2002 11:06 AM Breadth-First Search 12

Vous aimerez peut-être aussi