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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Class F<strong>in</strong>dPathDFS (Code Fragment 13.8) f<strong>in</strong>ds a path between a pair of given<br />

start <strong>and</strong> target vertices. It performs a depth-first search traversal beg<strong>in</strong>n<strong>in</strong>g at the<br />

start vertex. We ma<strong>in</strong>ta<strong>in</strong> the path of discovery edges from the start vertex to the<br />

current vertex. When we encounter an unexplored vertex, we add it to the end of<br />

the path, <strong>and</strong> when we f<strong>in</strong>ish process<strong>in</strong>g a vertex, we remove it from the path. The<br />

traversal is term<strong>in</strong>ated when the target vertex is encountered, <strong>and</strong> the path is<br />

returned as an iterable collection of vertices <strong>and</strong> edges (both k<strong>in</strong>ds of positions <strong>in</strong><br />

a graph). Note that the path found by this class consists of discovery edges.<br />

Code Fragment 13.8: Specialization of class DFS to<br />

f<strong>in</strong>d a path between start <strong>and</strong> target vertices.<br />

820

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

Saved successfully!

Ooh no, something went wrong!