graph.h

This interface exports a simple graph abstraction. In this abstraction, graphs closely model their mathematical formulation as a set of nodes connected by a set of arcs.
Types
Graph This type represents the abstract type for a graph.
Node This type is the abstract type for a node in a graph.
Arc This type is the abstract type for an arc in a graph.
Functions
newGraph() Returns a new graph with no nodes or arcs.
freeGraph(g) Frees the storage for the graph, along with its nodes and arcs.
addNode(g, name) Adds a node to the graph with the specified name.
removeNode(g, node) Removes and frees the specified node from the graph, along with any arcs that enter or leave that node.
getNode(g, name) Returns the node in the graph that has the specified name.
addArc(g, n1, n2) Adds a new arc to the graph connecting nodes n1 and n2.
removeArc(g, arc) Removes and frees the specified arc.
isConnected(n1, n2) Returns true if there is an arc from n1 to n2.
getNodeSet(g) Returns a set consisting of all nodes in the graph.
getArcSet(g)
getArcSet(node) 
Returns a set consisting of all arcs, either in the graph or starting at the specified node.
getNeighbors(node) Returns a set consisting of the nodes to which a given node is connected.
getName(node) Returns the name of the node.
startOfArc(arc) Returns the node at the beginning of the specified arc.
endOfArc(arc) Returns the node at the end of the specified arc.
getCost(arc) Returns the "cost" associated with traversing an arc.
setCost(arc, cost) Sets the cost of traversing the arc.
setNodeOrdering(graph, cmpFn) Sets the comparison function used to order the nodes in a graph when they are enumerated.
setArcOrdering(graph, cmpFn) Sets the comparison function used to order the arcs in a graph when they are enumerated.

Type detail


typedef struct GraphCDT *Graph;
This type represents the abstract type for a graph. Conceptually, a Graph consists of a set of nodes together with a set of arcs. Each arc connects two nodes in one direction. Undirected graphs must include two arcs for each bidirectional connection.

typedef struct NodeCDT *Node;
This type is the abstract type for a node in a graph. Clients can store their own data in a node by using the functions getBlockData and setBlockData described in cslib.h.

typedef struct ArcCDT *Arc;
This type is the abstract type for an arc in a graph. Clients can store their own data in an arc by using the functions getBlockData and setBlockData described in cslib.h.

Function detail


Graph newGraph(void);
Returns a new graph with no nodes or arcs.

Usage:

g = newGraph();

void freeGraph(Graph g);
Frees the storage for the graph, along with its nodes and arcs.

Usage:

freeGraph(g);

Node addNode(Graph g, string name);
Adds a node to the graph with the specified name. If there is already a node with that name, addNode generates an error. The function returns the Node value.

Usage:

node = addNode(g, name);

void removeNode(Graph g, Node node);
Removes and frees the specified node from the graph, along with any arcs that enter or leave that node.

Usage:

removeNode(g, node);

Node getNode(Graph g, string name);
Returns the node in the graph that has the specified name. If there is no such node, getNode returns NULL.

Usage:

node = getNode(g, name);

Arc addArc(Graph g, Node n1, Node n2);
Adds a new arc to the graph connecting nodes n1 and n2. The function returns the Arc value.

Usage:

arc = addArc(g, n1, n2);

void removeArc(Graph g, Arc arc);
Removes and frees the specified arc.

Usage:

removeArc(g, arc);

bool isConnected(Node n1, Node n2);
Returns true if there is an arc from n1 to n2.

Usage:

if (isConnected(n1, n2)) . . .

Set getNodeSet(Graph g);
Returns a set consisting of all nodes in the graph. This function is typically used in conjunction with the foreach macro to initialize an iterator. For example, the following idiom iterates over the nodes in the specified graph:
   foreach (node in getNodeSet(g)) . . .

Usage:

nodeSet = getNodeSet(g);

Set getArcSet(void *arg);
Returns a set consisting of all arcs, either in the graph or starting at the specified node. This function is typically used in conjunction with the foreach macro to initialize an iterator. For example, the following idiom iterates over the arcs in the specified graph:
   foreach (arc in getArcSet(g)) . . .

Usage:

arcSet = getArcSet(g);
arcSet = getArcSet(node);

Set getNeighbors(Node node);
Returns a set consisting of the nodes to which a given node is connected. This function is typically used in conjunction with the foreach macro to initialize an iterator. For example, the following idiom iterates over the nodes to which the node start is connected:
   foreach (node in getNeighbors(start)) . . .

Usage:

nodeSet = getNeighbors(node);

string getName(Node node);
Returns the name of the node.

Usage:

str = getName(node);

Node startOfArc(Arc arc);
Returns the node at the beginning of the specified arc.

Usage:

node = startOfArc(arc);

Node endOfArc(Arc arc);
Returns the node at the end of the specified arc.

Usage:

node = endOfArc(arc);

double getCost(Arc arc);
Returns the "cost" associated with traversing an arc. This cost need not be economic and will often refer to some other metric, such as distance.

Usage:

cost = getCost(arc);

void setCost(Arc arc, double cost);
Sets the cost of traversing the arc.

Usage:

setCost(arc, cost);

void setNodeOrdering(Graph graph, CompareFn cmpFn);
Sets the comparison function used to order the nodes in a graph when they are enumerated. By default, nodes are sorted in alphabetical order by name.

Usage:

setNodeOrdering(graph, cmpFn);

void setArcOrdering(Graph graph, CompareFn cmpFn);
Sets the comparison function used to order the arcs in a graph when they are enumerated. By default, arcs are sorted in alphabetical order by the name of the start node followed by the name of the end node.

Usage:

setArcOrdering(graph, cmpFn);