The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

Connected Components Next: Topological Sorting Up: Graph Problems: Polynomial-Time Previous: Graph Problems: Polynomial-Time Connected Components Input description: A directed or undirected graph G. Problem description: Traverse each edge and vertex of all connected components of G. Discussion: The connected components of a graph represent, in grossest terms, the pieces of the graph. Two vertices are in the same component of G if and only if there is some path between them. Finding connected components is at the heart of many graph applications. For example, consider the problem of identifying clusters in a set of items. We can represent each item by a vertex and add an edge between each pair of items that are deemed ``similar.'' The connected components of this graph correspond to different classes of items. Testing whether a graph is connected is an essential preprocessing step for every graph algorithm. Such tests can be performed so quickly and easily that you should always verify that your input graph is connected, even when you know it has to be. Subtle, difficult-to-detect bugs often result when your algorithm is run only on one component of a disconnected graph. file:///E|/BOOK/BOOK4/NODE159.HTM (1 of 4) [19/1/2003 1:30:54]

Connected Components Testing the connectivity of any undirected graph is a job for either depth-first or breadth-first search, as discussed in Section . Which one you choose doesn't really matter. Both traversals initialize a component-number field for each vertex to 0, and then start the search for component 1 from vertex . As each vertex is visited, the value of this field is set to the current component number. When the initial traversal ends, the component number is incremented, and the search begins again from the first vertex with component-number still 0. Properly implemented using adjacency lists, this runs in O(n+m), or time linear in the number of edges and vertices. Other notions of connectivity also arise in practice: ● What if my graph is directed? - There are two distinct definitions of connected components for directed graphs. A directed graph is weakly connected if it would be connected by ignoring the direction of edges. Thus a weakly connected graph consists of a single piece. A directed graph is strongly connected if there is a directed path between every pair of vertices. This distinction is best made clear by considering a network of one- and two-way streets in a city. The network is strongly connected if it is possible to drive legally between every two positions. The network is weakly connected if it is possible to drive legally or illegally between every two positions. The network is disconnected if there is no possible way to drive from a to b. The weakly and strongly connected components define unique partitions on the vertices. The output figure above illustrates a directed graph consisting of two weakly connected or five strongly connected components (also called blocks of G). Testing whether a directed graph is weakly connected can be done easily in linear time. Simply turn all edges into undirected edges and use the DFS-based connected components algorithm described above. Tests for strong connectivity are somewhat more complicated. The simplest algorithm performs a breadth-first search from each vertex and verifies that all vertices have been visited on each search. Thus in O(mn) time, it can be confirmed whether the graph is strongly connected. Further, this algorithm can be modified to extract all strongly connected components if it is not. In fact, strongly connected components can be found in linear time using one of two more sophisticated DFS-based algorithms. See the references below for details. It is probably easier to start from an existing implementation below than a textbook description. ● How reliable is my network; i.e. how well connected is it? - A chain is only as strong as its weakest link. When it is missing one or more links, it is disconnected. The notion of connectivity of graphs measures the strength of the graph - how many edges or vertices must be removed in order to break it, or disconnect it. Connectivity is an essential invariant for network design and other structural problems. Algorithmic connectivity problems are discussed in Section . In particular, biconnected components are pieces of the graph that result by cutting the edges incident on a single vertex. All file:///E|/BOOK/BOOK4/NODE159.HTM (2 of 4) [19/1/2003 1:30:54]

Connected Components<br />

Next: Topological Sorting Up: Graph Problems: Polynomial-Time Previous: Graph Problems:<br />

Polynomial-Time<br />

Connected Components<br />

Input description: A directed or undirected graph G.<br />

Problem description: Traverse each edge and vertex of all connected components of G.<br />

Discussion: <strong>The</strong> connected components of a graph represent, in grossest terms, the pieces of the graph.<br />

Two vertices are in the same component of G if and only if there is some path between them.<br />

Finding connected components is at the heart of many graph applications. For example, consider the<br />

problem of identifying clusters in a set of items. We can represent each item by a vertex and add an<br />

edge between each pair of items that are deemed ``similar.'' <strong>The</strong> connected components of this graph<br />

correspond to different classes of items.<br />

Testing whether a graph is connected is an essential preprocessing step for every graph algorithm. Such<br />

tests can be performed so quickly and easily that you should always verify that your input graph is<br />

connected, even when you know it has to be. Subtle, difficult-to-detect bugs often result when your<br />

algorithm is run only on one component of a disconnected graph.<br />

file:///E|/BOOK/BOOK4/NODE159.HTM (1 of 4) [19/1/2003 1:30:54]

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

Saved successfully!

Ooh no, something went wrong!