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.

once, <strong>and</strong> each edge is traversed exactly once (from its orig<strong>in</strong>). Hence, if ns vertices<br />

<strong>and</strong> ms edges are reachable from vertex s, a directed DFS start<strong>in</strong>g at s runs <strong>in</strong> O(n s<br />

+ m s ) time, provided the digraph is represented with a data structure that supports<br />

constant-time vertex <strong>and</strong> edge methods. The adjacency list structure satisfies this<br />

requirement, for example.<br />

By Proposition 13.16, we can use DFS to f<strong>in</strong>d all the vertices reachable from a<br />

given vertex, <strong>and</strong> hence to f<strong>in</strong>d the transitive closure of . That is, we can perform<br />

a DFS, start<strong>in</strong>g from each vertex v of , to see which vertices w are reachable<br />

from v, add<strong>in</strong>g an edge (v, w) to the transitive closure for each such w. Likewise, by<br />

repeatedly travers<strong>in</strong>g digraph with a DFS, start<strong>in</strong>g <strong>in</strong> turn at each vertex, we can<br />

easily test whether is strongly connected. Namely, is strongly connected if<br />

each DFS visits all the vertices of<br />

Thus, we may immediately derive the proposition that follows.<br />

Proposition 13.17: Let be a digraph with n vertices <strong>and</strong> m edges. The<br />

follow<strong>in</strong>g problems can be solved by an algorithm that traverses n times us<strong>in</strong>g<br />

DFS, runs <strong>in</strong> O (n(n+m)) time, <strong>and</strong> uses O(n) auxiliary space:<br />

• Comput<strong>in</strong>g, for each vertex v of , the subgraph reachable from v<br />

• Test<strong>in</strong>g whether is strongly connected<br />

• Comput<strong>in</strong>g the transitive closure of .<br />

Test<strong>in</strong>g for Strong Connectivity<br />

Actually, we can determ<strong>in</strong>e if a directed graph is strongly connected much<br />

faster than this, just us<strong>in</strong>g two depth-first searches. We beg<strong>in</strong> by perform<strong>in</strong>g a<br />

DFS of our directed graph start<strong>in</strong>g at an arbitrary vertex s. If there is any<br />

vertex of that is not visited by this DFS, <strong>and</strong> is not reachable from s, then the<br />

graph is not strongly connected. So, if this first DFS visits each vertex of , then<br />

we reverse all the edges of (us<strong>in</strong>g the reverse Direction method) <strong>and</strong> perform<br />

another DFS start<strong>in</strong>g at s <strong>in</strong> this "reverse" graph. If every vertex of is visited<br />

by this second DFS, then the graph is strongly connected, for each of the vertices<br />

visited <strong>in</strong> this DFS can reach s. S<strong>in</strong>ce this algorithm makes just two DFS<br />

traversals of , it runs <strong>in</strong> O(n + m) time.<br />

832

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

Saved successfully!

Ooh no, something went wrong!