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

Using Adjacency Matrices

The GraphAsMatrix class is declared in Program gif. The GraphAsMatrix class is a concrete class derived from the base class Graph which is shown in Program gif. Since GraphAsMatrix is a concrete class, it must provide implementations for all the member functions declared as pure virtual functions in the base classes--the function prototypes have been elided for the sake of brevity.

   program49884
Program: GraphAsMatrix Class Definition

Each instance of the GraphAsMatrix class represents an undirected graph, say tex2html_wrap_inline71355. The two member variables, vertices and adjacencyMatrix, are used to represent the sets tex2html_wrap_inline71357 and tex2html_wrap_inline71363, respectively.

The set of vertices, tex2html_wrap_inline71357, is represented using a one-dimensional array of pointers to Vertex instances. The implementation uses the Array<T> class given in Section gif. The set of edges, tex2html_wrap_inline71363, is represented using a two-dimensional matrix of pointers to Edge instances. The implementation uses the Array2D<T> class given in Section gif.

The GraphAsMatrix constructor takes a single argument of type unsigned int that specifies the maximum number of vertices that the graph may contain. This quantity specifies the length of the array of vertices and the dimensions of the adjacency matrix. The implementation of the GraphAsMatrix class is left as programming project for the reader (Project gif).


next up previous contents index

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