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.

• Given a start vertex s of G, comput<strong>in</strong>g, for every vertex v of G,a path with<br />

the m<strong>in</strong>imum number of edges between s <strong>and</strong> v, or report<strong>in</strong>g that no such path<br />

exists.<br />

• Comput<strong>in</strong>g a cycle <strong>in</strong> G, or report<strong>in</strong>g that G has no cycles.<br />

13.4 Directed Graphs<br />

In this section, we consider issues that are specific to directed graphs. Recall that a<br />

directed graph (digraph), is a graph whose edges are all directed.<br />

Methods Deal<strong>in</strong>g with Directed Edges<br />

When we allow for some or all the edges <strong>in</strong> a graph to be directed, we should add<br />

the follow<strong>in</strong>g two methods to the graph ADT <strong>in</strong> order to deal with edge directions.<br />

isDirected(e): Test whether edge e is directed.<br />

<strong>in</strong>sertDirectedEdge(v, w, o): Insert <strong>and</strong> return a new directed edge<br />

with orig<strong>in</strong> v <strong>and</strong> dest<strong>in</strong>ation w <strong>and</strong> stor<strong>in</strong>g element o.<br />

Also, if an edge e is directed, the method endVertices(e) should return an array<br />

A such that A[0] is the orig<strong>in</strong> of e <strong>and</strong> A[1] is the dest<strong>in</strong>ation of e. The runn<strong>in</strong>g time<br />

for the method isDirected(e) should be O(1), <strong>and</strong> the runn<strong>in</strong>g time of the<br />

method <strong>in</strong>sertDirectedEdge(v, w, o) should match that of undirected edge<br />

<strong>in</strong>sertion.<br />

Reachability<br />

One of the most fundamental issues with directed graphs is the notion of<br />

reachability, which deals with determ<strong>in</strong><strong>in</strong>g where we can get to <strong>in</strong> a directed graph.<br />

A traversal <strong>in</strong> a directed graph always goes along directed paths, that is, paths<br />

where all the edges are traversed accord<strong>in</strong>g to their respective directions. Given<br />

vertices u <strong>and</strong> v of a digraph<br />

, we say that u reaches v (<strong>and</strong> v is reachable from<br />

u) if has a directed path from u to v. We also say that a vertex v reaches an edge<br />

(w,z) if v reaches the orig<strong>in</strong> vertex w of the edge.<br />

A digraph is strongly connected if for any two vertices u <strong>and</strong> v of , u reaches<br />

v <strong>and</strong> v reaches u. A directed cycle of is a cycle where all the edges are<br />

traversed accord<strong>in</strong>g to their respective directions. (Note that may have a cycle<br />

consist<strong>in</strong>g of two edges with opposite direction between the same pair of vertices.)<br />

827

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

Saved successfully!

Ooh no, something went wrong!