This page was last updated February 21, 2003


Graph Basics


Graph Terms


Graph Symbols

Frequently symbols are used to represent graphs and their components as a sort of short-hand. The following are the more commonly used symbols.

Graph Representation

There are two primary way of representing graphs in code. These are the Adjacency Matrix and the Adjacency Set

Adjacency Matrix

In an Adjacency Matrix representation the nodes in a graph are represented as an array of data structures. The links are then represented in a matrix defined as a two-dimensional array. Each row in the matrix represents a the node in the array with the same index. The columns in the row then represent links to other nodes. For example, suppose we have the following simple graph:

We can define the needed data arrays and structures as:
	struct GraphNode
	{
		// Define data to be stored in each node
	};

	GraphNode NodeArray[5];
	int AdjMatrix[5][5];
To represent the links we would let the rows in AdjMatrix represent each node with row 0 corresponding to the node store in NodeArray[0] and representing Node 0 in the diagram. To represent the links from this node to others (Nodes 1, 2, and 3) in the graph we would set columns 1, 2, and 3 in of row 0 to 1 and all the other columns to 0. We let the collumn corresponding to the node itself default to 0. The complete matrix would look like this:

  0 1 2 3 4
0 0 1 1 1 0
1 1 0 1 1 1
2 1 1 0 0 1
3 1 1 0 0 1
4 0 1 1 1 0


Adjacency Set

In an Adjacency Set representation the nodes in a graph are represented as a linked list of data structures. The links are then represented as a linked list of link data structures. Using the same graph as above we can define appropriate data structures.

	struct GraphLink;  // Forward declaration so it can be used
				// in the next structure
	struct GraphNode
	{
		// Define data to be stored in each node
		GraphNode *next;
		GraphLink *links;
	};

	struct GraphLink	// Now define the GraphLink structure
	{
		GraphNode *linkTo;
		GraphLink *next;
	};

Now the graph would be represented as a linked list of linked lists like this: