For using the igraph C library
The igraph library can handle directed and undirected graphs. The igraph graphs are multisets of ordered (if directed) or unordered (if undirected) labeled pairs. The labels of the pairs plus the number of vertices always starts with zero and ends with the number of edges minus one. In addition to that, a table of metadata is also attached to every graph, its most important entries being the number of vertices in the graph and whether the graph is directed or undirected.
Like the edges, the igraph vertices are also labeled by numbers between zero and the number of vertices minus one. So, to summarize, a directed graph can be imagined like this:
( vertices: 6, directed: yes, { (0,2), (2,2), (3,2), (3,3), (3,4), (3,4), (4,3), (4,1) } )
Here the edges are ordered pairs or vertex ids, and the graph is a multiset of edges plus some metadata.
An undirected graph is like this:
( vertices: 6, directed: no, { (0,2), (2,2), (2,3), (3,3), (3,4), (3,4), (3,4), (1,4) } )
Here, an edge is an unordered pair of two vertex IDs. A graph is a multiset of edges plus metadata, just like in the directed case.
It is possible to convert between directed and undirected graphs,
see the
igraph_to_directed()
and
igraph_to_undirected()
functions.
igraph aims to robustly support multigraphs, i.e. graphs which
have more than one edge between some pairs of vertices, as well as
graphs with selfloops. Most functions which do not support such graphs
will check their input and issue an error if it is not valid. Those
rare functions which do not perform this check clearly indicate this
in their documentation. To eliminate multiple edges from a graph, you can use
igraph_simplify()
.
igraph has a simple and consistent interface. Most functions check their input for validity and display an informative error message when something goes wrong. In order to support this, the majority of functions return an error code. In basic usage, this code can be ignored, as the default behaviour is to abort the program immediately upon error. See the section on error handling for more information on this topic.
Results are typically returned through output arguments, i.e. pointers to a data structure into which the result will be written. In almost all cases, this data structure is expected to be preinitialized. A few simple functions communicate their result directly through their return value—these functions can never encounter an error.
igraph introduces a few aliases to standard C data types that are then used throughout the library. The most important of these types is igraph_integer_t, which is an alias to either a 32bit or a 64bit signed integer, depending on whether igraph was compiled in 32bit or 64bit mode. The size of igraph_integer_t also influences the maximum number of vertices that an igraph graph can represent as the number of vertices is stored in a variable of type igraph_integer_t.
Since the size of a variable of type igraph_integer_t may
change depending on how igraph is compiled, you cannot simply use
%d
or %ld
as a placeholder for igraph integers in
printf
format strings. igraph provides the
IGRAPH_PRId
macro, which maps to d
, ld
or lld
depending on the size of igraph_integer_t, and
you must use this macro in printf
format strings to avoid compiler
warnings.
Similarly to how igraph_integer_t maps to the standard size
signed integer in the library, igraph_uint_t maps to a 32bit or
a 64bit unsigned integer. It is guaranteed that the size of
igraph_integer_t is the same as the size of igraph_uint_t.
igraph provides IGRAPH_PRIu
as a format string placeholder for
variables of type igraph_uint_t.
Real numbers (i.e. quantities that can potentially be fractional or infinite) are represented with a type named igraph_real_t. Currently igraph_real_t is always aliased to double, but it is still good practice to use igraph_real_t in your own code for sake of consistency.
Boolean values are represented with a type named igraph_bool_t.
It tries to be as small as possible since it only needs to represent a truth
value. For printing purposes, you can treat it as an integer and use
%d
in format strings as a placeholder for an igraph_bool_t.
Upper and lower limits of igraph_integer_t and
igraph_uint_t are provided by the constants named
IGRAPH_INTEGER_MIN
, IGRAPH_INTEGER_MAX
,
IGRAPH_UINT_MIN
and IGRAPH_UINT_MAX
.
This is the very minimal API in igraph. All the other functions use this minimal set for creating and manipulating graphs.
This is a very important principle since it makes possible to implement other data representations by implementing only this minimal set.
This section lists all the functions and macros that are considered
as part of the core API from the point of view of the users
of igraph. Some of these functions and macros have sensible default
implementations that simply call some other core function (e.g.,
igraph_empty()
calls igraph_empty_attrs()
with a null attribute
table pointer). If you wish to experiment with implementing an alternative
data type, the actual number of functions that you need to replace is lower
as you can rely on the same default implementations in most cases.
igraph_empty
— Creates an empty graph with some vertices and no edges.igraph_empty_attrs
— Creates an empty graph with some vertices, no edges and some graph attributes.igraph_copy
— Creates an exact (deep) copy of a graph.igraph_destroy
— Frees the memory allocated for a graph object.
igraph_error_t igraph_empty(igraph_t *graph, igraph_integer_t n, igraph_bool_t directed);
The most basic constructor, all the other constructors should call this to create a minimal graph object. Our use of the term "empty graph" in the above description should be distinguished from the mathematical definition of the empty or null graph. Strictly speaking, the empty or null graph in graph theory is the graph with no vertices and no edges. However by "empty graph" as used in igraph we mean a graph having zero or more vertices, but no edges.
Arguments:

Pointer to a notyet initialized graph object. 


The number of vertices in the graph, a nonnegative integer number is expected. 


Boolean; whether the graph is directed or not. Supported values are:

Returns:
Error code:

Time complexity: O(V) for a graph with V vertices (and no edges).
Example 4.1. File examples/simple/creation.c
#include <igraph.h> #include <assert.h> int main(void) { igraph_t graph; igraph_vector_int_t edges; /* Create a directed graph with no vertices or edges. */ igraph_empty(&graph, 0, IGRAPH_DIRECTED); /* Add 5 vertices. Vertex IDs will range from 0 to 4, inclusive. */ igraph_add_vertices(&graph, 5, NULL); /* Add 5 edges, specified as 5 consecutive pairs of vertex IDs * stored in an integer vector. */ igraph_vector_int_init_int(&edges, 10, 0,1, 0,2, 3,1, 2,1, 0,4); igraph_add_edges(&graph, &edges, NULL); igraph_vector_int_destroy(&edges); /* Now the graph has 5 vertices and 5 edges. */ assert(igraph_vcount(&graph) == 5); assert(igraph_ecount(&graph) == 5); igraph_destroy(&graph); return 0; }
igraph_error_t igraph_empty_attrs( igraph_t *graph, igraph_integer_t n, igraph_bool_t directed, const igraph_attribute_record_list_t *attr );
Use this instead of igraph_empty()
if you wish to add some graph
attributes right after initialization. This function is currently
not very interesting for the ordinary user. Just supply 0 here or
use igraph_empty()
.
This function does not set any vertex attributes. To create a graph which has
vertex attributes, call this function specifying 0 vertices, then use
igraph_add_vertices()
to add vertices and their attributes.
Arguments:

Pointer to a notyet initialized graph object. 


The number of vertices in the graph; a nonnegative integer number is expected. 


Boolean; whether the graph is directed or not. Supported values are:



The graph attributes. Supply 
Returns:
Error code:

See also:

Time complexity: O(V) for a graph with V vertices (and no edges).
igraph_error_t igraph_copy(igraph_t *to, const igraph_t *from);
This function deeply copies a graph object to create an exact
replica of it. The new replica should be destroyed by calling
igraph_destroy()
on it when not needed any more.
You can also create a shallow copy of a graph by simply using the standard assignment operator, but be careful and do not destroy a shallow replica. To avoid this mistake, creating shallow copies is not recommended.
Arguments:

Pointer to an uninitialized graph object. 

Pointer to the graph object to copy. 
Returns:
Error code. 
Time complexity: O(V+E) for a graph with V vertices and E edges.
Example 4.2. File examples/simple/igraph_copy.c
#include <igraph.h> int main(void) { igraph_t g1, g2; igraph_vector_int_t v1, v2; igraph_vector_int_init(&v1, 8); VECTOR(v1)[0] = 0; VECTOR(v1)[1] = 1; VECTOR(v1)[2] = 1; VECTOR(v1)[3] = 2; VECTOR(v1)[4] = 2; VECTOR(v1)[5] = 3; VECTOR(v1)[6] = 2; VECTOR(v1)[7] = 2; igraph_create(&g1, &v1, 0, 0); igraph_copy(&g2, &g1); igraph_vector_int_init(&v2, 0); igraph_get_edgelist(&g2, &v2, 0); if (!igraph_vector_int_all_e(&v1, &v2)) { return 1; } igraph_vector_int_destroy(&v1); igraph_vector_int_destroy(&v2); igraph_destroy(&g1); igraph_destroy(&g2); return 0; }
void igraph_destroy(igraph_t *graph);
This function should be called for every graph object exactly once.
This function invalidates all iterators (of course), but the iterators of a graph should be destroyed before the graph itself anyway.
Arguments:

