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

Space Comparison

Consider the representation of a directed graph tex2html_wrap_inline69997. In addition to the tex2html_wrap_inline70423 GraphVertex class instances and the tex2html_wrap_inline70435 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_inline70537 matrix of Edges. Therefore, the amount of storage required by an adjacency matrix implementation is

  equation49315

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_inline70423 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_inline70435 linked lists elements, each of refers to the next element of the list and contains an Edge. Therefore, the total space required is

  equation49329

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

displaymath70529

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


next up previous contents index

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