Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Testing Strong Connectedness

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_inline70423 traversals--one starting from each vertex in tex2html_wrap_inline69999. 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 isStronglyConnected method of the AbstractGraph class which returns the boolean value true if the graph is strongly connected.

   program50549
Program: AbstractGraph class isConnected method.

The method consists of a loop over all the vertices of the graph. Each iteration does a depthFirstTraversal using a visitor that counts the number of vertices it visits. The running time for one iteration is essentially that of the depthFirstTraversal since tex2html_wrap_inline60212 for the counting visitor. Therefore, the worst-case running time for the isConnected method is tex2html_wrap_inline70819 when adjacency matrices are used and tex2html_wrap_inline70821 when adjacency lists are used to represent the graph.


next up previous contents index

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