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_inline70252. In addition to the tex2html_wrap_inline70678 GraphVertex class instances and the tex2html_wrap_inline70690 GraphEdge class instances contained by the graph, there is the storage required by the adjacency matrix. In this case, the matrix is a tex2html_wrap_inline70792 matrix of Edges. Therefore, the amount of storage required by an adjacency matrix implementation is

  equation49522

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_inline70678 linked lists. If we use the LinkedList class defined in Section gif, each such list has a head and tail field. Altogether there are tex2html_wrap_inline70690 linked lists elements each of which refers to the linked list itself, to the next element of the list and contains an Edge. Therefore, the total space required is

  equation49536

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 object references require the same amount of space, we can conclude that adjacency lists use less space than adjacency matrices when

displaymath70784

For example, given a 10 node graph, the adjacency lists version uses less space when there are fewer than 30 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_inline70798, satisfies tex2html_wrap_inline70800.


next up previous contents index

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