Adjacency Matrix

An unweighted Graph

A directed Graph

A weighted, directed Graph

Modelling the adjacency matrix

It is clear that we can used a 2D array to model an adjacency matrix.

An array is a static data structure, so brings with it advantages and disadvantages.

Advantages:

It is very quick and simple to test for an edge between vertices e.g.

IF (Matrix(2,4) == 1)

Disadvantages:

Having a lot of nodes with few edges means inefficient memory usage.

Theoretically creating additional nodes or deleting existing nodes is not computationally simple

As a consequence it is only sensible to use a matrix if there are lots of connections between nodes or you need to frequently test for the presence of an edge.

Imagine using a 2D array for a sparse graph...

Key Words

Adjacency Matrix

A method of storing a graph using a 2D array data structure that stores if there is an edge (or the weight of an edge) between nodes.