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

Space Comparison

Consider the representation of a directed graph tex2html_wrap_inline71355. In addition to the tex2html_wrap_inline71781 Vertex class instances and the tex2html_wrap_inline71793 Edge class instances contained by the graph, there is the storage required by the adjacency matrix. In this case, the matrix is a tex2html_wrap_inline71885 matrix of pointers to Edge instances. Therefore, the amount of storage required by an adjacency matrix implementation is

  equation49979

On the other hand, consider the amount of storage required when we represent the same graph using adjacency lists. In addition to the vertices and the edges themselves, there are tex2html_wrap_inline71781 linked lists. If we use the LinkedList class defined in Section gif, each such list has a head and tail pointer. Altogether there are tex2html_wrap_inline71793 linked lists elements, each of which consists of a pointer to the next element of the lists and a pointer to an edge. Therefore, the total space required is

  equation49991

Notice that the space for the vertices and edges themselves cancels out when we compare Equation gif with Equation gif. If we assume that all pointers require the same amount of space, we can conclude that adjacency lists use less space than adjacency matrices when

displaymath71877

For example, given a 10 node graph, the adjacency lists version uses less space when there are fewer than 45 edges. As a rough rule of thumb, we can say that adjacency lists use less space when the average degree of a node, tex2html_wrap_inline71891, satisfies tex2html_wrap_inline71893.


next up previous contents index

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