| igraph Reference Manual |
|---|
The functions generating graph objects are called graph generators. Stochastic (=randomized) graph generators are called “games”.
igraph can handle directed and undirected graphs. Most graph
generators are able to create both types of graphs and most other
functions are usually also capable of handling
both. Eg. igraph_shortest_paths()
which (surprisingly) calculates
shortest paths from a vertex to another vertices can calculate
directed or undirected paths.
igraph has sophisticated ways for creating graphs. The simplest
graphs are deterministic regular structures like star graphs
(igraph_star()),
ring graphs (igraph_ring()), lattices
(igraph_lattice()) or trees
(igraph_tree()).
The following example creates an undirected regular circular lattice, adds some random edges to it and calculates the average length of shortest paths between all pairs of vertices in the graph before and after adding the random edges. (The message is that some random edges can reduce path lengths a lot.)
#include <igraph.h>
int main(void) {
igraph_real_t avg_path;
igraph_t graph;
igraph_vector_t dimvector;
igraph_vector_t edges;
int i;
igraph_vector_init(&dimvector, 2);
VECTOR(dimvector)[0]=30;
VECTOR(dimvector)[1]=30;
igraph_lattice(&graph, &dimvector, 0, IGRAPH_UNDIRECTED, 0, 1);
srand(100);
igraph_vector_init(&edges, 20);
for (i=0; i<igraph_vector_size(&edges); i++) {
VECTOR(edges)[i] = rand() % (int)igraph_vcount(&graph);
}
igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1);
printf("Average path length (lattice): %f\n", (double) avg_path);
igraph_add_edges(&graph, &edges, 0);
igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1);
printf("Average path length (randomized lattice): %f\n", (double) avg_path);
igraph_vector_destroy(&dimvector);
igraph_vector_destroy(&edges);
igraph_destroy(&graph);
return 0;
}
This example illustrates some new points. igraph uses
igraph_vector_t
instead of plain C arrays. igraph_vector_t is superior to
regular arrays in almost every sense. Vectors are created by the
igraph_vector_init()
function and like graphs they should be destroyed if not
needed any more by calling
igraph_vector_destroy()
on them. A vector can be indexed by the
VECTOR() function
(right now it is a macro). Vectors
can be resized, eg. most igraph functions returning the result in a
vector resize it to the size of the result.
igraph_lattice()
takes a vector argument specifying the dimensions of
the lattice, in this example we generate a 30x30 two dimensional
lattice. See the documentation of
igraph_lattice() in
the reference manual for the other arguments.
The vertices in a graph are identified by an integer number between
0 and N-1, N is the number of vertices in the graph (this can be
obtained by
igraph_vcount(),
as in the example).
The igraph_add_edges()
function simply takes a graph and a vector of
vertex ids defining the new edges. The first edge is between the first
two vertex ids in the vector, the second edge is between the second
two, etc. This way we add ten random edges to the lattice.
Note that in the example it is possible to add loop edges, edges
pointing to the same vertex and multiple edges, more than one edge
between the same pair of vertices.
igraph_t can of course
represent loops and
multiple edges, although some routines expect simple graphs,
ie. graphs without loop and multiple edges, because for example some
structural properties are ill-defined for non-simple graphs. Loop
edges can be removed by calling
igraph_simplify().
| << 1. Lesson 1. Compiling programs using igraph. | 3. Lesson 3. Calculating various properties of graphs. >> |