23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

One of the nice properties of the BFS approach is that, <strong>in</strong> perform<strong>in</strong>g the BFS<br />

traversal, we can label each vertex by the length of a shortest path (<strong>in</strong> terms of the<br />

number of edges) from the start vertex s. In particular, if vertex v is placed <strong>in</strong>to<br />

level i by a BFS start<strong>in</strong>g at vertex s, then the length of a shortest path from s to v is<br />

i.<br />

As with DFS, we can visualize the BFS traversal by orient<strong>in</strong>g the edges along the<br />

direction <strong>in</strong> which they are explored dur<strong>in</strong>g the traversal, <strong>and</strong> by dist<strong>in</strong>guish<strong>in</strong>g the<br />

edges used to discover new vertices, called discovery edges, from those that lead to<br />

already visited vertices, called cross edges. (See Figure 13.7f.) As with the DFS, the<br />

discovery edges form a spann<strong>in</strong>g tree, which <strong>in</strong> this case we call the BFS tree. We<br />

do not call the nontree edges "back edges" <strong>in</strong> this case, however, for none of them<br />

connects a vertex to one of its ancestors. Every nontree edge connects a vertex v to<br />

another vertex that is neither v's ancestor nor its descendent.<br />

The BFS traversal algorithm has a number of <strong>in</strong>terest<strong>in</strong>g properties, some of which<br />

we explore <strong>in</strong> the proposition that follows.<br />

Proposition 13.14: Let G be an undirected graph on which a BFS traversal<br />

start<strong>in</strong>g at vertex s has been performed. Then<br />

• The traversal visits all vertices <strong>in</strong> the connected component of s.<br />

• The discovery-edges form a spann<strong>in</strong>g tree T, which we call the BFS tree,<br />

of the connected component of s.<br />

• For each vertex v at level i, the path of the BFS tree T between s <strong>and</strong> v has<br />

i edges, <strong>and</strong> any other path of G between s <strong>and</strong> v has at least i edges.<br />

• If (u, v) is an edge that is not <strong>in</strong> the BFS tree, then the level numbers of u<br />

<strong>and</strong> v differ by at most 1.<br />

We leave the justification of this proposition as an exercise (C-13.14). The analysis<br />

of the runn<strong>in</strong>g time of BFS is similar to the one of DFS, which implies the<br />

follow<strong>in</strong>g.<br />

Proposition 13.15: Let G be a graph with n vertices <strong>and</strong> m edges<br />

represented with the adjacency list structure. A BFS traversal of G takes O(n + m)<br />

time. Also, there exist O(n + m)-time algorithms based on BFS for the follow<strong>in</strong>g<br />

problems:<br />

• Test<strong>in</strong>g whether G is connected.<br />

• Comput<strong>in</strong>g a spann<strong>in</strong>g tree of G, if G is connected.<br />

• Comput<strong>in</strong>g the connected components of G.<br />

826

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!