Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [117]

By Root 1589 0
with the data.

Complexity

O (V ), where V is the number of vertices in the graph.

Name


graph_adjlist

Synopsis

int graph_adjlist(const Graph *graph, const void *data, AdjList **adjlist);

Return Value

0if retrieving the adjacency list is successful, or -1 otherwise.

Description

Retrieves vertices that are adjacent to the vertex specified by data in graph. The adjacent vertices are returned in the form of an AdjList structure, a structure containing the vertex matching data and a set of vertices adjacent to it. A pointer to the actual adjacency list in the graph is returned, so it must not be manipulated by the caller.

Complexity

O (V ), where V is the number of vertices in the graph.

Name


graph_is_adjacent

Synopsis

int graph_is_adjacent(const Graph *graph, const void *data1, const void *data2);

Return Value

1 if the second vertex is adjacent to the first vertex, or otherwise.

Description

Determines whether the vertex specified by data2 is adjacent to the vertex specified by data1 in graph.

Complexity

O (V ), where V is the number of vertices in the graph.

Name


graph_adjlists

Synopsis

List graph_adjlists(const Graph *graph);

Return Value

List of adjacency-list structures.

Description

Macro that evaluates to the list of adjacency-list structures in graph. Each element in the list is an AdjList structure. The actual list of adjacency-list structures in the graph is returned, so it must not be manipulated by the caller.

Complexity

O (1)

Name


graph_vcount

Synopsis

int graph_vcount(const Graph *graph);

Return Value

Number of vertices in the graph.

Description

Macro that evaluates to the number of vertices in the graph specified by graph.

Complexity

O (1)

Name


graph_ecount

Synopsis

int graph_ecount(const Graph *graph);

Return Value

Number of edges in the graph.

Description

Macro that evaluates to the number of edges in the graph specified by graph.

Complexity

O (1)

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

#include "list.h"

#include "set.h"

/*****************************************************************************

* *

* Define a structure for adjacency lists. *

* *

*****************************************************************************/

typedef struct AdjList_ {

void *vertex;

Set adjacent;

} AdjList;

/*****************************************************************************

* *

* Define a structure for graphs. *

* *

*****************************************************************************/

typedef struct Graph_ {

int vcount;

int ecount;

int (*match)(const void *key1, const void *key2);

void (*destroy)(void *data);

List adjlists;

Return Main Page Previous Page Next Page

®Online Book Reader