Pointer to the graph to free. 
Time complexity: operating system specific.
igraph_vcount
— The number of vertices in a graph.igraph_ecount
— The number of edges in a graph.igraph_is_directed
— Is this a directed graph?igraph_edge
— Returns the head and tail vertices of an edge.igraph_edges
— Gives the head and tail vertices of a series of edges.IGRAPH_FROM
— The source vertex of an edge.IGRAPH_TO
— The target vertex of an edge.IGRAPH_OTHER
— The other endpoint of an edge.igraph_get_eid
— Get the edge ID from the endpoints of an edge.igraph_get_eids
— Return edge IDs based on the adjacent vertices.igraph_get_all_eids_between
— Returns all edge IDs between a pair of vertices.igraph_neighbors
— Adjacent vertices to a vertex.igraph_incident
— Gives the incident edges of a vertex.igraph_degree
— The degree of some vertices in a graph.igraph_degree_1
— The degree of of a single vertex in the graph.
igraph_integer_t igraph_vcount(const igraph_t *graph);
Arguments:

The graph. 
Returns:
Number of vertices. 
Time complexity: O(1)
igraph_integer_t igraph_ecount(const igraph_t *graph);
Arguments:

The graph. 
Returns:
Number of edges. 
Time complexity: O(1)
igraph_bool_t igraph_is_directed(const igraph_t *graph);
Arguments:

The graph. 
Returns:
Logical value, 
Time complexity: O(1)
Example 4.3. File examples/simple/igraph_is_directed.c
#include <igraph.h> int main(void) { igraph_t g; igraph_empty(&g, 0, 0); if (igraph_is_directed(&g)) { return 1; } igraph_destroy(&g); igraph_empty(&g, 0, 1); if (!igraph_is_directed(&g)) { return 2; } igraph_destroy(&g); return 0; }
igraph_error_t igraph_edge( const igraph_t *graph, igraph_integer_t eid, igraph_integer_t *from, igraph_integer_t *to );
Arguments:

The graph object. 

The edge ID. 

Pointer to an igraph_integer_t. The tail (source) of the edge will be placed here. 

Pointer to an igraph_integer_t. The head (target) of the edge will be placed here. 
Returns:
Error code. 
See also:

Added in version 0.2.
Time complexity: O(1).
igraph_error_t igraph_edges(const igraph_t *graph, igraph_es_t eids, igraph_vector_int_t *edges);
Arguments:

The graph object. 

Edge selector, the series of edges. 

Pointer to an initialized vector. The start and endpoints of each edge will be placed here. 
Returns:
Error code. 
See also:

Time complexity: O(k) where k is the number of edges in the selector.
#define IGRAPH_FROM(graph,eid)
Faster than igraph_edge()
, but no error checking is done: eid
is assumed to be valid.
Arguments:

The graph. 

The edge ID. 
Returns:
The source vertex of the edge. 
See also:

#define IGRAPH_TO(graph,eid)
Faster than igraph_edge()
, but no error checking is done: eid
is assumed to be valid.
Arguments:

The graph object. 

The edge ID. 
Returns:
The target vertex of the edge. 
See also:

#define IGRAPH_OTHER(graph,eid,vid)
Typically used with undirected edges when one endpoint of the edge is known,
and the other endpoint is needed. No error checking is done:
eid
and vid
are assumed to be valid.
Arguments:

The graph object. 

The edge ID. 

The vertex ID of one endpoint of an edge. 
Returns:
The other endpoint of the edge. 
See also:

igraph_error_t igraph_get_eid(const igraph_t *graph, igraph_integer_t *eid, igraph_integer_t from, igraph_integer_t to, igraph_bool_t directed, igraph_bool_t error);
For undirected graphs from
and to
are exchangeable.
Arguments:

The graph object. 

Pointer to an integer, the edge ID will be stored here.
If 

The starting point of the edge. 

The end point of the edge. 

Logical constant, whether to search for directed edges in a directed graph. Ignored for undirected graphs. 

Logical scalar, whether to report an error if the edge
was not found. If it is false, then 
Returns:
Error code. 
See also:

