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.

order<strong>in</strong>g such that any directed path <strong>in</strong> G traverses vertices <strong>in</strong> <strong>in</strong>creas<strong>in</strong>g order. (See<br />

Figure 13.11.) Note that a digraph may have more than one topological order<strong>in</strong>g.<br />

Figure 13.11: Two topological order<strong>in</strong>gs of the same<br />

acyclic digraph.<br />

Proposition 13.21:<br />

has a topological order<strong>in</strong>g if <strong>and</strong> only if it is acyclic.<br />

Justification: The necessity (the "only if" part of the statement) is easy to<br />

demonstrate. Suppose<br />

is topologically ordered. Assume, for the sake of a<br />

contradiction, that has a cycle consist<strong>in</strong>g of edges (vi 0 , vi 1 ), (vi 1 , vi 2 ),…, (vik−<br />

1, vi 0 ). Because of the topological order<strong>in</strong>g, we must have i 0 < i 1 ... < ik− 1 < i 0 ,<br />

which is clearly impossible. Thus,<br />

must be acyclic.<br />

We now argue the sufficiency of the condition (the "if" part). Suppose is<br />

acyclic. We will give an algorithmic description of how to build a topological<br />

order<strong>in</strong>g for . S<strong>in</strong>ce is acyclic, must have a vertex with no <strong>in</strong>com<strong>in</strong>g edges<br />

(that is, with <strong>in</strong>-degree 0). Let v 1 be such a vertex. Indeed, if v 1 did not exist, then<br />

<strong>in</strong> trac<strong>in</strong>g a directed path from an arbitrary start vertex we would eventually<br />

encounter a previously visited vertex, thus contradict<strong>in</strong>g the acyclicity of<br />

. If we<br />

remove v 1 from , together with its outgo<strong>in</strong>g edges, the result<strong>in</strong>g digraph is still<br />

acyclic. Hence, the result<strong>in</strong>g digraph also has a vertex with no <strong>in</strong>com<strong>in</strong>g edges, <strong>and</strong><br />

we let v2 be such a vertex. By repeat<strong>in</strong>g this process until the digraph becomes<br />

837

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

Saved successfully!

Ooh no, something went wrong!