O'Reilly logo

Mastering Algorithms with C by Kyle Loudon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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" /***************************************************************************** ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required