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.

As with undirected graphs, we can explore a digraph <strong>in</strong> a systematic way with<br />

methods ak<strong>in</strong> to the depth-first search (DFS) <strong>and</strong> breadth-first search (BFS)<br />

algorithms def<strong>in</strong>ed previously for undirected graphs (Sections 13.3.1 <strong>and</strong> 13.3.3).<br />

Such explorations can be used, for example, to answer reachability questions. The<br />

directed depth-first search <strong>and</strong> breadth-first search methods we develop <strong>in</strong> this<br />

section for perform<strong>in</strong>g such explorations are very similar to their undirected<br />

counterparts. In fact, the only real difference is that the directed depth-first search<br />

<strong>and</strong> breadth-first search methods only traverse edges accord<strong>in</strong>g to their respective<br />

directions.<br />

The directed version of DFS start<strong>in</strong>g at a vertex v can be described by the recursive<br />

algorithm <strong>in</strong> Code Fragment 13.11. (See Figure 13.9.)<br />

Code Fragment 13.11: The Directed DFS<br />

algorithm.<br />

Figure 13.9: An example of a DFS <strong>in</strong> a digraph: (a)<br />

<strong>in</strong>termediate step, where, for the first time, an already<br />

visited vertex (DFW) is reached; (b) the completed DFS.<br />

The tree edges are shown with solid blue l<strong>in</strong>es, the back<br />

edges are shown with dashed blue l<strong>in</strong>es, <strong>and</strong> the<br />

forward <strong>and</strong> cross edges are shown with dashed black<br />

l<strong>in</strong>es. The order <strong>in</strong> which the vertices are visited is<br />

<strong>in</strong>dicated by a label next to each vertex. The edge<br />

(ORD,DFW) is a back edge, but (DFW,ORD) is a forward<br />

edge. Edge (BOS,SFO) is a forward edge, <strong>and</strong> (SFO,LAX)<br />

is a cross edge.<br />

830

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

Saved successfully!

Ooh no, something went wrong!