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.

vertices <strong>in</strong> the connected component of s, <strong>and</strong> the discovery edges form a<br />

spann<strong>in</strong>g tree of the connected component of s.<br />

Justification: Suppose there is at least one vertex v <strong>in</strong> s's connected<br />

component not visited, <strong>and</strong> let w be the first unvisited vertex on some path from s<br />

to v (we may have v = w). S<strong>in</strong>ce w is the first unvisited vertex on this path, it has<br />

a neighbor u that was visited. But when we visited u, we must have considered the<br />

edge (u,w); hence, it cannot be correct that w is unvisited. Therefore, there are no<br />

unvisited vertices <strong>in</strong> s's connected component.<br />

S<strong>in</strong>ce we only mark edges when we go to unvisited vertices, we will never form a<br />

cycle with discovery edges, that is, discovery edges form a tree. Moreover, this is<br />

a spann<strong>in</strong>g tree because, as we have just seen, the depth-first search visits each<br />

vertex <strong>in</strong> the connected component of s<br />

In terms of its runn<strong>in</strong>g time, depth-first search is an efficient method for<br />

travers<strong>in</strong>g a graph. Note that DFS is called exactly once on each vertex, <strong>and</strong> that<br />

every edge is exam<strong>in</strong>ed exactly twice, once from each of its end vertices. Thus, if<br />

n s vertices <strong>and</strong> m s edges are <strong>in</strong> the connected component of vertex s, a DFS<br />

start<strong>in</strong>g at s runs <strong>in</strong> O(n s + m s ) time, provided the follow<strong>in</strong>g conditions are<br />

satisfied:<br />

• The graph is represented by a data structure such that creat<strong>in</strong>g <strong>and</strong><br />

iterat<strong>in</strong>g through the <strong>in</strong>cidentEdges(v) iterable collection takes<br />

O(degree(v)) time, <strong>and</strong> the opposite(v,e) method takes O(1) time. The<br />

adjacency list structure is one such structure, but the adjacency matrix structure<br />

is not.<br />

• We have a way to "mark" a vertex or edge as explored, <strong>and</strong> to test if a<br />

vertex or edge has been explored <strong>in</strong> O(1) time. We discuss ways of<br />

implement<strong>in</strong>g DFS to achieve this goal <strong>in</strong> the next section.<br />

Given the assumptions above, we can solve a number of <strong>in</strong>terest<strong>in</strong>g problems.<br />

Proposition 13.13: Let G be a graph with n vertices <strong>and</strong> m edges<br />

represented with an adjacency list. A DFS traversal of G can be performed <strong>in</strong> O(n<br />

+ m) time, <strong>and</strong> can be used to solve the follow<strong>in</strong>g problems <strong>in</strong> O(n + m) time:<br />

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

• Comput<strong>in</strong>g a spann<strong>in</strong>g tree of G, if G is connected.<br />

• Comput<strong>in</strong>g the connected components of G.<br />

• Comput<strong>in</strong>g a path between two given vertices of G, if it exists.<br />

812

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

Saved successfully!

Ooh no, something went wrong!