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.

Directed Breadth-First Search<br />

As with DFS, we can extend breadth-first search (BFS) to work for directed<br />

graphs. The algorithm still visits vertices level by level <strong>and</strong> partitions the set of<br />

edges <strong>in</strong>to tree edges (or discovery edges), which together form a directed<br />

breadth-first search tree rooted at the start vertex, <strong>and</strong> nontree edges. Unlike the<br />

directed DFS method, however, the directed BFS method only leaves two k<strong>in</strong>ds of<br />

nontree edges: back edges, which connect a vertex to one of its ancestors, <strong>and</strong><br />

cross edges, which connect a vertex to another vertex that is neither its ancestor<br />

nor its descendent. There are no forward edges, which is a fact we explore <strong>in</strong> an<br />

Exercise (C-13.10).<br />

13.4.2 Transitive Closure<br />

In this section, we explore an alternative technique for comput<strong>in</strong>g the transitive<br />

closure of a digraph. Let<br />

be a digraph with n vertices <strong>and</strong> m edges. We compute<br />

the transitive closure of <strong>in</strong> a series of rounds. We <strong>in</strong>itialize = . We also<br />

arbitrarily number the vertices of as v 1 , v 2 ,…, v n . We then beg<strong>in</strong> the<br />

computation of the rounds, beg<strong>in</strong>n<strong>in</strong>g with round 1. In a generic round k, we<br />

cons truct digraph start<strong>in</strong>g with = <strong>and</strong> add<strong>in</strong>g to the directed<br />

edge (vi, v j ) if digraph conta<strong>in</strong>s both the edges (v i ,v K ) <strong>and</strong> (v k ,v j ). In this<br />

way, we will enforce a simple rule embodied <strong>in</strong> the proposition that follows.<br />

Proposition 13.18: For i=1,…,n, digraph<br />

only if digraph<br />

has an edge (v i , v j ) if <strong>and</strong><br />

has a directed path from v i to v j , whose <strong>in</strong>termediate vertices (if<br />

any) are <strong>in</strong> the set{v 1 ,…,v k }. In particular, is equal to , the transitive<br />

closure of .<br />

Proposition 13.18 suggests a simple algorithm for comput<strong>in</strong>g the transitive closure<br />

of<br />

that is based on the series of rounds we described above. This algorithm is<br />

known as the Floyd-Warshall algorithm, <strong>and</strong> its pseudo-code is given <strong>in</strong> Code<br />

Fragment 13.12. From this pseudo-code, we can easily analyze the runn<strong>in</strong>g time of<br />

the Floyd-Warshall algorithm assum<strong>in</strong>g that the data structure represent<strong>in</strong>g G<br />

supports methods areAdjacent <strong>and</strong> <strong>in</strong>sertDirectedEdge <strong>in</strong> O(1) time. The ma<strong>in</strong> loop<br />

is executed n times <strong>and</strong> the <strong>in</strong>ner loop considers each of O(n 2 ) pairs of vertices,<br />

perform<strong>in</strong>g a constant-time computation for each one. Thus, the total runn<strong>in</strong>g time<br />

of the Floyd-Warshall algorithm is O(n 3 ).<br />

Code Fragment 13.12: Pseudo-code for the Floyd-<br />

Warshall algorithm. This algorithm computes the<br />

833

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

Saved successfully!

Ooh no, something went wrong!