Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Connectedness of a Directed Graph

When dealing with directed graphs, we define two kinds of connectedness, strong and weak. Strong connectedness of a directed graph is defined as follows:

Definition (Strong Connectedness of a Directed Graph)   A directed graph tex2html_wrap_inline71355 is strongly connected   if there is a path in G between every pair of vertices in tex2html_wrap_inline71357.

For example, Figure gif shows the directed graph tex2html_wrap_inline72121 given by

eqnarray51045

Notice that the graph tex2html_wrap_inline72123 is not connected! E.g., there is no path from any of the vertices in tex2html_wrap_inline72125 to any of the vertices in tex2html_wrap_inline67128. Nevertheless, the graph ``looks'' connected in the sense that it is not made of up of separate parts in the way that the graph tex2html_wrap_inline72097 in Figure gif is.

This idea of ``looking'' connected is what weak connectedness represents. To define weak connectedness we need to introduce first the notion of the undirected graph that underlies a directed graph: Consider a directed graph tex2html_wrap_inline71355. The underlying undirected graph is the graph tex2html_wrap_inline72133 where tex2html_wrap_inline72135 represents the set of undirected edges that is obtained by removing the arrowheads from the directed edges in G:

displaymath72113

   figure51054
Figure: An Weakly Connected Directed Graph and the Underlying Undirected Graph

Weak connectedness of a directed graph is defined with respect to its underlying, undirected graph:

Definition (Weak Connectedness of a Directed Graph) A directed graph tex2html_wrap_inline71355 is weakly connected   if the underlying undirected graph tex2html_wrap_inline72145 is connected.

For example, since the undirected graph tex2html_wrap_inline72147 in Figure gif is connected, the directed graph tex2html_wrap_inline72123 is weakly connected. Consider what happens when we remove the edge (b,e) from the directed graph tex2html_wrap_inline72123. The underlying undirected graph that we get is tex2html_wrap_inline72097 in Figure gif. Therefore, when we remove edge (b,e) from tex2html_wrap_inline72123, the graph that remains is neither strongly connected nor weakly connected.

A traversal of a directed graph (either depth-first or breadth-first) starting from a given vertex will only visit all the vertices of an undirected graph if there is a path from the start vertex to every other vertex. Therefore, a simple way to test whether a directed graph is strongly connected uses tex2html_wrap_inline71781 traversals--one starting from each vertex in tex2html_wrap_inline71357. Each time the number of vertices visited is counted. The graph is strongly connected if all the vertices are visited in each traversal.

Program gif shows how this can be implemented. It shows the IsConnected member function of the Digraph class which returns the Boolean value true if the graph is strongly connected.

   program51227
Program: Digraph Class IsConnected Member Function Definition

The routine consists of a loop over all the vertices of the graph. Each iteration does a DepthFirstTraversal using the CountingVisitor given in Program gif. The running time for one iteration is essentially that of the DepthOrderTraversal since tex2html_wrap_inline61332 for the counting visitor. Therefore, the worst-case running time for the IsConnected routine is tex2html_wrap_inline72167 when adjacency matrices are used and tex2html_wrap_inline72169 when adjacency lists are used to represent the graph.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.