breadth-first search outline and reading breadth-first search bfs

2
5/7/2002 11:06 AM Breadth-First Search 1 Breadth-First Search C B A E D L 0 L 1 F L 2 5/7/2002 11:06 AM Breadth-First Search 2 Outline and Reading Breadth-first search (§6.3.3) Algorithm Example Properties Analysis Applications DFS vs. BFS (§6.3.3) Comparison of applications Comparison of edge labels 5/7/2002 11:06 AM Breadth-First Search 3 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 5/7/2002 11:06 AM Breadth-First Search 4 BFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G, s) L 0 new empty sequence L 0 .insertLast(s) setLabel(s, VISITED) i 0 while ¬L i .isEmpty() L i +1 new empty sequence for all v L i .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) L i +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) 5/7/2002 11:06 AM Breadth-First Search 5 Example C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L 0 L 1 F C B A E D L 0 L 1 F C B A E D L 0 L 1 F 5/7/2002 11:06 AM Breadth-First Search 6 Example (cont.) C B A E D L 0 L 1 F C B A E D L 0 L 1 F L 2 C B A E D L 0 L 1 F L 2 C B A E D L 0 L 1 F L 2

Upload: phungdieu

Post on 03-Jan-2017

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Breadth-First Search Outline and Reading Breadth-First Search BFS

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

1

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

Breadth-First Search

CB

A

E

D

L0

L1

FL2

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

Outline and ReadingBreadth-first search (§6.3.3)

AlgorithmExamplePropertiesAnalysisApplications

DFS vs. BFS (§6.3.3)Comparison of applicationsComparison of edge labels

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

Breadth-First SearchBreadth-first search (BFS) is a general technique for traversing a graphA BFS traversal of a graph G

Visits all the vertices and edges of GDetermines whether G is connectedComputes the connected components of GComputes a spanning forest of G

BFS on a graph with nvertices and m edges takes O(n + m ) timeBFS 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

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

BFS AlgorithmThe algorithm uses a mechanism for setting and getting “labels” of vertices and edges

Algorithm BFS(G, s)L0 ← new empty sequenceL0.insertLast(s)setLabel(s, VISITED)i ← 0while ¬Li.isEmpty()

Li +1 ← new empty sequencefor 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)

elsesetLabel(e, CROSS)

i ← i +1

Algorithm BFS(G)Input graph GOutput 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)

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

Example

CB

A

E

D

discovery edgecross edge

A visited vertexA unexplored vertex

unexplored edge

L0

L1

F

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

F

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

Example (cont.)

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 2: Breadth-First Search Outline and Reading Breadth-First Search BFS

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

2

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

Example (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

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

PropertiesNotation

Gs: connected component of sProperty 1

BFS(G, s) visits all the vertices and edges of Gs

Property 2The discovery edges labeled by BFS(G, s) form a spanning tree Tsof Gs

Property 3For each vertex v in Li

The path of Ts from s to v has iedges Every path from s to v in Gs has at least i edges

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

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

AnalysisSetting/getting a vertex/edge label takes O(1) timeEach vertex is labeled twice

once as UNEXPLOREDonce as VISITED

Each edge is labeled twiceonce as UNEXPLOREDonce as DISCOVERY or CROSS

Each vertex is inserted once into a sequence LiMethod incidentEdges is called once for each vertexBFS runs in O(n + m) time provided the graph is represented by the adjacency list structure

Recall that Σv deg(v) = 2m

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

ApplicationsUsing 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 GCompute 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 between them with the minimum number of edges, or report that no such path exists

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

DFS vs. BFS

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

DFS BFS

√Biconnected components

√Shortest paths

√√Spanning forest, connected components, paths, cycles

BFSDFSApplications

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

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

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

DFS BFS