next up previous index
Next: Adjacency lists Up: Graph representations Previous: Graph representations   Index


Adjacency matrix

Some types of graph are defined by knowing, for each pair of nodes, whether or not there is an arc connecting them. In the case of directed graphs, it matters which way round we consider the pair of nodes. There may be an arc in one direction but not in the other.

Assuming a correspondence between the $n$ nodes of a graph and the integers $0,\ldots,n-1$, the graph can be defined by an $n\times n$ matrix of boolean values. The $(i,j)$'th entry of the matrix is true if there is an arc from node $i$ to node $j$ and false otherwise. This is referred to as an adjacency matrix representation.

For a weighted graph it will be necessary to provide storage for the weights, either in a parallel $n\times n$ numerical matrix, or by having a matrix of pairs of booleans and numbers or, possibly, by collapsing the representation into a single matrix by using a device such as indicating non-existence of paths by ``infinite'' weights.

The matrix representation can have advantages in terms of simplicity and time efficiency, but it also suffers from disadvantages. First, it can be extravagant in terms of storage space. The amount of storage required is ${\cal O}(n^{2})$. But for sparse graphs, much less is really needed. It is only necessary to store those arcs and weights which exist. For example, in a graph with around 3000 nodes, almost 1,000,000 units of storage will be needed in the adjacency matrix. But if there are only about 6 or 7 arcs originating from a given node on average, only 20,000 units of storage are really needed.

Secondly the adjacency matrix representation, in its simple form, is unable to represent graphs in which there may be more than one arc between some nodes.


next up previous index
Next: Adjacency lists Up: Graph representations Previous: Graph representations   Index
Peter Williams 2005-06-07