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.

In Code Fragments 13.4 <strong>and</strong> 13.5, we show a <strong>Java</strong> implementation of a generic<br />

depth-first search traversal us<strong>in</strong>g a general class, DFS, which has a method,<br />

execute, which takes as <strong>in</strong>put the graph, a start vertex, <strong>and</strong> any auxiliary<br />

<strong>in</strong>formation needed, <strong>and</strong> then <strong>in</strong>itializes the graph <strong>and</strong> calls the recursive method,<br />

dfsTraversal, which activates the DFS traversal. Our implementation<br />

assumes that the vertices <strong>and</strong> edges are decorable positions, <strong>and</strong> it uses<br />

decorations to tell if vertices <strong>and</strong> edges have been visited or not. The DFS class<br />

conta<strong>in</strong>s the follow<strong>in</strong>g methods to allow it to do special tasks dur<strong>in</strong>g a DFS<br />

traversal:<br />

• setup(): called prior to do<strong>in</strong>g the DFS traversal call to<br />

dfsTraversal().<br />

• <strong>in</strong>itResult(): called at the beg<strong>in</strong>n<strong>in</strong>g of the execution of<br />

dfsTraversal().<br />

• startVisit(v): called at the start of the visit of v.<br />

• traverseDiscovery(e,v): called when a discovery edge e out of v is<br />

traversed.<br />

• traverseBack(e,v): called when a back edge e out of v is traversed.<br />

• isDone(): called to determ<strong>in</strong>e whether to end the traversal early.<br />

• f<strong>in</strong>ishVisit(v): called when we are f<strong>in</strong>ished explor<strong>in</strong>g from v.<br />

• result(): called to return the output of dfsTraversal.<br />

• f<strong>in</strong>alResult(r): called to return the output of the execute method,<br />

given the output, r, from dfsTraversal.<br />

Code Fragment 13.4: Instance variables <strong>and</strong> support<br />

methods of class DFS, which performs a generic DFS<br />

traversal. The methods visit, unVisit, <strong>and</strong><br />

isVisited are implemented us<strong>in</strong>g decorable<br />

positions that are parameterized us<strong>in</strong>g the wildcard<br />

symbol, "?", which can match either the V or the E<br />

parameter used for decorable positions. (Cont<strong>in</strong>ues <strong>in</strong><br />

Code Fragment 13.5.)<br />

816

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

Saved successfully!

Ooh no, something went wrong!