In this document, I introduce the concept of a graph and describe some ways of representing graphs in the C programming language.
A graph is a collection of nodes called vertices, and the connections between them, called edges.
A representation can often be simplified if it is only being used for undirected graphs, and I'll mention in passing how this can be achieved.
A vertex that is the end-point of an edge is called a neighbour of the vertex that is its starting-point. The first vertex is said to be adjacent to the second.
The following diagram shows a graph with 5 vertices and 7 edges. The edges between A and D and B and C are pairs that make a bidirectional connection, represented here by a double-headed arrow.
More formally, a graph is an ordered pair, G = <V, A>, where V is the set of vertices, and A, the set of arcs, is itself a set of ordered pairs of vertices.
For example, the following expressions describe the graph shown above in set-theoretic language:
V = {A, B, C, D, E}
A = {<A, B>, <A, D>, <B, C>, <C, B>, <D, A>, <D, C>, <D, E>}
A graph implementation needs a basic set of functions to assemble and modify graphs, and to enumerate vertices, edges and neighbours.
The following functions are provided by each representation. These are the declarations for the intuitive representation, MBgraph1:
All of the graph representations use the following definition of a vertex:
typedef struct {
char * name;
void * data;
void * body;
MBdeletefn deletefn;
} MBvertex;
Note the body field, which is not of interest to clients, but is used by some representations (Adjacency List and Incidence List) to add per-vertex strucure.
The following functions are provided for working with vertices:
How edges are implemented internally varies with the representation. In fact, in three representations, Adjacency List, Adjacency Matrix and Incidence Matrix, edges do not exist internally as objects at all. From the viewpoint of clients however, edges, as enumerated by the iterator returned by the function to retrieve edges, are this structure:
typedef struct {
MBvertex *from;
MBvertex *to;
} MBedge;
The following functions are provided for working with edges:
The following program constructs the graph shown in the introduction using the intuitive representation, MBgraph1, and then enumerates the vertices, neighbours and edges:
#include <stdio.h>
#include <graph1.h>
int main(void)
{
MBgraph1 *graph;
MBvertex *vertex;
MBvertex *A, *B, *C, *D, *E;
MBiterator *vertices, *edges;
MBedge *edge;
/* Create a graph */
graph = MBgraph1_create();
/* Add vertices */
A = MBgraph1_add(graph, "A", NULL);
B = MBgraph1_add(graph, "B", NULL);
C = MBgraph1_add(graph, "C", NULL);
D = MBgraph1_add(graph, "D", NULL);
E = MBgraph1_add(graph, "E", NULL);
/* Add edges */
MBgraph1_add_edge(graph, A, B);
MBgraph1_add_edge(graph, A, D);
MBgraph1_add_edge(graph, B, C);
MBgraph1_add_edge(graph, C, B);
MBgraph1_add_edge(graph, D, A);
MBgraph1_add_edge(graph, D, C);
MBgraph1_add_edge(graph, D, E);
/* Display */
printf("Vertices (%d) and their neighbours:\n\n", MBgraph1_get_vertex_count(graph));
vertices = MBgraph1_get_vertices(graph);
while ((vertex = MBiterator_get(vertices))) {
MBiterator *neighbours;
MBvertex *neighbour;
unsigned int n = 0;
printf("%s (%d): ", MBvertex_get_name(vertex), MBgraph1_get_neighbour_count(graph, vertex));
neighbours = MBgraph1_get_neighbours(graph, vertex);
while ((neighbour = MBiterator_get(neighbours))) {
printf("%s", MBvertex_get_name(neighbour));
if (n < MBgraph1_get_neighbour_count(graph, vertex) - 1) {
fputs(", ", stdout);
}
n++;
}
putchar('\n');
MBiterator_delete(neighbours);
}
putchar('\n');
MBiterator_delete(vertices);
printf("Edges (%d):\n\n", MBgraph1_get_edge_count(graph));
edges = MBgraph1_get_edges(graph);
while ((edge = MBiterator_get(edges))) {
printf("<%s, %s>\n", MBvertex_get_name(MBedge_get_from(edge)), MBvertex_get_name(MBedge_get_to(edge)));
}
putchar('\n');
MBiterator_delete(edges);
/* Delete */
MBgraph1_delete(graph);
return 0;
}
There are essentially 5 ways of representing a graph:
What I call the "intuitive" and can also called the "object-oriented" representation is a direct translation of the mathematical definition of a graph into a data type:
typedef struct {
MBset * vertices;
MBset * edges;
} MBgraph1;
The graph is made up of a set of vertices. Each vertex contains a set of vertices for its neighbours.
typedef struct {
MBset *vertices;
} MBgraph2;
typedef struct {
MBset *neighbours;
} vertex_body;
For the graph shown in the introduction, the sets of neighbours would look like this:
A: {B, D}
B: {C}
C: {B}
D: {A, C, E}
E: {}
The graph is made up of a set of vertices and a matrix, whose rows and columns are indexed by vertices, and which contains a 1 entry if the vertices are connected.
typedef struct {
MBset * vertices;
MBmatrix * edges;
} MBgraph3;
The adjacency matrix for the graph shown in the introduction would look like this:
The graph is made up of a set of vertices and a matrix, as in Adjacency Matrix, but the matrix is vertices × edges, with each column containing two non-zero entries, one for the starting-point vertex and one for the end-point.
typedef struct {
MBset * vertices;
MBmatrix * edges;
} MBgraph4;
The incidence matrix for the graph shown in the introduction looks like this (1 means "from" and 2 means "to"):
There is a set of vertices as in Adjacency List, but each vertex stores a list of the edges that it is the starting-point of, rather than neighbours.
typedef struct {
MBset * vertices;
} MBgraph5;
typedef struct {
MBset *edges;
} vertex_body;
For the graph shown in the introduction, the sets of edges would look like this:
A: {<A, B>, <A, D>}
B: {<B, C>}
C: {<C, B>}
D: {<D, A>, <D, C>, <D, E>}
E: {}
The following archives contain the full source code, example program and build instructions:
Copyright (C) 2010 Martin Broadhurst