Time complexity: O(log (d)), where d is smaller of the outdegree
of from
and indegree of to
if directed
is true. If directed
is false, then it is O(log(d)+log(d2)), where d is the same as before and
d2 is the minimum of the outdegree of to
and the indegree of from
.
Example 4.4. File examples/simple/igraph_get_eid.c
#include <igraph.h> int main(void) { igraph_t graph; igraph_integer_t eid; igraph_vector_int_t hist; /* DIRECTED */ igraph_star(&graph, 10, IGRAPH_STAR_OUT, 0); igraph_vector_int_init(&hist, 9); for (igraph_integer_t i = 1; i < 10; i++) { igraph_get_eid(&graph, &eid, 0, i, IGRAPH_DIRECTED, /*error=*/ true); VECTOR(hist)[eid] = 1; } igraph_vector_int_print(&hist); igraph_vector_int_destroy(&hist); igraph_destroy(&graph); /* UNDIRECTED */ igraph_star(&graph, 10, IGRAPH_STAR_UNDIRECTED, 0); igraph_vector_int_init(&hist, 9); for (igraph_integer_t i = 1; i < 10; i++) { igraph_get_eid(&graph, &eid, 0, i, IGRAPH_UNDIRECTED, /*error=*/ true); VECTOR(hist)[eid] += 1; igraph_get_eid(&graph, &eid, i, 0, IGRAPH_DIRECTED, /*error=*/ true); VECTOR(hist)[eid] += 1; } igraph_vector_int_print(&hist); igraph_vector_int_destroy(&hist); igraph_destroy(&graph); return 0; }
Added in version 0.2.
igraph_error_t igraph_get_eids(const igraph_t *graph, igraph_vector_int_t *eids, const igraph_vector_int_t *pairs, igraph_bool_t directed, igraph_bool_t error);
The pairs of vertex IDs for which the edges are looked up are taken
consecutively from the pairs
vector, i.e. VECTOR(pairs)[0]
and VECTOR(pairs)[1]
specify the first pair,
VECTOR(pairs)[2]
and VECTOR(pairs)[3]
the second
pair, etc.
If you have a sequence of vertex IDs that describe a path on the graph,
use igraph_expand_path_to_pairs()
to convert them to a list of vertex
pairs along the path.
If the error
argument is true, then it is an error to specify pairs
of vertices that are not connected. Otherwise 1 is reported for vertex pairs
without at least one edge between them.
If there are multiple edges in the graph, then these are ignored;
i.e. for a given pair of vertex IDs, igraph always returns the same edge ID,
even if the pair appears multiple times in pairs
.
Arguments:

The input graph. 

Pointer to an initialized vector, the result is stored here. It will be resized as needed. 

Vector giving pairs of vertices to fetch the edges for. 

Logical scalar, whether to consider edge directions in directed graphs. This is ignored for undirected graphs. 

Logical scalar, whether it is an error to supply nonconnected vertices. If false, then 1 is returned for nonconnected pairs. 
Returns:
Error code. 
Time complexity: O(n log(d)), where n is the number of queried edges and d is the average degree of the vertices.
See also:

Example 4.5. File examples/simple/igraph_get_eids.c
#include <igraph.h> #include <stdlib.h> int main(void) { igraph_t g; igraph_integer_t nodes = 100; igraph_integer_t edges = 1000; igraph_real_t p = 3.0 / nodes; igraph_integer_t runs = 10; igraph_integer_t r, e, ecount; igraph_vector_int_t eids, pairs, path; igraph_rng_seed(igraph_rng_default(), 42); /* make tests deterministic */ igraph_vector_int_init(&pairs, edges * 2); igraph_vector_int_init(&path, 0); igraph_vector_int_init(&eids, 0); for (r = 0; r < runs; r++) { igraph_vector_int_resize(&pairs, edges * 2); igraph_vector_int_clear(&path); igraph_vector_int_clear(&eids); igraph_erdos_renyi_game_gnp(&g, nodes, p, /*directed=*/ false, /*loops=*/ false); ecount = igraph_ecount(&g); for (e = 0; e < edges; e++) { igraph_integer_t edge = RNG_INTEGER(0, ecount  1); VECTOR(pairs)[2 * e] = IGRAPH_FROM(&g, edge); VECTOR(pairs)[2 * e + 1] = IGRAPH_TO(&g, edge); } igraph_get_eids(&g, &eids, &pairs, /* directed= */ false, /*error=*/ true); for (e = 0; e < edges; e++) { igraph_integer_t edge = VECTOR(eids)[e]; igraph_integer_t from1 = VECTOR(pairs)[2 * e]; igraph_integer_t to1 = VECTOR(pairs)[2 * e + 1]; igraph_integer_t from2 = IGRAPH_FROM(&g, edge); igraph_integer_t to2 = IGRAPH_TO(&g, edge); igraph_integer_t min1 = from1 < to1 ? from1 : to1; igraph_integer_t max1 = from1 < to1 ? to1 : from1; igraph_integer_t min2 = from2 < to2 ? from2 : to2; igraph_integer_t max2 = from2 < to2 ? to2 : from2; if (min1 != min2  max1 != max2) { return 11; } } igraph_diameter(&g, /*res=*/ NULL, /*from=*/ NULL, /*to=*/ NULL, &path, NULL, IGRAPH_UNDIRECTED, /*unconn=*/ true); igraph_vector_int_update(&pairs, &path); igraph_expand_path_to_pairs(&pairs); igraph_get_eids(&g, &eids, &pairs, 0, /*error=*/ true); for (e = 0; e < igraph_vector_int_size(&path)  1; e++) { igraph_integer_t edge = VECTOR(eids)[e]; igraph_integer_t from1 = VECTOR(path)[e]; igraph_integer_t to1 = VECTOR(path)[e + 1]; igraph_integer_t from2 = IGRAPH_FROM(&g, edge); igraph_integer_t to2 = IGRAPH_TO(&g, edge); igraph_integer_t min1 = from1 < to1 ? from1 : to1; igraph_integer_t max1 = from1 < to1 ? to1 : from1; igraph_integer_t min2 = from2 < to2 ? from2 : to2; igraph_integer_t max2 = from2 < to2 ? to2 : from2; if (min1 != min2  max1 != max2) { return 12; } } igraph_destroy(&g); } igraph_vector_int_destroy(&path); igraph_vector_int_destroy(&pairs); igraph_vector_int_destroy(&eids); return 0; }
igraph_error_t igraph_get_all_eids_between( const igraph_t *graph, igraph_vector_int_t *eids, igraph_integer_t source, igraph_integer_t target, igraph_bool_t directed );
For undirected graphs source
and target
are exchangeable.
Arguments:

