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.

The runn<strong>in</strong>g time of the Floyd-Warshall algorithm might appear to be slower than<br />

perform<strong>in</strong>g a DFS of a directed graph from each of its vertices, but this depends<br />

upon the representation of the graph. If a graph is represented us<strong>in</strong>g an adjacency<br />

matrix, then runn<strong>in</strong>g the DFS method once on a directed graph takes O(n 2 ) time<br />

(we explore the reason for this <strong>in</strong> Exercise R-13.10). Thus, runn<strong>in</strong>g DFS n times<br />

takes O(n3) time, which is no better than a s<strong>in</strong>gle execution of the Floyd-Warshall<br />

algorithm, but the Floyd-Warshall algorithm would be much simpler to<br />

implement. Nevertheless, if the graph is represented us<strong>in</strong>g an adjacency list<br />

structure, then runn<strong>in</strong>g the DFS algorithm n times would take O(n(n+m)) time to<br />

compute the transitive closure. Even so, if the graph is dense, that is, if it has<br />

&(n 2 ) edges, then this approach still runs <strong>in</strong> O(n 3 ) time <strong>and</strong> is more complicated<br />

than a s<strong>in</strong>gle <strong>in</strong>stance of the Floyd-Warshall algorithm. The only case where<br />

repeatedly call<strong>in</strong>g the DFS method is better is when the graph is not dense <strong>and</strong> is<br />

represented us<strong>in</strong>g an adjacency list structure.<br />

13.4.3 Directed Acyclic Graphs<br />

Directed graphs without directed cycles are encountered <strong>in</strong> many applications. Such<br />

a digraph is often referred to as a directed acyclic graph, or DAG, for short.<br />

Applications of such graphs <strong>in</strong>clude the follow<strong>in</strong>g:<br />

• Inheritance between classes of a <strong>Java</strong> program.<br />

• Prerequisites between courses of a degree program.<br />

• Schedul<strong>in</strong>g constra<strong>in</strong>ts between the tasks of a project.<br />

Example 13.20: In order to manage a large project, it is convenient to break it<br />

up <strong>in</strong>to a collection of smaller tasks. The tasks, however, are rarely <strong>in</strong>dependent,<br />

because schedul<strong>in</strong>g constra<strong>in</strong>ts exist between them. (For example, <strong>in</strong> a house<br />

build<strong>in</strong>g project, the task of order<strong>in</strong>g nails obviously precedes the task of nail<strong>in</strong>g<br />

sh<strong>in</strong>gles to the roof deck.) Clearly, schedul<strong>in</strong>g constra<strong>in</strong>ts cannot have circularities,<br />

because they would make the project impossible. (For example, <strong>in</strong> order to get a job<br />

you need to have work experience, but <strong>in</strong> order to get work experience you need to<br />

have a job.) The schedul<strong>in</strong>g constra<strong>in</strong>ts impose restrictions on the order <strong>in</strong> which<br />

the tasks can be executed. Namely, if a constra<strong>in</strong>t says that task a must be<br />

completed before task b is started, then a must precede b <strong>in</strong> the order of execution<br />

of the tasks. Thus, if we model a feasible set of tasks as vertices of a directed graph,<br />

<strong>and</strong> we place a directed edge from v tow whenever the task for v must be executed<br />

before the task for w, then we def<strong>in</strong>e a directed acyclic graph.<br />

The example above motivates the follow<strong>in</strong>g def<strong>in</strong>ition. Let be a digraph with n<br />

vertices. A topological order<strong>in</strong>g of is an order<strong>in</strong>g v 1 ,...,v n of the vertices of<br />

such that for every edge (vi, vj) of , i < j. That is, a topological order<strong>in</strong>g is an<br />

836

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

Saved successfully!

Ooh no, something went wrong!