| 1 |
\documentclass[11pt,letterpaper]{article} |
| 2 |
\usepackage{url} |
| 3 |
\usepackage{graphicx} |
| 4 |
\usepackage{newtxtext} |
| 5 |
\usepackage{xcolor} |
| 6 |
\usepackage[]{hyperref} |
| 7 |
\usepackage[cmintegrals,cmbraces]{newtxmath} % times roman, the classic |
| 8 |
\hypersetup{ |
| 9 |
colorlinks = true, %Colours links instead of ugly boxes |
| 10 |
urlcolor = {blue!80!black}, %Colour for external hyperlinks |
| 11 |
linkcolor = {blue!80!black}, %Colour of internal links |
| 12 |
citecolor = red %Colour of citations |
| 13 |
} |
| 14 |
|
| 15 |
\usepackage{lgrind} |
| 16 |
\usepackage{footnote} |
| 17 |
|
| 18 |
%\usepackage[margin=0.5in]{geometry} |
| 19 |
|
| 20 |
\addtolength{\oddsidemargin}{-.875in} |
| 21 |
\addtolength{\evensidemargin}{-.875in} |
| 22 |
\addtolength{\textwidth}{1.75in} |
| 23 |
|
| 24 |
\renewcommand\floatpagefraction{.9} |
| 25 |
\renewcommand\topfraction{.9} |
| 26 |
\renewcommand\bottomfraction{.9} |
| 27 |
\renewcommand\textfraction{.1} |
| 28 |
%\setcounter{totalnumber}{50} |
| 29 |
%\setcounter{topnumber}{50} |
| 30 |
%\setcounter{bottomnumber}{50} |
| 31 |
|
| 32 |
%\setlength{\textwidth}{8.0in} |
| 33 |
%\oddsidemargin 0in |
| 34 |
%\evensidemargin .5in |
| 35 |
%\textwidth 7.5in |
| 36 |
|
| 37 |
\author{Stephen C. North \\ |
| 38 |
{\small [email protected]} |
| 39 |
\and Emden R.~Gansner \\ |
| 40 |
{\small [email protected]} |
| 41 |
} |
| 42 |
\title{Cgraph Tutorial} |
| 43 |
\date{\today} |
| 44 |
|
| 45 |
\begin{document} |
| 46 |
|
| 47 |
%\begin{titlepage} |
| 48 |
\maketitle |
| 49 |
\newpage |
| 50 |
\tableofcontents |
| 51 |
\newpage |
| 52 |
%\thispagestyle{empty} |
| 53 |
%\end{titlepage} |
| 54 |
%\pagenumbering{arabic} |
| 55 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 56 |
|
| 57 |
\section{Introduction} |
| 58 |
\label{sec:introduction} |
| 59 |
Cgraph is a C library for graph programming. |
| 60 |
It defines data types and operations for graphs comprised |
| 61 |
of attributed nodes, edges and subgraphs. |
| 62 |
Attributes may be string name-value pairs for convenient |
| 63 |
file I/O, or internal C data structures for efficient algorithm |
| 64 |
implementation. |
| 65 |
|
| 66 |
Cgraph is aimed at graph representation; it is not an library of |
| 67 |
higher-level algorithms such as shortest path or network flow. |
| 68 |
We envision these as higher-level libraries written on top of |
| 69 |
Cgraph. Efforts were made in Cgraph's design to strive |
| 70 |
for time and space efficiency. The basic (unattributed) graph representation |
| 71 |
takes 104 bytes per node and 64 bytes per edge, so storage of graphs with |
| 72 |
millions of objects is reasonable. For attributed graphs, Cgraph also |
| 73 |
maintains an internal shared string pool, so if all the nodes of a graph |
| 74 |
have \verb'color=red', only one copy of \verb'"color"' and \verb'"red"' |
| 75 |
are made. There are other tricks that experts can exploit for |
| 76 |
flat-out coding efficiency. For example, there are ways to inline the |
| 77 |
instructions for edge list traversal and internal data structure access. |
| 78 |
|
| 79 |
Cgraph uses Phong Vo's dictionary library, libcdt, to store node |
| 80 |
and edge sets. This library provides a uniform interface to hash |
| 81 |
tables and splay trees, and its API is usable for general programming |
| 82 |
(such as storing multisets, hash tables, lists and queues) in Cgraph |
| 83 |
programs. |
| 84 |
|
| 85 |
{\bf Notation} In the following, we use \verb"TRUE" to denote a non-zero |
| 86 |
value, and \verb"FALSE" to denote zero. |
| 87 |
\section{Graph Objects} |
| 88 |
\label{sec:graphobjects} |
| 89 |
Almost all Cgraph programming can be done with pointers to |
| 90 |
these data types: |
| 91 |
|
| 92 |
\begin{itemize} |
| 93 |
\item \verb"Agraph_t": a graph or subgraph |
| 94 |
\item \verb"Agnode_t": a node from a particular graph or subgraph |
| 95 |
\item \verb"Agedge_t": an edge from a particular graph or subgraph |
| 96 |
\item \verb"Agsym_t": a descriptor for a string-value pair attribute |
| 97 |
\item \verb"Agrec_t": an internal C data record attribute of a graph object |
| 98 |
\end{itemize} |
| 99 |
|
| 100 |
Cgraph is responsible for its own memory management; allocation |
| 101 |
and deallocation of Cgraph data structures is always done through |
| 102 |
Cgraph calls. |
| 103 |
|
| 104 |
\section{Graphs} |
| 105 |
\label{sec:graphs} |
| 106 |
A top-level graph (also called a root graph) defines a universe |
| 107 |
of nodes, edges, subgraphs, data dictionaries and other information. |
| 108 |
A graph has a name and two properties: whether it is directed or |
| 109 |
undirected, and whether it is strict (multi-edges are |
| 110 |
forbidden). \footnote{It is possible to specify that a graph is simple |
| 111 |
(neither multi-edges nor loops), or can have multi-edges but not loops.} |
| 112 |
|
| 113 |
Note that nodes, edges and subgraphs exists in exactly one root graph. They |
| 114 |
cannot be used independently of that graph, or attached to another root graph. |
| 115 |
|
| 116 |
The following examples use the convention that \verb"G" and |
| 117 |
\verb"g" are \verb"Agraph_t*" (graph pointers), \verb"n", \verb"u", |
| 118 |
\verb"v", \verb"w" are \verb"Agnode_t*" (node pointers), and |
| 119 |
\verb"e", \verb"f" are \verb"Agedge_t*" (edge pointers). |
| 120 |
|
| 121 |
To make a new, empty top-level directed graph: |
| 122 |
|
| 123 |
\begin{verbatim} |
| 124 |
Agraph_t *g; |
| 125 |
g = agopen("G", Agdirected, NULL); |
| 126 |
\end{verbatim} |
| 127 |
|
| 128 |
The first argument to \verb"agopen" is any string, and is not |
| 129 |
interpreted by Cgraph, except it is recorded and preserved when |
| 130 |
the graph is written as a file.\footnote{An application |
| 131 |
could, of course, maintain its own graph catalogue using graph names.} |
| 132 |
The second argument indicates the type of graph, and should be one of |
| 133 |
{\tt Agdirected}, {\tt Agstrictdirected}, {\tt Agundirected}, |
| 134 |
or {\tt Agstrictundirected}. |
| 135 |
The third argument is an optional pointer to a collection of |
| 136 |
methods for overriding certain default behaviors of Cgraph, |
| 137 |
and in most situations can just be {\tt NULL}. |
| 138 |
|
| 139 |
You can get the name of a graph by \verb"agnameof(g)", and |
| 140 |
you can get its properties by the predicate functions |
| 141 |
{\tt agisdirected(g)} and {\tt agisstrict(g)}. |
| 142 |
|
| 143 |
You can also construct a new graph by reading a file: |
| 144 |
\begin{verbatim} |
| 145 |
g = agread(stdin,NULL); |
| 146 |
\end{verbatim} |
| 147 |
|
| 148 |
Here, the graph's name, type and contents including attributes depend |
| 149 |
on the file contents. (The second argument is the same optional method |
| 150 |
pointer mentioned above for \verb"agopen"). If the default I/O discipline |
| 151 |
is used and the stream is wide-oriented, the behavior is undefined. |
| 152 |
|
| 153 |
Sometimes it is convenient to have the graph represented concretely as a |
| 154 |
character string \verb"str". |
| 155 |
In this case, the graph can be created using: |
| 156 |
\begin{verbatim} |
| 157 |
g = agmemread (str); |
| 158 |
\end{verbatim} |
| 159 |
|
| 160 |
You can write a representation of a graph to a file: |
| 161 |
|
| 162 |
\begin{verbatim} |
| 163 |
g = agwrite(g,stdout); |
| 164 |
\end{verbatim} |
| 165 |
|
| 166 |
\verb"agwrite" creates an external representation of a graph's |
| 167 |
contents and attributes (except for internal attributes), |
| 168 |
that can later be reconstructed by calling \verb"agread" |
| 169 |
on the same file.\footnote{It is the application programmer's job to |
| 170 |
convert between internal attributes to external strings |
| 171 |
when graphs are read and written, if desired. |
| 172 |
This seemed better than inventing a complicated way |
| 173 |
to automate this conversion.} If the default I/O discipline is used |
| 174 |
and the stream is wide-oriented, the behavior is undefined. |
| 175 |
|
| 176 |
\verb"agnnodes(g)", \verb"agnedges(g)" and \verb"agnsubg(g)" return the count of nodes, |
| 177 |
edges and (immediate) subgraphs in a graph (or subgraph). |
| 178 |
|
| 179 |
To delete a graph and its associated data structures, |
| 180 |
freeing their memory, one uses: |
| 181 |
\begin{verbatim} |
| 182 |
agclose(g); |
| 183 |
\end{verbatim} |
| 184 |
|
| 185 |
Finally, there is an interesting if obscure function to concatenate |
| 186 |
the contents of a graph file onto an existing graph, as shown here. |
| 187 |
\begin{verbatim} |
| 188 |
g = agconcat(g,stdin,NULL); |
| 189 |
\end{verbatim} |
| 190 |
|
| 191 |
\section{Nodes} |
| 192 |
\label{sec:nodes} |
| 193 |
In Cgraph, a node is usually identified by a unique string name |
| 194 |
and a unique internal integer ID assigned by Cgraph. |
| 195 |
(For convenience, you can also create "anonymous" nodes by |
| 196 |
giving \verb"NULL" as the node name.) A node also has in- and out-edge sets, |
| 197 |
even in undirected graphs. |
| 198 |
|
| 199 |
Once you have a graph, you can create or search for nodes this way: |
| 200 |
\begin{verbatim} |
| 201 |
Agnode_t *n; |
| 202 |
n = agnode(g,"node28",TRUE); |
| 203 |
\end{verbatim} |
| 204 |
|
| 205 |
The first argument is a graph or subgraph in which the node |
| 206 |
is to be created. The second is the name (or NULL for anonymous nodes.) |
| 207 |
When the third argument is \verb"TRUE", the node is created if it doesn't |
| 208 |
already exist. When it's \verb"FALSE", as shown below, then Cgraph |
| 209 |
searches to locate an existing node with the given name, returning NULL if |
| 210 |
none is found. |
| 211 |
|
| 212 |
\begin{verbatim} |
| 213 |
n = agnode(g,"node28",FALSE); |
| 214 |
\end{verbatim} |
| 215 |
|
| 216 |
The function \verb"agdegree(g, n, in, out)" gives the degree |
| 217 |
of a node in (sub)graph \verb"g", where \verb"in" and \verb"out" select the edge sets. |
| 218 |
\begin{itemize} |
| 219 |
\item \verb"agdegree(g,n,TRUE,FALSE)" returns the in-degree. |
| 220 |
\item \verb"agdegree(g,n,FALSE,TRUE)" returns the out-degree. |
| 221 |
\item \verb"agdegree(g,n,TRUE,TRUE)" returns their sum. |
| 222 |
\end{itemize} |
| 223 |
The function \verb"agcountuniqedges" is identical to {\tt agdegree} |
| 224 |
except when the last two arguments are both \verb"TRUE". In this case, a loop is only |
| 225 |
counted once. |
| 226 |
|
| 227 |
\verb"agnameof(n)" returns the printable string name of a node. |
| 228 |
Note that for various reasons this string may be a temporary |
| 229 |
buffer that can be overwritten by subsequent calls. |
| 230 |
Thus, the usage |
| 231 |
\begin{verbatim} |
| 232 |
printf("%s %s\n",agnameof(agtail(e)),agnameof(aghead(e))); |
| 233 |
\end{verbatim} |
| 234 |
is unsafe because the buffer may be overwritten when the |
| 235 |
arguments to printf are being computed. |
| 236 |
|
| 237 |
A node can be deleted from a graph or subgraph by \verb"agdelnode(g,n)". |
| 238 |
|
| 239 |
\section{Edges} |
| 240 |
\label{sec:edges} |
| 241 |
An edge is a node pair: an ordered pair in a directed graph, |
| 242 |
unordered in an undirected graph. For convenience there is |
| 243 |
a common edge data structure for both kinds and the endpoints |
| 244 |
are the fields \verb"tail" and \verb"head". \footnote{When an edge is created, |
| 245 |
the first node will be used as the tail node, and the second node as the head.} |
| 246 |
|
| 247 |
Because an edge is implemented as an edge pair, there are two valid pointers |
| 248 |
to the same edge, so simple pointer comparison does not work for edge equality. |
| 249 |
The function \verb"ageqedge(Agedge_t *e0, Agedge_t *e1)" evaluates to true if |
| 250 |
the two pointers represent the same abstract edge, and should usually be used |
| 251 |
for edge comparisons. |
| 252 |
|
| 253 |
An edge is made using |
| 254 |
|
| 255 |
\begin{verbatim} |
| 256 |
Agnode_t *u, *v; |
| 257 |
Agedge_t *e; |
| 258 |
|
| 259 |
/* assume u and v are already defined */ |
| 260 |
e = agedge(g,u,v,"e28",TRUE); |
| 261 |
\end{verbatim} |
| 262 |
|
| 263 |
\verb"u" and \verb"v" must belong to the same graph or subgraph |
| 264 |
for the operation to succeed. The ``name'' of an edge |
| 265 |
(more correctly, identifier) is treated as a unique identifier for edges |
| 266 |
between a particular node pair. That is, there can only be at most |
| 267 |
one edge with name \verb"e28" between any given \verb"u" and \verb"v", |
| 268 |
but there can be many other edges \verb"e28" between other nodes. |
| 269 |
|
| 270 |
\verb"agtail(e)" and \verb"aghead(e)" return the endpoints of \verb"e". |
| 271 |
If \verb"e" is created as in the call to \verb"agedge" above, \verb"u" |
| 272 |
will be the tail node and \verb"v" will be the head node. This holds true |
| 273 |
even for undirected graphs. |
| 274 |
|
| 275 |
The value \verb"e->node" is the ``other'' endpoint |
| 276 |
with respect to the node from which e was obtained. |
| 277 |
That is, if \verb"e" is an out-edge of node \verb"n" (equivalently, \verb"n" |
| 278 |
is the tail of \verb"e"), then \verb"e->node" is the head of \verb"e". |
| 279 |
A common idiom is: |
| 280 |
\begin{verbatim} |
| 281 |
for (e = agfstout(g,n); e; e = agnxtout(g,e)) |
| 282 |
/* do something with e->node */ |
| 283 |
\end{verbatim} |
| 284 |
|
| 285 |
\verb"agedge" can also search for edges: |
| 286 |
|
| 287 |
\begin{verbatim} |
| 288 |
/* finds any u,v edge */ |
| 289 |
e = agedge(g,u,v,NULL,FALSE); |
| 290 |
/* finds a u,v edge with name "e8" */ |
| 291 |
e = agedge(g,u,v,"e8",FALSE); |
| 292 |
\end{verbatim} |
| 293 |
|
| 294 |
In an undirected graph, an edge search will consider the given vertices |
| 295 |
as both tail and head nodes. |
| 296 |
|
| 297 |
An edge can be deleted from a graph or subgraph by \verb"agdeledge(g,e)". |
| 298 |
The \verb"agnameof" function can be used to get the ``name'' of an edge. |
| 299 |
Note that this will be the identifier string provided at edge creation. |
| 300 |
The names |
| 301 |
of the head and tail nodes will not be part of the string. In addition, it |
| 302 |
returns NULL for anonymous edges. |
| 303 |
|
| 304 |
\section{Traversals} |
| 305 |
\label{sec:traversals} |
| 306 |
|
| 307 |
Cgraph has functions for iterating over graph objects. |
| 308 |
For example, we can scan all the edges of a graph (directed |
| 309 |
or undirected) by the following: |
| 310 |
|
| 311 |
\begin{verbatim} |
| 312 |
for (n = agfstnode(g); n; n = agnxtnode(g,n)) { |
| 313 |
for (e = agfstout(g,n); e; e = agnxtout(g,e)) { |
| 314 |
/* do something with e */ |
| 315 |
} |
| 316 |
} |
| 317 |
\end{verbatim} |
| 318 |
\label{traversal} |
| 319 |
|
| 320 |
The functions \verb"agfstin(g,n)" and \verb"afnxtin(g,e)" are |
| 321 |
provided for walking in-edge lists. |
| 322 |
|
| 323 |
In the case of a directed edge, the meanings of ``in'' and ``out'' are |
| 324 |
obvious. For undirected graphs, Cgraph assigns an |
| 325 |
orientation based on the analogous order of the two nodes when the edge |
| 326 |
is created. |
| 327 |
|
| 328 |
To visit all the edges of a node in an undirected graph: |
| 329 |
|
| 330 |
\begin{verbatim} |
| 331 |
for (e = agfstedge(g,n); e; e = agnxtedge(g,e,n)) |
| 332 |
/* do something with e */ |
| 333 |
\end{verbatim} |
| 334 |
|
| 335 |
Be careful if your code deletes an edge or node during the traversal, as |
| 336 |
then the object will no longer be valid to get the next object. |
| 337 |
This is typically handled by code like: |
| 338 |
\begin{verbatim} |
| 339 |
for (e = agfstedge(g,n); e; e = f) { |
| 340 |
f = agnxtedge(g,e,n)) |
| 341 |
/* delete e */ |
| 342 |
} |
| 343 |
\end{verbatim} |
| 344 |
|
| 345 |
Traversals are guaranteed to visit the nodes of a graph, or edges of a node, |
| 346 |
in their order of creation in the root graph (unless we allow programmers |
| 347 |
to override object ordering, as mentioned in section~\ref{sec:openissues}). |
| 348 |
|
| 349 |
\section{External Attributes} |
| 350 |
\label{sec:externalattributes} |
| 351 |
|
| 352 |
Graph objects may have associated string name-value pairs. |
| 353 |
When a graph file is read, Cgraph's parser takes care of |
| 354 |
the details of this, so attributes can just be added |
| 355 |
anywhere in the file. In C programs, values must be |
| 356 |
declared before use. |
| 357 |
|
| 358 |
Cgraph assumes that all objects of a given kind (graphs/subgraphs, |
| 359 |
nodes, or edges) have the same attributes - there's no notion of |
| 360 |
subtyping within attributes. Information about attributes is |
| 361 |
stored in data dictionaries. Each graph has three (for |
| 362 |
graphs/subgraphs, nodes, and edges) for which you'll need the |
| 363 |
predefined constants AGRAPH, AGNODE and AGEDGE in |
| 364 |
calls to create, search and walk these dictionaries. |
| 365 |
|
| 366 |
Thus, to create an attribute for nodes, one uses: |
| 367 |
\begin{verbatim} |
| 368 |
Agsym_t *sym; |
| 369 |
sym = agattr(g,AGNODE,"shape","box"); |
| 370 |
\end{verbatim} |
| 371 |
If this succeeds, \verb"sym" points to a descriptor for the |
| 372 |
newly created (or updated) attribute. (Thus, even if \verb"shape" |
| 373 |
was previously declared and had some other default value, |
| 374 |
it would be set to \verb"box" by the above.) |
| 375 |
|
| 376 |
By using a NULL pointer as the value, |
| 377 |
you can use the same function to search the attribute definitions of a graph. |
| 378 |
\begin{verbatim} |
| 379 |
sym = agattr(g,AGNODE,"shape",0); |
| 380 |
if (sym) |
| 381 |
printf("The default shape is %s.\n",sym->defval); |
| 382 |
\end{verbatim} |
| 383 |
If you have the pointer to some graph object, you can also use the function |
| 384 |
\verb"agattrsym". |
| 385 |
\begin{verbatim} |
| 386 |
Agnode_t* n; |
| 387 |
Agsym_t* sym = agattrsym (n,"shape"); |
| 388 |
if (sym) |
| 389 |
printf("The default shape is %s.\n",sym->defval); |
| 390 |
\end{verbatim} |
| 391 |
Both functions return NULL if the attribute is not defined. |
| 392 |
|
| 393 |
Instead of looking for a particular attribute, it is possible to |
| 394 |
iterate over all of them: |
| 395 |
|
| 396 |
\begin{verbatim} |
| 397 |
sym = 0; /* to get the first one */ |
| 398 |
while (sym = agnxtattr(g,AGNODE,sym) |
| 399 |
printf("%s = %s\n",sym->name,sym->defval); |
| 400 |
\end{verbatim} |
| 401 |
|
| 402 |
Assuming an attribute already exists for some object, its value can be |
| 403 |
obtained or set using its string name or its \verb"Agsym_t" descriptor. |
| 404 |
To use the string name, we have: |
| 405 |
|
| 406 |
\begin{verbatim} |
| 407 |
str = agget(n,"shape"); |
| 408 |
agset(n,"shape","hexagon"); |
| 409 |
\end{verbatim} |
| 410 |
|
| 411 |
If an attribute will be referenced often, it is faster to |
| 412 |
use its descriptor as an index, as shown here: |
| 413 |
|
| 414 |
\begin{verbatim} |
| 415 |
Agsym_t *sym = agattr(g,AGNODE,"shape","box"); |
| 416 |
str = agxget(n,sym); |
| 417 |
agxset(n,sym,"hexagon"); |
| 418 |
\end{verbatim} |
| 419 |
|
| 420 |
Cgraph provides two helper functions for dealing with attributes. The function |
| 421 |
\verb"agsafeset(void *obj, char *name, char *value, char *def)" first checks |
| 422 |
that the attribute has been defined, defining it with the default value \verb"def" |
| 423 |
if not. It then uses \verb"value" as the specific value assigned to \verb"obj". |
| 424 |
|
| 425 |
It is sometimes useful to copy all of the values from one object to another. This can |
| 426 |
be easily done using \verb"agcopyattr(void *src, void* tgt)". This assumes that the |
| 427 |
source and target are the same type of graph objects, and that the attributes of |
| 428 |
\verb"src" have already been defined for \verb"tgt". If \verb"src" and \verb"tgt" |
| 429 |
belong to the same root graph, this will automatically be true. |
| 430 |
|
| 431 |
\section{Internal Attributes} |
| 432 |
\label{sec:internalattributes} |
| 433 |
|
| 434 |
It would be possible to do everything using just string-valued |
| 435 |
attributes. In general, though, this will be too inefficient. |
| 436 |
To deal with this, |
| 437 |
each graph object (graph, node or edge) may have a list of |
| 438 |
associated internal data records. The layout of each such |
| 439 |
record is programmer-defined, except each must have an |
| 440 |
\verb"Agrec_t" header. The records are allocated through |
| 441 |
Cgraph. For example: |
| 442 |
|
| 443 |
\begin{verbatim} |
| 444 |
typedef struct mynode_s { |
| 445 |
Agrec_t h; |
| 446 |
int count; |
| 447 |
} mynode_t; |
| 448 |
|
| 449 |
mynode_t *data; |
| 450 |
Agnode_t *n; |
| 451 |
n = agnode(g,"mynodename",TRUE); |
| 452 |
data = (mynode_t*)agbindrec(n,"mynode_t", |
| 453 |
sizeof(mynode_t),FALSE); |
| 454 |
data->count = 1; |
| 455 |
\end{verbatim} |
| 456 |
|
| 457 |
In a similar way, \verb"aggetrec" searches for a record, returning a pointer |
| 458 |
to the record if it exists and NULL otherwise; |
| 459 |
\verb"agdelrec" removes a record from an object. |
| 460 |
|
| 461 |
Although each graph object may have its own unique, individual collection |
| 462 |
of records, for convenience, there are functions that update an entire graph |
| 463 |
by allocating or removing the same record from all nodes, |
| 464 |
edges or subgraphs at the same time. These functions are: |
| 465 |
|
| 466 |
\begin{verbatim} |
| 467 |
void aginit(Agraph_t *g, int kind, char *rec_name, |
| 468 |
int rec_size, int move_to_front); |
| 469 |
void agclean(Agraph_t *g, int kind, char *rec_name); |
| 470 |
\end{verbatim} |
| 471 |
|
| 472 |
Note that in addition to \verb"agdelrec" and \verb"agclean", records are removed |
| 473 |
and their storage freed when their associated graph object is deleted. Only the |
| 474 |
record data structure is freed. If the application has attached any additional heap |
| 475 |
memory to a record, it is the responsibility of the application to handle this before |
| 476 |
the actual record is deleted. |
| 477 |
|
| 478 |
For further efficiency, there is a way to ``lock'' the data pointer of |
| 479 |
a graph object to point to a given record. This can be done by using \verb"TRUE" |
| 480 |
as the last argument in \verb"agbindrec", \verb"aginit" or \verb"aggetrec". |
| 481 |
If this is done, in the above example |
| 482 |
we could then simply cast this pointer to the appropriate type |
| 483 |
for direct (un-typesafe) access to the data. |
| 484 |
|
| 485 |
\begin{verbatim} |
| 486 |
(mydata_t*) (n->base.data)->count = 1; |
| 487 |
\end{verbatim} |
| 488 |
|
| 489 |
Typically, it is convenient to encapsulate this access using macros. For example, |
| 490 |
we may have: |
| 491 |
\begin{verbatim} |
| 492 |
#define ND_count(n) (((mydata_t*)(AGDATA(n)))->count) |
| 493 |
ND_count(n) = 1; |
| 494 |
\end{verbatim} |
| 495 |
|
| 496 |
As this is unsafe if the record was not allocated for some object, it is good form |
| 497 |
to two versions of the macro: |
| 498 |
\begin{verbatim} |
| 499 |
#ifdef DEBUG |
| 500 |
#define ND_count(n) \ |
| 501 |
(assert(aggetrec(n,"mynode_t",1)),((mynode_t*)(AGDATA(n)))->count) |
| 502 |
#else |
| 503 |
#define ND_count(n) (((mydata_t*)(AGDATA(n)))->count) |
| 504 |
#endif |
| 505 |
\end{verbatim} |
| 506 |
|
| 507 |
\section{Subgraphs} |
| 508 |
\label{sec:subgraphs} |
| 509 |
Subgraphs are an important construct in Cgraph. They are intended for |
| 510 |
organizing subsets of graph objects and can be used interchangeably with |
| 511 |
top-level graphs in almost all Cgraph functions. |
| 512 |
|
| 513 |
A subgraph may contain any nodes or edges of its parent. |
| 514 |
(When an edge is inserted in a subgraph, its nodes |
| 515 |
are also implicitly inserted if necessary. Similarly, |
| 516 |
insertion of a node or edge automatically implies |
| 517 |
insertion in all containing subgraphs up to the root.) |
| 518 |
Subgraphs of a graph form a hierarchy (a tree). |
| 519 |
Cgraph has functions to create, search, and iterate over subgraphs. |
| 520 |
|
| 521 |
For example, |
| 522 |
\begin{verbatim} |
| 523 |
Agraph_t *g, *h; |
| 524 |
|
| 525 |
/* search for subgraph by name */ |
| 526 |
h = agsubg(g,"mysubgraph",FALSE); |
| 527 |
if (!h) |
| 528 |
/* create subgraph by name */ |
| 529 |
h = agsubg(g,"mysubgraph",TRUE); |
| 530 |
|
| 531 |
for (h = agfstsubg(g); h; h = agnxtsubg(h)) { |
| 532 |
/* agparent is up one level */ |
| 533 |
assert (g == agparent(h)); |
| 534 |
/* Use subgraph h */ |
| 535 |
} |
| 536 |
\end{verbatim} |
| 537 |
|
| 538 |
The function \verb"agparent" returns the (sub)graph immediately containing the |
| 539 |
argument subgraph. The iteration done using \verb"agfstsubg" and \verb"agnxtsubg" |
| 540 |
returns only immediate subgraphs. To find subgraphs further down the hierarchy |
| 541 |
requires a recursive search. |
| 542 |
|
| 543 |
It is not uncommon to want to populate a subgraph with nodes and edges that have |
| 544 |
already been created. This can be done using |
| 545 |
the functions \verb"agsubnode" and \verb"agsubedge", |
| 546 |
\begin{verbatim} |
| 547 |
Agnode_t *agsubnode(Agraph_t *g, Agnode_t *n, int create); |
| 548 |
Agedge_t *agsubedge(Agraph_t *g, Agedge_t *e, int create); |
| 549 |
\end{verbatim} |
| 550 |
which take a subgraph, |
| 551 |
and an object from another subgraph of the same |
| 552 |
graph (or possibly a top-level object) and add it to the argument subgraph |
| 553 |
if the \verb"create" flag is \verb"TRUE". |
| 554 |
It is also added to all enclosing subgraphs, if necessary. |
| 555 |
If the \verb"create" flag is \verb"FALSE", then the |
| 556 |
request is only treated as a search and returns NULL for failure. |
| 557 |
|
| 558 |
A subgraph can be removed by \verb"agdelsubg(g,subg)" or by |
| 559 |
\verb"agclose(subg)". |
| 560 |
|
| 561 |
\section{Utility Functions and Macros} |
| 562 |
\label{sec:utilityfunctionsandmacros} |
| 563 |
|
| 564 |
For convenience, Cgraph provides some polymorphic functions and macros |
| 565 |
that apply to all Cgraph objects. (Most of these functions could |
| 566 |
be implemented in terms of others already described, or by accessing |
| 567 |
fields in the \verb"Agobj_t" base object. |
| 568 |
|
| 569 |
\begin{itemize} |
| 570 |
\item \verb"AGTYPE(obj)": object type - AGRAPH, AGNODE, or AGEDGE |
| 571 |
\item \verb"AGID(obj)": internal object ID (an unsigned long) |
| 572 |
\item \verb"AGSEQ(obj)": object creation timestamp (an integer) |
| 573 |
\item \verb"AGDATA(obj)": data record pointer (an \verb"Agrec_t*") |
| 574 |
\end{itemize} |
| 575 |
|
| 576 |
Other useful functions include: |
| 577 |
\begin{verbatim} |
| 578 |
/* Returns root graph of obj */ |
| 579 |
Agraph_t *agroot(void* obj); |
| 580 |
/* Returns root graph of obj or obj if a (sub)graph */ |
| 581 |
Agraph_t *agraphof(void* obj); |
| 582 |
/* True of obj belongs to g */ |
| 583 |
int agcontains(Agraph_t *g, void *obj); |
| 584 |
/* Delete obj from the (sub)graph */ |
| 585 |
int agdelete(Agraph_t *g, void *obj); |
| 586 |
/* Synonym of AGTYPE */ |
| 587 |
int agobjkind(void *obj); |
| 588 |
\end{verbatim} |
| 589 |
A root graph \verb"g" will always have |
| 590 |
\begin{verbatim} |
| 591 |
g == agroot(g) == agraphof(g) |
| 592 |
\end{verbatim} |
| 593 |
|
| 594 |
\section{Error Handling} |
| 595 |
\label{sec:errorhandling} |
| 596 |
|
| 597 |
Cgraph provides some basic error handling functions, hampered by the |
| 598 |
lack of exceptions in C. At present, there are basically two types of anomalies: |
| 599 |
warnings and errors. |
| 600 |
|
| 601 |
To report an anomaly, one uses: |
| 602 |
\begin{verbatim} |
| 603 |
typedef enum { AGWARN, AGERR, AGMAX, AGPREV |
| 604 |
} agerrlevel_t; |
| 605 |
|
| 606 |
int agerr(agerrlevel_t level, const char *fmt, ...); |
| 607 |
\end{verbatim} |
| 608 |
The \verb"agerr" function has a \verb"printf"-interface, with the first argument |
| 609 |
indicating the severity of the problem. |
| 610 |
A message is only written if its severity is higher than a programmer-controlled minimum, |
| 611 |
which is AGWARN by default. The programmer can set this value using \verb"agseterr", |
| 612 |
which returns the previous value. Calling \verb"agseterr(AGMAX)" turns off the writing of messages. |
| 613 |
|
| 614 |
Sometimes additional context information is only available in functions calling the function |
| 615 |
where the error is actually caught. In this case, the calling function can indicate that it |
| 616 |
is continuing the current error by using \verb"AGPREV" as the first argument. |
| 617 |
|
| 618 |
The function \verb"agwarningf" is shorthand for \verb"agerr(AGWARN,...)"; similarly, |
| 619 |
\verb"agerrorf" is shorthand for \verb"agerr(AGERR,...)". |
| 620 |
|
| 621 |
Some applications desire to directly control the writing of messages. Such an application can |
| 622 |
use the function \verb"agseterrf" to register a function that the library should call to actually |
| 623 |
write the message: |
| 624 |
\begin{verbatim} |
| 625 |
typedef int (*agusererrf) (char*); |
| 626 |
|
| 627 |
agusererrf agseterrf(agusererrf); |
| 628 |
\end{verbatim} |
| 629 |
The previous error function is returned. By default, the messages are written to \verb"stderr". |
| 630 |
Errors not written are stored in a log file. The last recorded error can be retrieved by |
| 631 |
calling \verb"aglasterr". |
| 632 |
The function \verb"agerrors" returns the maximum severity reported to \verb"agerr". |
| 633 |
The function \verb"agreseterrors" is identical, except it also resets the error level |
| 634 |
as though no errors have been reported. |
| 635 |
|
| 636 |
\section{Strings} |
| 637 |
\label{sec:strings} |
| 638 |
As mentioned, Cgraph maintains a reference-counted shared string pool for each graph. |
| 639 |
As there are often many identical strings used in a graph, this helps to reduce the |
| 640 |
memory overhead. |
| 641 |
\begin{verbatim} |
| 642 |
char *agstrdup(Agraph_t *, char *); |
| 643 |
char *agstrbind(Agraph_t *, char*); |
| 644 |
int agstrfree(Agraph_t *, char *); |
| 645 |
|
| 646 |
char *agstrdup_html(Agraph_t *, char *); |
| 647 |
int aghtmlstr(char *); |
| 648 |
|
| 649 |
char *agcanonStr(char *str); |
| 650 |
char *agstrcanon(char *, char *); |
| 651 |
char *agcanon(char *, int); |
| 652 |
\end{verbatim} |
| 653 |
Cgraph has functions to directly create and destroy references to shared strings. |
| 654 |
As with any reference-counted object, you can use these as ordinary |
| 655 |
strings, though the data should not be modified. If you need to store the pointer |
| 656 |
in some data structure, you should call \verb"agstrdup(g,s)". This will create a |
| 657 |
new copy of \verb"s" if necessary, and increment |
| 658 |
the count of references, ensuring that the string will not be freed by some |
| 659 |
other part of the program while you are still using it. Since \verb"agstrdup" makes |
| 660 |
a copy of the string, the argument string can use temporary storage. |
| 661 |
|
| 662 |
A call to \verb"agstrfree(g,s)" decrements the reference count and, if this becomes |
| 663 |
zero, frees the string. |
| 664 |
The function \verb"agstrbind(g,s)" checks if a shared string identical to \verb"s" exists |
| 665 |
and, if so, returns it. Otherwise, it returns NULL. |
| 666 |
|
| 667 |
The DOT language supports a special type of string, containing HTML-like markups. To make |
| 668 |
sure that the HTML semantics are used, the application should call \verb"agstrdup_html(g,s)" |
| 669 |
rather than \verb"agstrdup(g,s)". The following code makes sure the node's label will be |
| 670 |
interpreted as an HTML-like string and appear as a table. If \verb"agstrdup" were used, |
| 671 |
the label would appear as the literal string \verb"s". |
| 672 |
\begin{verbatim} |
| 673 |
Agraph_t* g; |
| 674 |
Agnode_t* n; |
| 675 |
char* s = "<TABLE><TR><TD>one cell</TD></TR></TABLE>"; |
| 676 |
agset (n, "label", agsgtrdup_html (g,s)); |
| 677 |
\end{verbatim} |
| 678 |
The predicate \verb"aghtmlstr" returns \verb"TRUE" if the argument string is marked as an |
| 679 |
HTML-like string. {\bf N.B.} This is only valid if the argument string is a shared string. |
| 680 |
HTML-like strings are also freed using \verb"agstrfree". |
| 681 |
|
| 682 |
The DOT language uses various special characters and escape sequences. When writing out |
| 683 |
strings as concrete text, it is important that these lexical features are used in order |
| 684 |
that the string can be re-read as DOT and be interpreted correctly. |
| 685 |
Cgraph provides three functions to handle this. The simplest is \verb"agcanonStr(s)". |
| 686 |
It returns a pointer to a canonical version of the input string. It uses an internal |
| 687 |
buffer, so the returned string should be written or copied before another call to |
| 688 |
\verb"agcanonStr". \verb"agstrcanon(s,buf)" is identical, except the calling function |
| 689 |
also supplies a buffer where the canonical version may be written. An application should only use |
| 690 |
the returned pointer, as it is possible the buffer will not be used at all. The buffer |
| 691 |
needs to be large enough to hold the canonical version. Normally, an expansion of |
| 692 |
\verb"2*strlen(s)+2" is sufficient. |
| 693 |
|
| 694 |
Both \verb"agcanonStr" and \verb"agstrcanon" assume that string argument is a |
| 695 |
shared string. For convenience, the library also provides the function \verb"agcanon(s,h)". |
| 696 |
This is identical to \verb"agcanonStr(s)", except \verb"s" can be any string. If \verb"h" |
| 697 |
is \verb"TRUE", the canonicalization assumes \verb"s" is an HTML-like string. |
| 698 |
|
| 699 |
\section{Expert-level Tweaks} |
| 700 |
\label{sec:expertleveltweaks} |
| 701 |
|
| 702 |
\subsection{Callbacks} |
| 703 |
\label{subsec:callbacks} |
| 704 |
|
| 705 |
There is a way to register client functions to be called |
| 706 |
whenever graph objects are inserted into or deleted from a graph or subgraph, |
| 707 |
or have their string attributes modified. |
| 708 |
The arguments to the callback functions for insertion and |
| 709 |
deletion (an \verb"agobjfn_t") are the containing (sub)graph, the object and a pointer |
| 710 |
to a piece of state data supplied by the application. |
| 711 |
The object update callback (an \verb"agobjupdfn_t") also |
| 712 |
receives the data dictionary entry pointer for the |
| 713 |
name-value pair that was changed. |
| 714 |
The graph argument will be the root graph. |
| 715 |
|
| 716 |
\begin{verbatim} |
| 717 |
typedef void (*agobjfn_t)(Agraph_t*, Agobj_t*, void*); |
| 718 |
typedef void (*agobjupdfn_t)(Agraph_t*, Agobj_t*, |
| 719 |
void*, Agsym_t*); |
| 720 |
|
| 721 |
struct Agcbdisc_s { |
| 722 |
struct { |
| 723 |
agobjfn_t ins; |
| 724 |
agobjupdfn_t mod; |
| 725 |
agobjfn_t del; |
| 726 |
} graph, node, edge; |
| 727 |
}; |
| 728 |
\end{verbatim} |
| 729 |
|
| 730 |
Callback functions are installed by \verb"agpushdisc", which also takes |
| 731 |
a pointer to the client data structure \verb"state" that is |
| 732 |
later passed to the callback function when it is invoked. |
| 733 |
|
| 734 |
\begin{verbatim} |
| 735 |
agpushdisc(Agraph_t *g, Agcbdisc_t *disc, void *state); |
| 736 |
\end{verbatim} |
| 737 |
|
| 738 |
Callbacks are removed by \verb"agpopdisc", which deletes a |
| 739 |
previously installed set of callbacks anywhere in the stack. |
| 740 |
This function returns zero for success. (In real life, this |
| 741 |
function isn't used much; generally callbacks are set up and |
| 742 |
left alone for the lifetime of a graph.) |
| 743 |
|
| 744 |
\begin{verbatim} |
| 745 |
int agpopdisc(Agraph_t *g, Agcbdisc_t *disc); |
| 746 |
\end{verbatim} |
| 747 |
|
| 748 |
The default is for callbacks to be issued synchronously, but it is |
| 749 |
possible to hold them in a queue of pending callbacks to be delivered |
| 750 |
on demand. This feature is controlled by the interface: |
| 751 |
|
| 752 |
\begin{verbatim} |
| 753 |
/* returns previous value */ |
| 754 |
int agcallbacks(Agraph_t *g, int flag); |
| 755 |
\end{verbatim} |
| 756 |
|
| 757 |
If the flag is zero, callbacks are kept pending. |
| 758 |
If the flag is one, pending callbacks are immediately issued, |
| 759 |
and the graph is put into immediate callback mode. |
| 760 |
(Therefore the state must be reset via \verb"agcallbacks" |
| 761 |
if they are to be kept pending again.) |
| 762 |
|
| 763 |
{\bf N.B.}: it is a small inconsistency that Cgraph depends on the client |
| 764 |
to maintain the storage for the callback function structure. |
| 765 |
(Thus it should probably not be allocated on the dynamic stack.) |
| 766 |
The semantics of \verb"agpopdisc" currently identify callbacks by |
| 767 |
the address of this structure so it would take a bit of reworking |
| 768 |
to fix this. In practice, callback functions are usually passed |
| 769 |
in a static struct. |
| 770 |
|
| 771 |
\subsection{Disciplines} |
| 772 |
\label{subsec:disciplines} |
| 773 |
A graph has an associated set of methods (``disciplines'') |
| 774 |
for file I/O and graph object ID assignment. |
| 775 |
\begin{verbatim} |
| 776 |
typedef struct { |
| 777 |
Agiddisc_t *id; |
| 778 |
Agiodisc_t *io; |
| 779 |
} Agdisc_t; |
| 780 |
\end{verbatim} |
| 781 |
A pointer to an \verb"Agdisc_t" structure is used as an argument when |
| 782 |
a graph is created or read using \verb"agopen", |
| 783 |
\verb"agread" and \verb"agconcat". If the pointer is NULL, the default |
| 784 |
Cgraph disciplines are used. An application can pass in its own disciplines |
| 785 |
to override the defaults. Note that it doesn't need to provide disciplines for |
| 786 |
all three fields. If any field is the NULL pointer, Cgraph will use the default |
| 787 |
discipline for that task. |
| 788 |
|
| 789 |
The default disciplines are also accessible by name. |
| 790 |
\begin{verbatim} |
| 791 |
Agiddisc_t AgIdDisc; |
| 792 |
Agiodisc_t AgIoDisc; |
| 793 |
Agdisc_t AgDefaultDisc; |
| 794 |
\end{verbatim} |
| 795 |
This is useful because, unlike with \verb"Agdisc_t", all of the fields of |
| 796 |
specific disciplines must be non-NULL. |
| 797 |
|
| 798 |
Cgraph copies the three individual pointers. Thus, these three structures must remain |
| 799 |
allocated for the life of the graph, though the \verb"Agdisc_t" may be temporary. |
| 800 |
|
| 801 |
\subsubsection{I/O management} |
| 802 |
The I/O discipline is probably the most frequently used of the three, as the I/O requirements of |
| 803 |
applications vary widely. |
| 804 |
\begin{verbatim} |
| 805 |
typedef struct Agiodisc_s { |
| 806 |
int (*afread)(void *chan, char *buf, int bufsize); |
| 807 |
int (*putstr)(void *chan, char *str); |
| 808 |
int (*flush)(void *chan); |
| 809 |
} Agiodisc_t ; |
| 810 |
\end{verbatim} |
| 811 |
The default I/O discipline uses stdio and the FILE structure for reading and writing. The functions |
| 812 |
\verb"afread", \verb"putstr" and \verb"flush" should have semantics roughly equivalent to |
| 813 |
\verb"fread", \verb"fputs" and \verb"fflush", with the obvious permutation of arguments. |
| 814 |
|
| 815 |
The implementation of the \verb"agmemread" function of Cgraph provides a typical example of using |
| 816 |
a tailored I/O discipline. The idea is to read a graph from a given string of characters. The |
| 817 |
implementation of the function is given below. |
| 818 |
The \verb"rdr_t" provides a miniature version of FILE, providing the necessary state information. |
| 819 |
The function \verb"memiofread" fills the role of \verb"afread" using the state provided by \verb"rdr_t". |
| 820 |
The \verb"agmemread" puts everything together, creating the needed state using the argument string, |
| 821 |
and constructing a discipline structure using \verb"memiofread" and the defaults. It then calls |
| 822 |
\verb"agread" with the state and discipline to actually create the graph. |
| 823 |
|
| 824 |
\lgrindfile{agmemread.tex} |
| 825 |
|
| 826 |
\subsubsection{ID management} |
| 827 |
Graph objects (nodes, edges, subgraphs) use an uninterpreted long integer value as keys. |
| 828 |
The ID discipline gives the application control over how these keys are allocated, and how |
| 829 |
they are mapped to and from strings. |
| 830 |
The ID discipline makes it possible for a Cgraph client |
| 831 |
control this mapping. For instance, in one application, the client |
| 832 |
may create IDs that are pointers into another object space defined by |
| 833 |
a front-end interpreter. In general, the ID discipline must provide |
| 834 |
a map between internal IDs and external strings. |
| 835 |
|
| 836 |
\begin{verbatim} |
| 837 |
typedef unsigned long ulong; |
| 838 |
|
| 839 |
typedef struct Agiddisc_s { |
| 840 |
void *(*open)(Agraph_t *g, Agdisc_t*); |
| 841 |
long (*map)(void *state, int objtype, char *name, |
| 842 |
ulong *id, int createflag); |
| 843 |
long (*alloc)(void *state, int objtype, ulong id); |
| 844 |
void (*free)(void *state, int objtype, ulong id); |
| 845 |
char *(*print)(void *state, int objtype, ulong id); |
| 846 |
void (*close)(void *state); |
| 847 |
void (*idregister) (void *state, int objtype, void *obj); |
| 848 |
} Agiddisc_t; |
| 849 |
\end{verbatim} |
| 850 |
|
| 851 |
The \verb"open" function permits the ID discipline to initialize any data structures that it maintains |
| 852 |
per individual graph. Its return value is then passed as the first argument (\verb"void *state") to all |
| 853 |
subsequent ID manager calls. |
| 854 |
When the graph is closed, the discipline's \verb"close" function is called to allow for finalizing and |
| 855 |
freeing any resources used. |
| 856 |
|
| 857 |
The \verb"alloc" and \verb"free" functions explicitly create and destroy IDs. |
| 858 |
The former is used by Cgraph to see if the given ID is available for use. If it is available, the function |
| 859 |
should allocate it and return \verb"TRUE"; otherwise, it should return \verb"FALSE". |
| 860 |
If it is not available, the calling function will abort the operation. |
| 861 |
\verb"free" is called to inform the ID manager that the object labeled with the given ID is about to be |
| 862 |
deleted. |
| 863 |
|
| 864 |
If supported buy the ID discipline, the \verb"map" function is called |
| 865 |
to convert a string name into an ID for a given object type |
| 866 |
(AGRAPH, AGNODE, or AGEDGE), with an optional flag that tells if the |
| 867 |
ID should be allocated if it does not already exist. |
| 868 |
|
| 869 |
There are four cases: |
| 870 |
\begin{description} |
| 871 |
\item[{\tt name} \&\& {\tt createflag}] |
| 872 |
Map the string (e.g., a name in a graph file) |
| 873 |
into an ID. If the ID manager can comply, then it stores the result |
| 874 |
in the \verb"id" parameter and returns \verb"TRUE". It is then also responsible for |
| 875 |
being able to print the ID again as a string. Otherwise, the ID manager |
| 876 |
may return \verb"FALSE" but it must implement the following case, at least |
| 877 |
in order for the reading and writing of graph files to work. |
| 878 |
\item[!{\tt name} \&\& {\tt createflag}] |
| 879 |
Create a unique new ID. It may return FALSE if it does not support |
| 880 |
anonymous objects, but this is strongly discouraged |
| 881 |
in order for Cgraph to support ``local names'' in graph files. |
| 882 |
\item[{\tt name} \&\& !{\tt createflag}] |
| 883 |
Test if \verb"name" has already been mapped and allocated. If so, the ID |
| 884 |
should be returned in the \verb"id" parameter. |
| 885 |
Otherwise, the ID manager may |
| 886 |
either return \verb"FALSE", or may store any unallocated ID into result. |
| 887 |
This is convenient, for example, if names |
| 888 |
are known to be digit strings that are directly converted into integer values. |
| 889 |
\item[!{\tt name} \&\& !{\tt createflag}] Never used. |
| 890 |
\end{description} |
| 891 |
|
| 892 |
The \verb"print" function is called to convert an internal ID back to a string. |
| 893 |
It is allowed to return a pointer to a static buffer; |
| 894 |
a caller must copy its value if needed past subsequent calls. |
| 895 |
NULL should be returned by ID managers that do not map names. |
| 896 |
|
| 897 |
Note that the \verb"alloc" and \verb"map" functions do not receive pointers to any |
| 898 |
newly created object. If a client needs to install object pointers in a handle table, |
| 899 |
it can obtain them via new object callbacks (Section~\ref{subsec:callbacks}). |
| 900 |
|
| 901 |
To make this mechanism accessible, Cgraph provides functions |
| 902 |
to create objects by ID instead of external name: |
| 903 |
\begin{verbatim} |
| 904 |
Agnode_t *agidnode(Agraph_t *g, unsigned long id, int create); |
| 905 |
Agedge_t *agidedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, |
| 906 |
unsigned long id, int create); |
| 907 |
Agraph_t *agidsubg(Agraph_t *g, unsigned long id, int create); |
| 908 |
\end{verbatim} |
| 909 |
Note that, with the default ID discipline, these functions return NULL. |
| 910 |
|
| 911 |
\subsection{Flattened node and edge lists} |
| 912 |
For random access, nodes and edges are usually stored in splay trees. |
| 913 |
This adds a small but noticeable overhead when traversing the ``lists.'' |
| 914 |
For flat-out efficiency, |
| 915 |
there is a way of linearizing the splay trees in which node |
| 916 |
and edge sets are stored, converting them into flat lists. After |
| 917 |
this they can be traversed very quickly. |
| 918 |
|
| 919 |
\section{Related Libraries} |
| 920 |
\label{sec:relatedlibraries} |
| 921 |
{\bf Libgraph} is a predecessor of Cgraph and is now considered |
| 922 |
obsolete. All of the Graphviz code is now written using Cgraph. |
| 923 |
As some older applications using libgraph |
| 924 |
may need to be converted to Cgraph, we note some of the main |
| 925 |
differences. |
| 926 |
|
| 927 |
A key difference between the two libraries is the handling of |
| 928 |
C data structure attributes. Libgraph hard-wires these |
| 929 |
at the end of the graph, node and edge structuress. That is, |
| 930 |
an application programmer defines the structs {\tt graphinfo}, {\tt nodeinfo} |
| 931 |
and {\tt edgeinfo} before including {\tt graph.h}, and the library inquires of |
| 932 |
the size of these structures at runtime so it can allocate graph |
| 933 |
objects of the proper size. Because there is only one shot at |
| 934 |
defining attributes, this design creates an impediment to writing |
| 935 |
separate algorithm libraries. |
| 936 |
The dynamic \verb"Agrec_t" structures, described in Section~\ref{sec:internalattributes}, |
| 937 |
allows each algorithm to attach its own required data structure. |
| 938 |
|
| 939 |
As noted in Section~\ref{sec:edges}, edges are implemented slightly |
| 940 |
differently in the two libraries, so comparison of edge pointers for |
| 941 |
equality should be replaced with \verb"ageqedge" unless you are certain |
| 942 |
that the two pointers have consistent types. As an example where problems |
| 943 |
could arise, we have the following code: |
| 944 |
\begin{verbatim} |
| 945 |
void traverse(Agraph_t *g, Agedge_t *e0) |
| 946 |
{ |
| 947 |
Agnode_t *n = aghead (e0); |
| 948 |
Agedge_t *e; |
| 949 |
|
| 950 |
for (e = agfstout(g, n); n; n = agnxtout(g, e)) { |
| 951 |
if (e == e0) continue; /* avoid entry edge */ |
| 952 |
/* do something with e */ |
| 953 |
} |
| 954 |
\end{verbatim} |
| 955 |
If \verb"e0" is an in-edge (\verb"AGTYPE(e) == AGINEDGE"), the comparison |
| 956 |
with \verb"e" will always fail, as the latter is an out-edge. |
| 957 |
|
| 958 |
In Cgraph, the nesting of subgraphs forms a tree. |
| 959 |
In libgraph, a subgraph can belong to more than one parent, |
| 960 |
so they form a DAG (directed acyclic graph). Libgraph |
| 961 |
actually represents this DAG as a special meta-graph |
| 962 |
that is navigated by libgraph calls. After gaining |
| 963 |
experience with libgraph, we decided this complexity |
| 964 |
was not worth its cost. In libgraph, the code for traversing |
| 965 |
subgraphs would have a form something like\label{subg}: |
| 966 |
\begin{verbatim} |
| 967 |
Agedge_t* me; |
| 968 |
Agnode_t* mn; |
| 969 |
Agraph_t* mg = g->meta_node->graph; |
| 970 |
Agraph_t* subg; |
| 971 |
for (me = agfstout(mg, g->meta_node); me; me = agnxtout(mg, me)) { |
| 972 |
mn = me->head; |
| 973 |
subg = agusergraph(mn); |
| 974 |
/* use subgraph subg */ |
| 975 |
} |
| 976 |
\end{verbatim} |
| 977 |
The similar traversal using Cgraph would have the form |
| 978 |
\begin{verbatim} |
| 979 |
Agraph_t* subg; |
| 980 |
for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) { |
| 981 |
/* use subgraph subg */ |
| 982 |
} |
| 983 |
\end{verbatim} |
| 984 |
|
| 985 |
Finally, there are some small syntactic differences in the APIs. |
| 986 |
For example, in libgraph, the name of a node or graph and |
| 987 |
the head and tail nodes of an edge are |
| 988 |
directly accessible via pointers while in Cgraph, it is necessary to use |
| 989 |
the functions \verb"agnameof", \verb"agtail" and \verb"aghead". |
| 990 |
Libgraph tends to have separate functions for creating and finding |
| 991 |
an object, or for handling different types of objects, e.g., |
| 992 |
\verb"agnode" and \verb"agfindnode". Instead, Cgraph will use a single function |
| 993 |
with an additional parameter. Overall, the two libraries are vary close, both |
| 994 |
syntactically and semantically, so conversion is fairly straightforward. |
| 995 |
|
| 996 |
Tables~\ref{table:libgraph:g}--\ref{table:libgraph:a} list the common |
| 997 |
constants and operations in |
| 998 |
libgraph, and the corresponding value or procedure in Cgraph, if any. |
| 999 |
|
| 1000 |
\begin{savenotes} |
| 1001 |
\begin{table*}[htb] |
| 1002 |
\centering |
| 1003 |
\begin{tabular}{|l|l|} \hline |
| 1004 |
\multicolumn{1}{|c|}{\em libgraph} & \multicolumn{1}{c|}{\em Cgraph} \\ \hline |
| 1005 |
\verb"AGRAPH" & \verb"Agundirected" \\ \hline |
| 1006 |
\verb"AGRAPHSTRICT" & \verb"Agstrictundirected" \\ \hline |
| 1007 |
\verb"AGDIGRAPH" & \verb"Agdirected" \\ \hline |
| 1008 |
\verb"AGDIGRAPHSTRICT" & \verb"Agstrictdirected" \\ \hline |
| 1009 |
\verb"aginit" & not necessary\footnote{The Cgraph library has a function called \texttt{aginit}, but it has a different signature and semantics.} \\ \hline |
| 1010 |
\verb"agopen(name,type)" & \verb"agopen(name,type,NULL)"\\ \hline |
| 1011 |
\verb"agread(filep)" & \verb"agread(filep,NULL)"\\ \hline |
| 1012 |
\verb"agread_usergets(filep,gets)" \\ |
| 1013 |
& \verb"Agiddisc_t id = AgIdDisc;" \\ |
| 1014 |
& \verb"Agiodisc_t io = AgIoDisc;" \\ |
| 1015 |
& \verb"io.afread = gets;"\footnote{Note that the order of the parameters differs from the \texttt{gets} used in libgraph.} \\ |
| 1016 |
& \verb"agread(filep,disc);"\\ \hline |
| 1017 |
\verb"agsetiodisc" & no direct analogue; see above \\ \hline |
| 1018 |
\verb"obj->name" & \verb"agnameof(obj);" \\ \hline |
| 1019 |
\verb"graph->root" & \verb"agroot(graph);" \\ \hline |
| 1020 |
\verb"node->graph" & \verb"aggraphof(node);" \\ \hline |
| 1021 |
\verb"edge->head" & \verb"aghead(edge);" \\ \hline |
| 1022 |
\verb"edge->tail" & \verb"agtail(edge);" \\ \hline |
| 1023 |
\verb"AG_IS_DIRECTED(graph)" & \verb"agisdirected(graph)" \\ \hline |
| 1024 |
\verb"AG_IS_STRICT(graph)" & \verb"agisstrict(graph)" \\ \hline |
| 1025 |
\verb"agobjkind(obj)" & \verb"AGTYPE(obj)" \\ \hline |
| 1026 |
\verb"agsubg(parent,name)" & \verb"agsubg(parent,name,1)" \\ \hline |
| 1027 |
\verb"agfindsubg(parent,name)" & \verb"agsubg(parent,name,0)" \\ \hline |
| 1028 |
\verb"g->meta_node_graph" & \verb"agfstsubg/agnxtsubg" \\ |
| 1029 |
\verb"agusergraph(graph)" & See the examples on Page~\pageref{subg}\\ \hline |
| 1030 |
\verb"aginsert(graph, obj)" & \verb"agsubnode(graph,obj);" if obj is a node \\ |
| 1031 |
& \verb"agsubedge(graph,obj);" if obj is a edge \\ |
| 1032 |
& not allowed if obj is a graph \\ \hline |
| 1033 |
\end{tabular} |
| 1034 |
\caption{Graph function conversions} |
| 1035 |
\label{table:libgraph:g} |
| 1036 |
\end{table*} |
| 1037 |
|
| 1038 |
\begin{table*}[ht] |
| 1039 |
\centering |
| 1040 |
\begin{tabular}{|l|l|} \hline |
| 1041 |
\multicolumn{1}{|c|}{\em libgraph} & \multicolumn{1}{c|}{\em Cgraph} \\ \hline |
| 1042 |
\verb"agnode(graph,name)" & \verb"agnode(graph,name,1") \\ \hline |
| 1043 |
\verb"agfindnode(graph,name") & \verb"agnode(graph,name,0") \\ \hline |
| 1044 |
\verb"agedge(graph,tail,head") & \verb"agedge(graph,tail,head,NULL,1") \\ \hline |
| 1045 |
\verb"agfindedge(graph,tail,head") & \verb"agedge(graph,tail,head,NULL,0") \\ \hline |
| 1046 |
\end{tabular} |
| 1047 |
\caption{Node and edge function conversions} |
| 1048 |
\label{table:libgraph:n} |
| 1049 |
\end{table*} |
| 1050 |
|
| 1051 |
\begin{table*}[ht] |
| 1052 |
\centering |
| 1053 |
\begin{tabular}{|l|l|} \hline |
| 1054 |
\multicolumn{1}{|c|}{\em libgraph} & \multicolumn{1}{c|}{\em Cgraph} \\ \hline |
| 1055 |
\verb"agprotograph()" & no analogue \\ \hline |
| 1056 |
\verb"agprotonode(graph)" & not used \\ \hline |
| 1057 |
\verb"agprotoedge(graph)" & not used \\ \hline |
| 1058 |
\verb"agattr(obj, name, default)" & \verb"agattr(agroot(obj),AGTYPE(obj),name,default)" \\ \hline |
| 1059 |
\verb"agfindattr(obj, name)"& \verb"agattrsym(obj,name)" \\ \hline |
| 1060 |
\verb"agraphattr(graph, name default)" & \verb"agattr(graph,AGRAPH,name,default)"\\ \hline |
| 1061 |
\verb"agnodeattr(graph, name, default)" & \verb"agattr(graph,AGNODE,name,default)"\\ \hline |
| 1062 |
\verb"agedgeattr(graph, name, default)" & \verb"agattr(graph,AGEDGE,name,default)"\\ \hline |
| 1063 |
\verb"agfstattr(obj)" & \verb"agnxtattr(agroot(obj),AGTYPE(obj),NULL)"\\ \hline |
| 1064 |
\verb"agnxtattr(obj,sym)" & \verb"agnxtattr(agroot(obj),AGTYPE(obj),sym)"\\ \hline |
| 1065 |
\verb"aglstattr(obj)" & no analogue\\ \hline |
| 1066 |
\verb"agprvattr(obj, sym)" & no analogue\\ \hline |
| 1067 |
\end{tabular} |
| 1068 |
\caption{Attribute function conversions} |
| 1069 |
\label{table:libgraph:a} |
| 1070 |
\end{table*} |
| 1071 |
|
| 1072 |
|
| 1073 |
{\bf Lgraph} is a successor to Cgraph, written in C++ by Gordon Woodhull. |
| 1074 |
It follows Cgraph's overall graph model (particularly, its subgraphs and |
| 1075 |
emphasis on efficient dynamic graph operations) but uses the C++ type |
| 1076 |
system of templates and inheritance to provide typesafe, modular and |
| 1077 |
efficient internal attributes. (LGraph relies on cdt for dictionary |
| 1078 |
sets, with an STL-like C++ interface layer.) A fairly mature prototype |
| 1079 |
of the Dynagraph system (a successor to dot and neato to handle |
| 1080 |
online maintenance of dynamic diagrams) has been prototyped in LGraph. |
| 1081 |
See the dgwin (Dynagraph for Windows) page |
| 1082 |
\url{https://www.dynagraph.org/dgwin/} for further details. |
| 1083 |
|
| 1084 |
\section{Interfaces to Other Languages} |
| 1085 |
\label{sec:interfacetootherlanguages} |
| 1086 |
|
| 1087 |
If enabled, the Graphviz package contains bindings for Cgraph in a variety |
| 1088 |
of languages, including Java, Perl, PHP, Tcl, Python and Ruby. |
| 1089 |
|
| 1090 |
\section{Open Issues} |
| 1091 |
\label{sec:openissues} |
| 1092 |
|
| 1093 |
{\bf Node and Edge Ordering.} The intent in Cgraph's design was to |
| 1094 |
eventually support user-defined node and edge set ordering, overriding |
| 1095 |
the default (which is object creation timestamp order). For example, |
| 1096 |
in topologically embedded graphs, edge lists could potentially be sorted |
| 1097 |
in clockwise order around nodes. |
| 1098 |
Because Cgraph assumes that all edge sets in the same \verb"Agraph_t" |
| 1099 |
have the same ordering, there should probably be a new primitive to |
| 1100 |
switching node or edge set ordering functions. Please contact the |
| 1101 |
author if you need this feature. |
| 1102 |
|
| 1103 |
{\bf XML.} XML dialects such as GXL and GraphML have been proposed |
| 1104 |
for graphs. Although it is simple to encode nodes and edges in XML, |
| 1105 |
there are subtleties to representing more complicated structures, such as |
| 1106 |
Cgraph's subgraphs and nesting of attribute defaults. |
| 1107 |
On the other hand, GXL and GraphML provide notions of compound graphs, |
| 1108 |
which are not available in DOT. |
| 1109 |
We've prototyped an XML parser and would like to complete and release |
| 1110 |
this work if we had a compelling application. (Simple XML output of |
| 1111 |
graphs is not difficult to program.) |
| 1112 |
|
| 1113 |
{\bf Graph views; external graphs.} At times it would be convenient |
| 1114 |
to relate one graph's objects to another's without making one into a |
| 1115 |
subgraph of another. At other times there is a need to populate |
| 1116 |
a graph from objects delivered on demand from a foreign API (such as a |
| 1117 |
relational database that stores graphs). We are now experimenting with |
| 1118 |
attacks on some of these problems. |
| 1119 |
|
| 1120 |
%{\bf Additional primitives.} To be done: Object renaming, cloning, first-class |
| 1121 |
%nodes and edges, etc. |
| 1122 |
|
| 1123 |
\end{savenotes} |
| 1124 |
\section{Example} |
| 1125 |
\label{sec:example} |
| 1126 |
|
| 1127 |
The following is a simple Cgraph filter that reads a graph and |
| 1128 |
emits its strongly connected components, each as a separate graph, |
| 1129 |
plus an overview map of the relationships between the components. |
| 1130 |
To save space, the auxiliary functions in the header ingraph.h |
| 1131 |
are not shown; the entire program can be found in the Graphviz |
| 1132 |
source code release under \verb"cmd/tools". |
| 1133 |
|
| 1134 |
About lines 32-41 are the declarations for internal records for |
| 1135 |
graphs and nodes. Line 43-48 define access macros |
| 1136 |
for fields in these records. Lines 52-83 define a simple stack |
| 1137 |
structure needed for the strongly connected component algorithm |
| 1138 |
and down to line 97 are some global definitions for the program. |
| 1139 |
|
| 1140 |
The rest of the code can be read from back-to-front. From |
| 1141 |
around line 262 to the end is boilerplate code that handles |
| 1142 |
command-line arguments and opening multiple graph files. |
| 1143 |
The real work is done starting with the function {\it process} |
| 1144 |
about line 223, which works on one graph at a time. After initializing |
| 1145 |
the node and graph internal records using {it aginit}, it creates a new graph for the |
| 1146 |
overview map, and it calls {\it visit} on unvisited nodes to |
| 1147 |
find components. {\it visit} implements a standard algorithm to form |
| 1148 |
the next strongly connected component on a stack. When one has been |
| 1149 |
completed, a new subgraph is created and the nodes of the component |
| 1150 |
are installed. (There is an option to skip trivial components that |
| 1151 |
contain only one node.) {\it nodeInduce} is called to process the |
| 1152 |
out-edges of nodes in this subgraph. Such edges either belong to |
| 1153 |
the component (and are added to it), or else point to a node in |
| 1154 |
another component that must already have been processed. |
| 1155 |
%\newpage |
| 1156 |
|
| 1157 |
\lgrindfile{sccmap.tex} |
| 1158 |
|
| 1159 |
\end{document} |