The input graph. 

Pointer to an initialized vector, the result is stored here. It will be resized as needed. 

The ID of the source vertex 

The ID of the target vertex 

Logical scalar, whether to consider edge directions in directed graphs. This is ignored for undirected graphs. 
Returns:
Error code. 
Time complexity: TODO
See also:

igraph_error_t igraph_neighbors(const igraph_t *graph, igraph_vector_int_t *neis, igraph_integer_t pnode, igraph_neimode_t mode);
Arguments:

The graph to work on. 

This vector will contain the result. The vector should be initialized beforehand and will be resized. Starting from igraph version 0.4 this vector is always sorted, the vertex IDs are in increasing order. If one neighbor is connected with multiple edges, the neighbor will be returned multiple times. 

The id of the node for which the adjacent vertices are to be searched. 

Defines the way adjacent vertices are searched in
directed graphs. It can have the following values:

Returns:
Error code:

Time complexity: O(d), d is the number of adjacent vertices to the queried vertex.
Example 4.6. File examples/simple/igraph_neighbors.c
#include <igraph.h> int main(void) { igraph_t g; igraph_vector_int_t v; igraph_vector_int_init(&v, 0); igraph_small(&g, 4, IGRAPH_DIRECTED, 0,1, 1,2, 2,3, 2,2, 1); igraph_neighbors(&g, &v, 2, IGRAPH_OUT); igraph_vector_int_sort(&v); igraph_vector_int_print(&v); igraph_neighbors(&g, &v, 2, IGRAPH_IN); igraph_vector_int_sort(&v); igraph_vector_int_print(&v); igraph_neighbors(&g, &v, 2, IGRAPH_ALL); igraph_vector_int_sort(&v); igraph_vector_int_print(&v); igraph_vector_int_destroy(&v); igraph_destroy(&g); return 0; }
igraph_error_t igraph_incident(const igraph_t *graph, igraph_vector_int_t *eids, igraph_integer_t pnode, igraph_neimode_t mode);
Arguments:

The graph object. 

An initialized vector. It will be resized to hold the result. 

A vertex ID. 

Specifies what kind of edges to include for directed
graphs. 
Returns:
Error code. 
Added in version 0.2.
Time complexity: O(d), the number of incident edges to pnode
.
igraph_error_t igraph_degree(const igraph_t *graph, igraph_vector_int_t *res, const igraph_vs_t vids, igraph_neimode_t mode, igraph_bool_t loops);
This function calculates the in, out or total degree of the specified vertices.
This function returns the result as a vector of igraph_integer_t
values. In applications where igraph_real_t
is desired, use
igraph_strength()
with NULL
weights.
Arguments:

The graph. 

Integer vector, this will contain the result. It should be initialized and will be resized to be the appropriate size. 

Vertex selector, giving the vertex IDs of which the degree will be calculated. 

Defines the type of the degree for directed graphs. Valid modes are:


