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.

A DFS on a digraph partitions the edges of reachable from the start<strong>in</strong>g<br />

vertex <strong>in</strong>to tree edges or discovery edges, which lead us to discover a new vertex,<br />

<strong>and</strong> nontree edges, which take us to a previously visited vertex. The tree edges<br />

form a tree rooted at the start<strong>in</strong>g vertex, called the depth-first search tree, <strong>and</strong> there<br />

are three k<strong>in</strong>ds of nontree edges:<br />

• back edges, which connect a vertex to an ancestor <strong>in</strong> the DFS tree<br />

• forward edges, which connect a vertex to a descendent <strong>in</strong> the DFS tree<br />

• cross edges, which connect a vertex to a vertex that is neither its ancestor<br />

nor its descendent.<br />

Refer back to Figure 13.9b to see an example of each type of nontree edge.<br />

Proposition 13.16: Let be a digraph. Depth-first search on start<strong>in</strong>g at<br />

a vertex s visits all the vertices of that are reachable from s. Also, the DFS tree<br />

conta<strong>in</strong>s directed paths from s to every vertex reachable from s.<br />

Justification: Let V s be the subset of vertices of visited by DFS start<strong>in</strong>g<br />

at vertex s. We want to show that V s conta<strong>in</strong>s s <strong>and</strong> every vertex reachable from s<br />

belongs to V s . Suppose now, for the sake of a contradiction, that there is a vertex w<br />

reachable from s that is not <strong>in</strong> V s . Consider a directed path from s to w, <strong>and</strong> let (u, v)<br />

be the first edge on such a path tak<strong>in</strong>g us out of V s , that is, u is <strong>in</strong> V s but v is not <strong>in</strong><br />

V s . When DFS reaches u, it explores all the outgo<strong>in</strong>g edges of u, <strong>and</strong> thus must<br />

reach also vertex v via edge (u,v). Hence, v should be <strong>in</strong> V s , <strong>and</strong> we have obta<strong>in</strong>ed a<br />

contradiction. Therefore, V s must conta<strong>in</strong> every vertex reachable from s<br />

Analyz<strong>in</strong>g the runn<strong>in</strong>g time of the directed DFS method is analogous to that for its<br />

undirected counterpart. In particular, a recursive call is made for each vertex exactly<br />

831

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

Saved successfully!

Ooh no, something went wrong!