Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Adjacency Matrices

Consider a directed graph tex2html_wrap_inline70549 with n vertices, tex2html_wrap_inline70855. The simplest graph representation scheme uses an tex2html_wrap_inline68097 matrix A of zeroes and ones given by

displaymath70841

That is, the tex2html_wrap_inline60455 element of the matrix, is a one only if tex2html_wrap_inline70863 is an edge in G. The matrix A is called an adjacency matrix  .

For example, the adjacency matrix for graph tex2html_wrap_inline70643 in Figure gif is

displaymath70842

Clearly, the number of ones in the adjacency matrix is equal to the number of edges in the graph.

One advantage of using an adjacency matrix is that it is easy to determine the sets of edges emanating from a given vertex. For example, consider vertex tex2html_wrap_inline70715. Each one in the tex2html_wrap_inline57847 row corresponds to an edge that emanates from vertex tex2html_wrap_inline70715. Conversely, each one in the tex2html_wrap_inline57847 column corresponds to an edge incident on vertex tex2html_wrap_inline70715.

We can also use adjacency matrices to represent undirected graphs. That is, we represent an undirected graph tex2html_wrap_inline70549 with n vertices, using an tex2html_wrap_inline68097 matrix A of zeroes and ones given by

displaymath70843

Since the two sets tex2html_wrap_inline70889 and tex2html_wrap_inline70891 are equivalent, matrix A is symmetric about the diagonal. That is, tex2html_wrap_inline70895. Furthermore, all of the entries on the diagonal are zero. That is, tex2html_wrap_inline70897 for tex2html_wrap_inline68947.

For example, the adjacency matrix for graph tex2html_wrap_inline70797 in Figure gif is

displaymath70844

In this case, there are twice as many ones in the adjacency matrix as there are edges in the undirected graph.

A simple variation allows us to use an adjacency matrix to represent an edge-labeled graph. For example, given numeric edge labels, we can represent a graph (directed or undirected) using an tex2html_wrap_inline68097 matrix A in which the tex2html_wrap_inline68117 is the numeric label associated with edge tex2html_wrap_inline70909 in the case of a directed graph, and edge tex2html_wrap_inline70889, in an undirected graph.

For example, the adjacency matrix for the graph tex2html_wrap_inline70827 in Figure gif is

displaymath70845

In this case, the array entries corresponding to non-existent edges have all been set to tex2html_wrap_inline68397. Here tex2html_wrap_inline68397 serves as a kind of sentinel . The value to use for the sentinel depends on the application. For example, if the edges represent routes between geographic locations, then a route of length tex2html_wrap_inline68397 is much like one that does not exist.

Since the adjacency matrix has tex2html_wrap_inline70835 entries, the amount of spaced needed to represent the edges of a graph is tex2html_wrap_inline70923, regardless of the actual number of edges in the graph. If the graph contains relatively few edges, e.g., if tex2html_wrap_inline70925, then most of the elements of the adjacency matrix will be zero (or tex2html_wrap_inline68397). A matrix in which most of the elements are zero (or tex2html_wrap_inline68397) is a sparse matrix  .


next up previous index

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