Boolean, gives whether the selfloops should be counted. 
Returns:
Error code:

Time complexity: O(v) if loops
is true
, and
O(v*d) otherwise. v is the number of
vertices for which the degree will be calculated, and
d is their (average) degree.
See also:

Example 4.7. File examples/simple/igraph_degree.c
#include <igraph.h> igraph_bool_t handshaking_lemma(igraph_t *g, igraph_vector_int_t *v) { /* Consistency check of the handshaking lemma: * If d is the sum of all vertex degrees, then d = 2E. */ return igraph_vector_int_sum(v) == 2 * igraph_ecount(g); } int main(void) { igraph_t g; igraph_vector_int_t v; igraph_vector_int_t seq; igraph_integer_t mdeg; /* Create graph */ igraph_vector_int_init(&v, 8); igraph_small(&g, 4, IGRAPH_DIRECTED, 0,1, 1,2, 2,3, 2,2, 1); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_NO_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_NO_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); igraph_vector_int_print(&v); if (!handshaking_lemma(&g, &v)) { exit(3); } igraph_destroy(&g); igraph_small(&g, 4, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,3, 2,2, 1); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_NO_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_NO_LOOPS); igraph_vector_int_print(&v); igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); igraph_vector_int_print(&v); if (!handshaking_lemma(&g, &v)) { exit(4); } /* Degree of the same vertex multiple times */ igraph_vector_int_init(&seq, 3); VECTOR(seq)[0] = 2; VECTOR(seq)[1] = 0; VECTOR(seq)[2] = 2; igraph_degree(&g, &v, igraph_vss_vector(&seq), IGRAPH_ALL, IGRAPH_LOOPS); igraph_vector_int_print(&v); igraph_destroy(&g); igraph_vector_int_destroy(&seq); /* Maximum degree */ igraph_ring(&g, 10, IGRAPH_UNDIRECTED, /* mutual */ false, /* circular */ false); igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); if (mdeg != 2) { exit(5); } igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); if (! handshaking_lemma(&g, &v)) { exit(6); } igraph_destroy(&g); igraph_full(&g, 10, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); if (mdeg != 9) { exit(7); } igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); if (! handshaking_lemma(&g, &v)) { exit(8); } igraph_destroy(&g); igraph_star(&g, 10, IGRAPH_STAR_OUT, /* center */ 0); igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS); if (mdeg != 9) { exit(9); } igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS); if (mdeg != 1) { exit(10); } igraph_maxdegree(&g, &mdeg, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); if (mdeg != 9) { exit(11); } igraph_degree(&g, &v, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); if (! handshaking_lemma(&g, &v)) { exit(12); } igraph_destroy(&g); igraph_vector_int_destroy(&v); return 0; }
igraph_error_t igraph_degree_1(const igraph_t *graph, igraph_integer_t *deg, igraph_integer_t vid, igraph_neimode_t mode, igraph_bool_t loops);
This function calculates the in, out or total degree of a single vertex.
For a single vertex, it is more efficient than calling igraph_degree()
.
Arguments:

The graph. 

Pointer to the integer where the computed degree will be stored. 

The vertex for which the degree will be calculated. 

Defines the type of the degree for directed graphs. Valid modes are:


Boolean, gives whether the selfloops should be counted. 
Returns:
Error code. 
See also:

Time complexity: O(1) if loops
is true
, and
O(d) otherwise, where d is the degree.
igraph_add_edge
— Adds a single edge to a graph.igraph_add_edges
— Adds edges to a graph object.igraph_add_vertices
— Adds vertices to a graph.igraph_delete_edges
— Removes edges from a graph.igraph_delete_vertices
— Removes some vertices (with all their edges) from the graph.igraph_delete_vertices_map
— Removes some vertices (with all their edges) from the graph.
igraph_error_t igraph_add_edge(igraph_t *graph, igraph_integer_t from, igraph_integer_t to);
For directed graphs the edge points from from
to to
.
Note that if you want to add many edges to a big graph, then it is
inefficient to add them one by one, it is better to collect them into
a vector and add all of them via a single igraph_add_edges()
call.
Arguments:

The graph. 

The id of the first vertex of the edge. 

The id of the second vertex of the edge. 
Returns:
Error code. 
See also:

Time complexity: O(V+E), the number of edges plus the number of vertices.
igraph_error_t igraph_add_edges( igraph_t *graph, const igraph_vector_int_t *edges, const igraph_attribute_record_list_t *attr );
The edges are given in a vector, the
first two elements define the first edge (the order is
from
, to
for directed
graphs). The vector
should contain even number of integer numbers between zero and the
number of vertices in the graph minus one (inclusive). If you also
want to add new vertices, call igraph_add_vertices()
first.
Arguments:

