Implementation and Analysis of Graphs

An adjacency-list representation of a graph primarily consists of a linked list of adjacency-list structures. Each structure in the list contains two members: a vertex and a list of vertices adjacent to the vertex. In the implementation presented here, an individual adjacency list is represented by the structure AdjList (see Example 11.1). As you would expect, this structure has two members that correspond to those just mentioned. Each adjacency list is implemented as a set (see Chapter 7) for reasons discussed in the questions and answers at the end of the chapter. The structure Graph is the graph data structure (see Example 11.1). This structure consists of five members: vcount is the number of vertices in the graph, ecount is the number of edges, match and destroy are members used to encapsulate the functions passed to graph_init, and adjlists is the linked list of adjacency-list structures. Example 11.1 also defines an enumerated type for vertex colors, which are often used when working with graphs.

Example 11.1. Header for the Graph Abstract Datatype
/***************************************************************************** * * * -------------------------------- graph.h ------------------------------- * * * *****************************************************************************/ #ifndef GRAPH_H #define GRAPH_H #include <stdlib.h> #include "list.h" #include "set.h" /***************************************************************************** ...

Get Mastering Algorithms with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.