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

Connectedness of an Undirected Graph

Definition (Connectedness of an Undirected Graph)   An undirected graph tex2html_wrap_inline70252 is connected   if there is a path in G between every pair of vertices in tex2html_wrap_inline70254.

Consider the undirected graph shown in Figure gif. It is tempting to interpret this figure as a picture of two graphs. However, the figure actually represents the undirected graph tex2html_wrap_inline71002, given by

eqnarray50372

Clearly, the graph tex2html_wrap_inline71004 is not connected. For example, there is no path between vertices a and d. In fact, the graph tex2html_wrap_inline71004 consists of two, unconnected parts, each of which is a connected sub-graph. The connected sub-graphs of a graph are called connected components  .

   figure50377
Figure: An unconnected, undirected graph with two (connected) components.

A traversal of an undirected graph (either depth-first or breadth-first) starting from any vertex will only visit all the other vertices of the graph if that graph is connected. Therefore, there is a very simply way to test whether an undirected graph is connected: Count the number of vertices visited during a traversal of the graph. Only if all the vertices are visited is the graph connected.

Program gif shows how this can be implemented. The IsConnected property of the AbstractGraph class provides a bool-valued get accessor that returns true if the graph is connected.

   program50555
Program: AbstractGraph class IsConnected property.

The property is implemented using a the DepthFirstTraversal method and a visitor that simply counts the number of vertices it visits. The Visit method adds one the count field of the counter each time it is called.

The worst-case running time of the IsConnected property is determined by the time taken by the DepthFirstTraversal. Clearly in this case tex2html_wrap_inline60481. Therefore, the running time of IsConnected is tex2html_wrap_inline70626 when adjacency matrices are used to represent the graph and tex2html_wrap_inline70822 when adjacency lists are used.


next up previous contents index

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