The graph to which the edges will be added. 

The edges themselves. 

The attributes of the new edges. You can supply a null pointer here if you do not need edge attributes. 
Returns:
Error code:

This function invalidates all iterators.
Time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the new, extended graph.
Example 4.8. File examples/simple/creation.c
#include <igraph.h> #include <assert.h> int main(void) { igraph_t graph; igraph_vector_int_t edges; /* Create a directed graph with no vertices or edges. */ igraph_empty(&graph, 0, IGRAPH_DIRECTED); /* Add 5 vertices. Vertex IDs will range from 0 to 4, inclusive. */ igraph_add_vertices(&graph, 5, NULL); /* Add 5 edges, specified as 5 consecutive pairs of vertex IDs * stored in an integer vector. */ igraph_vector_int_init_int(&edges, 10, 0,1, 0,2, 3,1, 2,1, 0,4); igraph_add_edges(&graph, &edges, NULL); igraph_vector_int_destroy(&edges); /* Now the graph has 5 vertices and 5 edges. */ assert(igraph_vcount(&graph) == 5); assert(igraph_ecount(&graph) == 5); igraph_destroy(&graph); return 0; }
igraph_error_t igraph_add_vertices( igraph_t *graph, igraph_integer_t nv, const igraph_attribute_record_list_t *attr );
This function invalidates all iterators.
Arguments:

The graph object to extend. 

Nonnegative integer specifying the number of vertices to add. 

The attributes of the new vertices. You can supply a null pointer here if you do not need vertex attributes. 
Returns:
Error code:

Time complexity: O(V) where V is the number of vertices in the new, extended graph.
Example 4.9. File examples/simple/creation.c
#include <igraph.h> #include <assert.h> int main(void) { igraph_t graph; igraph_vector_int_t edges; /* Create a directed graph with no vertices or edges. */ igraph_empty(&graph, 0, IGRAPH_DIRECTED); /* Add 5 vertices. Vertex IDs will range from 0 to 4, inclusive. */ igraph_add_vertices(&graph, 5, NULL); /* Add 5 edges, specified as 5 consecutive pairs of vertex IDs * stored in an integer vector. */ igraph_vector_int_init_int(&edges, 10, 0,1, 0,2, 3,1, 2,1, 0,4); igraph_add_edges(&graph, &edges, NULL); igraph_vector_int_destroy(&edges); /* Now the graph has 5 vertices and 5 edges. */ assert(igraph_vcount(&graph) == 5); assert(igraph_ecount(&graph) == 5); igraph_destroy(&graph); return 0; }
igraph_error_t igraph_delete_edges(igraph_t *graph, igraph_es_t edges);
The edges to remove are specified as an edge selector.
This function cannot remove vertices; vertices will be kept even if they lose all their edges.
This function invalidates all iterators.
Arguments:

The graph to work on. 

The edges to remove. 
Returns:
Error code. 
Time complexity: O(V+E) where V and E are the number of vertices and edges in the original graph, respectively.
Example 4.10. File examples/simple/igraph_delete_edges.c
#include <igraph.h> int main(void) { igraph_t g; igraph_es_t es; igraph_small(&g, 4, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,2, 2,3, 1); igraph_es_pairs_small(&es, IGRAPH_DIRECTED, 3, 2, 1); igraph_delete_edges(&g, es); if (igraph_ecount(&g) != 3) { return 1; } igraph_es_destroy(&es); igraph_destroy(&g); return 0; }
igraph_error_t igraph_delete_vertices(igraph_t *graph, const igraph_vs_t vertices);
This function changes the IDs of the vertices (except in some very special cases, but these should not be relied on anyway).
This function invalidates all iterators.
Arguments:

The graph to work on. 

The IDs of the vertices to remove, in a vector. The vector may contain the same ID more than once. 
Returns:
Error code:

