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.

We can visualize a DFS traversal by orient<strong>in</strong>g the edges along the direction <strong>in</strong><br />

which they are explored dur<strong>in</strong>g the traversal, dist<strong>in</strong>guish<strong>in</strong>g the edges used to<br />

discover new vertices, called discovery edges, or tree edges, from those that lead<br />

to already visited vertices, called back edges. (See Figure 13.6f). In the analogy<br />

above, discovery edges are the edges where we unroll our str<strong>in</strong>g when we traverse<br />

them, <strong>and</strong> back edges are the edges where we immediately return without<br />

unroll<strong>in</strong>g any str<strong>in</strong>g. As we will see, the discovery edges form a spann<strong>in</strong>g tree of<br />

the connected component of the start<strong>in</strong>g vertex s. We call the edges not <strong>in</strong> this tree<br />

"back edges" because, assum<strong>in</strong>g that the tree is rooted at the start vertex, each<br />

such edge leads back from a vertex <strong>in</strong> this tree to one of its ancestors <strong>in</strong> the tree.<br />

The pseudo-code for a DFS traversal start<strong>in</strong>g at a vertex v follows our analogy<br />

with str<strong>in</strong>g <strong>and</strong> pa<strong>in</strong>t. We use recursion to implement the str<strong>in</strong>g analogy, <strong>and</strong> we<br />

assume that we have a mechanism (the pa<strong>in</strong>t analogy) to determ<strong>in</strong>e if a vertex or<br />

edge has been explored or not, <strong>and</strong> to label the edges as discovery edges or back<br />

edges. This mechanism will require additional space <strong>and</strong> may affect the runn<strong>in</strong>g<br />

time of the algorithm. A pseudo-code description of the recursive DFS algorithm<br />

is given <strong>in</strong> Code Fragment 13.1.<br />

Code Fragment 13.1: The DFS algorithm.<br />

There are a number of observations that we can make about the depth-first search<br />

algorithm, many of which derive from the way the DFS algorithm partitions the<br />

edges of the undirected graph G <strong>in</strong>to two groups, the discovery edges <strong>and</strong> the<br />

back edges. For example, s<strong>in</strong>ce back edges always connect a vertex v to a<br />

previously visited vertex u, each back edge implies a cycle <strong>in</strong> G, consist<strong>in</strong>g of the<br />

discovery edges from u to v plus the back edge (u, v).<br />

Proposition 13.12: Let G be an undirected graph on which a DFS<br />

traversal start<strong>in</strong>g at a vertex s has been performed. Then the traversal visits all<br />

811

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

Saved successfully!

Ooh no, something went wrong!