Time complexity: O(V+E), V and E are the number of vertices and edges in the original graph.
Example 4.11. File examples/simple/igraph_delete_vertices.c
#include <igraph.h> int main(void) { igraph_t g; /* without edges */ igraph_small(&g, 15, IGRAPH_UNDIRECTED, 1); igraph_delete_vertices(&g, igraph_vss_1(2)); if (igraph_vcount(&g) != 14) { return 2; } igraph_destroy(&g); /* with edges */ igraph_small(&g, 4, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,3, 2,2, 1); igraph_delete_vertices(&g, igraph_vss_1(2)); if (igraph_vcount(&g) != 3) { return 3; } if (igraph_ecount(&g) != 1) { return 4; } igraph_destroy(&g); return 0; }
igraph_error_t igraph_delete_vertices_map( igraph_t *graph, const igraph_vs_t vertices, igraph_vector_int_t *map, igraph_vector_int_t *invmap );
This function changes the IDs of the vertices (except in some very
special cases, but these should not be relied on anyway). You can use the
map
argument to obtain the mapping from old vertex IDs to the new ones,
and the newmap
argument to obtain the reverse mapping.
This function invalidates all iterators.
Arguments:

The graph to work on. 

The IDs of the vertices to remove, in a vector. The vector may contain the same ID more than once. 

An optional pointer to a vector that provides the mapping from
the vertex IDs before the removal to the vertex IDs after
the removal. You can supply 

An optional pointer to a vector that provides the mapping from
the vertex IDs after the removal to the vertex IDs before
the removal. You can supply 
Returns:
Error code:

Time complexity: O(V+E), V and E are the number of vertices and edges in the original graph.
IGRAPH_VCOUNT_MAX
— The maximum number of vertices supported in igraph graphs.IGRAPH_ECOUNT_MAX
— The maximum number of edges supported in igraph graphs.igraph_expand_path_to_pairs
— Helper function to convert a sequence of vertex IDs describing a path into a "pairs" vector.igraph_invalidate_cache
— Invalidates the internal cache of an igraph graph.igraph_is_same_graph
— Are two graphs identical as labelled graphs?
#define IGRAPH_VCOUNT_MAX
The value of this constant is one less than IGRAPH_INTEGER_MAX
.
When igraph is compiled in 32bit mode, this means that you are limited
to 2^{31} – 2 (about 2.1 billion) vertices. In
64bit mode, the limit is 2^{63} – 2 so you are much
more likely to hit outofmemory issues due to other reasons before reaching
this limit.
#define IGRAPH_ECOUNT_MAX
The value of this constant is half of IGRAPH_INTEGER_MAX
.
When igraph is compiled in 32bit mode, this means that you are limited
to approximately 2^{30} (about 1.07 billion)
vertices. In 64bit mode, the limit is approximately
2^{62} so you are much more likely to hit
outofmemory issues due to other reasons before reaching this limit.
igraph_error_t igraph_expand_path_to_pairs(igraph_vector_int_t* path);
This function is useful when you have a sequence of vertex IDs in a graph and
you would like to retrieve the IDs of the edges between them. The function
duplicates all but the first and the last elements in the vector, effectively
converting the path into a vector of vertex IDs that can be passed to
igraph_get_eids()
.
Arguments:

the input vector. It will be modified inplace and it will be resized as needed. When the vector contains less than two vertex IDs, it will be cleared. 
Returns:
Error code: 
void igraph_invalidate_cache(const igraph_t* graph);
igraph graphs cache some basic properties about themselves in an internal data structure. This function invalidates the contents of the cache and forces a recalculation of the cached properties the next time they are needed.
You should not need to call this function during normal usage; however, we
might ask you to call this function explicitly if we suspect that you are
running into a bug in igraph's cache handling. A telltale sign of an invalid
cache entry is that the result of a cached igraph function (such as
igraph_is_dag()
or igraph_is_simple()
) is different before and
after a cache invalidation.
Arguments:

The graph whose cache is to be invalidated. 
Time complexity: O(1).
igraph_error_t igraph_is_same_graph(const igraph_t *graph1, const igraph_t *graph2, igraph_bool_t *res);
Two graphs are considered to be the same if they have the same vertex and edge sets. Graphs which are the same may have multiple different representations in igraph, hence the need for this function.
This function verifies that the two graphs have the same directedness, the same number of vertices, and that they contain precisely the same edges (regardless of their ordering) when written in terms of vertex indices. Graph attributes are not taken into account.
This concept is different from isomorphism. For example, the graphs
01, 21
and 12, 01
are considered the same
because they only differ in the ordering of their edge lists and the ordering
of vertices in an undirected edge. However, they are not the same as
02, 12
, even though they are isomorphic to it.
Note that this latter graph contains the edge 02
while the former two do not — thus their edge sets differ.
Arguments:

The first graph object. 

The second graph object. 

The result will be stored here. 
Returns:
Error code. 
Time complexity: O(E), the number of edges in the graphs.
See also:

← Chapter 3. Tutorial  Chapter 5. Error handling → 